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.File;
22 import java.io.IOException;
23 import java.nio.charset.Charset;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.apache.commons.io.FileUtils;
29 import org.apache.maven.shared.verifier.VerificationException;
30 import org.apache.maven.shared.verifier.Verifier;
31 import org.hamcrest.Matcher;
32
33 import static java.nio.charset.StandardCharsets.UTF_8;
34 import static org.apache.maven.shared.verifier.Verifier.stripAnsi;
35 import static org.hamcrest.MatcherAssert.assertThat;
36
37
38
39
40
41
42 public class OutputValidator {
43 final Verifier verifier;
44
45 private final File baseDir;
46
47 public OutputValidator(Verifier verifier) {
48 this.verifier = verifier;
49 this.baseDir = new File(verifier.getBasedir());
50 }
51
52 public OutputValidator verifyTextInLog(String text) {
53 try {
54 verifier.verifyTextInLog(text);
55 } catch (VerificationException e) {
56 throw new SurefireVerifierException(e);
57 }
58 return this;
59 }
60
61 public OutputValidator verifyTextNotInLog(String text) throws VerificationException {
62 List<String> lines = verifier.loadFile(this.getBasedir(), verifier.getLogFileName(), false);
63 boolean result = false;
64
65 for (String line : lines) {
66 if (stripAnsi(line).contains(text)) {
67 result = true;
68 break;
69 }
70 }
71
72 if (!result) {
73 return this;
74 }
75 throw new SurefireVerifierException(new Exception(text + " found in logs"));
76 }
77
78 public OutputValidator verifyErrorFreeLog() {
79 try {
80 verifier.verifyErrorFreeLog();
81 } catch (VerificationException e) {
82 throw new SurefireVerifierException(e);
83 }
84 return this;
85 }
86
87 public OutputValidator verifyErrorFree(int total) {
88 try {
89 verifier.verifyErrorFreeLog();
90 this.assertTestSuiteResults(total, 0, 0, 0);
91 return this;
92 } catch (VerificationException e) {
93 throw new SurefireVerifierException(e);
94 }
95 }
96
97 public OutputValidator assertThatLogLine(Matcher<String> line, Matcher<Integer> nTimes)
98 throws VerificationException {
99 int counter = loadLogLines(line).size();
100 assertThat("log pattern does not match nTimes", counter, nTimes);
101 return this;
102 }
103
104 public List<String> loadLogLines() throws VerificationException {
105 return verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
106 }
107
108 public List<String> loadLogLines(Matcher<String> line) throws VerificationException {
109 List<String> matchedLines = new ArrayList<>();
110 for (String log : loadLogLines()) {
111 if (line.matches(log)) {
112 matchedLines.add(log);
113 }
114 }
115 return matchedLines;
116 }
117
118 public List<String> loadFile(File file, Charset charset) {
119
120 try {
121 return FileUtils.readLines(file, charset.name());
122 } catch (IOException e) {
123 throw new SurefireVerifierException(e);
124 }
125 }
126
127 public String getBasedir() {
128 return verifier.getBasedir();
129 }
130
131
132
133
134
135
136
137 public File getSubFile(String path) {
138 return new File(getBasedir(), path);
139 }
140
141 public OutputValidator assertTestSuiteResults(int total, int errors, int failures, int skipped) {
142 HelperAssertions.assertTestSuiteResults(total, errors, failures, skipped, baseDir);
143 return this;
144 }
145
146 public OutputValidator assertTestSuiteResults(int total, int errors, int failures, int skipped, int flakes) {
147 HelperAssertions.assertTestSuiteResults(total, errors, failures, skipped, flakes, baseDir);
148 return this;
149 }
150
151 public OutputValidator assertTestSuiteResults(int total) {
152 HelperAssertions.assertTestSuiteResults(total, baseDir);
153 return this;
154 }
155
156 public OutputValidator assertIntegrationTestSuiteResults(int total, int errors, int failures, int skipped) {
157 HelperAssertions.assertIntegrationTestSuiteResults(total, errors, failures, skipped, baseDir);
158 return this;
159 }
160
161 public OutputValidator assertIntegrationTestSuiteResults(int total) {
162 HelperAssertions.assertIntegrationTestSuiteResults(total, baseDir);
163 return this;
164 }
165
166 public TestFile getTargetFile(String modulePath, String fileName) {
167 File targetDir = getSubFile(modulePath + "/target");
168 return new TestFile(new File(targetDir, fileName), this);
169 }
170
171 public TestFile getTargetFile(String fileName) {
172 File targetDir = getSubFile("target");
173 return new TestFile(new File(targetDir, fileName), this);
174 }
175
176 public TestFile getSurefireReportsFile(String fileName, Charset charset) {
177 File targetDir = getSurefireReportsDirectory();
178 return new TestFile(new File(targetDir, fileName), charset, this);
179 }
180
181 public TestFile getSurefireReportsFile(String fileName) {
182 return getSurefireReportsFile(fileName, null);
183 }
184
185 public TestFile getSurefireReportsXmlFile(String fileName) {
186 File targetDir = getSurefireReportsDirectory();
187 return new TestFile(new File(targetDir, fileName), UTF_8, this);
188 }
189
190 public File getSurefireReportsDirectory() {
191 return getSubFile("target/surefire-reports");
192 }
193
194 public TestFile getSiteFile(String fileName) {
195 File targetDir = getSubFile("target/site");
196 return new TestFile(new File(targetDir, fileName), this);
197 }
198
199 public TestFile getReportsFile(String fileName) {
200 File targetDir = getSubFile("target/reports");
201 return new TestFile(new File(targetDir, fileName), this);
202 }
203
204 public File getBaseDir() {
205 return baseDir;
206 }
207
208 public String[] getStringsOrderInLog(String[] strings) throws VerificationException {
209 String[] retArr = new String[strings.length];
210 List<String> strList = new ArrayList<>(Arrays.asList(strings));
211 int i = 0;
212 for (String line : loadLogLines()) {
213 for (int j = 0; j < strList.size(); j++) {
214 if (line.startsWith(strList.get(j))) {
215 retArr[i] = strList.get(j);
216 ++i;
217 if (i == strings.length) {
218 return retArr;
219 }
220 strList.remove(j);
221 break;
222 }
223 }
224 }
225 return retArr;
226 }
227
228 public boolean stringsAppearInSpecificOrderInLog(String[] strings) throws VerificationException {
229 int i = 0;
230 for (String line : loadLogLines()) {
231 if (line.startsWith(strings[i])) {
232 if (i == strings.length - 1) {
233 return true;
234 }
235 ++i;
236 }
237 }
238 return false;
239 }
240 }