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