Skip to content

Commit 12c5283

Browse files
committed
HTMLElementsWithCache included in tests
1 parent c6f23fc commit 12c5283

File tree

6 files changed

+117
-37
lines changed

6 files changed

+117
-37
lines changed

src/test/java/org/htmlunit/cyberneko/CanonicalDomFragmentTest.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,18 @@ public class CanonicalDomFragmentTest extends AbstractCanonicalTest {
4949
@ParameterizedTest
5050
@MethodSource("testFiles")
5151
public void runTest(final File dataFile) throws Exception {
52-
final String domDataLines = getResult(dataFile);
52+
final String dataLines = getResult(dataFile, false);
53+
test(dataFile, dataLines);
54+
}
55+
56+
@ParameterizedTest
57+
@MethodSource("testFiles")
58+
public void runTestWithCachedElementsProvider(final File dataFile) throws Exception {
59+
final String dataLines = getResult(dataFile, true);
60+
test(dataFile, dataLines);
61+
}
5362

63+
private static void test(final File dataFile, final String dataLines) throws Exception {
5464
try {
5565
// prepare for future changes where canonical files are next to test file
5666
File canonicalFile = new File(dataFile.getParentFile(), dataFile.getName() + ".canonical-frg");
@@ -75,23 +85,23 @@ public void runTest(final File dataFile) throws Exception {
7585
}
7686

7787
if (!canonicalFile.exists()) {
78-
fail("Canonical file not found for input: " + dataFile.getAbsolutePath() + ": " + domDataLines);
88+
fail("Canonical file not found for input: " + dataFile.getAbsolutePath() + ": " + dataLines);
7989
}
8090

8191
final File nyiFile = new File(canonicalFile.getParentFile(), canonicalFile.getName() + ".nyi");
8292
if (nyiFile.exists()) {
8393
try {
84-
assertEquals(getCanonical(canonicalFile), domDataLines, dataFile.toString());
94+
assertEquals(getCanonical(canonicalFile), dataLines, dataFile.toString());
8595
fail("test " + dataFile.getName() + "is marked as not yet implemented but already works");
8696
}
8797
catch (final AssertionFailedError e) {
8898
// expected
8999
}
90100

91-
assertEquals(getCanonical(nyiFile), domDataLines, "NYI: " + dataFile);
101+
assertEquals(getCanonical(nyiFile), dataLines, "NYI: " + dataFile);
92102
}
93103
else {
94-
assertEquals(getCanonical(canonicalFile), domDataLines, dataFile.toString());
104+
assertEquals(getCanonical(canonicalFile), dataLines, dataFile.toString());
95105
}
96106
}
97107
catch (final AssertionFailedError e) {
@@ -100,15 +110,22 @@ public void runTest(final File dataFile) throws Exception {
100110
final File output = new File(OUTOUT_DIR, path + ".canonical-frg");
101111
Files.createDirectories(Paths.get(output.getParentFile().getPath()));
102112
try (PrintWriter pw = new PrintWriter(Files.newOutputStream(output.toPath()))) {
103-
pw.print(domDataLines);
113+
pw.print(dataLines);
104114
}
105115
throw e;
106116
}
107117
}
108118

109-
private static String getResult(final File infile) throws Exception {
119+
private static String getResult(final File infile, final boolean cached) throws Exception {
110120
try (StringWriter out = new StringWriter()) {
111-
final DOMFragmentParser parser = new DOMFragmentParser();
121+
final DOMFragmentParser parser;
122+
if (cached) {
123+
parser = new DOMFragmentParser(new HTMLElements.HTMLElementsWithCache(new HTMLElements()));
124+
}
125+
else {
126+
parser = new DOMFragmentParser();
127+
}
128+
112129

113130
final CoreDocumentImpl document = new CoreDocumentImpl();
114131
final DocumentFragmentImpl fragment = (DocumentFragmentImpl) document.createDocumentFragment();

src/test/java/org/htmlunit/cyberneko/CanonicalDomHtmlDocumentTest.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,18 @@ public class CanonicalDomHtmlDocumentTest extends AbstractCanonicalTest {
4848
@ParameterizedTest
4949
@MethodSource("testFiles")
5050
public void runTest(final File dataFile) throws Exception {
51-
final String domDataLines = getResult(dataFile);
51+
final String dataLines = getResult(dataFile, false);
52+
test(dataFile, dataLines);
53+
}
54+
55+
@ParameterizedTest
56+
@MethodSource("testFiles")
57+
public void runTestWithCachedElementsProvider(final File dataFile) throws Exception {
58+
final String dataLines = getResult(dataFile, true);
59+
test(dataFile, dataLines);
60+
}
5261

62+
private static void test(final File dataFile, final String dataLines) throws Exception {
5363
try {
5464
// prepare for future changes where canonical files are next to test file
5565
File canonicalFile = new File(dataFile.getParentFile(), dataFile.getName() + ".canonical-domhtml");
@@ -72,23 +82,23 @@ public void runTest(final File dataFile) throws Exception {
7282
}
7383

7484
if (!canonicalFile.exists()) {
75-
fail("Canonical file not found for input: " + dataFile.getAbsolutePath() + ": " + domDataLines);
85+
fail("Canonical file not found for input: " + dataFile.getAbsolutePath() + ": " + dataLines);
7686
}
7787

7888
final File nyiFile = new File(canonicalFile.getParentFile(), canonicalFile.getName() + ".nyi");
7989
if (nyiFile.exists()) {
8090
try {
81-
assertEquals(getCanonical(canonicalFile), domDataLines, dataFile.toString());
91+
assertEquals(getCanonical(canonicalFile), dataLines, dataFile.toString());
8292
fail("test " + dataFile.getName() + "is marked as not yet implemented but already works");
8393
}
8494
catch (final AssertionFailedError e) {
8595
// expected
8696
}
8797

88-
assertEquals(getCanonical(nyiFile), domDataLines, "NYI: " + dataFile);
98+
assertEquals(getCanonical(nyiFile), dataLines, "NYI: " + dataFile);
8999
}
90100
else {
91-
assertEquals(getCanonical(canonicalFile), domDataLines, dataFile.toString());
101+
assertEquals(getCanonical(canonicalFile), dataLines, dataFile.toString());
92102
}
93103
}
94104
catch (final AssertionFailedError e) {
@@ -98,15 +108,21 @@ public void runTest(final File dataFile) throws Exception {
98108
new File(OUTOUT_DIR + path).mkdirs();
99109
final File output = new File(OUTOUT_DIR + path + dataFile.getName() + ".canonical-domhtml");
100110
try (PrintWriter pw = new PrintWriter(Files.newOutputStream(output.toPath()))) {
101-
pw.print(domDataLines);
111+
pw.print(dataLines);
102112
}
103113
throw e;
104114
}
105115
}
106116

107-
private static String getResult(final File infile) throws Exception {
117+
private static String getResult(final File infile, final boolean cached) throws Exception {
108118
try (StringWriter out = new StringWriter()) {
109-
final DOMParser parser = new DOMParser(HTMLDocumentImpl.class);
119+
final DOMParser parser;
120+
if (cached) {
121+
parser = new DOMParser(new HTMLElements.HTMLElementsWithCache(new HTMLElements()), HTMLDocumentImpl.class);
122+
}
123+
else {
124+
parser = new DOMParser(HTMLDocumentImpl.class);
125+
}
110126

111127
final String infilename = infile.toString();
112128
final File insettings = new File(infilename + ".settings");

src/test/java/org/htmlunit/cyberneko/CanonicalDomTest.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,23 @@ public class CanonicalDomTest extends AbstractCanonicalTest {
5555
public void runTest(final File dataFile) throws Exception {
5656
final String infilename = dataFile.toString();
5757

58-
// for (int i = 1024 * 10; i < 1024 * 20; i++) {
59-
// System.out.println(i);
60-
// final DOMParser parser = new DOMParser(null);
61-
// setupParser(infilename, parser);
62-
// parser.setProperty(HTMLScanner.READER_BUFFER_SIZE, Integer.toString(i));
63-
//
64-
// String domDataLines = getResult(parser, infilename);
65-
// verify(dataFile, domDataLines);
66-
// }
67-
6858
final DOMParser parser = new DOMParser(null);
6959
setupParser(infilename, parser);
7060

7161
final String domDataLines = getResult(parser, infilename);
7262
verify(dataFile, domDataLines);
73-
//
74-
// // reset and run again
75-
// parser.reset();
76-
// domDataLines = getResult(parser, infilename);
77-
// verify(dataFile, domDataLines);
63+
}
64+
65+
// @ParameterizedTest
66+
@MethodSource("testFiles")
67+
public void runTestWithCachedElementsProvider(final File dataFile) throws Exception {
68+
final String infilename = dataFile.toString();
69+
70+
final DOMParser parser = new DOMParser(new HTMLElements.HTMLElementsWithCache(new HTMLElements()), null);
71+
setupParser(infilename, parser);
72+
73+
final String domDataLines = getResult(parser, infilename);
74+
verify(dataFile, domDataLines);
7875
}
7976

8077
// @ParameterizedTest

src/test/java/org/htmlunit/cyberneko/CanonicalHtmlWriterTest.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,18 @@ public class CanonicalHtmlWriterTest extends AbstractCanonicalTest {
4949
@ParameterizedTest
5050
@MethodSource("testFiles")
5151
public void runTest(final File dataFile) throws Exception {
52-
final String dataLines = getResult(dataFile);
52+
final String dataLines = getResult(dataFile, false);
53+
test(dataFile, dataLines);
54+
}
55+
56+
@ParameterizedTest
57+
@MethodSource("testFiles")
58+
public void runTestWithCachedElementsProvider(final File dataFile) throws Exception {
59+
final String dataLines = getResult(dataFile, true);
60+
test(dataFile, dataLines);
61+
}
5362

63+
private static void test(final File dataFile, final String dataLines) throws Exception {
5464
try {
5565
// prepare for future changes where canonical files are next to test file
5666
File canonicalFile = new File(dataFile.getParentFile(), dataFile.getName() + ".canonical-html");
@@ -91,13 +101,19 @@ public void runTest(final File dataFile) throws Exception {
91101
}
92102
}
93103

94-
private static String getResult(final File infile) throws Exception {
104+
private static String getResult(final File infile, final boolean cached) throws Exception {
95105
try (StringWriter out = new StringWriter()) {
96106
// create filters
97107
final XMLDocumentFilter[] filters = {new HTMLWriterFilter(out, "UTF-8", new HTMLElements())};
98108

99109
// create parser
100-
final XMLParserConfiguration parser = new HTMLConfiguration();
110+
final XMLParserConfiguration parser;
111+
if (cached) {
112+
parser = new HTMLConfiguration(new HTMLElements.HTMLElementsWithCache(new HTMLElements()));
113+
}
114+
else {
115+
parser = new HTMLConfiguration();
116+
}
101117

102118
// parser settings
103119
parser.setProperty("http://cyberneko.org/html/properties/filters", filters);

src/test/java/org/htmlunit/cyberneko/CanonicalSAXTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ public void runTest(final File dataFile) throws Exception {
6666
verify(dataFile, saxDataLines);
6767
}
6868

69+
@ParameterizedTest
70+
@MethodSource("testFiles")
71+
public void runTestWithCachedElementsProvider(final File dataFile) throws Exception {
72+
final String infilename = dataFile.toString();
73+
74+
final SAXParser parser = new SAXParser(new HTMLElements.HTMLElementsWithCache(new HTMLElements()));
75+
setupParser(infilename, parser);
76+
77+
String saxDataLines = getResult(parser, infilename);
78+
verify(dataFile, saxDataLines);
79+
80+
// reset and run again
81+
parser.reset();
82+
saxDataLines = getResult(parser, infilename);
83+
verify(dataFile, saxDataLines);
84+
}
85+
6986
private static void verify(final File dataFile, final String saxDataLines)
7087
throws IOException, AssertionFailedError {
7188
try {

src/test/java/org/htmlunit/cyberneko/CanonicalTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,18 @@ public class CanonicalTest extends AbstractCanonicalTest {
5050
@ParameterizedTest
5151
@MethodSource("testFiles")
5252
public void runTest(final File dataFile) throws Exception {
53-
final String dataLines = getResult(dataFile);
53+
final String dataLines = getResult(dataFile, false);
54+
test(dataFile, dataLines);
55+
}
56+
57+
@ParameterizedTest
58+
@MethodSource("testFiles")
59+
public void runTestWithCachedElementsProvider(final File dataFile) throws Exception {
60+
final String dataLines = getResult(dataFile, true);
61+
test(dataFile, dataLines);
62+
}
5463

64+
private static void test(final File dataFile, final String dataLines) throws Exception {
5565
try {
5666
// prepare for future changes where canonical files are next to test file
5767
File canonicalFile = new File(dataFile.getParentFile(), dataFile.getName() + ".canonical");
@@ -90,13 +100,20 @@ public void runTest(final File dataFile) throws Exception {
90100
}
91101
}
92102

93-
private static String getResult(final File infile) throws IOException {
103+
private static String getResult(final File infile, final boolean cached) throws IOException {
94104
try (StringWriter out = new StringWriter()) {
95105
// create filters
96106
final XMLDocumentFilter[] filters = {new Writer(out)};
97107

98108
// create parser
99-
final XMLParserConfiguration parser = new HTMLConfiguration();
109+
final XMLParserConfiguration parser;
110+
if (cached) {
111+
parser = new HTMLConfiguration(new HTMLElements.HTMLElementsWithCache(new HTMLElements()));
112+
}
113+
else {
114+
parser = new HTMLConfiguration();
115+
}
116+
100117

101118
// parser settings
102119
parser.setProperty("http://cyberneko.org/html/properties/filters", filters);

0 commit comments

Comments
 (0)