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.Collection;
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      protected final Verifier verifier;
43  
44      protected 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 = 0;
97          for ( String log : loadLogLines() )
98          {
99              if ( line.matches( log ) )
100             {
101                 counter++;
102             }
103         }
104         assertThat( "log pattern does not match nTimes", counter, nTimes );
105         return this;
106     }
107 
108     public Collection<String> loadLogLines()
109         throws VerificationException
110     {
111         return verifier.loadFile( verifier.getBasedir(), verifier.getLogFileName(), false );
112     }
113 
114     public List<String> loadFile( File file, Charset charset )
115     {
116         //noinspection unchecked
117         try
118         {
119             return FileUtils.readLines( file, charset.name() );
120         }
121         catch ( IOException e )
122         {
123             throw new SurefireVerifierException( e );
124         }
125     }
126 
127     public String getBasedir()
128     {
129         return verifier.getBasedir();
130     }
131 
132     /**
133      * Returns a file, referenced from the extracted root (where pom.xml is located)
134      *
135      * @param path The subdirectory under basedir
136      * @return A file
137      */
138     public File getSubFile( String path )
139     {
140         return new File( getBasedir(), path );
141     }
142 
143     public OutputValidator assertTestSuiteResults( int total, int errors, int failures, int skipped )
144     {
145         HelperAssertions.assertTestSuiteResults( total, errors, failures, skipped, baseDir );
146         return this;
147     }
148 
149     public OutputValidator assertTestSuiteResults( int total, int errors, int failures, int skipped, int flakes )
150     {
151         HelperAssertions.assertTestSuiteResults( total, errors, failures, skipped, flakes, baseDir );
152         return this;
153     }
154 
155     public OutputValidator assertTestSuiteResults( int total )
156     {
157         HelperAssertions.assertTestSuiteResults( total, baseDir );
158         return this;
159     }
160 
161     public OutputValidator assertIntegrationTestSuiteResults( int total, int errors, int failures, int skipped )
162     {
163         HelperAssertions.assertIntegrationTestSuiteResults( total, errors, failures, skipped, baseDir );
164         return this;
165     }
166 
167     public OutputValidator assertIntegrationTestSuiteResults( int total )
168     {
169         HelperAssertions.assertIntegrationTestSuiteResults( total, baseDir );
170         return this;
171     }
172 
173     public TestFile getTargetFile( String modulePath, String fileName )
174     {
175         File targetDir = getSubFile( modulePath + "/target" );
176         return new TestFile( new File( targetDir, fileName ), this );
177     }
178 
179     public TestFile getTargetFile( String fileName )
180     {
181         File targetDir = getSubFile( "target" );
182         return new TestFile( new File( targetDir, fileName ), this );
183     }
184 
185     public TestFile getSurefireReportsFile( String fileName )
186     {
187         File targetDir = getSurefireReportsDirectory();
188         return new TestFile( new File( targetDir, fileName ), this );
189     }
190 
191     public TestFile getSurefireReportsXmlFile( String fileName )
192     {
193         File targetDir = getSurefireReportsDirectory();
194         return new TestFile( new File( targetDir, fileName ), Charset.forName( "UTF-8" ), this );
195     }
196 
197     public File getSurefireReportsDirectory()
198     {
199         return getSubFile( "target/surefire-reports" );
200     }
201 
202     public TestFile getSiteFile( String fileName )
203     {
204         File targetDir = getSubFile( "target/site" );
205         return new TestFile( new File( targetDir, fileName ), this );
206     }
207 
208     public File getBaseDir()
209     {
210         return baseDir;
211     }
212 
213     public boolean stringsAppearInSpecificOrderInLog( String[] strings )
214         throws VerificationException
215     {
216         int i = 0;
217         for ( String line : loadLogLines() )
218         {
219             if ( line.startsWith( strings[i] ) )
220             {
221                 if ( i == strings.length - 1 )
222                 {
223                     return true;
224                 }
225                 ++i;
226             }
227         }
228         return false;
229     }
230 }