|
| 1 | +/* |
| 2 | + * This Test case makes use of some examples from |
| 3 | + * |
| 4 | + * "Learning SPARQL - Querying and Updating with SPARQL 1.1" by Bob DuCharme |
| 5 | + * |
| 6 | + * Publisher: O'Reilly |
| 7 | + * Author: Bob DuCharme |
| 8 | + * ISBN: 978-1-449-37143-2 |
| 9 | + * http://www.learningsparql.com/ |
| 10 | + * http://shop.oreilly.com/product/0636920030829.do |
| 11 | + * |
| 12 | + * We warmly appreciate and thank the author and O'Reilly for such permission. |
| 13 | + * |
| 14 | + */ |
| 15 | +package org.gazzax.labs.jena.nosql.fwk.learningSPARQL; |
| 16 | + |
| 17 | +import static org.gazzax.labs.jena.nosql.fwk.TestUtility.DUMMY_BASE_URI; |
| 18 | +import static org.gazzax.labs.jena.nosql.fwk.TestUtility.eheh; |
| 19 | +import static org.gazzax.labs.jena.nosql.fwk.learningSPARQL.MisteryGuest.misteryGuest; |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.URI; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Paths; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import org.gazzax.labs.jena.nosql.fwk.factory.StorageLayerFactory; |
| 32 | +import org.gazzax.labs.jena.nosql.fwk.log.Log; |
| 33 | +import org.junit.After; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.BeforeClass; |
| 36 | +import org.junit.Test; |
| 37 | +import org.slf4j.LoggerFactory; |
| 38 | + |
| 39 | +import com.hp.hpl.jena.query.Dataset; |
| 40 | +import com.hp.hpl.jena.query.DatasetFactory; |
| 41 | +import com.hp.hpl.jena.query.Query; |
| 42 | +import com.hp.hpl.jena.query.QueryExecution; |
| 43 | +import com.hp.hpl.jena.query.QueryExecutionFactory; |
| 44 | +import com.hp.hpl.jena.query.QueryFactory; |
| 45 | +import com.hp.hpl.jena.query.ResultSetFormatter; |
| 46 | +import com.hp.hpl.jena.rdf.model.Model; |
| 47 | +import com.hp.hpl.jena.sparql.resultset.ResultSetCompare; |
| 48 | + |
| 49 | +/** |
| 50 | + * SPARQL integration test. |
| 51 | + * |
| 52 | + * @author Andrea Gazzarini |
| 53 | + * @since 1.0 |
| 54 | + */ |
| 55 | +public class LearningSparql_ITCase { |
| 56 | + protected final String examplesDir = "src/test/resources/LearningSPARQLExamples"; |
| 57 | + |
| 58 | + protected final Log log = new Log(LoggerFactory.getLogger(LearningSparql_ITCase.class)); |
| 59 | + |
| 60 | + protected Dataset dataset; |
| 61 | + protected Dataset memoryDataset; |
| 62 | + |
| 63 | + protected StorageLayerFactory factory; |
| 64 | + |
| 65 | + static final List<MisteryGuest> DATA = new ArrayList<MisteryGuest>(); |
| 66 | + |
| 67 | + /** |
| 68 | + * Fills the data and queries map. |
| 69 | + */ |
| 70 | + @BeforeClass |
| 71 | + public static void init() { |
| 72 | + DATA.add(misteryGuest("ex003.rq", "ex002.ttl")); |
| 73 | + DATA.add(misteryGuest("ex006.rq", "ex002.ttl")); |
| 74 | + DATA.add(misteryGuest("ex007.rq", "ex002.ttl")); |
| 75 | + DATA.add(misteryGuest("ex008.rq", "ex002.ttl")); |
| 76 | + DATA.add(misteryGuest("ex010.rq", "ex002.ttl")); |
| 77 | + |
| 78 | + DATA.add(misteryGuest("ex013.rq", "ex012.ttl")); |
| 79 | + DATA.add(misteryGuest("ex015.rq", "ex012.ttl")); |
| 80 | + DATA.add(misteryGuest("ex017.rq", "ex012.ttl")); |
| 81 | + DATA.add(misteryGuest("ex019.rq", "ex012.ttl")); |
| 82 | + DATA.add(misteryGuest("ex021.rq", "ex012.ttl")); |
| 83 | + DATA.add(misteryGuest("ex047.rq", "ex012.ttl")); |
| 84 | + DATA.add(misteryGuest("ex052.rq", "ex050.ttl", "foaf.rdf")); |
| 85 | + DATA.add(misteryGuest("ex055.rq", "", "ex054.ttl")); |
| 86 | + DATA.add(misteryGuest("ex057.rq", "", "ex054.ttl")); |
| 87 | + DATA.add(misteryGuest("ex059.rq", "", "ex054.ttl")); |
| 88 | + DATA.add(misteryGuest("ex061.rq", "", "ex054.ttl")); |
| 89 | + DATA.add(misteryGuest("ex063.rq", "", "ex054.ttl")); |
| 90 | + DATA.add(misteryGuest("ex065.rq", "!BOUND", "ex054.ttl")); |
| 91 | + DATA.add(misteryGuest("ex067.rq", "FILTER NOT EXISTS", "ex054.ttl")); |
| 92 | + DATA.add(misteryGuest("ex068.rq", "MINUS", "ex054.ttl")); |
| 93 | + DATA.add(misteryGuest("ex070.rq", "Multiple tables", "ex069.ttl")); |
| 94 | + DATA.add(misteryGuest("ex070.rq", "Multiple tables with split datasets", "ex072.ttl", "ex073.ttl", "ex368.ttl")); |
| 95 | + DATA.add(misteryGuest("ex075.rq", "Bind either", "ex074.ttl")); |
| 96 | + DATA.add(misteryGuest("ex077.rq", "Bind either", "ex074.ttl")); |
| 97 | + DATA.add(misteryGuest("ex078.rq", "Property paths", "ex074.ttl")); |
| 98 | + DATA.add(misteryGuest("ex080.rq", "Property paths", "ex074.ttl")); |
| 99 | + DATA.add(misteryGuest("ex082.rq", "Property paths", "ex074.ttl")); |
| 100 | + DATA.add(misteryGuest("ex083.rq", "Property paths", "ex074.ttl")); |
| 101 | + DATA.add(misteryGuest("ex084.rq", "Property paths", "ex074.ttl")); |
| 102 | + DATA.add(misteryGuest("ex086.rq", "Querying blank nodes", "ex041.ttl")); |
| 103 | + DATA.add(misteryGuest("ex088.rq", "Querying blank nodes", "ex041.ttl")); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Setup fixture for this test. |
| 108 | + */ |
| 109 | + @Before |
| 110 | + public final void setUp() { |
| 111 | + factory = StorageLayerFactory.getFactory(); |
| 112 | + dataset = DatasetFactory.create(factory.getDatasetGraph()); |
| 113 | + memoryDataset = DatasetFactory.createMem(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Shutdown procedure for this test. |
| 118 | + */ |
| 119 | + @After |
| 120 | + public void tearDown() { |
| 121 | + clearDatasets(); |
| 122 | + factory.getClientShutdownHook().close(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void select() throws Exception { |
| 127 | + for (final MisteryGuest data : DATA) { |
| 128 | + log.info("Running " + data.description + " test..."); |
| 129 | + |
| 130 | + load(data); |
| 131 | + |
| 132 | + final Query query = QueryFactory.create(queryString(data.query)); |
| 133 | + QueryExecution execution = null; |
| 134 | + QueryExecution inMemoryExecution = null; |
| 135 | + |
| 136 | + try { |
| 137 | + assertTrue( |
| 138 | + data.datasets + ", " + data.query, |
| 139 | + ResultSetCompare.isomorphic( |
| 140 | + (execution = QueryExecutionFactory.create(query, dataset)).execSelect(), |
| 141 | + (inMemoryExecution = QueryExecutionFactory.create(query, memoryDataset)).execSelect())); |
| 142 | + |
| 143 | + if (log.isDebugEnabled()) { |
| 144 | + QueryExecution secondExecution = null; |
| 145 | + log.debug("\n" + ResultSetFormatter.asText( |
| 146 | + (secondExecution = (QueryExecutionFactory.create(query, dataset))).execSelect())); |
| 147 | + |
| 148 | + secondExecution.close(); |
| 149 | + } |
| 150 | + |
| 151 | + } finally { |
| 152 | + clearDatasets(); |
| 153 | + execution.close(); |
| 154 | + inMemoryExecution.close(); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Reads a query from the file associated with this test and builds a query string. |
| 161 | + * |
| 162 | + * @param filename the filename. |
| 163 | + * @return the query string associated with this test. |
| 164 | + * @throws IOException in case of I/O failure while reading the file. |
| 165 | + */ |
| 166 | + protected String queryString(final String filename) throws IOException { |
| 167 | + return readFile(filename); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Builds a string from a given file. |
| 172 | + * |
| 173 | + * @param filename the filename (without path). |
| 174 | + * @return a string with the file content. |
| 175 | + * @throws IOException in case of I/O failure while reading the file. |
| 176 | + */ |
| 177 | + protected String readFile(final String filename) throws IOException { |
| 178 | + return new String(Files.readAllBytes(Paths.get(source(filename)))); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Loads all triples found in the datafile associated with the given name. |
| 183 | + * |
| 184 | + * @param datafileName the name of the datafile. |
| 185 | + */ |
| 186 | + protected void load(final MisteryGuest data) { |
| 187 | + final Model model = dataset.getDefaultModel(); |
| 188 | + final Model memoryModel = memoryDataset.getDefaultModel(); |
| 189 | + |
| 190 | + for (final String datafileName : data.datasets) { |
| 191 | + final String dataURL = source(datafileName).toString(); |
| 192 | + final String lang = datafileName.endsWith("ttl") ? "TTL" : null; |
| 193 | + model.read(dataURL, DUMMY_BASE_URI, lang); |
| 194 | + memoryModel.read(dataURL, DUMMY_BASE_URI, lang); |
| 195 | + } |
| 196 | + |
| 197 | + eheh(); |
| 198 | + |
| 199 | + assertFalse(data.datasets + ", " + data.query, model.isEmpty()); |
| 200 | + assertTrue(data.datasets + ", " + data.query, model.isIsomorphicWith(memoryModel)); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Returns the URI of a given filename. |
| 205 | + * |
| 206 | + * @param filename the filename. |
| 207 | + * @return the URI (as string) of a given filename. |
| 208 | + */ |
| 209 | + URI source(final String filename) { |
| 210 | + return new File(examplesDir, filename).toURI(); |
| 211 | + } |
| 212 | + |
| 213 | + void clearDatasets() { |
| 214 | + dataset.getDefaultModel().removeAll(); |
| 215 | + memoryDataset.getDefaultModel().removeAll(); |
| 216 | + eheh(); |
| 217 | + } |
| 218 | +} |
0 commit comments