1 package org.apache.maven.surefire.its.misc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.maven.surefire.its.IntegrationTestSuiteResults;
22
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Locale;
27
28 import junit.framework.Assert;
29
30 @SuppressWarnings( { "JavaDoc" } )
31 public class HelperAssertions
32 {
33
34
35
36 public static void assertTestSuiteResults( int total, int errors, int failures, int skipped, File testDir )
37 {
38 IntegrationTestSuiteResults suite = parseTestResults( new File[]{ testDir } );
39 assertTestSuiteResults( total, errors, failures, skipped, suite );
40 }
41
42 public static void assertTestSuiteResults( int total, int errors, int failures, int skipped,
43 IntegrationTestSuiteResults actualSuite )
44 {
45 Assert.assertEquals( "wrong number of tests", total, actualSuite.getTotal() );
46 Assert.assertEquals( "wrong number of errors", errors, actualSuite.getErrors() );
47 Assert.assertEquals( "wrong number of failures", failures, actualSuite.getFailures() );
48 Assert.assertEquals( "wrong number of skipped", skipped, actualSuite.getSkipped() );
49 }
50
51 public static IntegrationTestSuiteResults parseTestResults( File testDir )
52 {
53 return parseTestResults( new File[]{ testDir } );
54 }
55
56 public static IntegrationTestSuiteResults parseTestResults( File[] testDirs )
57 {
58 List<ReportTestSuite> reports = extractReports( testDirs );
59 return parseReportList( reports );
60 }
61
62
63
64
65 public static IntegrationTestSuiteResults parseReportList( List<ReportTestSuite> reports )
66 {
67 Assert.assertTrue( "No reports!", reports.size() > 0 );
68 int total = 0, errors = 0, failures = 0, skipped = 0;
69 for ( int i = 0; i < reports.size(); i++ )
70 {
71 ReportTestSuite suite = (ReportTestSuite) reports.get( i );
72 total += suite.getNumberOfTests();
73 errors += suite.getNumberOfErrors();
74 failures += suite.getNumberOfFailures();
75 skipped += suite.getNumberOfSkipped();
76 }
77 return new IntegrationTestSuiteResults( total, errors, failures, skipped );
78 }
79
80 public static List<ReportTestSuite> extractReports( File[] testDirs )
81 {
82 List<File> reportsDirs = new ArrayList<File>();
83 for ( int i = 0; i < testDirs.length; i++ )
84 {
85 File testDir = testDirs[i];
86 File reportsDir = new File( testDir, "target/surefire-reports" );
87 Assert.assertTrue( "Reports directory is missing: " + reportsDir.getAbsolutePath(), reportsDir.exists() );
88 reportsDirs.add( reportsDir );
89 }
90 SurefireReportParser parser = new SurefireReportParser( reportsDirs, Locale.getDefault() );
91 List<ReportTestSuite> reports;
92 try
93 {
94 reports = parser.parseXMLReportFiles();
95 }
96 catch ( Exception e )
97 {
98 throw new RuntimeException( "Couldn't parse XML reports", e );
99 }
100 return reports;
101 }
102 }