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