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.xml;
17  
18  import java.io.IOException;
19  import java.util.List;
20  
21  import javax.xml.stream.XMLStreamException;
22  import javax.xml.stream.XMLStreamWriter;
23  
24  import org.codehaus.plexus.util.StringUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import de.smartics.maven.util.report.MessageHelper;
29  import de.smartics.maven.util.report.link.ExternalReport;
30  import de.smartics.testdoc.maven.export.AbstractReportExporter;
31  import de.smartics.testdoc.core.doc.ScenarioTestDoc;
32  import de.smartics.testdoc.core.doc.TestMethodDoc;
33  import de.smartics.testdoc.core.doc.Type;
34  import de.smartics.testdoc.core.doc.UnitTestDoc;
35  import de.smartics.testdoc.report.export.doc.ExternalReportReferences;
36  import de.smartics.testdoc.report.export.doc.ImageHelper;
37  import de.smartics.testdoc.report.export.doc.TestDocHelper;
38  import de.smartics.testdoc.report.junit.JUnitTestCaseDoc;
39  import de.smartics.testdoc.report.junit.JUnitTestCaseManager;
40  import de.smartics.testdoc.report.junit.JUnitTestMethodDoc;
41  import de.smartics.testdoc.report.junit.JUnitTestMethodDoc.ResultType;
42  import de.smartics.testdoc.report.junit.TestCaseReportException;
43  
44  /**
45   * Exports the report information to plain XML.
46   *
47   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
48   * @version $Revision:591 $
49   */
50  public class XmlExporter extends AbstractReportExporter
51  {
52    // ********************************* Fields *********************************
53  
54    // --- constants ------------------------------------------------------------
55  
56    protected static final String TESTDOC_NS =
57        "http://www.smartics.de/testdoc/1.0";
58  
59    // --- members --------------------------------------------------------------
60  
61    /**
62     * Reference to the logger for this class.
63     */
64    private final Logger log = LoggerFactory.getLogger(XmlExporter.class);
65  
66    /**
67     * The output stream to write to.
68     */
69    private final XMLStreamWriter xmlWriter;
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     * @param xmlWriter the output stream to write to.
82     */
83    public XmlExporter(final MessageHelper messages,
84        final TestDocHelper testDocHelper, final XMLStreamWriter xmlWriter)
85    {
86      super(messages, testDocHelper);
87      this.xmlWriter = xmlWriter;
88    }
89  
90    // ****************************** Inner Classes *****************************
91  
92    // ********************************* Methods ********************************
93  
94    // --- init -----------------------------------------------------------------
95  
96    // --- get&set --------------------------------------------------------------
97  
98    /**
99     * Returns the output stream to write to.
100    *
101    * @return the output stream to write to.
102    */
103   public XMLStreamWriter getXmlWriter()
104   {
105     return xmlWriter;
106   }
107 
108   // --- business -------------------------------------------------------------
109 
110   /**
111    * {@inheritDoc}
112    *
113    * @see de.smartics.testdoc.maven.export.AbstractReportExporter#renderTestDocTypeStart(de.smartics.testdoc.report.doc.UnitTestDoc)
114    */
115   @Override
116   protected void renderTestDocTypeStart(final UnitTestDoc testDoc)
117     throws IOException
118   {
119     try
120     {
121       xmlWriter.writeStartElement(TESTDOC_NS, "uut-type");
122       final Type type = testDoc.getUutType();
123       writeType(type);
124       xmlWriter.writeEndElement();
125     }
126     catch (final XMLStreamException e)
127     {
128       throw new IOException("Cannot write test doc type start.", e);
129     }
130   }
131 
132   private void writeType(final Type type) throws XMLStreamException
133   {
134     final String packageName = type.getPackageName();
135     if (StringUtils.isNotBlank(packageName))
136     {
137       writeElement("package", packageName);
138     }
139     writeElement("type", type.getTypeName());
140   }
141 
142   private void writeElement(final String tagName, final String content)
143     throws XMLStreamException
144   {
145     try
146     {
147       xmlWriter.writeStartElement(TESTDOC_NS, tagName);
148       xmlWriter.writeCharacters(content);
149       xmlWriter.writeEndElement();
150     }
151     catch (final XMLStreamException e)
152     {
153       throw new XMLStreamException("Cannot write element '" + tagName
154                                    + "' with content '" + content + "'.", e);
155     }
156   }
157 
158   /**
159    * {@inheritDoc}
160    *
161    * @see de.smartics.testdoc.maven.export.AbstractReportExporter#renderTestDocUutReportLinks(de.smartics.testdoc.report.doc.Type,
162    *      de.smartics.testdoc.report.export.doc.ExternalReportReferences)
163    */
164   @Override
165   protected void renderTestDocUutReportLinks(final Type type,
166       final ExternalReportReferences refs) throws IOException
167   {
168     try
169     {
170       renderReports(type, refs.getUutReports(), null);
171     }
172     catch (final XMLStreamException e)
173     {
174       throw new IOException("Cannot write UUT report links.", e);
175     }
176   }
177 
178   protected void renderReports(final Type type,
179       final List<ExternalReport> reports, final TestMethodDoc testMethod)
180     throws XMLStreamException
181   {
182     if (!reports.isEmpty())
183     {
184       xmlWriter.writeStartElement(TESTDOC_NS, "reports");
185       for (final ExternalReport report : reports)
186       {
187         xmlWriter.writeStartElement(TESTDOC_NS, "report");
188         final String messageKey = report.getLabelKey();
189         final String label = messages.getLabel(messageKey);
190         writeElement("name", label);
191         final String link =
192             report.constructLink(type.getPackageName(), type.getTypeName(),
193                 testMethod);
194         writeElement("link", link);
195 
196         xmlWriter.writeEndElement();
197       }
198       xmlWriter.writeEndElement();
199     }
200   }
201 
202   /**
203    * {@inheritDoc}
204    *
205    * @see de.smartics.testdoc.maven.export.AbstractReportExporter#renderRowNumber(int)
206    */
207   @Override
208   protected void renderRowNumber(final int counter) throws IOException
209   {
210     // Not exported to XML report.
211   }
212 
213   /**
214    * {@inheritDoc}
215    *
216    * @see de.smartics.testdoc.maven.export.AbstractReportExporter#renderJUnitStatus(de.smartics.testdoc.report.doc.ScenarioTestDoc)
217    */
218   @Override
219   protected void renderJUnitStatus(final ScenarioTestDoc scenario)
220     throws IOException
221   {
222     try
223     {
224       final JUnitTestCaseManager junitManager = testDocHelper.getJunitManager();
225       final Type testCaseType = scenario.getTestCaseType();
226       final TestMethodDoc testMethod = scenario.getTestMethod();
227       final String testMethodName = testMethod.getTestName();
228       xmlWriter.writeStartElement(TESTDOC_NS, "junit-status");
229       try
230       {
231         final JUnitTestCaseDoc testCase =
232             junitManager.readTestCase(testCaseType);
233         final JUnitTestMethodDoc method =
234             testCase.getTestMethod(testMethodName);
235 
236         final ImageHelper images = testDocHelper.getImageHelper();
237         final ResultType resultType = method.getResultType();
238         final String link = images.getImageLink(resultType);
239 
240         if (link != null)
241         {
242           writeElement("link", link);
243         }
244         writeElement("result", String.valueOf(resultType));
245       }
246       catch (final TestCaseReportException e)
247       {
248         if (log.isWarnEnabled())
249         {
250           log.warn("Cannot read JUnit information.", e);
251         }
252         writeElement("result", "unknown");
253       }
254       xmlWriter.writeEndElement();
255     }
256     catch (final XMLStreamException e)
257     {
258       throw new IOException("Cannot write JUnit status.", e);
259     }
260   }
261 
262   /**
263    * {@inheritDoc}
264    *
265    * @see de.smartics.testdoc.maven.export.AbstractReportExporter#renderSentence(de.smartics.testdoc.report.doc.ScenarioTestDoc)
266    */
267   @Override
268   protected void renderSentence(final ScenarioTestDoc scenario)
269     throws IOException
270   {
271     try
272     {
273       final String testSentence =
274           messages.getLabel(scenario.getId(), scenario.getTestMethod()
275               .getTestSentence());
276       writeElement("sentence", testSentence);
277     }
278     catch (final XMLStreamException e)
279     {
280       throw new IOException("Cannot write sentence.", e);
281     }
282   }
283 
284   /**
285    * {@inheritDoc}
286    *
287    * @see de.smartics.testdoc.maven.export.AbstractReportExporter#renderCategories(de.smartics.testdoc.report.doc.ScenarioTestDoc)
288    */
289   @Override
290   protected void renderCategories(final ScenarioTestDoc scenario)
291     throws IOException
292   {
293     try
294     {
295       final List<String> categories =
296           testDocHelper.filter(scenario.getCategories());
297       if (!categories.isEmpty())
298       {
299         xmlWriter.writeStartElement(TESTDOC_NS, "categories");
300 
301         for (final String category : categories)
302         {
303           writeElement("category", messages.getLabel(category));
304         }
305 
306         xmlWriter.writeEndElement();
307       }
308     }
309     catch (final XMLStreamException e)
310     {
311       throw new IOException("Cannot write categories.", e);
312     }
313   }
314 
315   /**
316    * {@inheritDoc}
317    *
318    * @see de.smartics.testdoc.maven.export.AbstractReportExporter#renderTestCase(de.smartics.testdoc.report.doc.ScenarioTestDoc)
319    */
320   @Override
321   protected void renderTestCase(final ScenarioTestDoc scenario)
322     throws IOException
323   {
324     try
325     {
326       final TestMethodDoc testMethodDoc = scenario.getTestMethod();
327       final Type type = scenario.getTestCaseType();
328       xmlWriter.writeStartElement(TESTDOC_NS, "test-case");
329       writeType(type);
330 
331       final ExternalReportReferences refs = testDocHelper.getReports();
332       renderReports(type, refs.getTestCaseReports(), testMethodDoc);
333 
334       xmlWriter.writeEndElement();
335     }
336     catch (final XMLStreamException e)
337     {
338       throw new IOException("Cannot write test case.", e);
339     }
340   }
341 
342   public void export(final UnitTestDoc testDoc) throws IOException
343   {
344     try
345     {
346       xmlWriter.writeStartElement(TESTDOC_NS, "uut");
347 
348       export(testDoc, null);
349 
350       xmlWriter.writeEndElement();
351     }
352     catch (final XMLStreamException e)
353     {
354       throw new IOException("Cannot write test case.", e);
355     }
356   }
357 
358   // --- object basics --------------------------------------------------------
359 
360 }