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