org.eclipse.rdf4j.testsuite.rio.turtle.TurtleParserTestCase Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* 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.
*
* SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/
package org.eclipse.rdf4j.testsuite.rio.turtle;
import java.io.InputStream;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.QueryLanguage;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
import org.eclipse.rdf4j.testsuite.rio.FailureMode;
import org.eclipse.rdf4j.testsuite.rio.NegativeParserTest;
import org.eclipse.rdf4j.testsuite.rio.PositiveParserTest;
import junit.framework.TestSuite;
/**
* JUnit test for the Turtle parser that uses the tests that are available
* online.
*/
public abstract class TurtleParserTestCase {
/*-----------*
* Constants *
*-----------*/
/**
* Base URL for W3C Tutle tests.
*/
protected static String TESTS_W3C_BASE_URL = "http://www.w3.org/2013/TurtleTests/";
/**
* Base directory for W3C Turtle tests
*/
private static final String TEST_W3C_FILE_BASE_PATH = "/testcases/turtle/tests-ttl-w3c-20170126/";
private static final String TEST_W3C_MANIFEST_URL = TEST_W3C_FILE_BASE_PATH + "manifest.ttl";
private static final String TEST_W3C_MANIFEST_URI_BASE = "http://www.w3.org/2013/TurtleTests/manifest.ttl#";
private static final String TEST_W3C_TEST_URI_BASE = "http://www.w3.org/2013/TurtleTests/";
private static final String NTRIPLES_TEST_URL = "http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt";
private static final String NTRIPLES_TEST_FILE = "/testcases/ntriples/test.nt";
/**
* Base directory for N-Triples compatibility tests that are part of the Turtle test-suite.
*/
private static final String TURTLE_NTRIPLES_FILE_BASE_PATH = "/testcases/turtle/tests-nt/";
/**
* Manifest for N-Triples compatibility tests that are part of the Turtle test-suite.
*/
private static final String TURTLE_NTRIPLES_MANIFEST_URL = "/testcases/turtle/tests-nt/manifest.ttl";
private static final String TURTLE_NTRIPLES_MANIFEST_URI_BASE = "https://dvcs.w3.org/hg/rdf/raw-file/default/rdf-turtle/tests-nt/manifest.ttl#";
private static final String TURTLE_NTRIPLES_TEST_URI_BASE = "https://dvcs.w3.org/hg/rdf/raw-file/default/rdf-turtle/tests-nt/";
/*--------------------*
* Static initializer *
*--------------------*/
public TestSuite createTestSuite() throws Exception {
// Create test suite
TestSuite suite = new TestSuite(TurtleParserTestCase.class.getName());
// Add the manifest for W3C test cases to a repository and query it
Repository w3cRepository = new SailRepository(new MemoryStore());
try (RepositoryConnection w3cCon = w3cRepository.getConnection()) {
InputStream inputStream = this.getClass().getResourceAsStream(TEST_W3C_MANIFEST_URL);
w3cCon.add(inputStream, TEST_W3C_MANIFEST_URI_BASE, RDFFormat.TURTLE);
parsePositiveTurtleSyntaxTests(suite, TEST_W3C_FILE_BASE_PATH, TESTS_W3C_BASE_URL, TEST_W3C_TEST_URI_BASE,
w3cCon);
parseNegativeTurtleSyntaxTests(suite, TEST_W3C_FILE_BASE_PATH, TESTS_W3C_BASE_URL, TEST_W3C_TEST_URI_BASE,
w3cCon);
parsePositiveTurtleEvalTests(suite, TEST_W3C_FILE_BASE_PATH, TESTS_W3C_BASE_URL, TEST_W3C_TEST_URI_BASE,
w3cCon);
parseNegativeTurtleEvalTests(suite, TEST_W3C_FILE_BASE_PATH, TESTS_W3C_BASE_URL, TEST_W3C_TEST_URI_BASE,
w3cCon);
}
w3cRepository.shutDown();
return suite;
}
private void parsePositiveTurtleSyntaxTests(TestSuite suite, String fileBasePath, String testBaseUrl,
String testLocationBaseUri, RepositoryConnection con) {
StringBuilder positiveQuery = new StringBuilder();
positiveQuery.append(" PREFIX mf: \n");
positiveQuery.append(" PREFIX qt: \n");
positiveQuery.append(" PREFIX rdft: \n");
positiveQuery.append(" SELECT ?test ?testName ?inputURL ?outputURL \n");
positiveQuery.append(" WHERE { \n");
positiveQuery.append(" ?test a rdft:TestTurtlePositiveSyntax . ");
positiveQuery.append(" ?test mf:name ?testName . ");
positiveQuery.append(" ?test mf:action ?inputURL . ");
positiveQuery.append(" }");
TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SPARQL, positiveQuery.toString()).evaluate();
// Add all positive parser tests to the test suite
while (queryResult.hasNext()) {
BindingSet bindingSet = queryResult.next();
IRI nextTestUri = (IRI) bindingSet.getValue("test");
String nextTestName = ((Literal) bindingSet.getValue("testName")).getLabel();
String nextTestFile = removeBase(((IRI) bindingSet.getValue("inputURL")).toString(), testLocationBaseUri);
String nextInputURL = fileBasePath + nextTestFile;
String nextBaseUrl = testBaseUrl + nextTestFile;
suite.addTest(new PositiveParserTest(nextTestUri, nextTestName, nextInputURL, null, nextBaseUrl,
createTurtleParser(), createNTriplesParser()));
}
queryResult.close();
}
private void parseNegativeTurtleSyntaxTests(TestSuite suite, String fileBasePath, String testBaseUrl,
String manifestBaseUrl, RepositoryConnection con) {
StringBuilder negativeQuery = new StringBuilder();
negativeQuery.append(" PREFIX mf: \n");
negativeQuery.append(" PREFIX qt: \n");
negativeQuery.append(" PREFIX rdft: \n");
negativeQuery.append(" SELECT ?test ?testName ?inputURL ?outputURL \n");
negativeQuery.append(" WHERE { \n");
negativeQuery.append(" ?test a rdft:TestTurtleNegativeSyntax . ");
negativeQuery.append(" ?test mf:name ?testName . ");
negativeQuery.append(" ?test mf:action ?inputURL . ");
negativeQuery.append(" }");
TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SPARQL, negativeQuery.toString()).evaluate();
// Add all negative parser tests to the test suite
while (queryResult.hasNext()) {
BindingSet bindingSet = queryResult.next();
IRI nextTestUri = (IRI) bindingSet.getValue("test");
String nextTestName = ((Literal) bindingSet.getValue("testName")).getLabel();
String nextTestFile = removeBase(((IRI) bindingSet.getValue("inputURL")).toString(), manifestBaseUrl);
String nextInputURL = fileBasePath + nextTestFile;
String nextBaseUrl = testBaseUrl + nextTestFile;
suite.addTest(new NegativeParserTest(nextTestUri, nextTestName, nextInputURL, nextBaseUrl,
createTurtleParser(), FailureMode.DO_NOT_IGNORE_FAILURE));
}
queryResult.close();
}
private void parsePositiveTurtleEvalTests(TestSuite suite, String fileBasePath, String testBaseUrl,
String manifestBaseUrl, RepositoryConnection con) {
StringBuilder positiveEvalQuery = new StringBuilder();
positiveEvalQuery.append(" PREFIX mf: \n");
positiveEvalQuery.append(" PREFIX qt: \n");
positiveEvalQuery.append(" PREFIX rdft: \n");
positiveEvalQuery.append(" SELECT ?test ?testName ?inputURL ?outputURL \n");
positiveEvalQuery.append(" WHERE { \n");
positiveEvalQuery.append(" ?test a rdft:TestTurtleEval . ");
positiveEvalQuery.append(" ?test mf:name ?testName . ");
positiveEvalQuery.append(" ?test mf:action ?inputURL . ");
positiveEvalQuery.append(" ?test mf:result ?outputURL . ");
positiveEvalQuery.append(" }");
TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SPARQL, positiveEvalQuery.toString())
.evaluate();
// Add all positive eval tests to the test suite
while (queryResult.hasNext()) {
BindingSet bindingSet = queryResult.next();
IRI nextTestUri = (IRI) bindingSet.getValue("test");
String nextTestName = ((Literal) bindingSet.getValue("testName")).getLabel();
String nextTestFile = removeBase(((IRI) bindingSet.getValue("inputURL")).toString(), manifestBaseUrl);
String nextInputURL = fileBasePath + nextTestFile;
String nextOutputURL = fileBasePath
+ removeBase(((IRI) bindingSet.getValue("outputURL")).toString(), manifestBaseUrl);
String nextBaseUrl = testBaseUrl + nextTestFile;
// if (nextTestName.contains("CARRIAGE_RETURN")) {
// // FIXME: RDF4J seems not to preserve the CARRIAGE_RETURN character
// // right now
// System.err.println("Ignoring Turtle Positive Parser Eval Test: " + nextInputURL);
// continue;
// }
suite.addTest(new PositiveParserTest(nextTestUri, nextTestName, nextInputURL, nextOutputURL, nextBaseUrl,
createTurtleParser(), createNTriplesParser()));
}
queryResult.close();
}
private void parseNegativeTurtleEvalTests(TestSuite suite, String fileBasePath, String testBaseUrl,
String manifestBaseUrl, RepositoryConnection con) {
StringBuilder negativeEvalQuery = new StringBuilder();
negativeEvalQuery.append(" PREFIX mf: \n");
negativeEvalQuery.append(" PREFIX qt: \n");
negativeEvalQuery.append(" PREFIX rdft: \n");
negativeEvalQuery.append(" SELECT ?test ?testName ?inputURL ?outputURL \n");
negativeEvalQuery.append(" WHERE { \n");
negativeEvalQuery.append(" ?test a rdft:TestTurtleNegativeEval . ");
negativeEvalQuery.append(" ?test mf:name ?testName . ");
negativeEvalQuery.append(" ?test mf:action ?inputURL . ");
negativeEvalQuery.append(" }");
TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SPARQL, negativeEvalQuery.toString())
.evaluate();
// Add all negative eval tests to the test suite
while (queryResult.hasNext()) {
BindingSet bindingSet = queryResult.next();
IRI nextTestUri = (IRI) bindingSet.getValue("test");
String nextTestName = ((Literal) bindingSet.getValue("testName")).getLabel();
String nextTestFile = removeBase(((IRI) bindingSet.getValue("inputURL")).toString(), manifestBaseUrl);
String nextInputURL = fileBasePath + nextTestFile;
String nextBaseUrl = testBaseUrl + nextTestFile;
suite.addTest(new NegativeParserTest(nextTestUri, nextTestName, nextInputURL, nextBaseUrl,
createTurtleParser(), FailureMode.DO_NOT_IGNORE_FAILURE));
}
queryResult.close();
}
private void parsePositiveNTriplesSyntaxTests(TestSuite suite, String fileBasePath, String testBaseUrl,
String manifestBaseUrl, RepositoryConnection con) {
StringBuilder positiveQuery = new StringBuilder();
positiveQuery.append(" PREFIX mf: \n");
positiveQuery.append(" PREFIX qt: \n");
positiveQuery.append(" PREFIX rdft: \n");
positiveQuery.append(" SELECT ?test ?testName ?inputURL ?outputURL \n");
positiveQuery.append(" WHERE { \n");
positiveQuery.append(" ?test a rdft:TestNTriplesPositiveSyntax . ");
positiveQuery.append(" ?test mf:name ?testName . ");
positiveQuery.append(" ?test mf:action ?inputURL . ");
positiveQuery.append(" }");
TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SPARQL, positiveQuery.toString()).evaluate();
// Add all positive parser tests to the test suite
while (queryResult.hasNext()) {
BindingSet bindingSet = queryResult.next();
IRI nextTestUri = (IRI) bindingSet.getValue("test");
String nextTestName = ((Literal) bindingSet.getValue("testName")).getLabel();
String nextTestFile = removeBase(((IRI) bindingSet.getValue("inputURL")).toString(), manifestBaseUrl);
String nextInputURL = fileBasePath + nextTestFile;
String nextBaseUrl = testBaseUrl + nextTestFile;
suite.addTest(new PositiveParserTest(nextTestUri, nextTestName, nextInputURL, null, nextBaseUrl,
createNTriplesParser(), createNTriplesParser()));
}
queryResult.close();
}
private void parseNegativeNTriplesSyntaxTests(TestSuite suite, String fileBasePath, String testBaseUrl,
String manifestBaseUrl, RepositoryConnection con) {
StringBuilder negativeQuery = new StringBuilder();
negativeQuery.append(" PREFIX mf: \n");
negativeQuery.append(" PREFIX qt: \n");
negativeQuery.append(" PREFIX rdft: \n");
negativeQuery.append(" SELECT ?test ?testName ?inputURL ?outputURL \n");
negativeQuery.append(" WHERE { \n");
negativeQuery.append(" ?test a rdft:TestNTriplesNegativeSyntax . ");
negativeQuery.append(" ?test mf:name ?testName . ");
negativeQuery.append(" ?test mf:action ?inputURL . ");
negativeQuery.append(" }");
TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SPARQL, negativeQuery.toString()).evaluate();
// Add all negative parser tests to the test suite
while (queryResult.hasNext()) {
BindingSet bindingSet = queryResult.next();
IRI nextTestUri = (IRI) bindingSet.getValue("test");
String nextTestName = ((Literal) bindingSet.getValue("testName")).getLabel();
String nextTestFile = removeBase(((IRI) bindingSet.getValue("inputURL")).toString(), manifestBaseUrl);
String nextInputURL = fileBasePath + nextTestFile;
String nextBaseUrl = testBaseUrl + nextTestFile;
suite.addTest(new NegativeParserTest(nextTestUri, nextTestName, nextInputURL, nextBaseUrl,
createNTriplesParser(), FailureMode.DO_NOT_IGNORE_FAILURE));
}
queryResult.close();
}
/**
* @return An implementation of a Turtle parser to test compliance with the Turtle Test Suite Turtle tests.
*/
protected abstract RDFParser createTurtleParser();
/**
* @return An implementation of an N-Triples parser to test compliance with the Turtle Test Suite N-Triples tests.
*/
protected abstract RDFParser createNTriplesParser();
private String removeBase(String baseUrl, String redundantBaseUrl) {
if (baseUrl.startsWith(redundantBaseUrl)) {
return baseUrl.substring(redundantBaseUrl.length());
}
return baseUrl;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy