Skip to content

Commit

Permalink
[ issue #10] Added 4 SPARQL integration tests with data from
Browse files Browse the repository at this point in the history
  • Loading branch information
agazzarini committed Aug 21, 2014
1 parent a64b96f commit 3177a0d
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Iterator<Node> listGraphNodes() {

@Override
protected void _close() {
// TODO Auto-generated method stub
factory.getDictionary().close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,10 @@ Iterator<byte[][]> query(final byte[][] query) throws StorageLayerException {
? dao.query(query)
: EMPTY_IDS_ITERATOR;
}

@Override
public void close() {
dictionary.close();
closed = true;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
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.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";
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
22 changes: 22 additions & 0 deletions jena-nosql-integration-tests/src/test/resources/log4j.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] %m%n" />
</layout>
</appender>
<category name="org.gazzax">
<priority value="DEBUG"/>
</category>
<category name="org.apache.cassandra.service">
<priority value="ERROR"/>
</category>
<category name="com">
<priority value="ERROR"/>
</category>
<root>
<priority value="ERROR"/>
<appender-ref ref="CONSOLE"/>
</root>
</log4j:configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE
{
?x foaf:name ?name .
?x foaf:mbox ?mbox
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
----------------------------------------------------
| name | mbox |
====================================================
| "Johnny Lee Outlaw" | <mailto:jlow@example.com> |
| "Peter Goodguy" | <mailto:peter@example.org> |
----------------------------------------------------
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Johnny Lee Outlaw" .
_:a foaf:mbox <mailto:[email protected]> .
_:b foaf:name "Peter Goodguy" .
_:b foaf:mbox <mailto:[email protected]> .
_:c foaf:mbox <mailto:[email protected]> .
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 "cat" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----
| v |
=====
-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@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#> .

:x ns:p "cat"@en .
:y ns:p "42"^^xsd:integer .
:z ns:p "abc"^^dt:specialDatatype .
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 "cat"@en }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
------
| v |
======
| :x |
------

0 comments on commit 3177a0d

Please sign in to comment.