View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.maven;
17  
18  import java.io.File;
19  import java.util.Locale;
20  
21  import org.apache.maven.reporting.MavenReportException;
22  
23  import de.smartics.maven.util.report.MavenExternalReportFactory;
24  import de.smartics.maven.util.report.link.ExternalReportFactory;
25  import de.smartics.maven.util.report.link.LinkConstructorStrategy;
26  import de.smartics.maven.util.report.link.LinkConstructorStrategyConfig;
27  import de.smartics.maven.util.report.link.ReportId;
28  import de.smartics.maven.util.report.link.strategy.DirectoryPathLinkConstructorStrategy;
29  import de.smartics.maven.util.report.link.strategy.FilePathLinkConstructorStrategy;
30  import de.smartics.maven.util.report.link.strategy.LineNumberDirectoryPathLinkConstructorStrategy;
31  import de.smartics.maven.util.report.link.strategy.SurefireLinkConstructorStrategy;
32  import de.smartics.testdoc.report.export.doc.ExternalReportReferences;
33  import de.smartics.testdoc.report.export.doc.ImageHelper;
34  import de.smartics.testdoc.report.junit.DirectoryJUnitTestCaseManager;
35  import de.smartics.testdoc.report.junit.JUnitTestCaseManager;
36  
37  /**
38   * The abstract base implementation for reports.
39   *
40   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
41   * @version $Revision:591 $
42   */
43  public abstract class AbstractSourceReportMojo extends AbstractReportMojo // NOPMD
44  {
45    // ********************************* Fields *********************************
46  
47    // --- constants ------------------------------------------------------------
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The location of the source code report (XRef) to reference.
53     *
54     * @parameter default-value="${project.reporting.outputDirectory}/xref"
55     */
56    protected File xrefLocation;
57  
58    /**
59     * Controls whether to reference the source code report (XRef) when found.
60     *
61     * @parameter expression="${linkXRef}" default-value="true"
62     */
63    protected boolean linkXref;
64  
65    /**
66     * Controls whether to reference the Javadoc report when found.
67     *
68     * @parameter expression="${linkJavadoc}" default-value="true"
69     */
70    protected boolean linkJavadoc;
71  
72    /**
73     * The location of the Javadoc report to reference.
74     *
75     * @parameter default-value="${project.reporting.outputDirectory}/apidocs"
76     */
77    protected File javadocLocation;
78  
79    /**
80     * The location of the source code report (XRef) to reference.
81     *
82     * @parameter default-value="${project.reporting.outputDirectory}/xref-test"
83     */
84    protected File xrefTestLocation;
85  
86    /**
87     * Controls whether to reference the source code report (XRef) when found.
88     *
89     * @parameter expression="${linkXRefTest}" default-value="true"
90     */
91    protected boolean linkXrefTest;
92  
93    /**
94     * Controls whether to reference the Javadoc test report when found.
95     *
96     * @parameter expression="${linkJavadocTest}" default-value="true"
97     */
98    protected boolean linkJavadocTest;
99  
100   /**
101    * The location of the Javadoc test report to reference.
102    *
103    * @parameter default-value="${project.reporting.outputDirectory}/testapidocs"
104    */
105   protected File javadocTestLocation;
106 
107   /**
108    * Controls whether to reference the javadoc report when found.
109    *
110    * @parameter expression="${linkSurefire}" default-value="true"
111    */
112   protected boolean linkSurefire;
113 
114   /**
115    * The location of the JavaDoc report to reference.
116    *
117    * @parameter
118    *            default-value="${project.reporting.outputDirectory}/surefire-report.html"
119    */
120   protected File surefireLocation;
121 
122   /**
123    * Controls whether to show test case information read from the JUnit XML
124    * reports.
125    *
126    * @parameter expression="${presentJUnitReport}" default-value="true"
127    */
128   protected boolean presentJUnitReport;
129 
130   /**
131    * The location of the JavaDoc report to reference.
132    *
133    * @parameter default-value="${project.build.directory}/surefire-reports"
134    */
135   protected File junitReportLocation;
136 
137   /**
138    * Controls whether to reference the Cobertura test report when found.
139    *
140    * @parameter expression="${linkCobertura}" default-value="true"
141    */
142   protected boolean linkCobertura;
143 
144   /**
145    * The location of the Cobertura test report to reference.
146    *
147    * @parameter default-value="${project.reporting.outputDirectory}/cobertura"
148    */
149   protected File coberturaLocation;
150 
151   /**
152    * Helper to construct links to external reports.
153    */
154   protected ExternalReportReferences reports;
155 
156   /**
157    * The manager to access JUnit report information. Is <code>null</code> if the
158    * information is not to be included.
159    */
160   protected JUnitTestCaseManager junitManager;
161 
162   /**
163    * Configuration of links to images used in reports.
164    */
165   protected ImageHelper imageHelper = new ImageHelper(null);
166 
167   // ****************************** Initializer *******************************
168 
169   // ****************************** Constructors ******************************
170 
171   // ****************************** Inner Classes *****************************
172 
173   // ********************************* Methods ********************************
174 
175   // --- init -----------------------------------------------------------------
176 
177   // --- get&set --------------------------------------------------------------
178 
179   // --- business -------------------------------------------------------------
180 
181   /**
182    * {@inheritDoc}
183    * <p>
184    * Configures the plugin logger.
185    * </p>
186    *
187    * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
188    */
189   @Override
190   protected void executeReport(final Locale locale) throws MavenReportException
191   {
192     super.executeReport(locale);
193 
194     final ExternalReportFactory factory =
195         new MavenExternalReportFactory(project, "");
196 
197     reports = new ExternalReportReferences(factory);
198 
199     addJxrReports();
200     addJavadocReports();
201     addSurefireReport();
202     addCoberturaReport();
203 
204     if (presentJUnitReport)
205     {
206       junitManager = new DirectoryJUnitTestCaseManager(junitReportLocation);
207     }
208   }
209 
210   private void addJxrReports() throws MavenReportException
211   {
212     final ReportId jxrReport =
213         provideLinkConstructor(linkXref,
214             MavenExternalReportFactory.MAVEN_JXR_PLUGIN,
215             ReportId.NORMAL_REPORT, xrefLocation, true, "report.link.jxr");
216     if (jxrReport != null)
217     {
218       reports.addUutReportId(jxrReport);
219     }
220 
221     final ReportId jxrTestReport =
222         provideLinkConstructor(linkXrefTest,
223             MavenExternalReportFactory.MAVEN_JXR_PLUGIN, ReportId.TEST_REPORT,
224             xrefTestLocation, true, "report.link.jxr");
225     if (jxrTestReport != null)
226     {
227       reports.addTestCaseReportId(jxrTestReport);
228     }
229   }
230 
231   private void addJavadocReports() throws MavenReportException
232   {
233     final ReportId javadocReport =
234         provideLinkConstructor(linkJavadoc,
235             MavenExternalReportFactory.MAVEN_JAVADOC_PLUGIN,
236             ReportId.NORMAL_REPORT, javadocLocation, true,
237             "report.link.javadoc");
238     if (javadocReport != null)
239     {
240       reports.addUutReportId(javadocReport);
241     }
242 
243     final ReportId javadocTestReport =
244         provideLinkConstructor(linkJavadocTest,
245             MavenExternalReportFactory.MAVEN_JAVADOC_PLUGIN,
246             ReportId.TEST_REPORT, javadocTestLocation, true,
247             "report.link.javadoc");
248     if (javadocTestReport != null)
249     {
250       reports.addTestCaseReportId(javadocTestReport);
251     }
252   }
253 
254   private void addSurefireReport() throws MavenReportException
255   {
256     final ReportId surefireReport =
257         provideLinkConstructor(linkSurefire,
258             MavenExternalReportFactory.MAVEN_SUREFIRE_PLUGIN,
259             ReportId.NORMAL_REPORT, surefireLocation, false,
260             "report.link.surefire");
261     if (surefireReport != null)
262     {
263       reports.addTestCaseReportId(surefireReport);
264     }
265   }
266 
267   private void addCoberturaReport() throws MavenReportException
268   {
269     final ReportId coberturaReport =
270         provideLinkConstructor(linkCobertura,
271             MavenExternalReportFactory.MAVEN_COBERTURA_PLUGIN,
272             ReportId.NORMAL_REPORT, coberturaLocation, false,
273             "report.link.cobertura");
274     if (coberturaReport != null)
275     {
276       reports.addUutReportId(coberturaReport);
277     }
278   }
279 
280   private ReportId provideLinkConstructor(
281       // NOPMD
282       final boolean addReport, final String reportArtifactId,
283       final String reportType, final File location, final boolean linkToMember,
284       final String labelKey) throws MavenReportException
285   {
286     final LinkConstructorStrategy strategy =
287         createStrategy(reportArtifactId, location, linkToMember, labelKey);
288 
289     final ReportId reportId = new ReportId(reportArtifactId, reportType);
290     if (reports.registerReport(addReport, reportId, strategy))
291     {
292       return reportId;
293     }
294     return null;
295   }
296 
297   protected LinkConstructorStrategy createStrategy(
298       final String reportArtifactId, final File location,
299       final boolean linkToMember, final String labelKey)
300     throws MavenReportException
301   {
302     final LinkConstructorStrategyConfig.Builder builder =
303         new LinkConstructorStrategyConfig.Builder();
304     // builder.setExtension(".html");
305     builder.setReferenceMember(linkToMember);
306     builder.setReportLocation(location);
307     builder.setLabelKey(labelKey);
308     final LinkConstructorStrategyConfig config = builder.build();
309 
310     final LinkConstructorStrategy strategy;
311     if (MavenExternalReportFactory.MAVEN_JAVADOC_PLUGIN
312         .equals(reportArtifactId))
313     {
314       strategy = new DirectoryPathLinkConstructorStrategy(config);
315     }
316     else if (MavenExternalReportFactory.MAVEN_COBERTURA_PLUGIN
317         .equals(reportArtifactId))
318     {
319       strategy = new FilePathLinkConstructorStrategy(config);
320     }
321     else if (MavenExternalReportFactory.MAVEN_JXR_PLUGIN
322         .equals(reportArtifactId))
323     {
324       strategy = new LineNumberDirectoryPathLinkConstructorStrategy(config);
325     }
326     else if (MavenExternalReportFactory.MAVEN_SUREFIRE_PLUGIN
327         .equals(reportArtifactId))
328     {
329       strategy = new SurefireLinkConstructorStrategy(config);
330     }
331     else
332     {
333       throw new MavenReportException("Unknown report artifact ID '"
334                                      + reportArtifactId + "'.");
335     }
336     return strategy;
337   }
338 
339   // --- object basics --------------------------------------------------------
340 
341 }