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.n3;
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.LinkedList;
013: import java.util.List;
014: import java.util.Set;
015:
016: import junit.framework.Test;
017: import junit.framework.TestCase;
018: import junit.framework.TestSuite;
019:
020: import org.openrdf.model.Statement;
021: import org.openrdf.model.util.ModelUtil;
022: import org.openrdf.query.BindingSet;
023: import org.openrdf.query.QueryLanguage;
024: import org.openrdf.query.TupleQueryResult;
025: import org.openrdf.repository.Repository;
026: import org.openrdf.repository.RepositoryConnection;
027: import org.openrdf.repository.sail.SailRepository;
028: import org.openrdf.rio.RDFFormat;
029: import org.openrdf.rio.RDFParseException;
030: import org.openrdf.rio.RDFParser;
031: import org.openrdf.rio.helpers.StatementCollector;
032: import org.openrdf.rio.ntriples.NTriplesParser;
033: import org.openrdf.rio.turtle.TurtleParser;
034: import org.openrdf.sail.memory.MemoryStore;
035:
036: /**
037: * JUnit test for the N3 parser that uses the tests that are available <a
038: * href="http://www.w3.org/2000/10/swap/test/n3parser.tests">online</a>.
039: */
040: public class N3ParserTest {
041:
042: /*-----------*
043: * Constants *
044: *-----------*/
045:
046: private static String BASE_URL = "http://www.w3.org/2000/10/swap/test/";
047:
048: private static String MANIFEST_URL = "http://www.w3.org/2000/10/swap/test/n3parser.tests";
049:
050: /*--------------------*
051: * Static initializer *
052: *--------------------*/
053:
054: public static Test suite() throws Exception {
055: // Create test suite
056: TestSuite suite = new TestSuite();
057:
058: // Add the manifest to a repository and query it
059: Repository repository = new SailRepository(new MemoryStore());
060: repository.initialize();
061: RepositoryConnection con = repository.getConnection();
062:
063: URL url = new URL(MANIFEST_URL);
064: con.add(url, MANIFEST_URL, RDFFormat.TURTLE);
065:
066: // Add all positive parser tests to the test suite
067: String query = "SELECT testURI, inputURL, outputURL "
068: + "FROM {testURI} rdf:type {n3test:PositiveParserTest}; "
069: + " n3test:inputDocument {inputURL}; "
070: + " n3test:outputDocument {outputURL} "
071: + "USING NAMESPACE n3test = <http://www.w3.org/2004/11/n3test#>";
072:
073: TupleQueryResult queryResult = con.prepareTupleQuery(
074: QueryLanguage.SERQL, query).evaluate();
075: while (queryResult.hasNext()) {
076: BindingSet bindingSet = queryResult.next();
077: String testURI = bindingSet.getValue("testURI").toString();
078: String inputURL = bindingSet.getValue("inputURL")
079: .toString();
080: String outputURL = bindingSet.getValue("outputURL")
081: .toString();
082:
083: suite.addTest(new PositiveParserTest(testURI, inputURL,
084: outputURL));
085: }
086:
087: queryResult.close();
088:
089: // Add all negative parser tests to the test suite
090: query = "SELECT testURI, inputURL "
091: + "FROM {testURI} rdf:type {n3test:NegativeParserTest}; "
092: + " n3test:inputDocument {inputURL} "
093: + "USING NAMESPACE n3test = <http://www.w3.org/2004/11/n3test#>";
094:
095: queryResult = con.prepareTupleQuery(QueryLanguage.SERQL, query)
096: .evaluate();
097:
098: while (queryResult.hasNext()) {
099: BindingSet bindingSet = queryResult.next();
100: String testURI = bindingSet.getValue("testURI").toString();
101: String inputURL = bindingSet.getValue("inputURL")
102: .toString();
103:
104: suite.addTest(new NegativeParserTest(testURI, inputURL));
105: }
106: queryResult.close();
107: con.close();
108: repository.shutDown();
109:
110: return suite;
111: }
112:
113: /*--------------------------------*
114: * Inner class PositiveParserTest *
115: *--------------------------------*/
116:
117: private static class PositiveParserTest extends TestCase {
118:
119: /*-----------*
120: * Variables *
121: *-----------*/
122:
123: private URL inputURL;
124:
125: private URL outputURL;
126:
127: /*--------------*
128: * Constructors *
129: *--------------*/
130:
131: public PositiveParserTest(String testURI, String inputURL,
132: String outputURL) throws MalformedURLException {
133: super (testURI);
134: this .inputURL = new URL(inputURL);
135: this .outputURL = new URL(outputURL);
136: }
137:
138: /*---------*
139: * Methods *
140: *---------*/
141:
142: @Override
143: protected void runTest() throws Exception {
144: // Parse input data
145: TurtleParser turtleParser = new TurtleParser();
146: turtleParser
147: .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
148:
149: Set<Statement> inputCollection = new LinkedHashSet<Statement>();
150: StatementCollector inputCollector = new StatementCollector(
151: inputCollection);
152: turtleParser.setRDFHandler(inputCollector);
153:
154: InputStream in = inputURL.openStream();
155: turtleParser.parse(in, inputURL.toExternalForm());
156: in.close();
157:
158: // Parse expected output data
159: NTriplesParser ntriplesParser = new NTriplesParser();
160: ntriplesParser
161: .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
162:
163: Set<Statement> outputCollection = new LinkedHashSet<Statement>();
164: StatementCollector outputCollector = new StatementCollector(
165: outputCollection);
166: ntriplesParser.setRDFHandler(outputCollector);
167:
168: in = outputURL.openStream();
169: ntriplesParser.parse(in, outputURL.toExternalForm());
170: in.close();
171:
172: // Check equality of the two models
173: if (!ModelUtil.equals(inputCollection, outputCollection)) {
174: System.err.println("===models not equal===");
175: // System.err.println("Expected: " + outputCollection);
176: // System.err.println("Actual : " + inputCollection);
177: // System.err.println("======================");
178:
179: List<Statement> missing = new LinkedList<Statement>(
180: outputCollection);
181: missing.removeAll(inputCollection);
182:
183: List<Statement> unexpected = new LinkedList<Statement>(
184: inputCollection);
185: unexpected.removeAll(outputCollection);
186:
187: if (!missing.isEmpty()) {
188: System.err.println("Missing : " + missing);
189: }
190: if (!unexpected.isEmpty()) {
191: System.err.println("Unexpected: " + unexpected);
192: }
193:
194: fail("models not equal");
195: }
196: }
197:
198: } // end inner class PositiveParserTest
199:
200: /*--------------------------------*
201: * Inner class NegativeParserTest *
202: *--------------------------------*/
203:
204: private static class NegativeParserTest extends TestCase {
205:
206: /*-----------*
207: * Variables *
208: *-----------*/
209:
210: private URL inputURL;
211:
212: /*--------------*
213: * Constructors *
214: *--------------*/
215:
216: public NegativeParserTest(String testURI, String inputURL)
217: throws MalformedURLException {
218: super (testURI);
219: this .inputURL = new URL(inputURL);
220: }
221:
222: /*---------*
223: * Methods *
224: *---------*/
225:
226: @Override
227: protected void runTest() {
228: try {
229: // Try parsing the input; this should result in an error being
230: // reported.
231: TurtleParser turtleParser = new TurtleParser();
232: turtleParser
233: .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
234:
235: turtleParser.setRDFHandler(new StatementCollector());
236:
237: InputStream in = inputURL.openStream();
238: turtleParser.parse(in, inputURL.toExternalForm());
239: in.close();
240:
241: fail("Parser parses erroneous data without reporting errors");
242: } catch (RDFParseException e) {
243: // This is expected as the input file is incorrect RDF
244: } catch (Exception e) {
245: fail("Error: " + e.getMessage());
246: }
247: }
248:
249: } // end inner class NegativeParserTest
250: }
|