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.io.IOException;
20  
21  import javax.xml.parsers.ParserConfigurationException;
22  
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.project.MavenProject;
27  
28  import de.smartics.maven.io.MojoIoUtils;
29  import de.smartics.maven.util.LoggingUtils;
30  import de.smartics.maven.util.PathUtils;
31  import de.smartics.testdoc.core.doc.OutputManager;
32  import de.smartics.testdoc.core.doc.UnitTestDocIndex;
33  import de.smartics.testdoc.core.export.FileOutputManager;
34  import de.smartics.testdoc.core.export.UnitTestDocIndexExporter;
35  import de.smartics.testdoc.core.export.XmlExporter;
36  import de.smartics.testdoc.report.export.doc.IndexProvider;
37  import de.smartics.testdoc.report.export.doc.TestDocHelper;
38  
39  /**
40   * Generates XML reports.
41   *
42   * @goal test-stories
43   * @phase process-test-classes
44   * @requiresProject
45   * @requiresDependencyResolution test
46   * @description Generates XML reports containing test stories.
47   */
48  public class TestStoriesXmlExportMojo extends AbstractMojo
49  {
50    // ********************************* Fields *********************************
51  
52    // --- constants ------------------------------------------------------------
53  
54    // --- members --------------------------------------------------------------
55  
56    /**
57     * The Maven project.
58     *
59     * @parameter expression="${project}"
60     * @required
61     * @readonly
62     * @since 1.0
63     */
64    protected MavenProject project;
65  
66    /**
67     * A simple flag to skip the generation of the XML files. If set on the
68     * command line use <code>-Dtestdoc.test-stories.skip</code>.
69     *
70     * @parameter expression="${testdoc.test-stories.skip}" default-value="false"
71     * @since 1.0
72     */
73    private boolean skip;
74  
75    /**
76     * Specifies the log level used for this plugin.
77     * <p>
78     * Allowed values are <code>SEVERE</code>, <code>WARNING</code>,
79     * <code>INFO</code> and <code>FINEST</code>.
80     * </p>
81     *
82     * @parameter expression="${testdoc.test-stories.logLevel}"
83     * @since 1.0
84     */
85    private String logLevel;
86  
87    /**
88     * Specifies the root directory to write the XML files to.
89     *
90     * @parameter expression="${project.build.directory}/testdoc/xml"
91     * @since 1.0
92     */
93    private String outputDirectory;
94  
95    /**
96     * The directory to write to. This is the {@link File} representation of
97     * {@link #outputDirectory}.
98     *
99     * @since 1.0
100    */
101   protected File outputDir;
102 
103   /**
104    * The output directory to write intermediate serialized files to.
105    *
106    * @parameter expression="${project.build.directory}/testdoc/ser"
107    * @since 1.0
108    */
109   private String serOutputDirectory;
110 
111   /**
112    * Helper methods to access test documentation information.
113    */
114   private TestDocHelper testDocHelper;
115 
116   // ****************************** Initializer *******************************
117 
118   // ****************************** Constructors ******************************
119 
120   // ****************************** Inner Classes *****************************
121 
122   // ********************************* Methods ********************************
123 
124   // --- init -----------------------------------------------------------------
125 
126   // --- get&set --------------------------------------------------------------
127 
128   // --- business -------------------------------------------------------------
129 
130   /**
131    * {@inheritDoc}
132    */
133   @Override
134   public void execute() throws MojoExecutionException, MojoFailureException
135   {
136     if (!skip)
137     {
138       if (canBuild())
139       {
140         final IndexProvider indexChecker =
141             new IndexProvider(serOutputDirectory);
142         testDocHelper = indexChecker.createHelper(null);
143         if (testDocHelper.isIndexProvided())
144         {
145           runExecution();
146         }
147         else
148         {
149           getLog().info(
150               "No test doc information found, no report will be generated.");
151         }
152       }
153       else
154       {
155         getLog()
156             .debug(
157                 "Skipping testdoc since no test classes are provided by this project.");
158       }
159     }
160     else
161     {
162       getLog().info("Skipping testdoc XML generation since skip=true.");
163     }
164   }
165 
166   private void runExecution() throws MojoExecutionException
167   {
168     init();
169 
170     try
171     {
172       final XmlExporter exporter = new XmlExporter(true);
173       final OutputManager manager = new FileOutputManager(outputDir, exporter);
174       final UnitTestDocIndexExporter indexExporter =
175           new UnitTestDocIndexExporter(manager);
176       final UnitTestDocIndex index = testDocHelper.getIndex();
177       indexExporter.export(index);
178     }
179     catch (final ParserConfigurationException e)
180     {
181       throw new MojoExecutionException("Cannot generate testdoc XML files.", e);
182     }
183     catch (final IOException e)
184     {
185       throw new MojoExecutionException("Cannot generate testdoc XML files.", e);
186     }
187   }
188 
189   @SuppressWarnings("unchecked")
190   private boolean canBuild()
191   {
192     final String packaging = project.getPackaging();
193     return !("pom".equals(packaging) || !PathUtils.exists(project
194         .getTestCompileSourceRoots()));
195   }
196 
197   private void init() throws MojoExecutionException
198   {
199     LoggingUtils.configureLogger(getLog(), logLevel);
200     this.outputDir = MojoIoUtils.provideMojoDirectory(outputDirectory);
201   }
202 
203   // --- object basics --------------------------------------------------------
204 
205 }