Skip to content

Commit 62baf8a

Browse files
Let TokenizerTester/TreeTester take directory name
This change allows TokenizerTester and TreeTester to be given a directory name rather than a list of files (or pathname with a shell wildcard). And when given a directory name, they recurse the directory looking for either *.test files (for TokenizerTester) or *.dat files (for TreeTester), and then run the tests from those files. Without this change, we can’t easily run TokenizerTester and TreeTester from AntRun in Maven — because we can’t use shell wildcards in the “arg” value for the Ant “java” task, and any list of files we otherwise construct within Maven ends up getting put into the java arg value as a single string (single argument) — including the spaces between filenames. So it’s far easier to just give TokenizerTester and TreeTester a directory name, and use directory recursion within Java to get all the filenames (as this change does).
1 parent 6fc41fb commit 62baf8a

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

test-src/nu/validator/htmlparser/test/TokenizerTester.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
package nu.validator.htmlparser.test;
2424

25+
import java.io.ByteArrayInputStream;
26+
import java.io.File;
2527
import java.io.FileInputStream;
2628
import java.io.IOException;
2729
import java.io.InputStream;
@@ -31,6 +33,9 @@
3133
import java.io.StringReader;
3234
import java.io.UnsupportedEncodingException;
3335
import java.io.Writer;
36+
import java.nio.charset.StandardCharsets;
37+
import java.nio.file.Files;
38+
import java.nio.file.Paths;
3439

3540
import nu.validator.htmlparser.common.XmlViolationPolicy;
3641
import nu.validator.htmlparser.impl.ErrorReportingTokenizer;
@@ -97,6 +102,7 @@ private static boolean jsonDeepEquals(JSONValue one, JSONValue other) {
97102

98103
private TokenizerTester(InputStream stream) throws TokenStreamException,
99104
RecognitionException, UnsupportedEncodingException {
105+
tests = null;
100106
tokenHandler = new JSONArrayTokenHandler();
101107
driver = new Driver(new ErrorReportingTokenizer(tokenHandler));
102108
driver.setCommentPolicy(XmlViolationPolicy.ALLOW);
@@ -106,6 +112,9 @@ private TokenizerTester(InputStream stream) throws TokenStreamException,
106112
driver.setXmlnsPolicy(XmlViolationPolicy.ALLOW);
107113
driver.setErrorHandler(tokenHandler);
108114
writer = new OutputStreamWriter(System.out, "UTF-8");
115+
if (stream == null) {
116+
return;
117+
}
109118
JSONParser jsonParser = new JSONParser(new InputStreamReader(stream,
110119
"UTF-8"));
111120
JSONObject obj = (JSONObject) jsonParser.nextValue();
@@ -202,19 +211,48 @@ private void runTestInner(String inputString, JSONArray expectedTokens,
202211
}
203212
}
204213

214+
private void recurseDirectory(File directory) throws Throwable {
215+
if (directory.canRead()) {
216+
File[] files = directory.listFiles();
217+
for (File file : files) {
218+
if (file.isDirectory()) {
219+
recurseDirectory(file);
220+
} else {
221+
test(file.getPath().toString());
222+
}
223+
}
224+
}
225+
}
226+
227+
private void test(String file)
228+
throws IOException, TokenStreamException, RecognitionException,
229+
SAXException {
230+
if (!file.endsWith(".test")) {
231+
return;
232+
}
233+
byte[] fileBytes = Files.readAllBytes(Paths.get(file));
234+
String fileContent = new String(fileBytes, StandardCharsets.UTF_8);
235+
String unescapedContent = fileContent.replace("\\\\u", "\\u");
236+
byte[] newBytes = unescapedContent.getBytes(StandardCharsets.UTF_8);
237+
ByteArrayInputStream bais = new ByteArrayInputStream(newBytes);
238+
TokenizerTester tester = new TokenizerTester(bais);
239+
tester.runTests();
240+
}
241+
242+
205243
/**
206244
* @param args
207-
* @throws RecognitionException
208-
* @throws TokenStreamException
209-
* @throws IOException
210-
* @throws SAXException
245+
* @throws Throwable
211246
*/
212-
public static void main(String[] args) throws TokenStreamException,
213-
RecognitionException, SAXException, IOException {
247+
public static void main(String[] args) throws Throwable {
214248
for (int i = 0; i < args.length; i++) {
215-
TokenizerTester tester = new TokenizerTester(new FileInputStream(
216-
args[i]));
217-
tester.runTests();
249+
TokenizerTester tester = new TokenizerTester(null);
250+
File file = new File(args[i]);
251+
if (file.isDirectory()) {
252+
tester.recurseDirectory(file);
253+
} else {
254+
tester.test(file.getPath().toString());
255+
}
218256
}
219257
}
220258

test-src/nu/validator/htmlparser/test/TreeTester.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.io.BufferedInputStream;
2626
import java.io.BufferedReader;
27+
import java.io.File;
2728
import java.io.FileInputStream;
2829
import java.io.IOException;
2930
import java.io.InputStream;
@@ -39,18 +40,22 @@
3940

4041
public class TreeTester {
4142

42-
private final BufferedInputStream aggregateStream;
43+
private BufferedInputStream aggregateStream;
4344

4445
private boolean streaming = false;
4546

4647
/**
4748
* @param aggregateStream
4849
*/
49-
public TreeTester(InputStream aggregateStream) {
50-
this.aggregateStream = new BufferedInputStream(aggregateStream);
50+
public TreeTester() {
51+
this.aggregateStream = null;
5152
}
5253

53-
private void runTests() throws Throwable {
54+
private void runTests(String filename) throws Throwable {
55+
if (!filename.endsWith(".dat")) {
56+
return;
57+
}
58+
aggregateStream = new BufferedInputStream(new FileInputStream(filename));
5459
if (aggregateStream.read() != '#') {
5560
System.err.println("No hash at start!");
5661
return;
@@ -257,14 +262,35 @@ private boolean skipLabel() throws IOException {
257262
}
258263
}
259264

265+
private void recurseDirectory(File directory) throws Throwable {
266+
if ("scripted".equals(directory.getName())) {
267+
return;
268+
}
269+
if (directory.canRead()) {
270+
File[] files = directory.listFiles();
271+
for (File file : files) {
272+
if (file.isDirectory()) {
273+
recurseDirectory(file);
274+
} else {
275+
runTests(file.getPath().toString());
276+
}
277+
}
278+
}
279+
}
280+
260281
/**
261282
* @param args
262283
* @throws Throwable
263284
*/
264285
public static void main(String[] args) throws Throwable {
265286
for (int i = 0; i < args.length; i++) {
266-
TreeTester tester = new TreeTester(new FileInputStream(args[i]));
267-
tester.runTests();
287+
TreeTester tester = new TreeTester();
288+
File file = new File(args[i]);
289+
if (file.isDirectory()) {
290+
tester.recurseDirectory(file);
291+
} else {
292+
tester.runTests(file.getPath().toString());
293+
}
268294
}
269295
}
270296

0 commit comments

Comments
 (0)