diff --git a/jena-nosql-framework/src/main/java/org/gazzax/labs/jena/nosql/fwk/graph/NoSqlDatasetGraph.java b/jena-nosql-framework/src/main/java/org/gazzax/labs/jena/nosql/fwk/graph/NoSqlDatasetGraph.java index a8483a1..4e777a3 100644 --- a/jena-nosql-framework/src/main/java/org/gazzax/labs/jena/nosql/fwk/graph/NoSqlDatasetGraph.java +++ b/jena-nosql-framework/src/main/java/org/gazzax/labs/jena/nosql/fwk/graph/NoSqlDatasetGraph.java @@ -25,7 +25,7 @@ public Iterator listGraphNodes() { @Override protected void _close() { - // TODO Auto-generated method stub + factory.getDictionary().close(); } @Override diff --git a/jena-nosql-framework/src/main/java/org/gazzax/labs/jena/nosql/fwk/graph/NoSqlGraph.java b/jena-nosql-framework/src/main/java/org/gazzax/labs/jena/nosql/fwk/graph/NoSqlGraph.java index e15e707..a5a75a0 100644 --- a/jena-nosql-framework/src/main/java/org/gazzax/labs/jena/nosql/fwk/graph/NoSqlGraph.java +++ b/jena-nosql-framework/src/main/java/org/gazzax/labs/jena/nosql/fwk/graph/NoSqlGraph.java @@ -142,4 +142,10 @@ Iterator query(final byte[][] query) throws StorageLayerException { ? dao.query(query) : EMPTY_IDS_ITERATOR; } + + @Override + public void close() { + dictionary.close(); + closed = true; + } } \ No newline at end of file diff --git a/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/SparqlIntegrationTestCase.java b/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/SparqlIntegrationTestCase.java index bb337e3..a93f110 100644 --- a/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/SparqlIntegrationTestCase.java +++ b/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/SparqlIntegrationTestCase.java @@ -1,12 +1,13 @@ -package org.gazzax.labs.jena.nosql.fwk.w3c; +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.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import org.gazzax.labs.jena.nosql.fwk.factory.StorageLayerFactory; import org.junit.After; @@ -23,90 +24,114 @@ import com.hp.hpl.jena.query.ResultSetFormatter; import com.hp.hpl.jena.rdf.model.Model; -public class Ex1ITCase { +/** + * Supertype layer for all SPARQL integration tests. + * + * @author Andrea Gazzarini + * @since 1.0 + */ +public abstract class SparqlIntegrationTestCase { protected static final String EXAMPLES_DIR = "src/test/resources/w3c/"; private Dataset dataset; private StorageLayerFactory factory; @Before - public void setUp() { + public final void setUp() { factory = StorageLayerFactory.getFactory(); dataset = DatasetFactory.create(factory.getDatasetGraph()); - BufferedReader reader = null; - try { - reader = new BufferedReader(new FileReader(new File(EXAMPLES_DIR, "chapter_2.1_ex1.ttl"))); - final Model model = dataset.getDefaultModel().read(reader, DUMMY_BASE_URI, "TTL"); - assertEquals(1, model.size()); - } catch (final Exception exception) { - // TODO: handle exception - } + load(testFilename() + ".ttl"); } + /** + * In case the conctete test consists in just 1 example, this method returns the filename associated with that example. + * + * @return the name of the file associated with the example. + */ + protected abstract String testFilename(); + + + /** + * Executes the test. + * + * @throws Exception hopefully never, otherwise the test fails. + */ @Test - public void test() { - final Query query = QueryFactory.create(query("chapter_2.1_ex1.rq")); - final QueryExecution execution = QueryExecutionFactory.create(query, dataset); - final ResultSet rs = execution.execSelect(); - assertEquals(ResultSetFormatter.asText(rs, query).trim(), results("chapter_2.1_ex1.rs").trim()); - execution.close(); + public void example1() throws Exception { + executeTestWithFile(testFilename()); } @After public void tearDown() { + factory.getTripleIndexDAO().clear(); + dataset.close(); factory.getClientShutdownHook().close(); } - private String query(final String sourcefileName) { - BufferedReader reader = null; - try { - reader = new BufferedReader(new FileReader(new File(EXAMPLES_DIR, sourcefileName))); - String actLine = null; - final StringBuilder builder = new StringBuilder(); - while ( (actLine = reader.readLine()) != null) { - builder.append(actLine); - } - return builder.toString(); - } catch (final Exception exception) { - throw new RuntimeException(exception); - } finally { - try { - reader.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + /** + * Reads a query from the file associated with this test and builds a query string. + * + * @param filename the filename. + * @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 { + return readFile(filename); + } + + /** + * Builds a string (from the file associated with this test) with the expected query results. + * + * @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); } - private String results(final String resultsFileName) { - BufferedReader reader = null; - try { - reader = new BufferedReader(new FileReader(new File(EXAMPLES_DIR, resultsFileName))); - String actLine = null; - final StringBuilder builder = new StringBuilder(); - int i = 0; - while ( (actLine = reader.readLine()) != null) { - if (i > 0) { - builder.append(System.getProperty("line.separator")); - } - builder.append(actLine); - i++; - } - - System.out.println(builder); - - return builder.toString(); - } catch (final Exception exception) { - throw new RuntimeException(exception); - } finally { - try { - reader.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + /** + * 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()))); } + + /** + * 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"); + 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(); + System.out.println(s); + assertEquals( + results(filename + ".rs").trim(), + s.trim()); + execution.close(); + } } diff --git a/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/w3c/Ch2_1_WritingSimpleQueries_ITCase.java b/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/w3c/Ch2_1_WritingSimpleQueries_ITCase.java new file mode 100644 index 0000000..bdced71 --- /dev/null +++ b/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/w3c/Ch2_1_WritingSimpleQueries_ITCase.java @@ -0,0 +1,18 @@ +package org.gazzax.labs.jena.nosql.fwk.w3c; + +import org.gazzax.labs.jena.nosql.fwk.SparqlIntegrationTestCase; + +/** + * SPARQL Integration test with examples taken from http://www.w3.org/TR/sparql11-query + * + * @see http://www.w3.org/TR/sparql11-query/#WritingSimpleQueries + * @author Andrea Gazzarini + * @since 1.0 + */ +public class Ch2_1_WritingSimpleQueries_ITCase extends SparqlIntegrationTestCase { + + @Override + protected String testFilename() { + return "chapter_2.1_ex1"; + } +} diff --git a/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/w3c/Ch2_2_MultipleMatches_ITCase.java b/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/w3c/Ch2_2_MultipleMatches_ITCase.java new file mode 100644 index 0000000..3c6f008 --- /dev/null +++ b/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/w3c/Ch2_2_MultipleMatches_ITCase.java @@ -0,0 +1,17 @@ +package org.gazzax.labs.jena.nosql.fwk.w3c; + +import org.gazzax.labs.jena.nosql.fwk.SparqlIntegrationTestCase; + +/** + * SPARQL Integration test with examples taken from http://www.w3.org/TR/sparql11-query + * + * @see http://www.w3.org/TR/sparql11-query/#MultipleMatches + * @author Andrea Gazzarini + * @since 1.0 + */ +public class Ch2_2_MultipleMatches_ITCase extends SparqlIntegrationTestCase { + @Override + protected String testFilename() { + return "chapter_2.2_ex1"; + } +} diff --git a/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/w3c/Ch2_3_MatchingRDFLiterals_ITCase.java b/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/w3c/Ch2_3_MatchingRDFLiterals_ITCase.java new file mode 100644 index 0000000..17f5905 --- /dev/null +++ b/jena-nosql-integration-tests/src/test/java/org/gazzax/labs/jena/nosql/fwk/w3c/Ch2_3_MatchingRDFLiterals_ITCase.java @@ -0,0 +1,28 @@ +package org.gazzax.labs.jena.nosql.fwk.w3c; + +import org.gazzax.labs.jena.nosql.fwk.SparqlIntegrationTestCase; +import org.junit.Test; + +/** + * SPARQL Integration test with examples taken from http://www.w3.org/TR/sparql11-query + * + * @see http://www.w3.org/TR/sparql11-query/#matchingRDFLiterals + * @author Andrea Gazzarini + * @since 1.0 + */ +public class Ch2_3_MatchingRDFLiterals_ITCase extends SparqlIntegrationTestCase { + @Override + protected String testFilename() { + return "chapter_2.3_ex1"; + } + + /** + * 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"); + } +} diff --git a/jena-nosql-integration-tests/src/test/resources/log4j.xml b/jena-nosql-integration-tests/src/test/resources/log4j.xml new file mode 100644 index 0000000..e393574 --- /dev/null +++ b/jena-nosql-integration-tests/src/test/resources/log4j.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.2_ex1.rq b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.2_ex1.rq new file mode 100644 index 0000000..a2963b1 --- /dev/null +++ b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.2_ex1.rq @@ -0,0 +1,7 @@ +PREFIX foaf: +SELECT ?name ?mbox +WHERE +{ + ?x foaf:name ?name . + ?x foaf:mbox ?mbox +} \ No newline at end of file diff --git a/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.2_ex1.rs b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.2_ex1.rs new file mode 100644 index 0000000..435ddb3 --- /dev/null +++ b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.2_ex1.rs @@ -0,0 +1,6 @@ +---------------------------------------------------- +| name | mbox | +==================================================== +| "Johnny Lee Outlaw" | | +| "Peter Goodguy" | | +---------------------------------------------------- diff --git a/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.2_ex1.ttl b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.2_ex1.ttl new file mode 100644 index 0000000..845bb2d --- /dev/null +++ b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.2_ex1.ttl @@ -0,0 +1,7 @@ +@prefix foaf: . + +_:a foaf:name "Johnny Lee Outlaw" . +_:a foaf:mbox . +_:b foaf:name "Peter Goodguy" . +_:b foaf:mbox . +_:c foaf:mbox . \ No newline at end of file diff --git a/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex1.rq b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex1.rq new file mode 100644 index 0000000..daca0f8 --- /dev/null +++ b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex1.rq @@ -0,0 +1,6 @@ +PREFIX dt: +PREFIX ns: +PREFIX : +PREFIX xsd: + +SELECT ?v WHERE { ?v ?p "cat" } \ No newline at end of file diff --git a/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex1.rs b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex1.rs new file mode 100644 index 0000000..35f4ac3 --- /dev/null +++ b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex1.rs @@ -0,0 +1,4 @@ +----- +| v | +===== +----- \ No newline at end of file diff --git a/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex1.ttl b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex1.ttl new file mode 100644 index 0000000..adcde74 --- /dev/null +++ b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex1.ttl @@ -0,0 +1,8 @@ +@prefix dt: . +@prefix ns: . +@prefix : . +@prefix xsd: . + +:x ns:p "cat"@en . +:y ns:p "42"^^xsd:integer . +:z ns:p "abc"^^dt:specialDatatype . \ No newline at end of file diff --git a/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex2.rq b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex2.rq new file mode 100644 index 0000000..ddde4cf --- /dev/null +++ b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex2.rq @@ -0,0 +1,6 @@ +PREFIX dt: +PREFIX ns: +PREFIX : +PREFIX xsd: + +SELECT ?v WHERE { ?v ?p "cat"@en } \ No newline at end of file diff --git a/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex2.rs b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex2.rs new file mode 100644 index 0000000..8ff2c32 --- /dev/null +++ b/jena-nosql-integration-tests/src/test/resources/w3c/chapter_2.3_ex2.rs @@ -0,0 +1,5 @@ +------ +| v | +====== +| :x | +------ \ No newline at end of file