View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.rdf.rdf4j;
19  
20  import java.io.IOException;
21  import java.io.UncheckedIOException;
22  import java.util.Collections;
23  import java.util.Set;
24  
25  import org.apache.commons.rdf.api.AbstractGraphTest;
26  import org.apache.commons.rdf.api.BlankNodeOrIRI;
27  import org.apache.commons.rdf.api.Dataset;
28  import org.apache.commons.rdf.api.IRI;
29  import org.apache.commons.rdf.api.Literal;
30  import org.apache.commons.rdf.api.Quad;
31  import org.apache.commons.rdf.api.RDFTerm;
32  import org.apache.commons.rdf.api.RDF;
33  import org.eclipse.rdf4j.repository.RepositoryConnection;
34  import org.eclipse.rdf4j.repository.RepositoryResult;
35  import org.eclipse.rdf4j.repository.sail.SailRepository;
36  import org.eclipse.rdf4j.sail.Sail;
37  import org.eclipse.rdf4j.sail.nativerdf.NativeStore;
38  import org.junit.After;
39  import org.junit.Rule;
40  import org.junit.rules.TemporaryFolder;
41  import org.junit.rules.Timeout;
42  
43  /**
44   * Test a graph within a file-based RDF4J {@link SailRepository}.
45   * <p>
46   * TIP: If the {@link #shutdownAndDelete()} take about 20 seconds this is a hint
47   * that a {@link RepositoryConnection} or {@link RepositoryResult} was not
48   * closed correctly.
49   *
50   */
51  public class NativeStoreGraphTest extends AbstractGraphTest {
52  
53      public final class NativeStoreRDF implements RDF {
54  
55          RDF4J rdf4jFactory = new RDF4J(getRepository().getValueFactory());
56  
57          @Override
58          public RDF4JGraph createGraph() {
59              // We re-use the repository connection, but use a different context
60              // every time
61              final Set<RDF4JBlankNode> context = Collections.singleton(rdf4jFactory.createBlankNode());
62              return rdf4jFactory.asGraph(getRepository(), context);
63          }
64  
65          @Override
66          public Dataset createDataset() {
67              throw new UnsupportedOperationException("Can't create more than one Dataset in this test");
68              // ...as the below would re-use the same repository:
69              // return rdf4jFactory.asRDFTermDataset(getRepository());
70          }
71  
72          // Delegate methods
73          @Override
74          public RDF4JBlankNode createBlankNode() {
75              return rdf4jFactory.createBlankNode();
76          }
77  
78          @Override
79          public RDF4JBlankNode createBlankNode(final String name) {
80              return rdf4jFactory.createBlankNode(name);
81          }
82  
83          @Override
84          public RDF4JIRI createIRI(final String iri) throws IllegalArgumentException, UnsupportedOperationException {
85              return rdf4jFactory.createIRI(iri);
86          }
87  
88          @Override
89          public RDF4JLiteral createLiteral(final String lexicalForm) {
90              return rdf4jFactory.createLiteral(lexicalForm);
91          }
92  
93          @Override
94          public Literal createLiteral(final String lexicalForm, final IRI dataType) {
95              return rdf4jFactory.createLiteral(lexicalForm, dataType);
96          }
97  
98          @Override
99          public Literal createLiteral(final String lexicalForm, final String languageTag) {
100             return rdf4jFactory.createLiteral(lexicalForm, languageTag);
101         }
102 
103         @Override
104         public RDF4JTriple createTriple(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
105             return rdf4jFactory.createTriple(subject, predicate, object);
106         }
107 
108         @Override
109         public Quad createQuad(final BlankNodeOrIRI graphName, final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object)
110                 throws IllegalArgumentException {
111             return rdf4jFactory.createQuad(graphName, subject, predicate, object);
112         }
113     }
114 
115     @Rule
116     public TemporaryFolder tempDir = new TemporaryFolder();
117 
118     private SailRepository repository;
119 
120     public void createRepository() throws IOException {
121         final Sail sail = new NativeStore(tempDir.newFolder());
122         repository = new SailRepository(sail);
123         repository.initialize();
124     }
125 
126     public synchronized SailRepository getRepository() {
127         if (repository == null) {
128             try {
129                 createRepository();
130             } catch (final IOException e) {
131                 throw new UncheckedIOException(e);
132             }
133         }
134         return repository;
135     }
136 
137     @Rule
138     /**
139      * A timeout of more than 15 seconds pr test indicates typically that
140      * shutdownAndDelete failed.
141      */
142     public Timeout globalTimeout = Timeout.seconds(15);
143 
144     @After
145     public void shutdownAndDelete() {
146         // must shutdown before we delete
147         if (repository != null) {
148             System.out.print("Shutting down rdf4j repository " + repository + "...");
149             // NOTE:
150             // If this takes about 20 seconds it means the code forgot to close
151             // a
152             // RepositoryConnection or RespositoryResult
153             // e.g. missing a try-with-resources block
154             repository.shutDown();
155             System.out.println("OK");
156         }
157     }
158 
159     // private static void deleteAll(Path dir) throws IOException {
160     // Files.walkFileTree(dir, new SimpleFileVisitor<Path>(){
161     // @Override
162     // public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
163     // throws IOException {
164     // Files.delete(file);
165     // return FileVisitResult.CONTINUE;
166     // }
167     // @Override
168     // public FileVisitResult postVisitDirectory(Path dir, IOException exc)
169     // throws IOException {
170     // FileVisitResult r = super.postVisitDirectory(dir, exc);
171     // Files.delete(dir);
172     // return r;
173     // }
174     // });
175     // }
176 
177     @Override
178     public RDF createFactory() {
179         return new NativeStoreRDF();
180     }
181 
182 }