public JenaDatabaseClient(DatabaseClient client, boolean periodicFlush) { this.client = client; this.graphManager = client.newGraphManager(); this.graphManager.setDefaultMimetype(RDFMimeTypes.NTRIPLES); this.sparqlQueryManager = client.newSPARQLQueryManager(); if (periodicFlush) { this.writeBuffer = new TriplesWriteBuffer(this); this.deleteBuffer = new TriplesDeleteBuffer(this); this.timer = new Timer(); timer.schedule(writeBuffer, DEFAULT_INITIAL_DELAY, DEFAULT_CACHE_MILLIS); timer.schedule(deleteBuffer, DEFAULT_INITIAL_DELAY + 250, DEFAULT_CACHE_MILLIS); } }
@Test public void testMergeAs_jsonxml_FileHandle() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.RDFJSON); File file = new File(datasource + "bug25348.json"); FileInputStream fis = new FileInputStream(file); Object graphData = convertInputStreamToString(fis); fis.close(); gmWriter.writeAs("htp://test.sem.graph/jsonMergeAs", graphData); gmWriter.setDefaultMimetype(RDFMimeTypes.TRIPLEXML); File file2 = new File(datasource + "relative5.xml"); Object graphData2 = convertFileToString(file2); gmWriter.mergeAs("htp://test.sem.graph/jsonMergeAs", graphData2); File readFile = gmWriter.readAs("htp://test.sem.graph/jsonMergeAs", File.class); String expectedContent = convertFileToString(readFile); assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://example.com/ns/person#firstName") && expectedContent.contains("Anna's Homepage")); }
@Test public void testThings_file() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.TRIPLEXML); String tripleGraphUri = "http://test.things.com/file"; File file = new File(datasource + "relative5.xml"); gmWriter.write(tripleGraphUri, new FileHandle(file)); TriplesReadHandle things = gmWriter.things(new StringHandle(), "about"); assertTrue("Things did not return expected Uri's", things.toString().contains("<about> <http://purl.org/dc/elements/1.1/title> \"Anna's Homepage\" .")); gmWriter.delete(tripleGraphUri); }
@Test public void testThingsAs() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.NTRIPLES); File file = new File(datasource + "relative1.nt"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("http://test.things.com/", filehandle); String things = gmWriter.thingsAs(String.class, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); assertTrue( "Did not return Expected graph Uri's", things.equals("<#electricVehicle2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://people.aifb.kit.edu/awa/2011/smartgrid/schema/smartgrid#ElectricVehicle> .")); }
@Test public void testThings_fileNomatch() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.TRIPLEXML); String tripleGraphUri = "http://test.things.com/file"; File file = new File(datasource + "relative5.xml"); gmWriter.write(tripleGraphUri, new FileHandle(file)); Exception exp = null; try { TriplesReadHandle things = gmWriter.things(new StringHandle(), "noMatch"); assertTrue("Things did not return expected Uri's", things == null); } catch (Exception e) { exp = e; } assertTrue(exp.toString().contains("ResourceNotFoundException")); gmWriter.delete(tripleGraphUri); }
@Test public void testThings_Differentmimetypes() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.TRIPLEXML); String tripleGraphUri = "http://test.things.com/multiple"; File file = new File(datasource + "relative5.xml"); gmWriter.write(tripleGraphUri, new FileHandle(file)); file = new File(datasource + "semantics.ttl"); gmWriter.merge(tripleGraphUri, new FileHandle(file).withMimetype(RDFMimeTypes.TURTLE)); StringHandle things = gmWriter.things(new StringHandle(), "http://dbpedia.org/resource/Hadoop"); assertTrue("Things did not return expected Uri's", things.get().contains("Apache Hadoop")); gmWriter.delete(tripleGraphUri); }
@Test public void testMergeAs_jsonxml_Trx_FileHandle() throws Exception { Transaction trx = writerClient.openTransaction(); GraphManager gmWriter = writerClient.newGraphManager(); gmWriter.setDefaultMimetype(RDFMimeTypes.RDFJSON); File file = new File(datasource + "bug25348.json"); FileInputStream fis = new FileInputStream(file); Object graphData = convertInputStreamToString(fis); fis.close(); gmWriter.writeAs("htp://test.sem.graph/jsonMergeAsTrx", graphData, trx); File file2 = new File(datasource + "relative6.json"); Object graphData2 = convertFileToString(file2); gmWriter.mergeAs("htp://test.sem.graph/jsonMergeAsTrx", graphData2, trx); File readFile = gmWriter.readAs("htp://test.sem.graph/jsonMergeAsTrx", File.class, trx); String expectedContent = convertFileToString(readFile); assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://example.com/ns/person#firstName") && expectedContent.contains("Anna's Homepage")); try { trx.commit(); trx = null; } catch (Exception e) { System.out.println(e); } finally { if (trx != null) { trx.rollback(); trx = null; } } }
@BeforeClass public static void beforeClass() { Common.connect(); gmgr = Common.client.newGraphManager(); String nTriples = triple1 + "\n" + triple2; gmgr.setDefaultMimetype(RDFMimeTypes.NTRIPLES); gmgr.write(graphUri, new StringHandle(nTriples)); smgr = Common.client.newSPARQLQueryManager(); }
@Test public void testMergeReplaceAs_Quads() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.NQUADS); File file = new File(datasource + "semantics.nq"); gmWriter.replaceGraphsAs(file); String uri = "http://en.wikipedia.org/wiki/Alexander_I_of_Serbia?oldid=492189987#absolute-line=1"; StringHandle readQuads = gmWriter.read(uri, new StringHandle()); assertTrue("Did not Replace Quads", readQuads.toString().contains("http://dbpedia.org/ontology/Monarch")); file = new File(datasource + "relative2.nq"); gmWriter.mergeGraphsAs(file); uri = "http://originalGraph"; readQuads = gmWriter.read(uri, new StringHandle()); assertTrue("Did not Replace Quads", readQuads.toString().contains("#electricVehicle2")); gmWriter.deleteGraphs(); try { StringHandle readContent = gmWriter.read(uri, new StringHandle()); assertTrue("Unexpected content read, expecting Resource not found exception", readContent.get() == null); } catch (Exception e) { assertTrue("Unexpected Exception Thrown", e.toString().contains("ResourceNotFoundException")); } }
@Test public void testMergeSameDoc_jsonxml_FileHandle() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.RDFJSON); File file = new File(datasource + "bug25348.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/trigMSameDoc", filehandle); gmWriter.merge("htp://test.sem.graph/trigMSameDoc", filehandle); FileHandle handle = gmWriter.read("htp://test.sem.graph/trigMSameDoc", new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue( "Did not insert document or inserted empty doc", expectedContent .equals("{\"http://example.com/ns/directory#m\":{\"http://example.com/ns/person#firstName\":[{\"value\":\"Michelle\", \"" + "type\":\"literal\", \"datatype\":\"http://www.w3.org/2001/XMLSchema#string\"}]}}")); }
@Test public void testThings() throws Exception { gmgr.setDefaultMimetype(RDFMimeTypes.NTRIPLES); String tripleGraphUri = "GraphsTest.testTriples"; String triplesAboutS5 = "<http://example.org/s5> <http://example.org/p1> <1> .\n" + "<http://example.org/s5> <http://example.org/p2> <2> .\n" + "<http://example.org/s5> <http://example.org/p3> <3> .\n"; String triplesAboutS6 = "<http://example.org/s6> <http://example.org/p4> <http://example.org/s5> ."; gmgr.write(tripleGraphUri, new StringHandle(triplesAboutS5)); gmgr.merge(tripleGraphUri, new StringHandle(triplesAboutS6)); // since all triples reference http://example.org/s5, all triples should be returned // in NTRIPLES format since that's the default mimetype on gmgr String things = gmgr.thingsAs(String.class, "http://example.org/s5"); assertEquals(triplesAboutS5 + triplesAboutS6, things); gmgr.delete(tripleGraphUri); } }
@Test public void testQuads() throws Exception { gmgr.setDefaultMimetype(RDFMimeTypes.NQUADS); String quadGraphUri = "GraphsTest.testQuads"; String quad1 = "<http://example.org/s1> <http://example.org/p1> <http://example.org/o1> <" + quadGraphUri + "> ."; String quad2 = "<http://example.org/s2> <http://example.org/p2> <http://example.org/o2> <" + quadGraphUri + "> ."; String quad3 = "<http://example.org/s3> <http://example.org/p2> <http://example.org/o2> <" + quadGraphUri + "> ."; String quad4 = "<http://example.org/s4> <http://example.org/p2> <http://example.org/o2> <" + quadGraphUri + "> ."; gmgr.replaceGraphs(new StringHandle(quad1)); StringHandle quadsHandle = gmgr.read(quadGraphUri, new StringHandle()); assertEquals(quad1, quadsHandle.get()); gmgr.mergeGraphs(new StringHandle(quad2)); StringHandle quads1and2 = gmgr.read(quadGraphUri, new StringHandle()); assertEquals(quad1 + "\n" + quad2, quads1and2.get()); gmgr.replaceGraphsAs(quad3); quadsHandle = gmgr.read(quadGraphUri, new StringHandle()); assertEquals(quad3, quadsHandle.get()); gmgr.mergeGraphsAs(quad4); String quads3and4 = gmgr.readAs(quadGraphUri, String.class); assertEquals(quad3 + "\n" + quad4, quads3and4); gmgr.delete(quadGraphUri); }
@Test public void testTransactions() { GraphManager graphManagerWriter = Common.client.newGraphManager(); graphManagerWriter.setDefaultMimetype(RDFMimeTypes.NTRIPLES); DatabaseClient readOnlyClient = Common.connectReadOnly(); GraphManager graphManagerReader = readOnlyClient.newGraphManager(); graphManagerReader.setDefaultMimetype(RDFMimeTypes.NTRIPLES); String t1 = "<s1> <p1> <o1> ."; String t2 = "<s2> <p2> <o2> .";
@Test public void testTriples() throws Exception { gmgr.setDefaultMimetype(RDFMimeTypes.NTRIPLES); String tripleGraphUri = "GraphsTest.testTriples"; String ntriple5 = "<http://example.org/s5> <http://example.org/p2> <http://example.org/o2> .";
@Test public void testQuadsWithTransaction() throws Exception { gmgr.setDefaultMimetype(RDFMimeTypes.NQUADS); String quadGraphUri = "GraphsTest.testQuadsWithTransaction"; String quad1 = "<http://example.org/s1> <http://example.org/p1> <http://example.org/o1> <" + quadGraphUri + "> .";
@Test public void testTransactions() { GraphManager graphManagerWriter = Common.client.newGraphManager(); graphManagerWriter.setDefaultMimetype(RDFMimeTypes.NTRIPLES); DatabaseClient readOnlyClient = Common.connectReadOnly(); SPARQLQueryManager sparqlManagerReader = readOnlyClient.newSPARQLQueryManager();