File: TreeTester.java

package info (click to toggle)
libhtml5parser-java 1.4%2Br20250916-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,900 kB
  • sloc: java: 28,661; xml: 144; sh: 136; makefile: 3
file content (279 lines) | stat: -rw-r--r-- 10,285 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (c) 2007 Henri Sivonen
 *
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

package nu.validator.htmlparser.test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.LinkedList;

import nu.validator.htmlparser.common.XmlViolationPolicy;
import nu.validator.htmlparser.sax.HtmlParser;

import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;

public class TreeTester {

    private final BufferedInputStream aggregateStream;

    private boolean streaming = false;

    static int exitStatus = 0;

    /**
     * @param aggregateStream
     */
    public TreeTester(InputStream aggregateStream) {
        this.aggregateStream = new BufferedInputStream(aggregateStream);
    }

    void runTests() throws Throwable {
        if (aggregateStream.read() != '#') {
            System.err.println("No hash at start!");
            return;
        }
        while (runTest()) {
            // spin
        }
    }

    @SuppressWarnings("resource")
    private boolean runTest() throws Throwable {
        UntilHashInputStream stream = null;
        try {
            String context = null;
            boolean scriptingEnabled = true;
            boolean hadScriptingDirective = false;
            aggregateStream.mark(12288);
            if (skipLabel()) { // #data
                return false;
            }
            stream = new UntilHashInputStream(aggregateStream);
            while (stream.read() != -1) {
                // spin
            }
            if (skipLabel()) { // #errors
                System.err.println("Premature end of test data.");
                return false;
            }
            stream = new UntilHashInputStream(aggregateStream);
            while (stream.read() != -1) {
                // spin
            }

            boolean newErrors = false;
            StringBuilder sb = new StringBuilder();
            int c;
            while ((c = aggregateStream.read()) != '\n') {
                sb.append((char) c);
            }
            String label = sb.toString();
            if ("new-errors".equals(label)) {
                newErrors = true;
                stream = new UntilHashInputStream(aggregateStream);
                while (stream.read() != -1) {
                    // spin
                }
                sb.setLength(0);
                while ((c = aggregateStream.read()) != '\n') {
                    sb.append((char) c);
                }
                label = sb.toString();
            }
            if ("document-fragment".equals(label)) {
                sb.setLength(0);
                while ((c = aggregateStream.read()) != '\n') {
                    sb.append((char) c);
                }
                context = sb.toString();
                // Now potentially gather #script-on/off
                sb.setLength(0);
                while ((c = aggregateStream.read()) != '\n') {
                    sb.append((char) c);
                }
                label = sb.toString();
            }
            if ("script-on".equals(label)) {
                hadScriptingDirective = true;
            } else if ("script-off".equals(label)) {
                hadScriptingDirective = true;
                scriptingEnabled = false;
            }
            aggregateStream.reset();
            if (skipLabel()) { // #data
                System.err.println("Premature end of test data.");
                return false;
            }
            stream = new UntilHashInputStream(aggregateStream);
            InputSource is = new InputSource(stream);
            is.setEncoding("UTF-8");
            StringWriter sw = new StringWriter();
            ListErrorHandler leh = new ListErrorHandler();
            TreeDumpContentHandler treeDumpContentHandler = new TreeDumpContentHandler(
                    sw);
            HtmlParser htmlParser = new HtmlParser(XmlViolationPolicy.ALLOW);
            if (streaming) {
                htmlParser.setStreamabilityViolationPolicy(XmlViolationPolicy.FATAL);
            }
            htmlParser.setContentHandler(treeDumpContentHandler);
            htmlParser.setLexicalHandler(treeDumpContentHandler);
            htmlParser.setErrorHandler(leh);
            htmlParser.setScriptingEnabled(scriptingEnabled);
            try {
                if (context == null) {
                    htmlParser.parse(is);
                } else {
                    String ns = "http://www.w3.org/1999/xhtml";
                    if (context.startsWith("svg ")) {
                        ns = "http://www.w3.org/2000/svg";
                        context = context.substring(4);
                    } else if (context.startsWith("math ")) {
                        ns = "http://www.w3.org/1998/Math/MathML";
                        context = context.substring(5);
                    }
                    htmlParser.parseFragment(is, context, ns);
                    treeDumpContentHandler.endDocument();
                }
            } catch (SAXParseException e) {
                // ignore
            }
            stream.close();

            if (skipLabel()) { // #errors
                System.err.println("Premature end of test data.");
                return false;
            }
            LinkedList<String> expectedErrors = new LinkedList<String>();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new UntilHashInputStream(aggregateStream), "UTF-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                expectedErrors.add(line);
            }

            if (newErrors) {
                if (skipLabel()) { // #new-errors
                    System.err.println("Premature end of test data.");
                    return false;
                }
                br = new BufferedReader(new InputStreamReader(
                        new UntilHashInputStream(aggregateStream), "UTF-8"));
                while ((line = br.readLine()) != null) {
                    expectedErrors.add(line);
                }
            }

            if (context != null) {
                if (skipLabel()) { // #document-fragment
                    System.err.println("Premature end of test data.");
                    return false;
                }
                UntilHashInputStream stream2 = new UntilHashInputStream(aggregateStream);
                while (stream2.read() != -1) {
                    // spin
                }
            }
            if (hadScriptingDirective && skipLabel()) { // #script-on/off
                System.err.println("Premature end of test data.");
                return false;                
            }

            if (skipLabel()) { // #document
                System.err.println("Premature end of test data.");
                return false;
            }

            StringBuilder expectedBuilder = new StringBuilder();
            br = new BufferedReader(new InputStreamReader(
                    new UntilHashInputStream(aggregateStream), "UTF-8"));
            int ch;
            while ((ch = br.read()) != -1) {
                expectedBuilder.append((char)ch);
            }
            String expected = expectedBuilder.toString();
            if (expected.contains("<keygen>")) {
                return true;
            }
            String actual = sw.toString();

            LinkedList<String> actualErrors = leh.getErrors();

            if (expected.equals(actual) || (streaming && leh.isFatal()) /*
             * && expectedErrors.size() ==
             * actualErrors.size()
             */) {
                // System.err.println(stream);
            } else {
                exitStatus = 1;
                System.err.print("Failure.\nData:\n" + stream + "\nExpected:\n"
                        + expected + "Got: \n" + actual);
                System.err.println("Expected errors:");
                for (String err : expectedErrors) {
                    System.err.println(err);
                }
                System.err.println("Actual errors:");
                for (String err : actualErrors) {
                    System.err.println(err);
                }
            }
        } catch (Throwable t) {
            exitStatus = 1;
            System.err.println("Failure.\nData:\n" + stream);
            throw t;
        }
        return true;
    }

    private boolean skipLabel() throws IOException {
        int b = aggregateStream.read();
        if (b == -1) {
            return true;
        }
        for (;;) {
            b = aggregateStream.read();
            if (b == -1) {
                return true;
            } else if (b == 0x0A) {
                return false;
            }
        }
    }

    /**
     * @param args
     * @throws Throwable
     */
    public static void main(String[] args) throws Throwable {
        for (int i = 0; i < args.length; i++) {
            TreeTester tester = new TreeTester(new FileInputStream(args[i]));
            tester.runTests();
        }
        System.exit(exitStatus);
    }

}