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.File;
23  import java.io.UnsupportedEncodingException;
24  import java.net.URL;
25  import java.net.URLDecoder;
26  import java.text.NumberFormat;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Locale;
30  import java.util.Map;
31  import org.apache.maven.reporting.MavenReportException;
32  
33  import junit.framework.TestCase;
34  
35  /**
36   *
37   */
38  public class SurefireReportParserTest
39      extends TestCase
40  {
41      private SurefireReportParser report;
42  
43      /**
44       * {@inheritDoc}
45       */
46      protected void setUp()
47          throws Exception
48      {
49          super.setUp();
50          report = new SurefireReportParser( null, Locale.ENGLISH );
51      }
52  
53      public void testParseXMLReportFiles()
54          throws MavenReportException, UnsupportedEncodingException
55      {
56          report.setReportsDirectory( getTestDir( "/test-reports" ) );
57  
58          List<ReportTestSuite> suites = report.parseXMLReportFiles();
59  
60          assertEquals( 8, suites.size() );
61  
62          for ( ReportTestSuite suite : suites )
63          {
64              assertNotNull( suite.getName() + " was not correctly parsed", suite.getTestCases() );
65              assertNotNull( suite.getName() );
66              assertNotNull( suite.getPackageName() );
67          }
68      }
69  
70      private File getTestDir( String path )
71          throws UnsupportedEncodingException
72      {
73          URL resource = getClass().getResource( path );
74          // URLDecoder.decode necessary for JDK 1.5+, where spaces are escaped to %20
75          return new File( URLDecoder.decode( resource.getPath(), "UTF-8" ) ).getAbsoluteFile();
76      }
77  
78      public void testParseTestSuiteName()
79      {
80          assertEquals( "CircleTest", report.parseTestSuiteName( "Battery: com.shape.CircleTest" ) );
81      }
82  
83      public void testParseTestSuitePackageName()
84      {
85          assertEquals( "com.shape", report.parseTestSuitePackageName( "Battery: com.shape.CircleTest" ) );
86      }
87  
88      public void testParseTestCaseName()
89      {
90          assertEquals( "testCase", report.parseTestCaseName( "testCase(com.shape.CircleTest)" ) );
91      }
92  
93      public void testGetSummary()
94          throws Exception
95      {
96          ReportTestSuite tSuite1 = new ReportTestSuite()
97              .setNumberOfErrors( 10 )
98              .setNumberOfFailures( 20 )
99              .setNumberOfSkipped( 2 )
100             .setTimeElapsed( 1.0f )
101             .setNumberOfTests( 100 );
102 
103         ReportTestSuite tSuite2 = new ReportTestSuite()
104             .setNumberOfErrors( 10 )
105             .setNumberOfFailures( 20 )
106             .setNumberOfSkipped( 2 )
107             .setTimeElapsed( 1.0f )
108             .setNumberOfTests( 100 );
109 
110         List<ReportTestSuite> suites = new ArrayList<ReportTestSuite>();
111 
112         suites.add( tSuite1 );
113 
114         suites.add( tSuite2 );
115 
116         Map<String, String> testMap = report.getSummary( suites );
117 
118         assertEquals( 20, Integer.parseInt( testMap.get( "totalErrors" ) ) );
119 
120         assertEquals( 40, Integer.parseInt( testMap.get( "totalFailures" ) ) );
121 
122         assertEquals( 200, Integer.parseInt( testMap.get( "totalTests" ) ) );
123 
124         assertEquals( 4, Integer.parseInt( testMap.get( "totalSkipped" ) ) );
125 
126         NumberFormat numberFormat = report.getNumberFormat();
127 
128         assertEquals( 2.0f, numberFormat.parse( testMap.get( "totalElapsedTime" ) ).floatValue(), 0.0f );
129 
130         assertEquals( 68.00f, numberFormat.parse( testMap.get( "totalPercentage" ) ).floatValue(), 0 );
131     }
132 
133     public void testGetSuitesGroupByPackage()
134     {
135         ReportTestSuite tSuite1 = new ReportTestSuite();
136 
137         ReportTestSuite tSuite2 = new ReportTestSuite();
138 
139         ReportTestSuite tSuite3 = new ReportTestSuite();
140 
141         tSuite1.setPackageName( "Package1" );
142 
143         tSuite2.setPackageName( "Package1" );
144 
145         tSuite3.setPackageName( "Package2" );
146 
147         List<ReportTestSuite> suites = new ArrayList<ReportTestSuite>();
148 
149         suites.add( tSuite1 );
150 
151         suites.add( tSuite2 );
152 
153         suites.add( tSuite3 );
154 
155         Map<String, List<ReportTestSuite>> groupMap = report.getSuitesGroupByPackage( suites );
156 
157         assertEquals( 2, groupMap.size() );
158 
159         assertEquals( tSuite1, groupMap.get( "Package1" ).get( 0 ) );
160 
161         assertEquals( tSuite2, groupMap.get( "Package1" ).get( 1 ) );
162 
163         assertEquals( tSuite3, groupMap.get( "Package2" ).get( 0 ) );
164     }
165 
166     public void testComputePercentage()
167         throws Exception
168     {
169         NumberFormat numberFormat = report.getNumberFormat();
170 
171         assertEquals( 70.00f, numberFormat.parse( report.computePercentage( 100, 20, 10, 0 ) ).floatValue(), 0 );
172     }
173 
174     public void testGetFailureDetails()
175     {
176         ReportTestSuite tSuite1 = new ReportTestSuite();
177 
178         ReportTestSuite tSuite2 = new ReportTestSuite();
179 
180         ReportTestCase tCase1 = new ReportTestCase();
181 
182         ReportTestCase tCase2 = new ReportTestCase();
183 
184         ReportTestCase tCase3 = new ReportTestCase();
185 
186         tCase1.setFailure( null, IllegalArgumentException.class.getName() );
187 
188         tCase3.setFailure( "index: 0, size: 0", IndexOutOfBoundsException.class.getName() );
189 
190         List<ReportTestCase> tCases = new ArrayList<ReportTestCase>();
191 
192         List<ReportTestCase> tCases2 = new ArrayList<ReportTestCase>();
193 
194         tCases.add( tCase1 );
195 
196         tCases.add( tCase2 );
197 
198         tCases2.add( tCase3 );
199 
200         tSuite1.setTestCases( tCases );
201 
202         tSuite2.setTestCases( tCases2 );
203 
204         List<ReportTestSuite> suites = new ArrayList<ReportTestSuite>();
205 
206         suites.add( tSuite1 );
207 
208         suites.add( tSuite2 );
209 
210         List<ReportTestCase> failures = report.getFailureDetails( suites );
211 
212         assertEquals( 2, failures.size() );
213 
214         assertEquals( tCase1, failures.get( 0 ) );
215 
216         assertEquals( tCase3, failures.get( 1 ) );
217     }
218 }