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