Skip to content

Commit 3177a0d

Browse files
author
agazzarini
committed
[ issue #10] Added 4 SPARQL integration tests with data from
http://www.w3.org/TR/sparql11-query
1 parent a64b96f commit 3177a0d

File tree

15 files changed

+233
-68
lines changed

15 files changed

+233
-68
lines changed

jena-nosql-framework/src/main/java/org/gazzax/labs/jena/nosql/fwk/graph/NoSqlDatasetGraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Iterator<Node> listGraphNodes() {
2525

2626
@Override
2727
protected void _close() {
28-
// TODO Auto-generated method stub
28+
factory.getDictionary().close();
2929
}
3030

3131
@Override

jena-nosql-framework/src/main/java/org/gazzax/labs/jena/nosql/fwk/graph/NoSqlGraph.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,10 @@ Iterator<byte[][]> query(final byte[][] query) throws StorageLayerException {
142142
? dao.query(query)
143143
: EMPTY_IDS_ITERATOR;
144144
}
145+
146+
@Override
147+
public void close() {
148+
dictionary.close();
149+
closed = true;
150+
}
145151
}
Lines changed: 92 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
package org.gazzax.labs.jena.nosql.fwk.w3c;
1+
package org.gazzax.labs.jena.nosql.fwk;
22

33
import static org.gazzax.labs.jena.nosql.fwk.TestUtility.DUMMY_BASE_URI;
44
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertFalse;
56

6-
import java.io.BufferedReader;
77
import java.io.File;
8-
import java.io.FileReader;
98
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
1011

1112
import org.gazzax.labs.jena.nosql.fwk.factory.StorageLayerFactory;
1213
import org.junit.After;
@@ -23,90 +24,114 @@
2324
import com.hp.hpl.jena.query.ResultSetFormatter;
2425
import com.hp.hpl.jena.rdf.model.Model;
2526

26-
public class Ex1ITCase {
27+
/**
28+
* Supertype layer for all SPARQL integration tests.
29+
*
30+
* @author Andrea Gazzarini
31+
* @since 1.0
32+
*/
33+
public abstract class SparqlIntegrationTestCase {
2734
protected static final String EXAMPLES_DIR = "src/test/resources/w3c/";
2835

2936
private Dataset dataset;
3037
private StorageLayerFactory factory;
3138

3239
@Before
33-
public void setUp() {
40+
public final void setUp() {
3441
factory = StorageLayerFactory.getFactory();
3542
dataset = DatasetFactory.create(factory.getDatasetGraph());
3643

37-
BufferedReader reader = null;
38-
try {
39-
reader = new BufferedReader(new FileReader(new File(EXAMPLES_DIR, "chapter_2.1_ex1.ttl")));
40-
final Model model = dataset.getDefaultModel().read(reader, DUMMY_BASE_URI, "TTL");
41-
assertEquals(1, model.size());
42-
} catch (final Exception exception) {
43-
// TODO: handle exception
44-
}
44+
load(testFilename() + ".ttl");
4545
}
4646

47+
/**
48+
* In case the conctete test consists in just 1 example, this method returns the filename associated with that example.
49+
*
50+
* @return the name of the file associated with the example.
51+
*/
52+
protected abstract String testFilename();
53+
54+
55+
/**
56+
* Executes the test.
57+
*
58+
* @throws Exception hopefully never, otherwise the test fails.
59+
*/
4760
@Test
48-
public void test() {
49-
final Query query = QueryFactory.create(query("chapter_2.1_ex1.rq"));
50-
final QueryExecution execution = QueryExecutionFactory.create(query, dataset);
51-
final ResultSet rs = execution.execSelect();
52-
assertEquals(ResultSetFormatter.asText(rs, query).trim(), results("chapter_2.1_ex1.rs").trim());
53-
execution.close();
61+
public void example1() throws Exception {
62+
executeTestWithFile(testFilename());
5463
}
5564

5665
@After
5766
public void tearDown() {
67+
factory.getTripleIndexDAO().clear();
68+
dataset.close();
5869
factory.getClientShutdownHook().close();
5970
}
6071

61-
private String query(final String sourcefileName) {
62-
BufferedReader reader = null;
63-
try {
64-
reader = new BufferedReader(new FileReader(new File(EXAMPLES_DIR, sourcefileName)));
65-
String actLine = null;
66-
final StringBuilder builder = new StringBuilder();
67-
while ( (actLine = reader.readLine()) != null) {
68-
builder.append(actLine);
69-
}
70-
return builder.toString();
71-
} catch (final Exception exception) {
72-
throw new RuntimeException(exception);
73-
} finally {
74-
try {
75-
reader.close();
76-
} catch (IOException e) {
77-
// TODO Auto-generated catch block
78-
e.printStackTrace();
79-
}
80-
}
72+
/**
73+
* Reads a query from the file associated with this test and builds a query string.
74+
*
75+
* @param filename the filename.
76+
* @return the query string associated with this test.
77+
* @throws IOException in case of I/O failure while reading the file.
78+
*/
79+
private String queryString(final String filename) throws IOException {
80+
return readFile(filename);
81+
}
82+
83+
/**
84+
* Builds a string (from the file associated with this test) with the expected query results.
85+
*
86+
* @return a string (from the file associated with this test) with the expected query results.
87+
* @throws IOException in case of I/O failure while reading the file.
88+
*/
89+
private String results(final String resultsFileName) throws IOException {
90+
return readFile(resultsFileName);
8191
}
8292

83-
private String results(final String resultsFileName) {
84-
BufferedReader reader = null;
85-
try {
86-
reader = new BufferedReader(new FileReader(new File(EXAMPLES_DIR, resultsFileName)));
87-
String actLine = null;
88-
final StringBuilder builder = new StringBuilder();
89-
int i = 0;
90-
while ( (actLine = reader.readLine()) != null) {
91-
if (i > 0) {
92-
builder.append(System.getProperty("line.separator"));
93-
}
94-
builder.append(actLine);
95-
i++;
96-
}
97-
98-
System.out.println(builder);
99-
100-
return builder.toString();
101-
} catch (final Exception exception) {
102-
throw new RuntimeException(exception);
103-
} finally {
104-
try {
105-
reader.close();
106-
} catch (IOException e) {
107-
// TODO Auto-generated catch block
108-
e.printStackTrace();
109-
}
110-
}
93+
/**
94+
* Builds a string from a given file.
95+
*
96+
* @param filename the filename (without path).
97+
* @return a string with the file content.
98+
* @throws IOException in case of I/O failure while reading the file.
99+
*/
100+
private String readFile(final String filename) throws IOException {
101+
return new String(Files.readAllBytes(Paths.get(new File(EXAMPLES_DIR, filename).toURI())));
111102
}
103+
104+
/**
105+
* Loads all triples found in the datafile associated with the given name.
106+
*
107+
* @param datafileName the name of the datafile.
108+
*/
109+
private void load(final String datafileName) {
110+
final Model model = dataset.getDefaultModel().read(new File(EXAMPLES_DIR, datafileName).toURI().toString(), DUMMY_BASE_URI, "TTL");
111+
assertFalse(model.isEmpty());
112+
}
113+
114+
/**
115+
* Internal method used to execute a query and assert corresponding results.
116+
* In case the test has just one method, there's nothing to do, the subclass already inherits
117+
* the predefined {@link #executeTest()}.
118+
*
119+
* Otherwise, if a test case includes more than one test, then that concrete subclass needs to define test methods
120+
* and call this method to execute and check queries.
121+
*
122+
* @param filename the filename.
123+
* @throws Exception hopefully never otherwise the test fails.
124+
*/
125+
protected void executeTestWithFile(final String filename) throws Exception {
126+
final Query query = QueryFactory.create(queryString(filename + ".rq"));
127+
final QueryExecution execution = QueryExecutionFactory.create(query, dataset);
128+
final ResultSet rs = execution.execSelect();
129+
130+
final String s = ResultSetFormatter.asText(rs, query).trim();
131+
System.out.println(s);
132+
assertEquals(
133+
results(filename + ".rs").trim(),
134+
s.trim());
135+
execution.close();
136+
}
112137
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.gazzax.labs.jena.nosql.fwk.w3c;
2+
3+
import org.gazzax.labs.jena.nosql.fwk.SparqlIntegrationTestCase;
4+
5+
/**
6+
* SPARQL Integration test with examples taken from http://www.w3.org/TR/sparql11-query
7+
*
8+
* @see http://www.w3.org/TR/sparql11-query/#WritingSimpleQueries
9+
* @author Andrea Gazzarini
10+
* @since 1.0
11+
*/
12+
public class Ch2_1_WritingSimpleQueries_ITCase extends SparqlIntegrationTestCase {
13+
14+
@Override
15+
protected String testFilename() {
16+
return "chapter_2.1_ex1";
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.gazzax.labs.jena.nosql.fwk.w3c;
2+
3+
import org.gazzax.labs.jena.nosql.fwk.SparqlIntegrationTestCase;
4+
5+
/**
6+
* SPARQL Integration test with examples taken from http://www.w3.org/TR/sparql11-query
7+
*
8+
* @see http://www.w3.org/TR/sparql11-query/#MultipleMatches
9+
* @author Andrea Gazzarini
10+
* @since 1.0
11+
*/
12+
public class Ch2_2_MultipleMatches_ITCase extends SparqlIntegrationTestCase {
13+
@Override
14+
protected String testFilename() {
15+
return "chapter_2.2_ex1";
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.gazzax.labs.jena.nosql.fwk.w3c;
2+
3+
import org.gazzax.labs.jena.nosql.fwk.SparqlIntegrationTestCase;
4+
import org.junit.Test;
5+
6+
/**
7+
* SPARQL Integration test with examples taken from http://www.w3.org/TR/sparql11-query
8+
*
9+
* @see http://www.w3.org/TR/sparql11-query/#matchingRDFLiterals
10+
* @author Andrea Gazzarini
11+
* @since 1.0
12+
*/
13+
public class Ch2_3_MatchingRDFLiterals_ITCase extends SparqlIntegrationTestCase {
14+
@Override
15+
protected String testFilename() {
16+
return "chapter_2.3_ex1";
17+
}
18+
19+
/**
20+
* Executes the 2nd test of the chapter.
21+
*
22+
* @throws Exception hopefully never, otherwise the test fails.
23+
*/
24+
@Test
25+
public void example2() throws Exception {
26+
executeTestWithFile("chapter_2.3_ex2");
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3+
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
4+
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
5+
<layout class="org.apache.log4j.PatternLayout">
6+
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] %m%n" />
7+
</layout>
8+
</appender>
9+
<category name="org.gazzax">
10+
<priority value="DEBUG"/>
11+
</category>
12+
<category name="org.apache.cassandra.service">
13+
<priority value="ERROR"/>
14+
</category>
15+
<category name="com">
16+
<priority value="ERROR"/>
17+
</category>
18+
<root>
19+
<priority value="ERROR"/>
20+
<appender-ref ref="CONSOLE"/>
21+
</root>
22+
</log4j:configuration>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
2+
SELECT ?name ?mbox
3+
WHERE
4+
{
5+
?x foaf:name ?name .
6+
?x foaf:mbox ?mbox
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
----------------------------------------------------
2+
| name | mbox |
3+
====================================================
4+
| "Johnny Lee Outlaw" | <mailto:jlow@example.com> |
5+
| "Peter Goodguy" | <mailto:peter@example.org> |
6+
----------------------------------------------------
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
2+
3+
_:a foaf:name "Johnny Lee Outlaw" .
4+
_:a foaf:mbox <mailto:[email protected]> .
5+
_:b foaf:name "Peter Goodguy" .
6+
_:b foaf:mbox <mailto:[email protected]> .
7+
_:c foaf:mbox <mailto:[email protected]> .

0 commit comments

Comments
 (0)