001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.rio.rdfxml;
007:
008: import java.io.InputStream;
009: import java.net.MalformedURLException;
010: import java.net.URL;
011: import java.util.LinkedHashSet;
012: import java.util.Set;
013:
014: import junit.framework.Test;
015: import junit.framework.TestCase;
016: import junit.framework.TestSuite;
017:
018: import org.openrdf.model.Statement;
019: import org.openrdf.model.util.ModelUtil;
020: import org.openrdf.query.BindingSet;
021: import org.openrdf.query.QueryLanguage;
022: import org.openrdf.query.TupleQueryResult;
023: import org.openrdf.repository.Repository;
024: import org.openrdf.repository.RepositoryConnection;
025: import org.openrdf.repository.sail.SailRepository;
026: import org.openrdf.rio.RDFFormat;
027: import org.openrdf.rio.RDFParseException;
028: import org.openrdf.rio.RDFParser;
029: import org.openrdf.rio.helpers.StatementCollector;
030: import org.openrdf.rio.ntriples.NTriplesParser;
031: import org.openrdf.sail.memory.MemoryStore;
032:
033: /**
034: * JUnit test for the RDF/XML parser that uses the test manifest that is
035: * available <a
036: * href="http://www.w3.org/2000/10/rdf-tests/rdfcore/Manifest.rdf">online</a>.
037: */
038: public class RDFXMLParserTest {
039:
040: /*-----------*
041: * Constants *
042: *-----------*/
043:
044: private static String W3C_TESTS_DIR = "http://www.w3.org/2000/10/rdf-tests/rdfcore/";
045:
046: private static String LOCAL_TESTS_DIR = "/testcases/rdfxml/";
047:
048: private static String W3C_MANIFEST_FILE = W3C_TESTS_DIR
049: + "Manifest.rdf";
050:
051: private static String OPENRDF_MANIFEST_FILE = LOCAL_TESTS_DIR
052: + "openrdf/Manifest.rdf";
053:
054: /*--------------------*
055: * Static initializer *
056: *--------------------*/
057:
058: public static Test suite() throws Exception {
059: // Create an RDF repository for the manifest data
060: Repository repository = new SailRepository(new MemoryStore());
061: repository.initialize();
062: RepositoryConnection con = repository.getConnection();
063:
064: // Add W3C's manifest
065: URL w3cManifest = resolveURL(W3C_MANIFEST_FILE);
066: con.add(w3cManifest, W3C_MANIFEST_FILE, RDFFormat.RDFXML);
067:
068: // Add our own manifest
069: URL localManifest = resolveURL(OPENRDF_MANIFEST_FILE);
070: con.add(localManifest, localManifest.toString(),
071: RDFFormat.RDFXML);
072:
073: // Create test suite
074: TestSuite suite = new TestSuite();
075:
076: // Add all positive parser tests
077: String query = "select TESTCASE, INPUT, OUTPUT "
078: + "from {TESTCASE} rdf:type {test:PositiveParserTest}; "
079: + " test:inputDocument {INPUT}; "
080: + " test:outputDocument {OUTPUT}; "
081: + " test:status {\"APPROVED\"} "
082: + "using namespace test = <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#>";
083: TupleQueryResult queryResult = con.prepareTupleQuery(
084: QueryLanguage.SERQL, query).evaluate();
085: while (queryResult.hasNext()) {
086: BindingSet bindingSet = queryResult.next();
087: String caseURI = bindingSet.getValue("TESTCASE").toString();
088: String inputURL = bindingSet.getValue("INPUT").toString();
089: String outputURL = bindingSet.getValue("OUTPUT").toString();
090: suite.addTest(new PositiveParserTest(caseURI, inputURL,
091: outputURL));
092: }
093:
094: queryResult.close();
095:
096: // Add all negative parser tests
097: query = "select TESTCASE, INPUT "
098: + "from {TESTCASE} rdf:type {test:NegativeParserTest}; "
099: + " test:inputDocument {INPUT}; "
100: + " test:status {\"APPROVED\"} "
101: + "using namespace test = <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#>";
102: queryResult = con.prepareTupleQuery(QueryLanguage.SERQL, query)
103: .evaluate();
104: while (queryResult.hasNext()) {
105: BindingSet bindingSet = queryResult.next();
106: String caseURI = bindingSet.getValue("TESTCASE").toString();
107: String inputURL = bindingSet.getValue("INPUT").toString();
108: suite.addTest(new NegativeParserTest(caseURI, inputURL));
109: }
110:
111: queryResult.close();
112: con.close();
113: repository.shutDown();
114:
115: return suite;
116: }
117:
118: private static URL resolveURL(String urlString)
119: throws MalformedURLException {
120: if (urlString.startsWith(W3C_TESTS_DIR)) {
121: // resolve to local copy
122: urlString = LOCAL_TESTS_DIR + "w3c-approved/"
123: + urlString.substring(W3C_TESTS_DIR.length());
124: }
125:
126: if (urlString.startsWith("/")) {
127: return RDFXMLParserTest.class.getResource(urlString);
128: } else {
129: return new URL(urlString);
130: }
131: }
132:
133: /*--------------------------------*
134: * Inner class PositiveParserTest *
135: *--------------------------------*/
136:
137: private static class PositiveParserTest extends TestCase {
138:
139: /*-----------*
140: * Variables *
141: *-----------*/
142:
143: private String inputURL;
144:
145: private String outputURL;
146:
147: /*--------------*
148: * Constructors *
149: *--------------*/
150:
151: public PositiveParserTest(String caseURI, String inputURL,
152: String outputURL) {
153: super (caseURI);
154: this .inputURL = inputURL;
155: this .outputURL = outputURL;
156: }
157:
158: /*---------*
159: * Methods *
160: *---------*/
161:
162: @Override
163: protected void runTest() throws Exception {
164: // Parse input data
165: RDFXMLParser rdfxmlParser = new RDFXMLParser();
166: rdfxmlParser
167: .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
168: rdfxmlParser.setParseStandAloneDocuments(true);
169:
170: Set<Statement> inputCollection = new LinkedHashSet<Statement>();
171: StatementCollector inputCollector = new StatementCollector(
172: inputCollection);
173: rdfxmlParser.setRDFHandler(inputCollector);
174:
175: InputStream in = resolveURL(inputURL).openStream();
176: rdfxmlParser.parse(in, inputURL);
177: in.close();
178:
179: // Parse expected output data
180: NTriplesParser ntriplesParser = new NTriplesParser();
181: ntriplesParser
182: .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
183:
184: Set<Statement> outputCollection = new LinkedHashSet<Statement>();
185: StatementCollector outputCollector = new StatementCollector(
186: outputCollection);
187: ntriplesParser.setRDFHandler(outputCollector);
188:
189: in = resolveURL(outputURL).openStream();
190: ntriplesParser.parse(in, inputURL);
191: in.close();
192:
193: // Check equality of the two models
194: if (!ModelUtil.equals(inputCollection, outputCollection)) {
195: StringBuilder sb = new StringBuilder(1024);
196: sb.append("models not equal\n");
197: sb.append("Expected:\n");
198: for (Statement st : outputCollection) {
199: sb.append(st).append("\n");
200: }
201: sb.append("Actual:\n");
202: for (Statement st : inputCollection) {
203: sb.append(st).append("\n");
204: }
205:
206: fail(sb.toString());
207: }
208: }
209:
210: } // end inner class PositiveParserTest
211:
212: /*--------------------------------*
213: * Inner class NegativeParserTest *
214: *--------------------------------*/
215:
216: private static class NegativeParserTest extends TestCase {
217:
218: /*-----------*
219: * Variables *
220: *-----------*/
221:
222: private String inputURL;
223:
224: /*--------------*
225: * Constructors *
226: *--------------*/
227:
228: public NegativeParserTest(String caseURI, String inputURL) {
229: super (caseURI);
230: this .inputURL = inputURL;
231: }
232:
233: /*---------*
234: * Methods *
235: *---------*/
236:
237: @Override
238: protected void runTest() {
239: try {
240: // Try parsing the input; this should result in an error being
241: // reported.
242: RDFXMLParser rdfxmlParser = new RDFXMLParser();
243: rdfxmlParser
244: .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
245: rdfxmlParser.setParseStandAloneDocuments(true);
246:
247: rdfxmlParser.setRDFHandler(new StatementCollector());
248:
249: InputStream in = resolveURL(inputURL).openStream();
250: rdfxmlParser.parse(in, inputURL);
251: in.close();
252:
253: fail("Parser parses erroneous data without reporting errors");
254: } catch (RDFParseException e) {
255: // This is expected as the input file is incorrect RDF
256: } catch (Exception e) {
257: fail("Error: " + e.getMessage());
258: }
259: }
260:
261: } // end inner class NegativeParserTest
262: }
|