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