1   package org.apache.maven.plugins.surefire.report;
2   
3   /*
4    * Copyright 2001-2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.TestCase;
20  import org.apache.maven.reporting.MavenReportException;
21  
22  import java.io.File;
23  import java.text.NumberFormat;
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Locale;
28  import java.util.Map;
29  
30  public class SurefireReportParserTest
31      extends TestCase
32  {
33      private SurefireReportParser report;
34  
35      protected void setUp()
36          throws Exception
37      {
38          super.setUp();
39  
40          report = new SurefireReportParser();
41  
42          report.setLocale( Locale.ENGLISH );
43      }
44  
45      public void testParseXMLReportFiles()
46          throws MavenReportException
47      {
48          report.setReportsDirectory( new File( System.getProperty( "basedir" ), "target/test-classes/test-reports" ) );
49  
50          List suites = report.parseXMLReportFiles();
51  
52          assertEquals( 4, suites.size() );
53  
54          Iterator it = suites.iterator();
55          while ( it.hasNext() )
56          {
57              ReportTestSuite suite = (ReportTestSuite) it.next();
58              assertNotNull( suite.getName() + " was not correctly parsed", suite.getTestCases() );
59              assertNotNull( suite.getName() );
60              assertNotNull( suite.getPackageName() );
61          }
62      }
63  
64      public void testParseTestSuiteName()
65      {
66          assertEquals( "CircleTest", report.parseTestSuiteName( "Battery: com.shape.CircleTest" ) );
67      }
68  
69      public void testParseTestSuitePackageName()
70      {
71          assertEquals( "com.shape", report.parseTestSuitePackageName( "Battery: com.shape.CircleTest" ) );
72      }
73  
74      public void testParseTestCaseName()
75      {
76          assertEquals( "testCase", report.parseTestCaseName( "testCase(com.shape.CircleTest)" ) );
77      }
78  
79      public void testGetSummary()
80          throws Exception
81      {
82          ReportTestSuite tSuite1 = new ReportTestSuite();
83  
84          ReportTestSuite tSuite2 = new ReportTestSuite();
85  
86          tSuite1.setNumberOfErrors( 10 );
87  
88          tSuite1.setNumberOfFailures( 20 );
89  
90          tSuite1.setNumberOfSkipped( 2 );
91  
92          tSuite1.setTimeElapsed( 1.0f );
93  
94          tSuite1.setNumberOfTests( 100 );
95  
96          tSuite2.setNumberOfErrors( 10 );
97  
98          tSuite2.setNumberOfFailures( 20 );
99          
100         tSuite2.setNumberOfSkipped( 2 );
101 
102         tSuite2.setTimeElapsed( 1.0f );
103 
104         tSuite2.setNumberOfTests( 100 );
105 
106         List suiteList = new ArrayList();
107 
108         suiteList.add( tSuite1 );
109 
110         suiteList.add( tSuite2 );
111 
112         Map testMap = report.getSummary( suiteList );
113 
114         assertEquals( 20, Integer.parseInt( testMap.get( "totalErrors" ).toString() ) );
115 
116         assertEquals( 40, Integer.parseInt( testMap.get( "totalFailures" ).toString() ) );
117 
118         assertEquals( 200, Integer.parseInt( testMap.get( "totalTests" ).toString() ) );
119 
120         assertEquals( 4, Integer.parseInt( testMap.get( "totalSkipped" ).toString() ) );
121 
122         NumberFormat numberFormat = report.getNumberFormat();
123 
124         assertEquals( 2.0f, numberFormat.parse( testMap.get( "totalElapsedTime" ).toString() )
125             .floatValue(), 0.0f );
126 
127         assertEquals( 68.00f, numberFormat.parse( (String) testMap.get( "totalPercentage" ) )
128             .floatValue(), 0 );
129     }
130 
131     public void testSetReportsDirectory()
132     {
133         report.setReportsDirectory( new File( "Reports_Directory" ) );
134 
135         assertEquals( new File( "Reports_Directory" ), report.getReportsDirectory() );
136     }
137 
138     public void testGetSuitesGroupByPackage()
139     {
140         ReportTestSuite tSuite1 = new ReportTestSuite();
141 
142         ReportTestSuite tSuite2 = new ReportTestSuite();
143 
144         ReportTestSuite tSuite3 = new ReportTestSuite();
145 
146         tSuite1.setPackageName( "Package1" );
147 
148         tSuite2.setPackageName( "Package1" );
149 
150         tSuite3.setPackageName( "Package2" );
151 
152         List suiteList = new ArrayList();
153 
154         suiteList.add( tSuite1 );
155 
156         suiteList.add( tSuite2 );
157 
158         suiteList.add( tSuite3 );
159 
160         Map groupMap = report.getSuitesGroupByPackage( suiteList );
161 
162         assertEquals( 2, groupMap.size() );
163 
164         assertEquals( tSuite1, ( (List) groupMap.get( "Package1" ) ).get( 0 ) );
165 
166         assertEquals( tSuite2, ( (List) groupMap.get( "Package1" ) ).get( 1 ) );
167 
168         assertEquals( tSuite3, ( (List) groupMap.get( "Package2" ) ).get( 0 ) );
169     }
170 
171     public void testComputePercentage()
172         throws Exception
173     {
174         NumberFormat numberFormat = report.getNumberFormat();
175 
176         assertEquals( 70.00f, numberFormat.parse( report.computePercentage( 100, 20, 10, 0 ) )
177             .floatValue(), 0 );
178     }
179 
180     public void testGetFailureDetails()
181     {
182         ReportTestSuite tSuite1 = new ReportTestSuite();
183 
184         ReportTestSuite tSuite2 = new ReportTestSuite();
185 
186         ReportTestCase tCase1 = new ReportTestCase();
187 
188         ReportTestCase tCase2 = new ReportTestCase();
189 
190         ReportTestCase tCase3 = new ReportTestCase();
191 
192         tCase1.addFailure( null, null );
193 
194         tCase3.addFailure( null, null );
195 
196         List tCaseList = new ArrayList();
197 
198         List tCaseList2 = new ArrayList();
199 
200         tCaseList.add( tCase1 );
201 
202         tCaseList.add( tCase2 );
203 
204         tCaseList2.add( tCase3 );
205 
206         tSuite1.setTestCases( tCaseList );
207 
208         tSuite2.setTestCases( tCaseList2 );
209 
210         List suiteList = new ArrayList();
211 
212         suiteList.add( tSuite1 );
213 
214         suiteList.add( tSuite2 );
215 
216         List failList = report.getFailureDetails( suiteList );
217 
218         assertEquals( 2, failList.size() );
219 
220         assertEquals( tCase1, failList.get( 0 ) );
221 
222         assertEquals( tCase3, failList.get( 1 ) );
223     }
224 }