Skip to content

Commit

Permalink
53: Querying string literals with diacritics or angle brackets yields no
Browse files Browse the repository at this point in the history
  • Loading branch information
agazzarini committed Apr 16, 2015
1 parent 463fee0 commit 29b6c0a
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 141 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package org.gazzax.labs.solrdf.integration;

import static org.gazzax.labs.solrdf.TestUtility.DUMMY_BASE_URI;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
Expand All @@ -12,25 +21,44 @@
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.gazzax.labs.solrdf.MisteryGuest;
import org.gazzax.labs.solrdf.log.Log;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.slf4j.LoggerFactory;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetAccessor;
import com.hp.hpl.jena.query.DatasetAccessorFactory;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.sparql.resultset.ResultSetCompare;

/**
* Supertype layer for all integration tests.
*
* @author Andrea Gazzarini
* @since 1.0
*/
public class IntegrationTestSupertypeLayer {
public abstract class IntegrationTestSupertypeLayer {
protected static final String SOLR_URI = "http://127.0.0.1:8080/solr/store";
protected static final String SPARQL_ENDPOINT_URI = SOLR_URI + "/sparql";
protected static final String GRAPH_STORE_ENDPOINT_URI = SOLR_URI + "/rdf-graph-store";

protected final Log log = new Log(LoggerFactory.getLogger(getClass()));

protected static HttpSolrServer solr;
protected QueryExecution execution;
protected QueryExecution inMemoryExecution;
protected Dataset memoryDataset;
protected static DatasetAccessor DATASET;

/**
* Initilisation procedure for this test case.
Expand All @@ -41,6 +69,7 @@ public class IntegrationTestSupertypeLayer {
public static void initClient() {
solr = new HttpSolrServer(SOLR_URI);
resetSolRDFXmlResponseParser();
DATASET = DatasetAccessorFactory.createHTTP(GRAPH_STORE_ENDPOINT_URI);
}

/**
Expand All @@ -54,6 +83,42 @@ public static void shutdownClient() throws Exception {
solr.shutdown();
}

/**
* Setup fixture for this test.
*/
@Before
public void setUp() throws Exception {
memoryDataset = DatasetFactory.createMem();
}

/**
* Shutdown procedure for this test.
*
* @throws Exception hopefully never.
*/
@After
public void tearDown() throws Exception {
if (execution != null) {
execution.close();
}

if (inMemoryExecution != null) {
inMemoryExecution.close();
}
}

/**
* Removes all data created by this test.
*
* @throws Exception hopefully never.
*/
protected void clearDatasets() throws Exception {
clearData();
if (memoryDataset != null) {
memoryDataset.getDefaultModel().removeAll();
}
}

/**
* Cleans all data previously indexed on SolRDF.
*
Expand Down Expand Up @@ -143,4 +208,93 @@ protected NamedList<Object> readNamedList(final XMLStreamReader parser) throws X
}
});
}

/**
* 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.
*/
protected String queryString(final String filename) throws IOException {
return readFile(filename);
}

/**
* 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.
*/
protected String readFile(final String filename) throws IOException {
return new String(Files.readAllBytes(Paths.get(source(filename))));
}

/**
* Returns the URI of a given filename.
*
* @param filename the filename.
* @return the URI (as string) of a given filename.
*/
protected URI source(final String filename) {
return new File(examplesDirectory(), filename).toURI();
}

/**
* Executes a given query against a given dataset.
*
* @param data the mistery guest containing test data (query and dataset)
* @throws Exception never, otherwise the test fails.
*/
protected void execute(final MisteryGuest data) throws Exception {
load(data);

final Query query = QueryFactory.create(queryString(data.query));
try {
assertTrue(
Arrays.toString(data.datasets) + ", " + data.query,
ResultSetCompare.isomorphic(
(execution = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT_URI, query)).execSelect(),
(inMemoryExecution = QueryExecutionFactory.create(query, memoryDataset)).execSelect()));
} catch (final Exception error) {
QueryExecution debugExecution = null;
log.debug("JNS\n" + ResultSetFormatter.asText(
(debugExecution = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT_URI, query)).execSelect()));

debugExecution.close();
log.debug("MEM\n" + ResultSetFormatter.asText(
(debugExecution = (QueryExecutionFactory.create(query, memoryDataset))).execSelect()));

debugExecution.close();
throw error;
}
}

