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.export;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  import java.util.List;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import de.smartics.maven.util.report.MessageHelper;
26  import de.smartics.testdoc.core.doc.ScenarioTestDoc;
27  import de.smartics.testdoc.core.doc.Type;
28  import de.smartics.testdoc.core.doc.UnitTestDoc;
29  import de.smartics.testdoc.core.export.UnitTestDocExporter;
30  import de.smartics.testdoc.report.export.doc.ExternalReportReferences;
31  import de.smartics.testdoc.report.export.doc.InformationFilter;
32  import de.smartics.testdoc.report.export.doc.TestDocHelper;
33  import de.smartics.testdoc.report.junit.JUnitTestCaseManager;
34  
35  /**
36   * Exports the test documentation to an output medium.
37   *
38   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
39   * @version $Revision:591 $
40   */
41  public abstract class AbstractReportExporter implements UnitTestDocExporter
42  { // NOPMD
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    // --- members --------------------------------------------------------------
48  
49    /**
50     * Reference to the logger for this class.
51     */
52    private final Logger log = LoggerFactory
53        .getLogger(AbstractReportExporter.class);
54  
55    /**
56     * The resource bundles with messages to display as labels in the generated
57     * report.
58     */
59    protected final MessageHelper messages;
60  
61    /**
62     * Provides access to configuration information.
63     */
64    protected final TestDocHelper testDocHelper;
65  
66    /**
67     * The configuration that tells which report information is to be displayed.
68     */
69    protected final InformationFilter informationFilter;
70  
71    // ****************************** Initializer *******************************
72  
73    // ****************************** Constructors ******************************
74  
75    /**
76     * Default constructor.
77     *
78     * @param messages the resource bundles with messages to display as labels in
79     *          the generated report.
80     * @param testDocHelper the value for testDocHelper.
81     */
82    protected AbstractReportExporter(final MessageHelper messages,
83        final TestDocHelper testDocHelper)
84    {
85      this.messages = messages;
86      this.testDocHelper = testDocHelper;
87      this.informationFilter = testDocHelper.getInformationFilter();
88    }
89  
90    // ****************************** Inner Classes *****************************
91  
92    // ********************************* Methods ********************************
93  
94    // --- init -----------------------------------------------------------------
95  
96    // --- get&set --------------------------------------------------------------
97  
98    /**
99     * {@inheritDoc}
100    *
101    * @see de.smartics.testdoc.report.export.UnitTestDocExporter#getFileExtension()
102    */
103   @Override
104   public String getFileExtension()
105   {
106     return "xml";
107   }
108 
109   /**
110    * Returns the configuration that tells which report information is to be
111    * displayed.
112    *
113    * @return the configuration that tells which report information is to be
114    *         displayed.
115    */
116   public InformationFilter getInformationFilter()
117   {
118     return testDocHelper.getInformationFilter();
119   }
120 
121   /**
122    * Returns the value for testDocHelper.
123    * <p>
124    * Provides access to configuration information.
125    *
126    * @return the value for testDocHelper.
127    */
128   public TestDocHelper getTestDocHelper()
129   {
130     return testDocHelper;
131   }
132 
133   // --- business -------------------------------------------------------------
134 
135   /**
136    * {@inheritDoc}
137    *
138    * @see de.smartics.testdoc.report.export.UnitTestDocExporter#export(de.smartics.testdoc.report.doc.UnitTestDoc,
139    *      java.io.OutputStream)
140    */
141   @Override
142   public void export(final UnitTestDoc testDoc, final OutputStream output)
143     throws IOException
144   {
145     final Type type = testDoc.getUutType();
146 
147     final List<ScenarioTestDoc> scenarios =
148         testDocHelper.filterScenarios(testDoc.getScenarios());
149 
150     if (scenarios.isEmpty())
151     {
152       if (log.isDebugEnabled())
153       {
154         log.debug("No stories for " + type + " passed the filter.");
155       }
156       return;
157     }
158 
159     renderTestDocTypeStart(testDoc);
160     final ExternalReportReferences reports = testDocHelper.getReports();
161     renderTestDocUutReportLinks(type, reports);
162 
163     final ScenarioTestDoc rep = scenarios.get(0);
164     renderTableStart(rep);
165 
166     int counter = 1;
167     for (final ScenarioTestDoc scenario : scenarios)
168     {
169       renderScenario(counter, scenario);
170       counter++;
171     }
172 
173     renderTableEnd(rep);
174 
175     renderTestDocTypeEnd(testDoc);
176   }
177 
178   /**
179    * Override if the report need to initialize the output before individual test
180    * documentation units are exported.
181    *
182    * @param scenarioRepresentant the representant of the documentation units.
183    *          This is the first unit peeked from the list to be exported.
184    */
185   protected void renderTableStart(final ScenarioTestDoc scenarioRepresentant)
186     throws IOException
187   {
188   }
189 
190   /**
191    * Override to export the test doc unit start information. This is the type
192    * information.
193    *
194    * @param testDoc the instance whose test type information is to be exported.
195    */
196   protected abstract void renderTestDocTypeStart(UnitTestDoc testDoc)
197     throws IOException;
198 
199   /**
200    * The symmetric method to {@link #renderTestDocTypeStart(UnitTestDoc)}.
201    *
202    * @param testDoc the unit whose export ends with this method.
203    */
204   protected void renderTestDocTypeEnd(final UnitTestDoc testDoc)
205     throws IOException
206   {
207   }
208 
209   /**
210    * Exports links to external reports.
211    *
212    * @param reports the reports to link to.
213    */
214   protected abstract void renderTestDocUutReportLinks(Type type,
215       ExternalReportReferences reports) throws IOException;
216 
217   /**
218    * Controls the output of a single scenario within the test documentation
219    * unit. Usually not to be overridden.
220    *
221    * @param counter the current counter of the scenario.
222    * @param scenario the scenario to be exported.
223    */
224   protected void renderScenario(final int counter,
225       final ScenarioTestDoc scenario) throws IOException
226   {
227     startScenario();
228     renderRowNumber(counter);
229     renderJUnitStatus(scenario);
230     renderSentence(scenario);
231     renderCategories(scenario);
232     renderTestCase(scenario);
233     endScenario();
234   }
235 
236   protected void startScenario()
237   {
238   }
239 
240   protected void endScenario()
241   {
242   }
243 
244   protected abstract void renderRowNumber(int counter) throws IOException;
245 
246   protected abstract void renderJUnitStatus(final ScenarioTestDoc scenario)
247     throws IOException;
248 
249   protected abstract void renderSentence(final ScenarioTestDoc scenario)
250     throws IOException;
251 
252   protected abstract void renderCategories(final ScenarioTestDoc scenario)
253     throws IOException;
254 
255   protected abstract void renderTestCase(final ScenarioTestDoc scenario)
256     throws IOException;
257 
258   protected void renderTableEnd(final ScenarioTestDoc scenarioRepresentant)
259     throws IOException
260   {
261     final Type testCaseType = scenarioRepresentant.getTestCaseType();
262     final JUnitTestCaseManager junitManager = testDocHelper.getJunitManager();
263     junitManager.evict(testCaseType);
264   }
265 
266   // --- object basics --------------------------------------------------------
267 
268 }