View Javadoc
1   package org.apache.maven.surefire.its.fixture;
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 java.io.File;
23  import java.io.IOException;
24  import java.nio.charset.Charset;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.commons.io.FileUtils;
29  import org.apache.maven.it.VerificationException;
30  import org.apache.maven.it.Verifier;
31  import org.hamcrest.Matcher;
32  
33  import static org.hamcrest.MatcherAssert.assertThat;
34  
35  /**
36   * A specialized verifier that enforces a standard use case for surefire IT's
37   *
38   * @author Kristian Rosenvold
39   */
40  public class OutputValidator
41  {
42      final Verifier verifier;
43  
44      private final File baseDir;
45  
46      public OutputValidator( Verifier verifier )
47      {
48          this.verifier = verifier;
49          this.baseDir = new File( verifier.getBasedir() );
50      }
51  
52      public OutputValidator verifyTextInLog( String text )
53      {
54          try
55          {
56              verifier.verifyTextInLog( text );
57          }
58          catch ( VerificationException e )
59          {
60              throw new SurefireVerifierException( e );
61          }
62          return this;
63      }
64  
65  
66      public OutputValidator verifyErrorFreeLog()
67      {
68          try
69          {
70              verifier.verifyErrorFreeLog();
71          }
72          catch ( VerificationException e )
73          {
74              throw new SurefireVerifierException( e );
75          }
76          return this;
77      }
78  
79      public OutputValidator verifyErrorFree( int total )
80      {
81          try
82          {
83              verifier.verifyErrorFreeLog();
84              this.assertTestSuiteResults( total, 0, 0, 0 );
85              return this;
86          }
87          catch ( VerificationException e )
88          {
89              throw new SurefireVerifierException( e );
90          }
91      }
92  
93      public OutputValidator assertThatLogLine( Matcher<String> line, Matcher<Integer> nTimes )
94          throws VerificationException
95      {
96          int counter = loadLogLines( line ).size();
97          assertThat( "log pattern does not match nTimes", counter, nTimes );
98          return this;
99      }
100 
101     public List<String> loadLogLines()
102         throws VerificationException
103     {
104         return verifier.loadFile( verifier.getBasedir(), verifier.getLogFileName(), false );
105     }
106 
107     public List<String> loadLogLines( Matcher<String> line )
108             throws VerificationException
109     {
110         List<String> matchedLines = new ArrayList<>();
111         for ( String log : loadLogLines() )
112         {
113             if ( line.matches( log ) )
114             {
115                 matchedLines.add( log );
116             }
117         }
118         return matchedLines;
119     }
120 
121     public List<String> loadFile( File file, Charset charset )
122     {
123         //noinspection unchecked
124         try
125         {
126             return FileUtils.readLines( file, charset.name() );
127         }
128         catch ( IOException e )
129         {
130             throw new SurefireVerifierException( e );
131         }
132     }
133 
134     public String getBasedir()
135     {
136         return verifier.getBasedir();
137     }
138 
139     /**
140      * Returns a file, referenced from the extracted root (where pom.xml is located)
141      *
142      * @param path The subdirectory under basedir
143      * @return A file
144      */
145     public File getSubFile( String path )
146     {
147         return new File( getBasedir(), path );
148     }
149 
150     public OutputValidator assertTestSuiteResults( int total, int errors, int failures, int skipped )
151     {
152         HelperAssertions.assertTestSuiteResults( total, errors, failures, skipped, baseDir );
153         return this;
154     }
155 
156     public OutputValidator assertTestSuiteResults( int total, int errors, int failures, int skipped, int flakes )
157     {
158         HelperAssertions.assertTestSuiteResults( total, errors, failures, skipped, flakes, baseDir );
159         return this;
160     }
161 
162     public OutputValidator assertTestSuiteResults( int total )
163     {
164         HelperAssertions.assertTestSuiteResults( total, baseDir );
165         return this;
166     }
167 
168     public OutputValidator assertIntegrationTestSuiteResults( int total, int errors, int failures, int skipped )
169     {
170         HelperAssertions.assertIntegrationTestSuiteResults( total, errors, failures, skipped, baseDir );
171         return this;
172     }
173 
174     public OutputValidator assertIntegrationTestSuiteResults( int total )
175     {
176         HelperAssertions.assertIntegrationTestSuiteResults( total, baseDir );
177         return this;
178     }
179 
180     public TestFile getTargetFile( String modulePath, String fileName )
181     {
182         File targetDir = getSubFile( modulePath + "/target" );
183         return new TestFile( new File( targetDir, fileName ), this );
184     }
185 
186     public TestFile getTargetFile( String fileName )
187     {
188         File targetDir = getSubFile( "target" );
189         return new TestFile( new File( targetDir, fileName ), this );
190     }
191 
192     public TestFile getSurefireReportsFile( String fileName, Charset charset )
193     {
194         File targetDir = getSurefireReportsDirectory();
195         return new TestFile( new File( targetDir, fileName ), charset, this );
196     }
197 
198     public TestFile getSurefireReportsFile( String fileName )
199     {
200         return getSurefireReportsFile( fileName, null );
201     }
202 
203     public TestFile getSurefireReportsXmlFile( String fileName )
204     {
205         File targetDir = getSurefireReportsDirectory();
206         return new TestFile( new File( targetDir, fileName ), Charset.forName( "UTF-8" ), this );
207     }
208 
209     public File getSurefireReportsDirectory()
210     {
211         return getSubFile( "target/surefire-reports" );
212     }
213 
214     public TestFile getSiteFile( String fileName )
215     {
216         File targetDir = getSubFile( "target/site" );
217         return new TestFile( new File( targetDir, fileName ), this );
218     }
219 
220     public File getBaseDir()
221     {
222         return baseDir;
223     }
224 
225     public boolean stringsAppearInSpecificOrderInLog( String[] strings )
226         throws VerificationException
227     {
228         int i = 0;
229         for ( String line : loadLogLines() )
230         {
231             if ( line.startsWith( strings[i] ) )
232             {
233                 if ( i == strings.length - 1 )
234                 {
235                     return true;
236                 }
237                 ++i;
238             }
239         }
240         return false;
241     }
242 }