forked from eclipse-rdf4j/rdf4j
/
RDFXMLParserCustomTest.java
228 lines (211 loc) · 8.42 KB
/
RDFXMLParserCustomTest.java
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
/*******************************************************************************
* Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/
package org.eclipse.rdf4j.rio.rdfxml;
import static org.junit.Assert.*;
import java.io.StringReader;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.rio.ParserConfig;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFParseException;
import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.RDFParser.DatatypeHandling;
import org.eclipse.rdf4j.rio.helpers.ParseErrorCollector;
import org.eclipse.rdf4j.rio.helpers.StatementCollector;
import org.eclipse.rdf4j.rio.helpers.XMLParserSettings;
import org.junit.Ignore;
import org.junit.Test;
/**
* Custom tests for RDFXML Parser.
*
* @author Michael Grove
*/
public class RDFXMLParserCustomTest {
/**
* Test with the default ParserConfig settings. Ie, setParserConfig is not called.
*
* @throws Exception
*/
@Test
public void testEntityExpansionDefaultSettings()
throws Exception
{
final Model aGraph = new LinkedHashModel();
ParseErrorCollector errorCollector = new ParseErrorCollector();
RDFParser aParser = Rio.createParser(RDFFormat.RDFXML).setRDFHandler(
new StatementCollector(aGraph)).setParseErrorListener(errorCollector);
try {
// this should trigger a SAX parse exception that will blow up at the
// 64k entity limit rather than OOMing
aParser.parse(
this.getClass().getResourceAsStream(
"/testcases/rdfxml/openrdf/bad-entity-expansion-limit.rdf"),
"http://example.org");
fail("Parser did not throw an exception");
}
catch (RDFParseException e) {
// assertTrue(e.getMessage().contains(
// "The parser has encountered more than \"64,000\" entity expansions in this document; this is the limit imposed by the "));
}
assertEquals(0, errorCollector.getWarnings().size());
assertEquals(0, errorCollector.getErrors().size());
assertEquals(1, errorCollector.getFatalErrors().size());
}
/**
* Test with unrelated ParserConfig settings
*
* @throws Exception
*/
@Test
public void testEntityExpansionUnrelatedSettings()
throws Exception
{
final Model aGraph = new LinkedHashModel();
ParseErrorCollector errorCollector = new ParseErrorCollector();
ParserConfig config = new ParserConfig();
RDFParser aParser = Rio.createParser(RDFFormat.RDFXML).setRDFHandler(
new StatementCollector(aGraph)).setParserConfig(config).setParseErrorListener(errorCollector);
try {
// this should trigger a SAX parse exception that will blow up at the
// 64k entity limit rather than OOMing
aParser.parse(
this.getClass().getResourceAsStream(
"/testcases/rdfxml/openrdf/bad-entity-expansion-limit.rdf"),
"http://example.org");
fail("Parser did not throw an exception");
}
catch (RDFParseException e) {
// assertTrue(e.getMessage().contains(
// "The parser has encountered more than \"64,000\" entity expansions in this document; this is the limit imposed by the "));
}
assertEquals(0, errorCollector.getWarnings().size());
assertEquals(0, errorCollector.getErrors().size());
assertEquals(1, errorCollector.getFatalErrors().size());
}
/**
* Test with Secure processing setting on.
*
* @throws Exception
*/
@Test
public void testEntityExpansionSecureProcessing()
throws Exception
{
final Model aGraph = new LinkedHashModel();
ParseErrorCollector errorCollector = new ParseErrorCollector();
RDFParser aParser = Rio.createParser(RDFFormat.RDFXML).setRDFHandler(
new StatementCollector(aGraph)).set(XMLParserSettings.SECURE_PROCESSING,
true).setParseErrorListener(errorCollector);
try {
// this should trigger a SAX parse exception that will blow up at the
// 64k entity limit rather than OOMing
aParser.parse(
this.getClass().getResourceAsStream(
"/testcases/rdfxml/openrdf/bad-entity-expansion-limit.rdf"),
"http://example.org");
fail("Parser did not throw an exception");
}
catch (RDFParseException e) {
// assertTrue(e.getMessage().contains(
// "The parser has encountered more than \"64,000\" entity expansions in this document; this is the limit imposed by the "));
}
assertEquals(0, errorCollector.getWarnings().size());
assertEquals(0, errorCollector.getErrors().size());
assertEquals(1, errorCollector.getFatalErrors().size());
}
/**
* Test with Secure processing setting off.
* <p>
* IMPORTANT: Only turn this on to verify it is still working, as there is no way to safely perform this
* test.
* <p>
* WARNING: This test will cause an OutOfMemoryException when it eventually fails, as it will eventually
* fail.
*
* @throws Exception
*/
@Ignore
@Test(timeout = 10000)
public void testEntityExpansionNoSecureProcessing()
throws Exception
{
final Model aGraph = new LinkedHashModel();
ParseErrorCollector errorCollector = new ParseErrorCollector();
RDFParser aParser = Rio.createParser(RDFFormat.RDFXML).setRDFHandler(
new StatementCollector(aGraph)).set(XMLParserSettings.SECURE_PROCESSING,
false).setParseErrorListener(errorCollector);
try {
// IMPORTANT: This will not use the entity limit
aParser.parse(
this.getClass().getResourceAsStream(
"/testcases/rdfxml/openrdf/bad-entity-expansion-limit.rdf"),
"http://example.org");
fail("Parser did not throw an exception");
}
catch (RDFParseException e) {
// assertTrue(e.getMessage().contains(
// "The parser has encountered more than \"64,000\" entity expansions in this document; this is the limit imposed by the"));
}
assertEquals(0, errorCollector.getWarnings().size());
assertEquals(0, errorCollector.getErrors().size());
assertEquals(1, errorCollector.getFatalErrors().size());
}
@Test
public void testParseCollection()
throws Exception
{
// Example from:
// http://www.w3.org/TR/rdf-syntax-grammar/#section-Syntax-parsetype-Collection
StringBuilder string = new StringBuilder();
string.append("<?xml version=\"1.0\"?>\n");
string.append("<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" ");
string.append(" xmlns:ex=\"http://example.org/stuff/1.0/\"> \n");
string.append(" <rdf:Description rdf:about=\"http://example.org/basket\"> \n");
string.append(" <ex:hasFruit rdf:parseType=\"Collection\">\n");
string.append(" <rdf:Description rdf:about=\"http://example.org/banana\"/>\n");
string.append(" <rdf:Description rdf:about=\"http://example.org/apple\"/>\n");
string.append(" <rdf:Description rdf:about=\"http://example.org/pear\"/>\n");
string.append(" </ex:hasFruit>\n");
string.append(" </rdf:Description>\n");
string.append("</rdf:RDF>");
Model parse = Rio.parse(new StringReader(string.toString()), "", RDFFormat.RDFXML);
Rio.write(parse, System.out, RDFFormat.NTRIPLES);
assertEquals(7, parse.size());
assertEquals(3, parse.filter(null, RDF.FIRST, null).size());
assertEquals(3, parse.filter(null, RDF.REST, null).size());
assertEquals(1, parse.filter(null, null, RDF.NIL).size());
}
@Test
public void testParseCommentAtStart()
throws Exception
{
// Example from:
// http://www.w3.org/TR/rdf-syntax-grammar/#section-Syntax-parsetype-Collection
StringBuilder string = new StringBuilder();
string.append("<!-- Test comment for parser to ignore -->\n");
string.append("<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" ");
string.append(" xmlns:ex=\"http://example.org/stuff/1.0/\"> \n");
string.append(" <rdf:Description rdf:about=\"http://example.org/basket\"> \n");
string.append(" <ex:hasFruit>\n");
string.append(" Mango\n");
string.append(" </ex:hasFruit>\n");
string.append(" </rdf:Description>\n");
string.append("</rdf:RDF>");
Model parse = Rio.parse(new StringReader(string.toString()), "", RDFFormat.RDFXML);
Rio.write(parse, System.out, RDFFormat.NTRIPLES);
assertEquals(1, parse.size());
}
@Test
public void testSupportedSettings()
throws Exception
{
assertEquals(22, Rio.createParser(RDFFormat.RDFXML).getSupportedSettings().size());
}
}