1 package org.apache.maven.surefire.its.fixture;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.charset.Charset;
25 import java.util.List;
26
27 import org.apache.commons.io.FileUtils;
28 import org.apache.maven.it.VerificationException;
29 import org.apache.maven.it.Verifier;
30
31
32
33
34
35
36 public class OutputValidator
37 {
38 protected final Verifier verifier;
39
40 protected final File baseDir;
41
42 public OutputValidator( Verifier verifier )
43 {
44 this.verifier = verifier;
45 this.baseDir = new File( verifier.getBasedir() );
46
47 }
48
49 public OutputValidator verifyTextInLog( String text )
50 {
51 try
52 {
53 verifier.verifyTextInLog( text );
54 }
55 catch ( VerificationException e )
56 {
57 throw new SurefireVerifierException( e );
58 }
59 return this;
60 }
61
62
63 public OutputValidator verifyErrorFreeLog()
64 {
65 try
66 {
67 verifier.verifyErrorFreeLog();
68 }
69 catch ( VerificationException e )
70 {
71 throw new SurefireVerifierException( e );
72 }
73 return this;
74 }
75
76 public OutputValidator verifyErrorFree( int total )
77 {
78 try
79 {
80 verifier.verifyErrorFreeLog();
81 this.assertTestSuiteResults( total, 0, 0, 0 );
82 return this;
83 }
84 catch ( VerificationException e )
85 {
86 throw new SurefireVerifierException( e );
87 }
88 }
89
90 public List<String> loadFile( File file, Charset charset )
91 {
92
93 try
94 {
95 return FileUtils.readLines( file, charset.name() );
96 }
97 catch ( IOException e )
98 {
99 throw new SurefireVerifierException( e );
100 }
101 }
102
103
104
105 public String getBasedir()
106 {
107 return verifier.getBasedir();
108 }
109
110
111
112
113
114
115
116 public File getSubFile( String path )
117 {
118 return new File( getBasedir(), path );
119 }
120
121
122 public OutputValidator assertTestSuiteResults( int total, int errors, int failures, int skipped )
123 {
124 HelperAssertions.assertTestSuiteResults( total, errors, failures, skipped, baseDir );
125 return this;
126 }
127
128 public OutputValidator assertIntegrationTestSuiteResults( int total, int errors, int failures, int skipped )
129 {
130 HelperAssertions.assertIntegrationTestSuiteResults( total, errors, failures, skipped, baseDir );
131 return this;
132 }
133
134 public TestFile getTargetFile( String modulePath, String fileName )
135 {
136 File targetDir = getSubFile( modulePath + "/target" );
137 return new TestFile( new File( targetDir, fileName ), this );
138 }
139
140 public TestFile getTargetFile( String fileName )
141 {
142 File targetDir = getSubFile( "target" );
143 return new TestFile( new File( targetDir, fileName ), this );
144 }
145
146
147 public TestFile getSurefireReportsFile( String fileName )
148 {
149 File targetDir = getSubFile( "target/surefire-reports" );
150 return new TestFile( new File( targetDir, fileName ), this );
151 }
152
153 public TestFile getSurefireReportsXmlFile( String fileName )
154 {
155 File targetDir = getSubFile( "target/surefire-reports" );
156 return new TestFile( new File( targetDir, fileName ), Charset.forName("UTF-8"), this );
157 }
158
159 public TestFile getSiteFile( String fileName )
160 {
161 File targetDir = getSubFile( "target/site" );
162 return new TestFile( new File( targetDir, fileName ), this );
163 }
164
165
166 public File getBaseDir()
167 {
168 return baseDir;
169 }
170
171 @SuppressWarnings( "unchecked" )
172 private List<String> getLog()
173 throws VerificationException
174 {
175 return verifier.loadFile( verifier.getBasedir(), verifier.getLogFileName(), false );
176 }
177
178 public boolean stringsAppearInSpecificOrderInLog( String[] strings )
179 throws VerificationException
180 {
181 int i = 0;
182 for ( String line : getLog() )
183 {
184 if ( line.startsWith( strings[i] ) )
185 {
186 if ( i == strings.length - 1 )
187 {
188 return true;
189 }
190 ++i;
191 }
192 }
193 return false;
194 }
195 }