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