1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.testdoc.report.junit;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.List;
21
22 import org.jdom.Document;
23 import org.jdom.Element;
24 import org.jdom.JDOMException;
25 import org.jdom.input.SAXBuilder;
26 import org.jdom.xpath.XPath;
27
28 import de.smartics.testdoc.report.junit.JUnitTestMethodDoc.ResultType;
29
30
31
32
33
34
35
36 public class JUnitXmlReportParser
37 {
38
39
40
41
42
43
44
45
46
47 private final SAXBuilder builder;
48
49
50
51
52
53
54
55
56 public JUnitXmlReportParser()
57 {
58 builder = new SAXBuilder(false);
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public JUnitTestCaseDoc read(final InputStream input) throws JDOMException,
80 IOException
81 {
82 final JUnitTestCaseDoc testCaseDoc = new JUnitTestCaseDoc();
83 final Document document = builder.build(input);
84 @SuppressWarnings("unchecked")
85 final List<Element> testCaseElements =
86 XPath.selectNodes(document, "/testsuite/testcase");
87 for (final Element testCaseElement : testCaseElements)
88 {
89 final JUnitTestMethodDoc testMethodDoc = createTestCase(testCaseElement);
90 testCaseDoc.addTestMethod(testMethodDoc);
91 }
92 return testCaseDoc;
93 }
94
95 private static JUnitTestMethodDoc createTestCase(final Element testCaseElement)
96 {
97 final String time = testCaseElement.getAttribute("time").getValue();
98 final String testCaseType =
99 testCaseElement.getAttribute("classname").getValue();
100 final String testMethodName =
101 testCaseElement.getAttribute("name").getValue();
102
103 final ResultType resultType = getResultType(testCaseElement);
104
105 final JUnitTestMethodDoc testCaseDoc =
106 new JUnitTestMethodDoc(resultType, time, testCaseType, testMethodName);
107 return testCaseDoc;
108 }
109
110 private static ResultType getResultType(final Element testCaseElement)
111 {
112 if (testCaseElement.getChild("skipped") != null)
113 {
114 return ResultType.SKIPPED;
115 }
116 else if (testCaseElement.getChild("failure") != null)
117 {
118 return ResultType.FAILURE;
119 }
120 else if (testCaseElement.getChild("error") != null)
121 {
122 return ResultType.ERROR;
123 }
124 else
125 {
126 return ResultType.SUCCESS;
127 }
128 }
129
130
131
132 }