Test jena-tdb as well

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/1a700701
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/1a700701
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/1a700701

Branch: refs/heads/master
Commit: 1a700701cdd15ee80fe28833acacbfce5e7b2c59
Parents: adec24e
Author: Stian Soiland-Reyes <[email protected]>
Authored: Sun Jan 11 23:10:22 2015 +0000
Committer: Stian Soiland-Reyes <[email protected]>
Committed: Mon Jan 12 01:19:17 2015 +0000

----------------------------------------------------------------------
 .../apache/jena/osgi/test/JenaOSGITestImpl.java | 132 +++++++++++++++----
 1 file changed, 105 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/1a700701/jena-osgi-test/src/main/java/org/apache/jena/osgi/test/JenaOSGITestImpl.java
----------------------------------------------------------------------
diff --git 
a/jena-osgi-test/src/main/java/org/apache/jena/osgi/test/JenaOSGITestImpl.java 
b/jena-osgi-test/src/main/java/org/apache/jena/osgi/test/JenaOSGITestImpl.java
index cce9781..3e119d9 100644
--- 
a/jena-osgi-test/src/main/java/org/apache/jena/osgi/test/JenaOSGITestImpl.java
+++ 
b/jena-osgi-test/src/main/java/org/apache/jena/osgi/test/JenaOSGITestImpl.java
@@ -18,75 +18,153 @@
 
 package org.apache.jena.osgi.test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
+import java.io.OutputStream;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
 
 import org.apache.jena.iri.IRI;
 import org.apache.jena.iri.IRIFactory;
-import org.apache.jena.iri.impl.IRIFactoryImpl;
+import org.apache.jena.riot.Lang;
+import org.apache.jena.riot.RDFDataMgr;
+import org.junit.Test;
 
 import com.hp.hpl.jena.ontology.Individual;
 import com.hp.hpl.jena.ontology.ObjectProperty;
 import com.hp.hpl.jena.ontology.OntModel;
 import com.hp.hpl.jena.ontology.OntModelSpec;
-import com.hp.hpl.jena.ontology.SymmetricProperty;
+import com.hp.hpl.jena.query.Dataset;
+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.QuerySolution;
+import com.hp.hpl.jena.query.ReadWrite;
+import com.hp.hpl.jena.query.ResultSet;
 import com.hp.hpl.jena.rdf.model.Model;
 import com.hp.hpl.jena.rdf.model.ModelFactory;
 import com.hp.hpl.jena.rdf.model.Property;
-import com.hp.hpl.jena.rdf.model.RDFNode;
-import com.hp.hpl.jena.rdf.model.ReifiedStatement;
 import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.tdb.TDBFactory;
 
+/**
+ * Brief tests of the Jena modules covered by jena-osgi
+ * <p>
+ * Do NOT add tests to this class without also adding a
+ * @Test-annotated method to the interface JenaOSGITest
+ * -- otherwise it won't be picked up when testing inside OSGi
+ *  
+ */
 public class JenaOSGITestImpl implements JenaOSGITest {
 
+       private static final String EXAMPLE_COM_GRAPH = 
"http://example.com/graph";;
+       private Resource alice;
+       private Property knows;
+       private Resource bob;
+
+       @Test
        @Override
        public void testJenaCore() throws Exception {
-               Model model = ModelFactory.createDefaultModel();
-               Resource alice = 
model.createResource("http://example.com/alice";);              
-               Property knows = 
model.createProperty("http://xmlns.com/foaf/0.1/knows";);
-               Resource bob = model.createResource("http://example.com/bob";);
-               model.add(model.createStatement(alice, knows, bob));
-               
-               // Does Model's Class.forName() still work?  
-               model.setWriterClassName("someWriter", 
"com.hp.hpl.jena.rdf.model.impl.NTripleWriter");
+               Model model = makeModel();
+
+               // Does Model's Class.forName() still work?
+               model.setWriterClassName("someWriter",
+                               "com.hp.hpl.jena.rdf.model.impl.NTripleWriter");
                Writer writer = new StringWriter();
                model.write(writer, "someWriter");
-               // yes, but only as long as that classname is accessible within 
jena-osgi bundle                
-               assertEquals("<http://example.com/alice> 
<http://xmlns.com/foaf/0.1/knows> <http://example.com/bob> .", 
+               // yes, but only as long as that classname is accessible within
+               // jena-osgi bundle
+               assertEquals(
+                               "<http://example.com/alice> 
<http://xmlns.com/foaf/0.1/knows> <http://example.com/bob> .",
                                writer.toString().trim());
-               
-               // Let's also test com.hp.hpl.jena.ontology             
-               OntModel ontModel = 
ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);
-               ObjectProperty knowsObjProp = 
ontModel.createObjectProperty(knows.getURI());
-               ObjectProperty hasFriend = 
ontModel.createObjectProperty("http://example.com/has_friend";);
+
+               // Let's also test com.hp.hpl.jena.ontology
+               OntModel ontModel = ModelFactory
+                               
.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);
+               ObjectProperty knowsObjProp = 
ontModel.createObjectProperty(knows
+                               .getURI());
+               ObjectProperty hasFriend = ontModel
+                               
.createObjectProperty("http://example.com/has_friend";);
                hasFriend.addSuperProperty(knowsObjProp);
-               
+
                Individual aliceIndividual = ontModel.createIndividual(alice);
                Individual bobIndividiual = ontModel.createIndividual(bob);
                ontModel.add(aliceIndividual, hasFriend, bobIndividiual);
                assertTrue(aliceIndividual.hasProperty(knowsObjProp, 
bobIndividiual));
        }
 
