1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
46
47
48
49
50 public class XmlExporter extends AbstractReportExporter
51 {
52
53
54
55
56 protected static final String TESTDOC_NS =
57 "http://www.smartics.de/testdoc/1.0";
58
59
60
61
62
63
64 private final Logger log = LoggerFactory.getLogger(XmlExporter.class);
65
66
67
68
69 private final XMLStreamWriter xmlWriter;
70
71
72
73
74
75
76
77
78
79
80
81
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
91
92
93
94
95
96
97
98
99
100
101
102
103 public XMLStreamWriter getXmlWriter()
104 {
105 return xmlWriter;
106 }
107
108
109
110
111
112
113
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
160
161
162
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
204
205
206
207 @Override
208 protected void renderRowNumber(final int counter) throws IOException
209 {
210
211 }
212
213
214
215
216
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
264
265
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
286
287
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
317
318
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
359
360 }