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.BufferedReader;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileNotFoundException;
26  import java.io.FileReader;
27  import java.io.IOException;
28  import java.net.URI;
29  import java.nio.charset.Charset;
30  import java.util.List;
31  import org.apache.commons.io.FileUtils;
32  
33  import junit.framework.Assert;
34  
35  import static junit.framework.Assert.assertTrue;
36  
37  /**
38   * @author Kristian Rosenvold
39   */
40  public class TestFile
41  {
42  
43      private final File file;
44  
45      private final Charset encoding;
46  
47      private final OutputValidator surefireVerifier;
48  
49      public TestFile( File file, OutputValidator surefireVerifier )
50      {
51          this( file, Charset.defaultCharset(), surefireVerifier);
52      }
53  
54      public TestFile( File file, Charset charset,OutputValidator surefireVerifier )
55      {
56          this.file = file;
57          this.encoding = charset;
58          this.surefireVerifier = surefireVerifier;
59      }
60  
61      public OutputValidator assertFileExists()
62      {
63          assertTrue( "File doesn't exist: " + file.getAbsolutePath(), file.exists() );
64          return surefireVerifier;
65      }
66  
67      public OutputValidator assertFileNotExists()
68      {
69          assertTrue( "File doesn't exist: " + file.getAbsolutePath(), !file.exists() );
70          return surefireVerifier;
71      }
72  
73      public void delete()
74      {
75          //noinspection ResultOfMethodCallIgnored
76          file.delete();
77      }
78  
79      public String getAbsolutePath()
80      {
81          return file.getAbsolutePath();
82      }
83  
84      public boolean exists()
85      {
86          return file.exists();
87      }
88  
89      public FileInputStream getFileInputStream()
90          throws FileNotFoundException
91      {
92          return new FileInputStream( file );
93      }
94  
95      public String slurpFile()
96      {
97          try
98          {
99              StringBuilder sb = new StringBuilder();
100             BufferedReader reader;
101             reader = new BufferedReader( new FileReader( file ) );
102             for ( String line = reader.readLine(); line != null; line = reader.readLine() )
103             {
104                 sb.append( line );
105             }
106             reader.close();
107             return sb.toString();
108         }
109         catch ( IOException e )
110         {
111             throw new SurefireVerifierException( e );
112         }
113 
114     }
115 
116     public String readFileToString()
117     {
118         try
119         {
120             return FileUtils.readFileToString( file );
121         }
122         catch ( IOException e )
123         {
124             throw new SurefireVerifierException( e );
125         }
126     }
127 
128     public boolean isFile()
129     {
130         return file.isFile();
131     }
132 
133     public TestFile assertContainsText( String text )
134     {
135         final List<String> list = surefireVerifier.loadFile( file, encoding );
136         for ( String line : list )
137         {
138             if ( line.contains( text ) )
139             {
140                 return this;
141             }
142         }
143         Assert.fail( "Did not find expected message in log" );
144         return null;
145     }
146 
147     public URI toURI()
148     {
149         return file.toURI();
150     }
151 }