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.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   * Reads the in-memory representation of a JUnit XML report.
32   *
33   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
34   * @version $Revision:591 $
35   */
36  public class JUnitXmlReportParser
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    // --- members --------------------------------------------------------------
43  
44    /**
45     * The builder to run the XML parser.
46     */
47    private final SAXBuilder builder;
48  
49    // ****************************** Initializer *******************************
50  
51    // ****************************** Constructors ******************************
52  
53    /**
54     * Default constructor.
55     */
56    public JUnitXmlReportParser()
57    {
58      builder = new SAXBuilder(false);
59    }
60  
61    // ****************************** Inner Classes *****************************
62  
63    // ********************************* Methods ********************************
64  
65    // --- init -----------------------------------------------------------------
66  
67    // --- get&set --------------------------------------------------------------
68  
69    // --- business -------------------------------------------------------------
70  
71    /**
72     * Reads the XML information from the stream.
73     *
74     * @param input the stream to read the JUnit XML report from.
75     * @return the parsed instance of the report.
76     * @throws JDOMException on any parsing problem.
77     * @throws IOException if the stream cannot be read.
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   // --- object basics --------------------------------------------------------
131 
132 }