1 package org.apache.maven.surefire.its.fixture;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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 }