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