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.turtle;
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.Literal;
019: import org.openrdf.model.Statement;
020: import org.openrdf.model.URI;
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.sail.memory.MemoryStore;
034:
035: /**
036: * JUnit test for the Turtle parser that uses the tests that are available <a
037: * href="http://cvs.ilrt.org/cvsweb/redland/raptor/tests/turtle/">online</a>.
038: */
039: public class TurtleParserTest {
040:
041: /*-----------*
042: * Constants *
043: *-----------*/
044:
045: protected static String BASE_URL = "http://www.w3.org/2001/sw/DataAccess/df1/tests/";
046:
047: private static String MANIFEST_GOOD_URL = "/testcases/turtle/manifest.ttl";
048:
049: private static String MANIFEST_BAD_URL = "/testcases/turtle/manifest-bad.ttl";
050:
051: private static String NTRIPLES_TEST_URL = "http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt";
052:
053: private static String NTRIPLES_TEST_FILE = "/testcases/ntriples/test.nt";
054:
055: /*--------------------*
056: * Static initializer *
057: *--------------------*/
058:
059: public static Test suite() throws Exception {
060: // Create test suite
061: TestSuite suite = new TestSuite();
062:
063: // Add the N-Triples test
064: String testName = "N-Triples tests";
065: URL url = TurtleParserTest.class
066: .getResource(NTRIPLES_TEST_FILE);
067: String inputURL = url.toExternalForm();
068: String outputURL = inputURL;
069: String baseURL = NTRIPLES_TEST_URL;
070: suite.addTest(new PositiveParserTest(testName, inputURL,
071: outputURL, baseURL));
072:
073: // Add the manifest for positive test cases to a repository and query it
074: Repository repository = new SailRepository(new MemoryStore());
075: repository.initialize();
076: RepositoryConnection con = repository.getConnection();
077:
078: url = TurtleParserTest.class.getResource(MANIFEST_GOOD_URL);
079: con.add(url, url.toExternalForm(), RDFFormat.TURTLE);
080:
081: String query = "SELECT testName, inputURL, outputURL "
082: + "FROM {} mf:name {testName}; "
083: + " mf:result {outputURL}; "
084: + " mf:action {} qt:data {inputURL} "
085: + "USING NAMESPACE "
086: + " mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, "
087: + " qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>";
088:
089: TupleQueryResult queryResult = con.prepareTupleQuery(
090: QueryLanguage.SERQL, query).evaluate();
091:
092: // Add all positive parser tests to the test suite
093: while (queryResult.hasNext()) {
094: BindingSet bindingSet = queryResult.next();
095: testName = ((Literal) bindingSet.getValue("testName"))
096: .getLabel();
097: inputURL = ((URI) bindingSet.getValue("inputURL"))
098: .toString();
099: outputURL = ((URI) bindingSet.getValue("outputURL"))
100: .toString();
101:
102: baseURL = BASE_URL + testName + ".ttl";
103:
104: suite.addTest(new PositiveParserTest(testName, inputURL,
105: outputURL, baseURL));
106: }
107:
108: queryResult.close();
109:
110: // Add the manifest for negative test cases to a repository and query it
111: con.clear();
112: url = TurtleParserTest.class.getResource(MANIFEST_BAD_URL);
113: con.add(url, url.toExternalForm(), RDFFormat.TURTLE);
114:
115: query = "SELECT testName, inputURL "
116: + "FROM {} mf:name {testName}; "
117: + " mf:action {} qt:data {inputURL} "
118: + "USING NAMESPACE "
119: + " mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, "
120: + " qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>";
121: queryResult = con.prepareTupleQuery(QueryLanguage.SERQL, query)
122: .evaluate();
123:
124: // Add all negative parser tests to the test suite
125: while (queryResult.hasNext()) {
126: BindingSet bindingSet = queryResult.next();
127: testName = ((Literal) bindingSet.getValue("testName"))
128: .toString();
129: inputURL = ((URI) bindingSet.getValue("inputURL"))
130: .toString();
131:
132: baseURL = BASE_URL + testName + ".ttl";
133:
134: suite.addTest(new NegativeParserTest(testName, inputURL,
135: baseURL));
136: }
137:
138: queryResult.close();
139: con.close();
140: repository.shutDown();
141:
142: return suite;
143: }
144:
145: /*--------------------------------*
146: * Inner class PositiveParserTest *
147: *--------------------------------*/
148:
149: private static class PositiveParserTest extends TestCase {
150:
151: /*-----------*
152: * Variables *
153: *-----------*/
154:
155: private URL inputURL;
156:
157: private URL outputURL;
158:
159: private String baseURL;
160:
161: /*--------------*
162: * Constructors *
163: *--------------*/
164:
165: public PositiveParserTest(String testName, String inputURL,
166: String outputURL, String baseURL)
167: throws MalformedURLException {
168: super (testName);
169: this .inputURL = new URL(inputURL);
170: this .outputURL = new URL(outputURL);
171: this .baseURL = baseURL;
172: }
173:
174: /*---------*
175: * Methods *
176: *---------*/
177:
178: @Override
179: protected void runTest() throws Exception {
180: // Parse input data
181: TurtleParser turtleParser = new TurtleParser();
182: turtleParser
183: .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
184:
185: Set<Statement> inputCollection = new LinkedHashSet<Statement>();
186: StatementCollector inputCollector = new StatementCollector(
187: inputCollection);
188: turtleParser.setRDFHandler(inputCollector);
189:
190: InputStream in = inputURL.openStream();
191: turtleParser.parse(in, baseURL);
192: in.close();
193:
194: // Parse expected output data
195: NTriplesParser ntriplesParser = new NTriplesParser();
196: ntriplesParser
197: .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
198:
199: Set<Statement> outputCollection = new LinkedHashSet<Statement>();
200: StatementCollector outputCollector = new StatementCollector(
201: outputCollection);
202: ntriplesParser.setRDFHandler(outputCollector);
203:
204: in = outputURL.openStream();
205: ntriplesParser.parse(in, baseURL);
206: in.close();
207:
208: // Check equality of the two models
209: if (!ModelUtil.equals(inputCollection, outputCollection)) {
210: System.err.println("===models not equal===");
211: System.err.println("Expected: " + outputCollection);
212: System.err.println("Actual : " + inputCollection);
213: System.err.println("======================");
214:
215: fail("models not equal");
216: }
217: }
218:
219: } // end inner class PositiveParserTest
220:
221: /*--------------------------------*
222: * Inner class NegativeParserTest *
223: *--------------------------------*/
224:
225: private static class NegativeParserTest extends TestCase {
226:
227: /*-----------*
228: * Variables *
229: *-----------*/
230:
231: private URL inputURL;
232:
233: private String baseURL;
234:
235: /*--------------*
236: * Constructors *
237: *--------------*/
238:
239: public NegativeParserTest(String caseURI, String inputURL,
240: String baseURL) throws MalformedURLException {
241: super (caseURI);
242: this .inputURL = new URL(inputURL);
243: this .baseURL = baseURL;
244: }
245:
246: /*---------*
247: * Methods *
248: *---------*/
249:
250: @Override
251: protected void runTest() {
252: try {
253: // Try parsing the input; this should result in an error being
254: // reported.
255: TurtleParser turtleParser = new TurtleParser();
256: turtleParser
257: .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
258:
259: turtleParser.setRDFHandler(new StatementCollector());
260:
261: InputStream in = inputURL.openStream();
262: turtleParser.parse(in, baseURL);
263: in.close();
264:
265: fail("Parser parses erroneous data without reporting errors");
266: } catch (RDFParseException e) {
267: // This is expected as the input file is incorrect RDF
268: } catch (Exception e) {
269: fail("Error: " + e.getMessage());
270: }
271: }
272:
273: } // end inner class NegativeParserTest
274: }
|