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