1- /**
2- * Copyright (c) 2017 Eclipse RDF4J contributors.
3- * All rights reserved. This program and the accompanying materials
4- * are made available under the terms of the Eclipse Distribution License v1.0
5- * which accompanies this distribution, and is available at
6- * http://www.eclipse.org/org/documents/edl-v10.php.
1+ /*
2+ * To change this license header, choose License Headers in Project Properties.
3+ * To change this template file, choose Tools | Templates
4+ * and open the template in the editor.
75 */
86package org .eclipse .rdf4j .lucene .spin ;
97
10- import java .io .File ;
11- import static org .eclipse .rdf4j .sail .lucene .LuceneSailSchema .ALL_MATCHES ;
12- import static org .eclipse .rdf4j .sail .lucene .LuceneSailSchema .SCORE ;
13- import static org .eclipse .rdf4j .sail .lucene .LuceneSailSchema .SEARCH ;
14-
15- import java .io .IOException ;
16- import java .net .URL ;
17- import java .util .Properties ;
18-
198import org .apache .commons .io .output .ByteArrayOutputStream ;
209import org .eclipse .rdf4j .common .iteration .Iterations ;
21- import org .eclipse .rdf4j .model .Model ;
2210import org .eclipse .rdf4j .model .Resource ;
2311import org .eclipse .rdf4j .model .Statement ;
2412import org .eclipse .rdf4j .model .vocabulary .GEO ;
2917import org .eclipse .rdf4j .query .TupleQuery ;
3018import org .eclipse .rdf4j .query .TupleQueryResult ;
3119import org .eclipse .rdf4j .query .resultio .text .csv .SPARQLResultsCSVWriter ;
32- import org .eclipse .rdf4j .repository .Repository ;
3320import org .eclipse .rdf4j .repository .RepositoryConnection ;
34- import org .eclipse .rdf4j .repository .RepositoryException ;
3521import org .eclipse .rdf4j .repository .RepositoryResult ;
36- import org .eclipse .rdf4j .repository .sail .SailRepository ;
37- import org .eclipse .rdf4j .rio .RDFFormat ;
38- import org .eclipse .rdf4j .rio .Rio ;
3922import org .eclipse .rdf4j .rio .turtle .TurtleWriter ;
40- import org .eclipse .rdf4j .sail .lucene .LuceneSail ;
4123import org .eclipse .rdf4j .sail .lucene .LuceneSailSchema ;
42- import org .eclipse .rdf4j .sail .memory . MemoryStore ;
43- import org .eclipse .rdf4j .sail .spin . SpinSail ;
44- import org .junit . After ;
24+ import static org .eclipse .rdf4j .sail .lucene . LuceneSailSchema . ALL_MATCHES ;
25+ import static org .eclipse .rdf4j .sail .lucene . LuceneSailSchema . SCORE ;
26+ import static org .eclipse . rdf4j . sail . lucene . LuceneSailSchema . SEARCH ;
4527import org .junit .Assert ;
46- import org .junit .Before ;
47- import org .junit .Rule ;
4828import org .junit .Test ;
49- import org .junit .rules .TemporaryFolder ;
5029import org .slf4j .Logger ;
5130import org .slf4j .LoggerFactory ;
5231
5635 * @see <a href="https://github.com/eclipse/rdf4j/issues/220">issue #220</a>
5736 * @see <a href="https://github.com/eclipse/rdf4j/issues/235">issue #235</a>
5837 * @see <a href="https://github.com/eclipse/rdf4j/issues/739">issue #739</a>
38+ * @author Jacek Grzebyta
39+ * @author Mark Hale
5940 */
60- public class LuceneSailSpinTest {
61-
62- private static final String DATA = "org/eclipse/rdf4j/sail/220-example.ttl" ;
63-
64- private static Logger log = LoggerFactory .getLogger (LuceneSailSpinTest .class );
41+ public abstract class AbstractLuceneSailSpinTest {
6542
66- @ Rule
67- public TemporaryFolder tempDir = new TemporaryFolder ();
43+ private static final Logger log = LoggerFactory .getLogger (AbstractLuceneSailSpinTest .class );
6844
69- private Repository repository ;
70-
71- private RepositoryConnection connection ;
72-
73- @ Before
74- public void setUp ()
75- throws Exception
76- {
77-
78- //activate sail debug mode
79- // System.setProperty("org.eclipse.rdf4j.repository.debug", "true");
80- // load data into memory store
81- MemoryStore store = new MemoryStore ();
82-
83- // add Support for SPIN function
84- SpinSail spin = new SpinSail (store );
85-
86- // add Lucene Spin Sail support
87- LuceneSpinSail luc = new LuceneSpinSail (spin );
88- File tmpDirFolder = tempDir .newFolder ();
89- log .debug ("data file: {}" , tmpDirFolder );
90- luc .setDataDir (tmpDirFolder );
91- repository = new SailRepository (luc );
92-
93- // set up parameters
94- configure (luc .getParameters ());
95-
96- repository .initialize ();
97- // local connection used only for population
98- try (RepositoryConnection localConn = repository .getConnection ()) {
99- localConn .begin ();
100- populate (localConn );
101- localConn .commit ();
102- }
103-
104- // local connection for verification only
105- try (RepositoryConnection localConn = repository .getConnection ()) {
106- // validate population. Transaction is not required
107- //localConn.begin();
108- int count = countStatements (localConn );
109- log .trace ("storage contains {} triples" , count );
110- Assert .assertTrue (count > 0 );
111- //localConn.commit();
112- localConn .close ();
113- }
114-
115- // testing connection
116- connection = repository .getConnection ();
117- connection .begin ();
118- Assert .assertTrue ("connection is not active" , connection .isActive ());
119- }
120-
121- @ After
122- public void tearDown ()
123- throws RepositoryException , IOException
124- {
125- try {
126- if (connection != null ) {
127- connection .close ();
128- }
129- }
130- finally {
131- if (repository != null ) {
132- repository .shutDown ();
133- }
134- }
135- }
136-
137- protected void populate (RepositoryConnection repoConn )
138- throws Exception
139- {
140- // load resources
141- assert repoConn .isActive ();
142- URL resourceURL = LuceneSailSpinTest .class .getClassLoader ().getResource (DATA );
143- log .debug ("Resource URL: {}" , resourceURL .toString ());
144- Model model = Rio .parse (resourceURL .openStream (), resourceURL .toString (), RDFFormat .TURTLE );
145- for (Statement stmt : model ) {
146- repoConn .add (stmt );
147- }
148- }
45+ public abstract RepositoryConnection getConnection ();
14946
15047 /**
15148 * Positive control test: ie valid SPARQL query
@@ -158,7 +55,7 @@ public void simpleTest()
15855 {
15956 StringBuilder buffer = new StringBuilder ();
16057 buffer .append ("select ?s ?p ?o where { ?s ?p ?o } limit 10" );
161- TupleQuery query = connection .prepareTupleQuery (QueryLanguage .SPARQL , buffer .toString ());
58+ TupleQuery query = getConnection () .prepareTupleQuery (QueryLanguage .SPARQL , buffer .toString ());
16259 try (TupleQueryResult res = query .evaluate ()) {
16360 int count = countTupleResults (res );
16461 log .info ("count statements: {}" , count );
@@ -187,7 +84,7 @@ public void simpleSearchTest()
18784 buffer .append ("}\n " );
18885 log .info ("Request query: \n ====================\n {}\n ======================\n " , buffer .toString ());
18986
190- TupleQuery query = connection .prepareTupleQuery (QueryLanguage .SPARQL , buffer .toString ());
87+ TupleQuery query = getConnection () .prepareTupleQuery (QueryLanguage .SPARQL , buffer .toString ());
19188 log .debug ("query class: {}" , query .getClass ());
19289 //log.debug("query representation: \n{}", query);
19390 //printTupleResult(query);
@@ -222,7 +119,7 @@ public void test220Issue()
222119 buffer .append ("}\n " );
223120 log .info ("Request query: \n ====================\n {}\n ======================\n " , buffer .toString ());
224121
225- TupleQuery query = connection .prepareTupleQuery (QueryLanguage .SPARQL , buffer .toString ());
122+ TupleQuery query = getConnection () .prepareTupleQuery (QueryLanguage .SPARQL , buffer .toString ());
226123 printTupleResult (query );
227124 try (TupleQueryResult res = query .evaluate ()) {
228125 int count = countTupleResults (res );
@@ -266,7 +163,7 @@ public void test235Issue()
266163 buffer .append (" }" );
267164 log .info ("Request query: \n ====================\n {}\n ======================\n " , buffer .toString ());
268165
269- GraphQuery query = connection .prepareGraphQuery (QueryLanguage .SPARQL , buffer .toString ());
166+ GraphQuery query = getConnection () .prepareGraphQuery (QueryLanguage .SPARQL , buffer .toString ());
270167 printGraphResult (query );
271168 try (GraphQueryResult res = query .evaluate ()) {
272169 int cnt = countGraphResults (res );
@@ -278,6 +175,7 @@ public void test235Issue()
278175 public void testDistanceFunction ()
279176 throws Exception
280177 {
178+ RepositoryConnection connection = getConnection ();
281179 String queryStr = "prefix geo: <" + GEO .NAMESPACE + ">" + "prefix geof: <" + GEOF .NAMESPACE + ">"
282180 + "prefix search: <" + LuceneSailSchema .NAMESPACE + ">"
283181 + "select ?toUri ?fromUri ?dist where {(?from ?range ?units geo:asWKT search:distance)"
@@ -335,7 +233,4 @@ public void printTupleResult(TupleQuery query) {
335233 log .info ("\n =============\n " + new String (resultoutput .toByteArray ()) + "\n =============" );
336234 }
337235
338- public void configure (Properties parameters ) {
339- parameters .setProperty (LuceneSail .INDEX_CLASS_KEY , LuceneSail .DEFAULT_INDEX_CLASS );
340- }
341236}
0 commit comments