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