Skip to content

Commit

Permalink
[ issue #10 ] Added integration tests from Chapter 2 of
Browse files Browse the repository at this point in the history
  • Loading branch information
agazzarini committed Aug 25, 2014
1 parent 268e125 commit db78bf8
Show file tree
Hide file tree
Showing 29 changed files with 220 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.gazzax.labs.jena.nosql.fwk;

import static org.gazzax.labs.jena.nosql.fwk.TestUtility.DUMMY_BASE_URI;
import static org.junit.Assert.assertTrue;

import java.io.File;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

/**
* Supertype layer for all SPARQL integration tests.
*
* @author Andrea Gazzarini
* @since 1.0
*/
public abstract class SparqlConstructIntegrationTestCase extends SparqlIntegrationTestCase {
@Override
protected void executeTestWithFile(final String filename) throws Exception {
final Query query = QueryFactory.create(queryString(filename + ".rq"));
final QueryExecution execution = QueryExecutionFactory.create(query, dataset);
final Model rs = execution.execConstruct();

final Model model = ModelFactory.createDefaultModel().read(new File(EXAMPLES_DIR + File.separator + chapter() + File.separator, filename + ".rs").toURI().toString(), DUMMY_BASE_URI, "TTL");

assertTrue(rs.isIsomorphicWith(model));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.gazzax.labs.jena.nosql.fwk;

import static org.gazzax.labs.jena.nosql.fwk.TestUtility.DUMMY_BASE_URI;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.io.File;
Expand All @@ -16,12 +15,6 @@

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;

/**
Expand All @@ -33,8 +26,8 @@
public abstract class SparqlIntegrationTestCase {
protected static final String EXAMPLES_DIR = "src/test/resources/w3c/";

private Dataset dataset;
private StorageLayerFactory factory;
protected Dataset dataset;
protected StorageLayerFactory factory;

/**
* Setup fixture for this test.
Expand All @@ -44,25 +37,48 @@ public final void setUp() {
factory = StorageLayerFactory.getFactory();
dataset = DatasetFactory.create(factory.getDatasetGraph());

load(testFilename() + ".ttl");
load("data.ttl");
}

/**
* In case the conctete test consists in just 1 example, this method returns the filename associated with that example.
* Returns the chapter the test refers to (e.g. 2.1, 2.2, 3.2).
*
* @return the name of the file associated with the example.
* @return the chapter the test refers to.
*/
protected abstract String testFilename();

protected abstract String chapter();

/**
* Executes the test.
* Executes the first test.
*
* @throws Exception hopefully never, otherwise the test fails.
*/
@Test
public void example1() throws Exception {
executeTestWithFile(testFilename());
public void test() throws Exception {
for (int i = 1; i < howManyExamples() + 1; i++) {
executeTestWithFile("ex" + i);
}
}

/**
* Internal method used to execute a query and assert corresponding results.
* In case the test has just one method, there's nothing to do, the subclass already inherits
* the predefined {@link #executeTest()}.
*
* Otherwise, if a test case includes more than one test, then that concrete subclass needs to define test methods
* and call this method to execute and check queries.
*
* @param filename the filename.
* @throws Exception hopefully never otherwise the test fails.
*/
protected abstract void executeTestWithFile(final String filename) throws Exception;

/**
* Returns how many examples belong to this test.
*
* @return how many examples belong to this test.
*/
protected int howManyExamples() {
return 1;
}

/**
Expand All @@ -82,63 +98,28 @@ public void tearDown() {
* @return the query string associated with this test.
* @throws IOException in case of I/O failure while reading the file.
*/
private String queryString(final String filename) throws IOException {
protected String queryString(final String filename) throws IOException {
return readFile(filename);
}

/**
* Builds a string (from the file associated with this test) with the expected query results.
*
* @param resultsFileName the results filename.
* @return a string (from the file associated with this test) with the expected query results.
* @throws IOException in case of I/O failure while reading the file.
*/
private String results(final String resultsFileName) throws IOException {
return readFile(resultsFileName);
}

/**
* Builds a string from a given file.
*
* @param filename the filename (without path).
* @return a string with the file content.
* @throws IOException in case of I/O failure while reading the file.
*/
private String readFile(final String filename) throws IOException {
return new String(Files.readAllBytes(Paths.get(new File(EXAMPLES_DIR, filename).toURI())));
protected String readFile(final String filename) throws IOException {
return new String(Files.readAllBytes(Paths.get(new File(EXAMPLES_DIR + File.separator + chapter() + File.separator, filename).toURI())));
}

/**
* Loads all triples found in the datafile associated with the given name.
*
* @param datafileName the name of the datafile.
*/
private void load(final String datafileName) {
final Model model = dataset.getDefaultModel().read(new File(EXAMPLES_DIR, datafileName).toURI().toString(), DUMMY_BASE_URI, "TTL");
protected void load(final String datafileName) {
final Model model = dataset.getDefaultModel().read(new File(EXAMPLES_DIR + File.separator + chapter() + File.separator, datafileName).toURI().toString(), DUMMY_BASE_URI, "TTL");
assertFalse(model.isEmpty());
}

/**
* Internal method used to execute a query and assert corresponding results.
* In case the test has just one method, there's nothing to do, the subclass already inherits
* the predefined {@link #executeTest()}.
*
* Otherwise, if a test case includes more than one test, then that concrete subclass needs to define test methods
* and call this method to execute and check queries.
*
* @param filename the filename.
* @throws Exception hopefully never otherwise the test fails.
*/
protected void executeTestWithFile(final String filename) throws Exception {
final Query query = QueryFactory.create(queryString(filename + ".rq"));
final QueryExecution execution = QueryExecutionFactory.create(query, dataset);
final ResultSet rs = execution.execSelect();

final String s = ResultSetFormatter.asText(rs, query).trim();

assertEquals(
results(filename + ".rs").trim(),
s.trim());
execution.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.gazzax.labs.jena.nosql.fwk;

import static org.junit.Assert.assertEquals;

import java.io.IOException;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

/**
* Supertype layer for all SPARQL integration tests.
*
* @author Andrea Gazzarini
* @since 1.0
*/
public abstract class SparqlSelectIntegrationTestCase extends SparqlIntegrationTestCase {
@Override
protected void executeTestWithFile(final String filename) throws Exception {
final Query query = QueryFactory.create(queryString(filename + ".rq"));
final QueryExecution execution = QueryExecutionFactory.create(query, dataset);
final ResultSet rs = execution.execSelect();

final String s = ResultSetFormatter.asText(rs, query).trim();

assertEquals(
results(filename + ".rs").trim(),
s.trim());
execution.close();
}

/**
* Builds a string (from the file associated with this test) with the expected query results.
*
* @param resultsFileName the results filename.
* @return a string (from the file associated with this test) with the expected query results.
* @throws IOException in case of I/O failure while reading the file.
*/
protected String results(final String resultsFileName) throws IOException {
return readFile(resultsFileName);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.gazzax.labs.jena.nosql.fwk.w3c;

import org.gazzax.labs.jena.nosql.fwk.SparqlIntegrationTestCase;
import org.gazzax.labs.jena.nosql.fwk.SparqlSelectIntegrationTestCase;

/**
* SPARQL Integration test with examples taken from http://www.w3.org/TR/sparql11-query.
Expand All @@ -9,10 +9,9 @@
* @author Andrea Gazzarini
* @since 1.0
*/
public class Ch2_1_WritingSimpleQueries_ITCase extends SparqlIntegrationTestCase {

@Override
protected String testFilename() {
return "chapter_2.1_ex1";
public class Ch2_1_WritingSimpleQueries_ITCase extends SparqlSelectIntegrationTestCase {
@Override
protected String chapter() {
return "2.1";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.gazzax.labs.jena.nosql.fwk.w3c;

import org.gazzax.labs.jena.nosql.fwk.SparqlIntegrationTestCase;
import org.gazzax.labs.jena.nosql.fwk.SparqlSelectIntegrationTestCase;

/**
* SPARQL Integration test with examples taken from http://www.w3.org/TR/sparql11-query.
Expand All @@ -9,9 +9,9 @@
* @author Andrea Gazzarini
* @since 1.0
*/
public class Ch2_2_MultipleMatches_ITCase extends SparqlIntegrationTestCase {
public class Ch2_2_MultipleMatches_ITCase extends SparqlSelectIntegrationTestCase {
@Override
protected String testFilename() {
return "chapter_2.2_ex1";
protected String chapter() {
return "2.2";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.gazzax.labs.jena.nosql.fwk.w3c;

import org.gazzax.labs.jena.nosql.fwk.SparqlIntegrationTestCase;
import org.junit.Test;
import org.gazzax.labs.jena.nosql.fwk.SparqlSelectIntegrationTestCase;

/**
* SPARQL Integration test with examples taken from http://www.w3.org/TR/sparql11-query.
Expand All @@ -10,19 +9,15 @@
* @author Andrea Gazzarini
* @since 1.0
*/
public class Ch2_3_MatchingRDFLiterals_ITCase extends SparqlIntegrationTestCase {
public class Ch2_3_MatchingRDFLiterals_ITCase extends SparqlSelectIntegrationTestCase {

@Override
protected String testFilename() {
return "chapter_2.3_ex1";
protected String chapter() {
return "2.3";
}

/**
* Executes the 2nd test of the chapter.
*
* @throws Exception hopefully never, otherwise the test fails.
*/
@Test
public void example2() throws Exception {
executeTestWithFile("chapter_2.3_ex2");
@Override
protected int howManyExamples() {
return 3;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.gazzax.labs.jena.nosql.fwk.w3c;

import org.gazzax.labs.jena.nosql.fwk.SparqlSelectIntegrationTestCase;

/**
* SPARQL Integration test with examples taken from http://www.w3.org/TR/sparql11-query.
*
* @see http://www.w3.org/TR/sparql11-query/#CreatingValuesWithExpressions
* @author Andrea Gazzarini
* @since 1.0
*/
public class Ch2_5_CreatingValuesWithExpressions_ITCase extends SparqlSelectIntegrationTestCase {
@Override
protected String chapter() {
return "2.5";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.gazzax.labs.jena.nosql.fwk.w3c;

import org.gazzax.labs.jena.nosql.fwk.SparqlConstructIntegrationTestCase;

/**
* SPARQL Integration test with examples taken from http://www.w3.org/TR/sparql11-query.
*
* @see http://www.w3.org/TR/sparql11-query/#constructGraph
* @author Andrea Gazzarini
* @since 1.0
*/
public class Ch2_6_BuildingRDFGraphs_ITCase extends SparqlConstructIntegrationTestCase {
@Override
protected String chapter() {
return "2.6";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PREFIX dt: <http://example.org/datatype#>
PREFIX ns: <http://example.org/ns#>
PREFIX : <http://example.org/ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?v WHERE { ?v ?p 42 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
------
| v |
======
| :y |
------
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PREFIX dt: <http://example.org/datatype#>
PREFIX ns: <http://example.org/ns#>
PREFIX : <http://example.org/ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?v WHERE { ?v ?p "abc"^^<http://example.org/datatype#specialDatatype> }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
------
| v |
======
| :z |
------
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:givenName "John" .
_:a foaf:surname "Doe" .
Loading

0 comments on commit db78bf8

Please sign in to comment.