+       private Model makeModel() {
+               Model model = ModelFactory.createDefaultModel();
+               alice = model.createResource("http://example.com/alice";);
+               knows = model.createProperty("http://xmlns.com/foaf/0.1/knows";);
+               bob = model.createResource("http://example.com/bob";);
+               model.add(model.createStatement(alice, knows, bob));
+               return model;
+       }
+
+       @Test
        @Override
        public void testJenaArq() throws Exception {
-               //fail("Not quite");
+               Dataset dataset = DatasetFactory.createMem();
+               dataset.addNamedModel(EXAMPLE_COM_GRAPH, makeModel());
+
+               Path path = Files.createTempFile("example", ".jsonld");
+               // System.out.println(path);
+               path.toFile().deleteOnExit();
+
+               try (OutputStream output = Files.newOutputStream(path)) {
+                       RDFDataMgr.write(output, dataset, Lang.JSONLD);
+               }
+               // We test JSON-LD as it involves multiple other bundles
+
+               Dataset dataset2 = 
RDFDataMgr.loadDataset(path.toUri().toString());
+               assertTrue(dataset2.containsNamedModel(EXAMPLE_COM_GRAPH));
+
+               runQuery(dataset2);
+
        }
 
+       private void runQuery(Dataset dataset) {
+               Query query = QueryFactory.create(""
+                               + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>"
+                               + "SELECT ?bob WHERE { "
+                               + "  GRAPH <http://example.com/graph> { "
+                               + "      ?alice foaf:knows ?bob . " + "  }" + 
"}");
+               try (QueryExecution qexec = QueryExecutionFactory.create(query,
+                               dataset)) {
+                       ResultSet results = qexec.execSelect();
+                       assertTrue(results.hasNext());
+                       QuerySolution r = results.next();
+                       assertEquals(bob, r.get("bob").asResource());
+               }
+       }
+
+       @Test
        @Override
-       public void testJenaIRI()throws Exception {
+       public void testJenaIRI() throws Exception {
                IRIFactory iriFactory = IRIFactory.jenaImplementation();
                IRI iri = iriFactory.create("http://example.com/";);
                assertEquals("http://example.com/";, iri.toASCIIString());
-       
-               // This should not work within OSGi
-               //assertTrue(iriFactory instanceof IRIFactoryImpl);
        }
 
+       @Test
        @Override
        public void testJenaTdb() throws Exception {
-               //assertEquals(1,1);
+               Path tdbDir = Files.createTempDirectory("jena-tdb-test");
+               Dataset dataset = TDBFactory.createDataset(tdbDir.toString());
+               
+               dataset.begin(ReadWrite.WRITE) ;
+               dataset.addNamedModel(EXAMPLE_COM_GRAPH, makeModel());
+               dataset.commit();
+               dataset.end();
                
+               dataset.begin(ReadWrite.READ);
+               runQuery(dataset);
+               dataset.end();
        }
 }

Reply via email to