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.trig;
007:
008: import java.io.InputStream;
009: import java.net.MalformedURLException;
010: import java.net.URL;
011: import java.util.Collection;
012: import java.util.LinkedHashSet;
013: import java.util.Set;
014:
015: import junit.framework.Test;
016: import junit.framework.TestCase;
017: import junit.framework.TestSuite;
018:
019: import org.openrdf.model.Statement;
020: import org.openrdf.model.util.ModelUtil;
021: import org.openrdf.query.BindingSet;
022: import org.openrdf.query.QueryLanguage;
023: import org.openrdf.query.TupleQueryResult;
024: import org.openrdf.repository.Repository;
025: import org.openrdf.repository.RepositoryConnection;
026: import org.openrdf.repository.sail.SailRepository;
027: import org.openrdf.repository.util.RepositoryUtil;
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 TriG parser.
037: */
038: public class TriGParserTest {
039:
040: /*-----------*
041: * Constants *
042: *-----------*/
043:
044: protected static String BASE_URL = "http://www.w3.org/2001/sw/DataAccess/df1/tests/";
045:
046: private static String MANIFEST_GOOD_URL = "/testcases/trig/manifest.ttl";
047:
048: private static String MANIFEST_BAD_URL = "/testcases/trig/manifest-bad.ttl";
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 for positive test cases 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 = TriGParserTest.class.getResource(MANIFEST_GOOD_URL);
064: con.add(url, url.toExternalForm(), RDFFormat.TURTLE);
065:
066: String query = "SELECT testName, inputURL, outputURL "
067: + "FROM {} mf:name {testName}; "
068: + " mf:result {outputURL}; "
069: + " mf:action {} qt:data {inputURL} "
070: + "USING NAMESPACE "
071: + " mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, "
072: + " qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>";
073:
074: TupleQueryResult queryResult = con.prepareTupleQuery(
075: QueryLanguage.SERQL, query).evaluate();
076:
077: // Add all positive parser tests to the test suite
078: while (queryResult.hasNext()) {
079: BindingSet bindingSet = queryResult.next();
080: String testName = bindingSet.getValue("testName")
081: .toString();
082: String inputURL = bindingSet.getValue("inputURL")
083: .toString();
084: String outputURL = bindingSet.getValue("outputURL")
085: .toString();
086:
087: String baseURL = BASE_URL + testName + ".ttl";
088:
089: suite.addTest(new PositiveParserTest(testName, inputURL,
090: outputURL, baseURL));
091: }
092:
093: queryResult.close();
094:
095: // Add the manifest for negative test cases to a repository and query it
096: con.clear();
097: url = TriGParserTest.class.getResource(MANIFEST_BAD_URL);
098: con.add(url, url.toExternalForm(), RDFFormat.TURTLE);
099:
100: query = "SELECT testName, inputURL "
101: + "FROM {} mf:name {testName}; "
102: + " mf:action {} qt:data {inputURL} "
103: + "USING NAMESPACE "
104: + " mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, "
105: + " qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>";
106: queryResult = con.prepareTupleQuery(QueryLanguage.SERQL, query)
107: .evaluate();
108:
109: // Add all negative parser tests to the test suite
110: while (queryResult.hasNext()) {
111: BindingSet bindingSet = queryResult.next();
112: String testName = bindingSet.getValue("testName")
113: .toString();
114: String inputURL = bindingSet.getValue("inputURL")
115: .toString();
116:
117: String baseURL = BASE_URL + testName + ".ttl";
118:
119: suite.addTest(new NegativeParserTest(testName, inputURL,
120: baseURL));
121: }
122:
123: queryResult.close();
124: con.close();
125: repository.shutDown();
126:
127: return suite;
128: }
129:
130: /*--------------------------------*
131: * Inner class PositiveParserTest *
132: *--------------------------------*/
133:
134: private static class PositiveParserTest extends TestCase {
135:
136: /*-----------*
137: * Variables *
138: *-----------*/
139:
140: private URL inputURL;
141:
142: private URL outputURL;
143:
144: private String baseURL;
145:
146: /*--------------*
147: * Constructors *
148: *--------------*/
149:
150: public PositiveParserTest(String testName, String inputURL,
151: String outputURL, String baseURL)
152: throws MalformedURLException {
153: super (testName);
154: this .inputURL = new URL(inputURL);
155: this .outputURL = new URL(outputURL);
156: this .baseURL = baseURL;
157: }
158:
159: /*---------*
160: * Methods *
161: *---------*/
162:
163: @Override
164: protected void runTest() throws Exception {
165: // Parse input data
166: TriGParser turtleParser = new TriGParser();
167: turtleParser
168: .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
169:
170: Set<Statement> inputCollection = new LinkedHashSet<Statement>();
171: StatementCollector inputCollector = new StatementCollector(
172: inputCollection);
173: turtleParser.setRDFHandler(inputCollector);
174:
175: InputStream in = inputURL.openStream();
176: turtleParser.parse(in, baseURL);
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 = outputURL.openStream();
190: ntriplesParser.parse(in, baseURL);
191: in.close();
192:
193: // Check equality of the two models
194: if (!ModelUtil.equals(inputCollection, outputCollection)) {
195: Collection<? extends Statement> missingStatements = RepositoryUtil
196: .difference(outputCollection, inputCollection);
197: Collection<? extends Statement> unexpectedStatements = RepositoryUtil
198: .difference(inputCollection, outputCollection);
199:
200: System.err.println("===models not equal===");
201: if (!missingStatements.isEmpty()) {
202: System.err.println("Missing statements : "
203: + missingStatements);
204: }
205: if (!unexpectedStatements.isEmpty()) {
206: System.err.println("Unexpected statements: "
207: + unexpectedStatements);
208: }
209: System.err.println("======================");
210:
211: fail("models not equal");
212: }
213: }
214:
215: } // end inner class PositiveParserTest
216:
217: /*--------------------------------*
218: * Inner class NegativeParserTest *
219: *--------------------------------*/
220:
221: private static class NegativeParserTest extends TestCase {
222:
223: /*-----------*
224: * Variables *
225: *-----------*/
226:
227: private URL inputURL;
228:
229: private String baseURL;
230:
231: /*--------------*
232: * Constructors *
233: *--------------*/
234:
235: public NegativeParserTest(String caseURI, String inputURL,
236: String baseURL) throws MalformedURLException {
237: super (caseURI);
238: this .inputURL = new URL(inputURL);
239: this .baseURL = baseURL;
240: }
241:
242: /*---------*
243: * Methods *
244: *---------*/
245:
246: @Override
247: protected void runTest() {
248: try {
249: // Try parsing the input; this should result in an error being
250: // reported.
251: TriGParser turtleParser = new TriGParser();
252: turtleParser
253: .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
254:
255: turtleParser.setRDFHandler(new StatementCollector());
256:
257: InputStream in = inputURL.openStream();
258: turtleParser.parse(in, baseURL);
259: in.close();
260:
261: fail("Parser parses erroneous data without reporting errors");
262: } catch (RDFParseException e) {
263: // This is expected as the input file is incorrect RDF
264: } catch (Exception e) {
265: fail("Error: " + e.getMessage());
266: }
267: }
268:
269: } // end inner class NegativeParserTest
270: }
|