/**
* Loads all triples found in the datafile associated with the given name.
*
* @param datafileName the name of the datafile.
* @throws Exception hopefully never, otherwise the test fails.
*/
protected void load(final MisteryGuest data) throws Exception {
final Model memoryModel = memoryDataset.getDefaultModel();

for (final String datafileName : data.datasets) {
final String dataURL = source(datafileName).toString();
final String lang = datafileName.endsWith("ttl") ? "TTL" : null;
memoryModel.read(dataURL, DUMMY_BASE_URI, lang);
}

DATASET.add(memoryModel);
commitChanges();

final Model model = DATASET.getModel();

assertFalse(Arrays.toString(data.datasets) + ", " + data.query, model.isEmpty());
assertTrue(Arrays.toString(data.datasets) + ", " + data.query, model.isIsomorphicWith(memoryModel));
}


protected abstract String examplesDirectory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.junit.BeforeClass;
import org.junit.Test;

import com.hp.hpl.jena.query.DatasetAccessor;
import com.hp.hpl.jena.query.DatasetAccessorFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

Expand All @@ -42,7 +40,6 @@
public class FacetObjectQueries_ITCase extends IntegrationTestSupertypeLayer {
private final static String TEST_DATA_URI = new File("src/test/resources/sample_data/faceting_test_dataset.nt").toURI().toString();

private static DatasetAccessor DATASET;
private SolrQuery query;

private final String publisherQuery = "p:<http\\://purl.org/dc/elements/1.1/publisher>";
Expand Down Expand Up @@ -111,7 +108,6 @@ public final static void loadSampleData() throws SolrServerException, IOExceptio
final Model memoryModel = ModelFactory.createDefaultModel();
memoryModel.read(TEST_DATA_URI, DUMMY_BASE_URI, "N-TRIPLES");

DATASET = DatasetAccessorFactory.createHTTP(GRAPH_STORE_ENDPOINT_URI);
DATASET.add(memoryModel);

commitChanges();
Expand All @@ -128,7 +124,7 @@ public final static void loadSampleData() throws SolrServerException, IOExceptio
* @throws Exception hopefully never, otherwise the test fails.
*/
@Before
public final void setUp() throws Exception {
public void init() throws Exception {
query = new SolrQuery("SELECT * WHERE { ?s ?p ?o }");
query.setRows(0);
query.setFacet(true);
Expand Down Expand Up @@ -394,5 +390,10 @@ private void assertOneFacetQuery(
assertNull(facetObjectQueries.get(alias));
assertFacetResults(expectedResults, (NamedList<?>) facetObjectQueries.get(facetQuery));
}
}

@Override
protected String examplesDirectory() {
throw new IllegalStateException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This Test case makes use of some examples from
*
* "Learning SPARQL - Querying and Updating with SPARQL 1.1" by Bob DuCharme
*
* Publisher: O'Reilly
* Author: Bob DuCharme
* ISBN: 978-1-449-37143-2
* http://www.learningsparql.com/
* http://shop.oreilly.com/product/0636920030829.do
*
* We warmly appreciate and thank the author and O'Reilly for such permission.
*
*/
package org.gazzax.labs.solrdf.integration.sparql;

import static org.gazzax.labs.solrdf.MisteryGuest.misteryGuest;

import org.gazzax.labs.solrdf.integration.IntegrationTestSupertypeLayer;
import org.junit.After;
import org.junit.Test;

/**
* Querying string literals with diacritics or angle brackets yields no result.
*
* @author Andrea Gazzarini
* @since 1.0
* @see
*/
public class Issue53_ITCase extends IntegrationTestSupertypeLayer {
protected final static String EXAMPLES_DIR = "src/test/resources/sample_data";

/**
* Shutdown procedure for this test.
*
* @throws Exception hopefully never.
*/
@After
public void tearDown() throws Exception {
clearDatasets();
}

@Test
public void diacriticsInWhereCondition() throws Exception {
execute(misteryGuest("issue_53_diacriticts.rq", "issue_53_diacriticts.ttl"));
}

@Test
public void squareBracketsInLiterals() throws Exception {
execute(misteryGuest("issue_53_square_brackets.rq", "issue_53_square_brackets.ttl"));
}

@Override
protected String examplesDirectory() {
return EXAMPLES_DIR;
}
}
Loading

0 comments on commit 29b6c0a

Please sign in to comment.