|
1 |
| -package org.gazzax.labs.jena.nosql.fwk.w3c; |
| 1 | +package org.gazzax.labs.jena.nosql.fwk; |
2 | 2 |
|
3 | 3 | import static org.gazzax.labs.jena.nosql.fwk.TestUtility.DUMMY_BASE_URI;
|
4 | 4 | import static org.junit.Assert.assertEquals;
|
| 5 | +import static org.junit.Assert.assertFalse; |
5 | 6 |
|
6 |
| -import java.io.BufferedReader; |
7 | 7 | import java.io.File;
|
8 |
| -import java.io.FileReader; |
9 | 8 | import java.io.IOException;
|
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Paths; |
10 | 11 |
|
11 | 12 | import org.gazzax.labs.jena.nosql.fwk.factory.StorageLayerFactory;
|
12 | 13 | import org.junit.After;
|
|
23 | 24 | import com.hp.hpl.jena.query.ResultSetFormatter;
|
24 | 25 | import com.hp.hpl.jena.rdf.model.Model;
|
25 | 26 |
|
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 { |
27 | 34 | protected static final String EXAMPLES_DIR = "src/test/resources/w3c/";
|
28 | 35 |
|
29 | 36 | private Dataset dataset;
|
30 | 37 | private StorageLayerFactory factory;
|
31 | 38 |
|
32 | 39 | @Before
|
33 |
| - public void setUp() { |
| 40 | + public final void setUp() { |
34 | 41 | factory = StorageLayerFactory.getFactory();
|
35 | 42 | dataset = DatasetFactory.create(factory.getDatasetGraph());
|
36 | 43 |
|
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"); |
45 | 45 | }
|
46 | 46 |
|
| 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 | + */ |
47 | 60 | @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()); |
54 | 63 | }
|
55 | 64 |
|
56 | 65 | @After
|
57 | 66 | public void tearDown() {
|
| 67 | + factory.getTripleIndexDAO().clear(); |
| 68 | + dataset.close(); |
58 | 69 | factory.getClientShutdownHook().close();
|
59 | 70 | }
|
60 | 71 |
|
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); |
81 | 91 | }
|
82 | 92 |
|
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()))); |
111 | 102 | }
|
| 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 | + } |
112 | 137 | }
|
0 commit comments