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