View Javadoc
1   package org.apache.maven.plugins.surefire.report;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.InputStreamReader;
27  import java.util.Collection;
28  
29  import javax.xml.parsers.ParserConfigurationException;
30  
31  import junit.framework.TestCase;
32  import org.xml.sax.SAXException;
33  
34  /**
35   * @author Kristian Rosenvold
36   */
37  public class TestSuiteXmlParserTest
38      extends TestCase
39  {
40      public void testParse()
41          throws Exception
42      {
43          TestSuiteXmlParser testSuiteXmlParser = new TestSuiteXmlParser();
44          String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
45              "<testsuite failures=\"4\" time=\"0.005\" errors=\"0\" skipped=\"0\" tests=\"4\" name=\"wellFormedXmlFailures.TestSurefire3\">\n"
46              +
47              "  <properties>\n" +
48              "    <property name=\"java.runtime.name\" value=\"Java(TM) SE Runtime Environment\"/>\n" +
49              "    <property name=\"sun.cpu.isalist\" value=\"amd64\"/>\n" +
50              "  </properties>\n" +
51              "  <testcase time=\"0.005\" classname=\"wellFormedXmlFailures.TestSurefire3\" name=\"testLower\">\n" +
52              "    <failure message=\"&lt;\" type=\"junit.framework.AssertionFailedError\"><![CDATA[junit.framework.AssertionFailedError: <\n"
53              +
54              "\tat junit.framework.Assert.fail(Assert.java:47)\n" +
55              "\tat wellFormedXmlFailures.TestSurefire3.testLower(TestSurefire3.java:30)\n" +
56              "]]></failure>\n" +
57              "  </testcase>\n" +
58              "  <testcase time=\"0\" classname=\"wellFormedXmlFailures.TestSurefire3\" name=\"testU0000\">\n" +
59              "    <failure message=\"&amp;0#;\" type=\"junit.framework.AssertionFailedError\">junit.framework.AssertionFailedError:  \n"
60              +
61              "\tat junit.framework.Assert.fail(Assert.java:47)\n" +
62              "\tat wellFormedXmlFailures.TestSurefire3.testU0000(TestSurefire3.java:40)\n" +
63              "</failure>\n" +
64              "  </testcase>\n" +
65              "  <testcase time=\"0\" classname=\"wellFormedXmlFailures.TestSurefire3\" name=\"testGreater\">\n" +
66              "    <failure message=\"&gt;\" type=\"junit.framework.AssertionFailedError\">junit.framework.AssertionFailedError: >\n"
67              +
68              "\tat junit.framework.Assert.fail(Assert.java:47)\n" +
69              "\tat wellFormedXmlFailures.TestSurefire3.testGreater(TestSurefire3.java:35)\n" +
70              "</failure>\n" +
71              "  </testcase>\n" +
72              "  <testcase time=\"0\" classname=\"wellFormedXmlFailures.TestSurefire3\" name=\"testQuote\">\n" +
73              "    <failure message=\"&quot;\" type=\"junit.framework.AssertionFailedError\">junit.framework.AssertionFailedError: \"\n"
74              +
75              "\tat junit.framework.Assert.fail(Assert.java:47)\n" +
76              "\tat wellFormedXmlFailures.TestSurefire3.testQuote(TestSurefire3.java:25)\n" +
77              "</failure>\n" +
78              "  </testcase>\n" +
79              "</testsuite>";
80          InputStream byteArrayIs = new ByteArrayInputStream( xml.getBytes() );
81          Collection<ReportTestSuite> parse = testSuiteXmlParser.parse( new InputStreamReader(byteArrayIs, "UTF-8") );
82      }
83  
84      public void testParser()
85          throws IOException, SAXException, ParserConfigurationException
86      {
87          TestSuiteXmlParser parser = new TestSuiteXmlParser();
88  
89          Collection<ReportTestSuite> oldResult = parser.parse(
90              "src/test/resources/fixture/testsuitexmlparser/TEST-org.apache.maven.surefire.test.FailingTest.xml" );
91  
92          assertNotNull( oldResult );
93  
94          assertEquals( 1, oldResult.size() );
95          ReportTestSuite next = oldResult.iterator().next();
96          assertEquals( 2, next.getNumberOfTests() );
97  
98  
99      }
100 
101     public void noTestParserBadFile() // Determine problem with xml file.
102         throws IOException, SAXException, ParserConfigurationException
103     {
104         TestSuiteXmlParser parser = new TestSuiteXmlParser();
105 
106         File file = new File( "../surefire-integration-tests/target/UmlautDirIT/junit-pathWith\u00DCmlaut/target/surefire-reports/TEST-umlautTest.BasicTest.xml"  );
107         assertTrue(file.exists());
108         Collection<ReportTestSuite> oldResult = parser.parse(
109             "..\\surefire-integration-tests\\target\\UmlautDirIT\\junit-pathWith\u00DCmlaut\\target\\surefire-reports\\TEST-umlautTest.BasicTest.xml" );
110 
111         assertNotNull( oldResult );
112 
113         assertEquals( 1, oldResult.size() );
114         ReportTestSuite next = oldResult.iterator().next();
115         assertEquals( 2, next.getNumberOfTests() );
116 
117 
118     }
119 
120     public void testParserHitsFailsafeSummary()
121         throws IOException, SAXException, ParserConfigurationException
122     {
123         TestSuiteXmlParser parser = new TestSuiteXmlParser();
124 
125         parser.parse( "src/test/resources/fixture/testsuitexmlparser/failsafe-summary.xml" );
126 
127         assertFalse( parser.isValid() );
128 
129         parser.parse(
130             "src/test/resources/fixture/testsuitexmlparser/TEST-org.apache.maven.surefire.test.FailingTest.xml" );
131 
132         assertTrue( parser.isValid() );
133     }
134 
135 
136 }