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.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   * A specialized verifier that enforces a standard use case for surefire IT's
33   *
34   * @author Kristian Rosenvold
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          //noinspection unchecked
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      * Returns a file, referenced from the extracted root (where pom.xml is located)
112      *
113      * @param path The subdirectory under basedir
114      * @return A file
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 }