1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.its.fixture;
20
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.net.URI;
28 import java.nio.charset.Charset;
29 import java.util.List;
30
31 import junit.framework.Assert;
32 import org.apache.commons.io.FileUtils;
33 import org.hamcrest.Matcher;
34
35 import static java.nio.charset.Charset.defaultCharset;
36 import static junit.framework.Assert.assertFalse;
37 import static junit.framework.Assert.assertTrue;
38 import static org.hamcrest.Matchers.containsString;
39
40
41
42
43 public class TestFile {
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 this(file, defaultCharset(), surefireVerifier);
52 }
53
54 public TestFile(File file, Charset charset, OutputValidator surefireVerifier) {
55 try {
56 this.file = file.getCanonicalFile();
57 this.encoding = charset == null ? defaultCharset() : charset;
58 this.surefireVerifier = surefireVerifier;
59 } catch (IOException e) {
60 throw new IllegalArgumentException(file.getPath());
61 }
62 }
63
64 public OutputValidator assertFileExists() {
65 assertTrue("File doesn't exist: " + file.getAbsolutePath(), file.exists());
66 return surefireVerifier;
67 }
68
69 public OutputValidator assertFileNotExists() {
70 assertFalse("File doesn't exist: " + file.getAbsolutePath(), file.exists());
71 return surefireVerifier;
72 }
73
74 public void delete() {
75
76 file.delete();
77 }
78
79 public String getAbsolutePath() {
80 return file.getAbsolutePath();
81 }
82
83 public boolean exists() {
84 return file.exists();
85 }
86
87 public FileInputStream getFileInputStream() throws FileNotFoundException {
88 return new FileInputStream(file);
89 }
90
91 public String slurpFile() {
92 StringBuilder sb = new StringBuilder();
93 try (BufferedReader reader = new BufferedReader(new InputStreamReader(getFileInputStream(), encoding))) {
94 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
95 sb.append(line);
96 }
97 return sb.toString();
98 } catch (IOException e) {
99 throw new SurefireVerifierException(e);
100 }
101 }
102
103 public String readFileToString() {
104 try {
105 return FileUtils.readFileToString(file, encoding);
106 } catch (IOException e) {
107 throw new SurefireVerifierException(e);
108 }
109 }
110
111 public boolean isFile() {
112 return file.isFile();
113 }
114
115 public TestFile assertContainsText(Matcher<String> matcher) {
116 final List<String> list = surefireVerifier.loadFile(file, encoding);
117 for (String line : list) {
118 if (matcher.matches(line)) {
119 return this;
120 }
121 }
122 Assert.fail("Did not find expected message in log");
123 return null;
124 }
125
126 public TestFile assertContainsText(String text) {
127 return assertContainsText(containsString(text));
128 }
129
130 public TestFile assertNotContainsText(Matcher<String> matcher) {
131 final List<String> list = surefireVerifier.loadFile(file, encoding);
132 for (String line : list) {
133 if (matcher.matches(line)) {
134 Assert.fail("Found unexpected message in log");
135 return null;
136 }
137 }
138 return this;
139 }
140
141 public TestFile assertNotContainsText(String text) {
142 return assertNotContainsText(containsString(text));
143 }
144
145 public URI toURI() {
146 return file.toURI();
147 }
148
149 public File getFile() {
150 return file;
151 }
152 }