http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java ---------------------------------------------------------------------- diff --git a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java index 422e97b..7b1310c 100644 --- a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java +++ b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java @@ -41,8 +41,8 @@ import org.junit.Test; * <p> * To add to your implementation's tests, create a subclass with a name ending * in <code>Test</code> and provide {@link #createFactory()} which minimally - * must support {@link RDF#createGraph()} and - * {@link RDF#createIRI(String)}, but ideally support all operations. + * must support {@link RDF#createGraph()} and {@link RDF#createIRI(String)}, but + * ideally support all operations. * <p> * This test uses try-with-resources blocks for calls to {@link Graph#stream()} * and {@link Graph#iterate()}. @@ -129,15 +129,13 @@ public abstract class AbstractGraphTest { graph.add(bnode1, name, secretClubName); graph.add(bnode2, name, companyName); } - } + } } - @Test public void size() throws Exception { assertTrue(graph.size() > 0); - Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName, - companyName, bobNameTriple); + Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName, companyName, bobNameTriple); // Can only reliably predict size if we could create all triples assertEquals(8, graph.size()); } @@ -158,18 +156,18 @@ public abstract class AbstractGraphTest { // aborted iteration Iterable<Triple> iterate = graph.iterate(); - Iterator<Triple> it = iterate.iterator(); - + Iterator<Triple> it = iterate.iterator(); + assertTrue(it.hasNext()); it.next(); closeIterable(iterate); - // second iteration - should start from fresh and // get the same count long count = 0; Iterable<Triple> iterable = graph.iterate(); - for (@SuppressWarnings("unused") Triple t : iterable) { + for (@SuppressWarnings("unused") + Triple t : iterable) { count++; } assertEquals(graph.size(), count); @@ -178,11 +176,11 @@ public abstract class AbstractGraphTest { /** * Special triple closing for RDF4J. */ - private void closeIterable(Iterable<Triple> iterate) throws Exception { + private void closeIterable(Iterable<Triple> iterate) throws Exception { if (iterate instanceof AutoCloseable) { - ((AutoCloseable) iterate).close(); + ((AutoCloseable) iterate).close(); } - } + } @Test public void iterateFilter() throws Exception { @@ -197,10 +195,10 @@ public abstract class AbstractGraphTest { // .. can we iterate over zero hits? Iterable<Triple> iterate = graph.iterate(bob, knows, alice); - for (Triple unexpected : iterate) { - fail("Unexpected triple " + unexpected); + for (Triple unexpected : iterate) { + fail("Unexpected triple " + unexpected); } - //closeIterable(iterate); + // closeIterable(iterate); } @Test @@ -210,11 +208,10 @@ public abstract class AbstractGraphTest { assertTrue(graph.contains(alice, knows, bob)); try (Stream<? extends Triple> stream = graph.stream()) { - Optional<? extends Triple> first = stream.skip(4) - .findFirst(); - Assume.assumeTrue(first.isPresent()); - Triple existingTriple = first.get(); - assertTrue(graph.contains(existingTriple)); + Optional<? extends Triple> first = stream.skip(4).findFirst(); + Assume.assumeTrue(first.isPresent()); + Triple existingTriple = first.get(); + assertTrue(graph.contains(existingTriple)); } Triple nonExistingTriple = factory.createTriple(bob, knows, alice); @@ -254,19 +251,19 @@ public abstract class AbstractGraphTest { Triple otherTriple; try (Stream<? extends Triple> stream = graph.stream()) { - Optional<? extends Triple> anyTriple = stream.findAny(); - Assume.assumeTrue(anyTriple.isPresent()); - otherTriple = anyTriple.get(); + Optional<? extends Triple> anyTriple = stream.findAny(); + Assume.assumeTrue(anyTriple.isPresent()); + otherTriple = anyTriple.get(); } graph.remove(otherTriple); assertEquals(shrunkSize - 1, graph.size()); graph.remove(otherTriple); assertEquals(shrunkSize - 1, graph.size()); // no change - + // for some reason in rdf4j this causes duplicates! graph.add(otherTriple); - //graph.stream().forEach(System.out::println); + // graph.stream().forEach(System.out::println); // should have increased assertTrue(graph.size() >= shrunkSize); } @@ -282,19 +279,18 @@ public abstract class AbstractGraphTest { @Test public void getTriples() throws Exception { - long tripleCount; + long tripleCount; try (Stream<? extends Triple> stream = graph.stream()) { - tripleCount = stream.count(); + tripleCount = stream.count(); } assertTrue(tripleCount > 0); try (Stream<? extends Triple> stream = graph.stream()) { - assertTrue(stream.allMatch(t -> graph.contains(t))); + assertTrue(stream.allMatch(t -> graph.contains(t))); } - + // Check exact count - Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName, - companyName, bobNameTriple); + Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName, companyName, bobNameTriple); assertEquals(8, tripleCount); } @@ -302,19 +298,19 @@ public abstract class AbstractGraphTest { public void getTriplesQuery() throws Exception { try (Stream<? extends Triple> stream = graph.stream(alice, null, null)) { - long aliceCount = stream.count(); - assertTrue(aliceCount > 0); - Assume.assumeNotNull(aliceName); - assertEquals(3, aliceCount); + long aliceCount = stream.count(); + assertTrue(aliceCount > 0); + Assume.assumeNotNull(aliceName); + assertEquals(3, aliceCount); } Assume.assumeNotNull(bnode1, bnode2, bobName, companyName, secretClubName); try (Stream<? extends Triple> stream = graph.stream(null, name, null)) { - assertEquals(4, stream.count()); + assertEquals(4, stream.count()); } Assume.assumeNotNull(bnode1); try (Stream<? extends Triple> stream = graph.stream(null, member, null)) { - assertEquals(3, stream.count()); + assertEquals(3, stream.count()); } } @@ -322,32 +318,31 @@ public abstract class AbstractGraphTest { public void addBlankNodesFromMultipleGraphs() { try { - // Create two separate Graph instances + // Create two separate Graph instances Graph g1 = createGraph1(); Graph g2 = createGraph2(); // and add them to a new Graph g3 - Graph g3 = factory.createGraph(); + Graph g3 = factory.createGraph(); addAllTriples(g1, g3); addAllTriples(g2, g3); - // Let's make a map to find all those blank nodes after insertion - // (The Graph implementation is not currently required to - // keep supporting those BlankNodes with contains() - see COMMONSRDF-15) + // (The Graph implementation is not currently required to + // keep supporting those BlankNodes with contains() - see + // COMMONSRDF-15) final Map<String, BlankNodeOrIRI> whoIsWho = new ConcurrentHashMap<>(); // ConcurrentHashMap as we will try parallel forEach below, // which should not give inconsistent results (it does with a // HashMap!) - + // look up BlankNodes by name IRI name = factory.createIRI("http://xmlns.com/foaf/0.1/name"); try (Stream<? extends Triple> stream = g3.stream(null, name, null)) { - stream.parallel().forEach( t -> - whoIsWho.put( t.getObject().ntriplesString(), t.getSubject())); - } - + stream.parallel().forEach(t -> whoIsWho.put(t.getObject().ntriplesString(), t.getSubject())); + } + assertEquals(4, whoIsWho.size()); // and contains 4 unique values assertEquals(4, new HashSet<BlankNodeOrIRI>(whoIsWho.values()).size()); @@ -400,24 +395,28 @@ public abstract class AbstractGraphTest { /** * Add all triples from the source to the target. * <p> - * The triples may be copied in any order. - * No special conversion or adaptation of {@link BlankNode}s are performed. + * The triples may be copied in any order. No special conversion or + * adaptation of {@link BlankNode}s are performed. * - * @param source Source Graph to copy triples from - * @param target Target Graph where triples will be added + * @param source + * Source Graph to copy triples from + * @param target + * Target Graph where triples will be added */ private void addAllTriples(Graph source, Graph target) { // unordered() as we don't need to preserve triple order - // sequential() as we don't (currently) require target Graph to be thread-safe - - try (Stream<? extends Triple> stream = source.stream()) { - stream.unordered().sequential().forEach(t -> target.add(t)); - } + // sequential() as we don't (currently) require target Graph to be + // thread-safe + + try (Stream<? extends Triple> stream = source.stream()) { + stream.unordered().sequential().forEach(t -> target.add(t)); + } } /** - * Make a new graph with two BlankNodes - each with a different uniqueReference + * Make a new graph with two BlankNodes - each with a different + * uniqueReference */ private Graph createGraph1() { RDF factory1 = createFactory(); @@ -426,50 +425,51 @@ public abstract class AbstractGraphTest { Graph g1 = factory1.createGraph(); BlankNode b1 = createOwnBlankNode("b1", "0240eaaa-d33e-4fc0-a4f1-169d6ced3680"); g1.add(b1, name, factory1.createLiteral("Alice")); - - + BlankNode b2 = createOwnBlankNode("b2", "9de7db45-0ce7-4b0f-a1ce-c9680ffcfd9f"); g1.add(b2, name, factory1.createLiteral("Bob")); IRI hasChild = factory1.createIRI("http://example.com/hasChild"); - g1.add(b1, hasChild, b2); + g1.add(b1, hasChild, b2); return g1; } - /** + /** * Create a different implementation of BlankNode to be tested with - * graph.add(a,b,c); - * (the implementation may or may not then choose to translate such to - * its own instances) + * graph.add(a,b,c); (the implementation may or may not then choose to + * translate such to its own instances) * * @param name * @return */ - private BlankNode createOwnBlankNode(String name, String uuid) { - return new BlankNode() { - @Override - public String ntriplesString() { - return "_: " + name; - } - @Override - public String uniqueReference() { - return uuid; - } - @Override - public int hashCode() { - return uuid.hashCode(); - } - @Override - public boolean equals(Object obj) { - if (!( obj instanceof BlankNode)) { - return false; - } - BlankNode other = (BlankNode)obj; - return uuid.equals(other.uniqueReference()); - } - }; - } + private BlankNode createOwnBlankNode(String name, String uuid) { + return new BlankNode() { + @Override + public String ntriplesString() { + return "_: " + name; + } + + @Override + public String uniqueReference() { + return uuid; + } + + @Override + public int hashCode() { + return uuid.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof BlankNode)) { + return false; + } + BlankNode other = (BlankNode) obj; + return uuid.equals(other.uniqueReference()); + } + }; + } private Graph createGraph2() { RDF factory2 = createFactory(); @@ -485,7 +485,7 @@ public abstract class AbstractGraphTest { IRI hasChild = factory2.createIRI("http://example.com/hasChild"); // NOTE: Opposite direction of loadGraph1 - g2.add(b2, hasChild, b1); + g2.add(b2, hasChild, b1); return g2; } @@ -493,13 +493,14 @@ public abstract class AbstractGraphTest { * An attempt to use the Java 8 streams to look up a more complicated query. * <p> * FYI, the equivalent SPARQL version (untested): + * * <pre> - * SELECT ?orgName WHERE { - * ?org foaf:name ?orgName . - * ?alice foaf:member ?org . - * ?bob foaf:member ?org . - * ?alice foaf:knows ?bob . - * FILTER NOT EXIST { ?bob foaf:knows ?alice } + * SELECT ?orgName WHERE { + * ?org foaf:name ?orgName . + * ?alice foaf:member ?org . + * ?bob foaf:member ?org . + * ?alice foaf:knows ?bob . + * FILTER NOT EXIST { ?bob foaf:knows ?alice } * } * </pre> * @@ -509,31 +510,31 @@ public abstract class AbstractGraphTest { public void whyJavaStreamsMightNotTakeOverFromSparql() throws Exception { Assume.assumeNotNull(bnode1, bnode2, secretClubName); // Find a secret organizations - try (Stream<? extends Triple> stream = graph.stream(null, knows, null)) { - assertEquals("\"The Secret Club\"", - // Find One-way "knows" - stream.filter(t -> !graph.contains((BlankNodeOrIRI) t.getObject(), knows, t.getSubject())) - .map(knowsTriple -> { - try (Stream<? extends Triple> memberOf = graph - // and those they know, what are they - // member of? - .stream((BlankNodeOrIRI) knowsTriple.getObject(), member, null)) { - return memberOf - // keep those which first-guy is a - // member of - .filter(memberTriple -> graph.contains(knowsTriple.getSubject(), member, - // First hit is good enough - memberTriple.getObject())) - .findFirst().get().getObject(); - } - }) - // then look up the name of that org - .map(org -> { - try (Stream<? extends Triple> orgName = graph.stream((BlankNodeOrIRI) org, name, - null)) { - return orgName.findFirst().get().getObject().ntriplesString(); - } - }).findFirst().get()); - } + try (Stream<? extends Triple> stream = graph.stream(null, knows, null)) { + assertEquals("\"The Secret Club\"", + // Find One-way "knows" + stream.filter(t -> !graph.contains((BlankNodeOrIRI) t.getObject(), knows, t.getSubject())) + .map(knowsTriple -> { + try (Stream<? extends Triple> memberOf = graph + // and those they know, what are they + // member of? + .stream((BlankNodeOrIRI) knowsTriple.getObject(), member, null)) { + return memberOf + // keep those which first-guy is a + // member of + .filter(memberTriple -> graph.contains(knowsTriple.getSubject(), member, + // First hit is good enough + memberTriple.getObject())) + .findFirst().get().getObject(); + } + }) + // then look up the name of that org + .map(org -> { + try (Stream<? extends Triple> orgName = graph.stream((BlankNodeOrIRI) org, name, + null)) { + return orgName.findFirst().get().getObject().ntriplesString(); + } + }).findFirst().get()); + } } }
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTest.java ---------------------------------------------------------------------- diff --git a/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTest.java b/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTest.java index c664da8..e58ede6 100644 --- a/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTest.java +++ b/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTest.java @@ -60,9 +60,8 @@ public abstract class AbstractRDFTest { BlankNode bnode = factory.createBlankNode(); BlankNode bnode2 = factory.createBlankNode(); - assertNotEquals( - "Second blank node has not got a unique internal identifier", - bnode.uniqueReference(), bnode2.uniqueReference()); + assertNotEquals("Second blank node has not got a unique internal identifier", bnode.uniqueReference(), + bnode2.uniqueReference()); } @Test @@ -90,8 +89,7 @@ public abstract class AbstractRDFTest { // We don't know what the ntriplesString is, but it MUST be the same assertEquals(bnode1.ntriplesString(), bnode2.ntriplesString()); // and here it MUST differ - assertNotEquals(bnode1.uniqueReference(), - bnode3.uniqueReference()); + assertNotEquals(bnode1.uniqueReference(), bnode3.uniqueReference()); assertNotEquals(bnode1.ntriplesString(), bnode3.ntriplesString()); } @@ -101,31 +99,28 @@ public abstract class AbstractRDFTest { bnode1 = factory.createBlankNode(); // it MUST differ from a second factory differentFactory = createFactory().createBlankNode(); - - // NOTE: We can't make similar assumption if we provide a + + // NOTE: We can't make similar assumption if we provide a // name to createBlankNode(String) as its documentation // only says: - // + // // * BlankNodes created using this method with the same parameter, for // * different instances of RDFFactory, SHOULD NOT be equivalent. // // https://github.com/apache/incubator-commonsrdf/pull/7#issuecomment-92312779 assertNotEquals(bnode1, differentFactory); - assertNotEquals(bnode1.uniqueReference(), - differentFactory.uniqueReference()); + assertNotEquals(bnode1.uniqueReference(), differentFactory.uniqueReference()); // but we can't require: - //assertNotEquals(bnode1.ntriplesString(), differentFactory.ntriplesString()); + // assertNotEquals(bnode1.ntriplesString(), + // differentFactory.ntriplesString()); } - @Test public void testCreateGraph() { Graph graph = factory.createGraph(); assertEquals("Graph was not empty", 0, graph.size()); - graph.add(factory.createBlankNode(), - factory.createIRI("http://example.com/"), - factory.createBlankNode()); + graph.add(factory.createBlankNode(), factory.createIRI("http://example.com/"), factory.createBlankNode()); Graph graph2 = factory.createGraph(); assertNotSame(graph, graph2); @@ -147,16 +142,12 @@ public abstract class AbstractRDFTest { // and now for the international fun! IRI latin1 = factory.createIRI("http://accént.example.com/première"); - assertEquals("http://accént.example.com/première", - latin1.getIRIString()); - assertEquals("<http://accént.example.com/première>", - latin1.ntriplesString()); + assertEquals("http://accént.example.com/première", latin1.getIRIString()); + assertEquals("<http://accént.example.com/première>", latin1.ntriplesString()); IRI cyrillic = factory.createIRI("http://example.иÑпÑÑание/ÐиÑиллиÑа"); - assertEquals("http://example.иÑпÑÑание/ÐиÑиллиÑа", - cyrillic.getIRIString()); - assertEquals("<http://example.иÑпÑÑание/ÐиÑиллиÑа>", - cyrillic.ntriplesString()); + assertEquals("http://example.иÑпÑÑание/ÐиÑиллиÑа", cyrillic.getIRIString()); + assertEquals("<http://example.иÑпÑÑание/ÐиÑиллиÑа>", cyrillic.ntriplesString()); IRI deseret = factory.createIRI("http://ð.example.com/ð"); assertEquals("http://ð.example.com/ð", deseret.getIRIString()); @@ -168,24 +159,19 @@ public abstract class AbstractRDFTest { Literal example = factory.createLiteral("Example"); assertEquals("Example", example.getLexicalForm()); assertFalse(example.getLanguageTag().isPresent()); - assertEquals("http://www.w3.org/2001/XMLSchema#string", example - .getDatatype().getIRIString()); + assertEquals("http://www.w3.org/2001/XMLSchema#string", example.getDatatype().getIRIString()); // http://lists.w3.org/Archives/Public/public-rdf-comments/2014Dec/0004.html assertEquals("\"Example\"", example.ntriplesString()); } @Test public void testCreateLiteralDateTime() throws Exception { - Literal dateTime = factory - .createLiteral( - "2014-12-27T00:50:00T-0600", - factory.createIRI("http://www.w3.org/2001/XMLSchema#dateTime")); + Literal dateTime = factory.createLiteral("2014-12-27T00:50:00T-0600", + factory.createIRI("http://www.w3.org/2001/XMLSchema#dateTime")); assertEquals("2014-12-27T00:50:00T-0600", dateTime.getLexicalForm()); assertFalse(dateTime.getLanguageTag().isPresent()); - assertEquals("http://www.w3.org/2001/XMLSchema#dateTime", dateTime - .getDatatype().getIRIString()); - assertEquals( - "\"2014-12-27T00:50:00T-0600\"^^<http://www.w3.org/2001/XMLSchema#dateTime>", + assertEquals("http://www.w3.org/2001/XMLSchema#dateTime", dateTime.getDatatype().getIRIString()); + assertEquals("\"2014-12-27T00:50:00T-0600\"^^<http://www.w3.org/2001/XMLSchema#dateTime>", dateTime.ntriplesString()); } @@ -195,8 +181,7 @@ public abstract class AbstractRDFTest { assertEquals("Example", example.getLexicalForm()); assertEquals("en", example.getLanguageTag().get()); - assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", - example.getDatatype().getIRIString()); + assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", example.getDatatype().getIRIString()); assertEquals("\"Example\"@en", example.ntriplesString()); } @@ -206,19 +191,17 @@ public abstract class AbstractRDFTest { Literal vls = factory.createLiteral("Herbert Van de Sompel", "vls"); // JENA-827 assertEquals("vls", vls.getLanguageTag().get()); - assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", - vls.getDatatype().getIRIString()); + assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", vls.getDatatype().getIRIString()); assertEquals("\"Herbert Van de Sompel\"@vls", vls.ntriplesString()); } @Test public void testCreateLiteralString() throws Exception { - Literal example = factory.createLiteral("Example", factory - .createIRI("http://www.w3.org/2001/XMLSchema#string")); + Literal example = factory.createLiteral("Example", + factory.createIRI("http://www.w3.org/2001/XMLSchema#string")); assertEquals("Example", example.getLexicalForm()); assertFalse(example.getLanguageTag().isPresent()); - assertEquals("http://www.w3.org/2001/XMLSchema#string", example - .getDatatype().getIRIString()); + assertEquals("http://www.w3.org/2001/XMLSchema#string", example.getDatatype().getIRIString()); // http://lists.w3.org/Archives/Public/public-rdf-comments/2014Dec/0004.html assertEquals("\"Example\"", example.ntriplesString()); } @@ -239,10 +222,10 @@ public abstract class AbstractRDFTest { @Test public void testCreateTripleBnodeIRI() { - BlankNode subject = factory.createBlankNode("b1"); - IRI predicate = factory.createIRI("http://example.com/pred"); - IRI object = factory.createIRI("http://example.com/obj"); - Triple triple = factory.createTriple(subject, predicate, object); + BlankNode subject = factory.createBlankNode("b1"); + IRI predicate = factory.createIRI("http://example.com/pred"); + IRI object = factory.createIRI("http://example.com/obj"); + Triple triple = factory.createTriple(subject, predicate, object); // bnode equivalence should be OK as we used the same // factory and have not yet inserted Triple into a Graph @@ -253,11 +236,11 @@ public abstract class AbstractRDFTest { @Test public void testCreateTripleBnodeTriple() { - BlankNode subject = factory.createBlankNode(); - IRI predicate = factory.createIRI("http://example.com/pred"); - Literal object = factory.createLiteral("Example", "en"); - Triple triple = factory.createTriple(subject, predicate, object); - + BlankNode subject = factory.createBlankNode(); + IRI predicate = factory.createIRI("http://example.com/pred"); + Literal object = factory.createLiteral("Example", "en"); + Triple triple = factory.createTriple(subject, predicate, object); + // bnode equivalence should be OK as we used the same // factory and have not yet inserted Triple into a Graph assertEquals(subject, triple.getSubject()); @@ -279,8 +262,7 @@ public abstract class AbstractRDFTest { assertFalse(withColon.ntriplesString().contains("with:colon")); // and creating it twice gets the same ntriplesString - assertEquals(withColon.ntriplesString(), - factory.createBlankNode("with:colon").ntriplesString()); + assertEquals(withColon.ntriplesString(), factory.createBlankNode("with:colon").ntriplesString()); } @Test(expected = IllegalArgumentException.class) @@ -316,11 +298,7 @@ public abstract class AbstractRDFTest { @Test public void hashCodeLiteral() throws Exception { Literal literal = factory.createLiteral("Hello"); - assertEquals(Objects.hash( - literal.getLexicalForm(), - literal.getDatatype(), - literal.getLanguageTag() - ), + assertEquals(Objects.hash(literal.getLexicalForm(), literal.getDatatype(), literal.getLanguageTag()), literal.hashCode()); } @@ -328,7 +306,6 @@ public abstract class AbstractRDFTest { public void hashCodeTriple() throws Exception { IRI iri = factory.createIRI("http://example.com/"); Triple triple = factory.createTriple(iri, iri, iri); - assertEquals(Objects.hash(iri, iri, iri), - triple.hashCode()); + assertEquals(Objects.hash(iri, iri, iri), triple.hashCode()); } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/api/src/test/java/org/apache/commons/rdf/api/RDFSyntaxTest.java ---------------------------------------------------------------------- diff --git a/api/src/test/java/org/apache/commons/rdf/api/RDFSyntaxTest.java b/api/src/test/java/org/apache/commons/rdf/api/RDFSyntaxTest.java index 5781822..a1cb62e 100644 --- a/api/src/test/java/org/apache/commons/rdf/api/RDFSyntaxTest.java +++ b/api/src/test/java/org/apache/commons/rdf/api/RDFSyntaxTest.java @@ -25,91 +25,91 @@ import org.junit.Test; public class RDFSyntaxTest { - @Test - public void byFileExtension() throws Exception { - assertEquals(RDFSyntax.JSONLD, RDFSyntax.byFileExtension(".jsonld").get()); - assertEquals(RDFSyntax.NQUADS, RDFSyntax.byFileExtension(".nq").get()); - assertEquals(RDFSyntax.NTRIPLES, RDFSyntax.byFileExtension(".nt").get()); - assertEquals(RDFSyntax.RDFA_HTML, RDFSyntax.byFileExtension(".html").get()); - assertEquals(RDFSyntax.RDFA_XHTML, RDFSyntax.byFileExtension(".xhtml").get()); - assertEquals(RDFSyntax.RDFXML, RDFSyntax.byFileExtension(".rdf").get()); - assertEquals(RDFSyntax.TRIG, RDFSyntax.byFileExtension(".trig").get()); - assertEquals(RDFSyntax.TURTLE, RDFSyntax.byFileExtension(".ttl").get()); - } - - @Test - public void byFileExtensionFailsWithoutDot() throws Exception { - assertEquals(Optional.empty(), RDFSyntax.byFileExtension("rdf")); - } - - @Test - public void byFileExtensionLowerCase() throws Exception { - assertEquals(RDFSyntax.TURTLE, RDFSyntax.byFileExtension(".TtL").get()); - } - - @Test - public void byFileExtensionUnknown() throws Exception { - assertEquals(Optional.empty(), RDFSyntax.byFileExtension(".tar")); - } - - @Test - public void byMediaType() throws Exception { - assertEquals(RDFSyntax.JSONLD, RDFSyntax.byMediaType("application/ld+json").get()); - assertEquals(RDFSyntax.NQUADS, RDFSyntax.byMediaType("application/n-quads").get()); - assertEquals(RDFSyntax.NTRIPLES, RDFSyntax.byMediaType("application/n-triples").get()); - assertEquals(RDFSyntax.RDFA_HTML, RDFSyntax.byMediaType("text/html").get()); - assertEquals(RDFSyntax.RDFA_XHTML, RDFSyntax.byMediaType("application/xhtml+xml").get()); - assertEquals(RDFSyntax.RDFXML, RDFSyntax.byMediaType("application/rdf+xml").get()); - assertEquals(RDFSyntax.TRIG, RDFSyntax.byMediaType("application/trig").get()); - assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle").get()); - } - - @Test - public void byMediaTypeContentType() throws Exception { - assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle; charset=\"UTF-8\"").get()); - assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle ; charset=\"UTF-8\"").get()); - assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle, text/plain").get()); - assertEquals(Optional.empty(), RDFSyntax.byMediaType(" text/turtle")); - } - - @Test - public void byMediaTypeLowerCase() throws Exception { - assertEquals(RDFSyntax.JSONLD, RDFSyntax.byMediaType("APPLICATION/ld+JSON").get()); - } - - @Test - public void byMediaTypeUnknown() throws Exception { - assertEquals(Optional.empty(), RDFSyntax.byMediaType("application/octet-stream")); - } - - @Test - public void fileExtension() throws Exception { - assertEquals(".jsonld", RDFSyntax.JSONLD.fileExtension); - assertEquals(".nq", RDFSyntax.NQUADS.fileExtension); - assertEquals(".nt", RDFSyntax.NTRIPLES.fileExtension); - assertEquals(".html", RDFSyntax.RDFA_HTML.fileExtension); - assertEquals(".xhtml", RDFSyntax.RDFA_XHTML.fileExtension); - assertEquals(".rdf", RDFSyntax.RDFXML.fileExtension); - assertEquals(".trig", RDFSyntax.TRIG.fileExtension); - assertEquals(".ttl", RDFSyntax.TURTLE.fileExtension); - } - - @Test - public void mediaType() throws Exception { - assertEquals("application/ld+json", RDFSyntax.JSONLD.mediaType); - assertEquals("application/n-quads", RDFSyntax.NQUADS.mediaType); - assertEquals("application/n-triples", RDFSyntax.NTRIPLES.mediaType); - assertEquals("text/html", RDFSyntax.RDFA_HTML.mediaType); - assertEquals("application/xhtml+xml", RDFSyntax.RDFA_XHTML.mediaType); - assertEquals("application/rdf+xml", RDFSyntax.RDFXML.mediaType); - assertEquals("application/trig", RDFSyntax.TRIG.mediaType); - assertEquals("text/turtle", RDFSyntax.TURTLE.mediaType); - } - - @Test - public void name() throws Exception { - assertEquals("JSON-LD 1.0", RDFSyntax.JSONLD.toString()); - assertEquals("RDF 1.1 Turtle", RDFSyntax.TURTLE.toString()); - } + @Test + public void byFileExtension() throws Exception { + assertEquals(RDFSyntax.JSONLD, RDFSyntax.byFileExtension(".jsonld").get()); + assertEquals(RDFSyntax.NQUADS, RDFSyntax.byFileExtension(".nq").get()); + assertEquals(RDFSyntax.NTRIPLES, RDFSyntax.byFileExtension(".nt").get()); + assertEquals(RDFSyntax.RDFA_HTML, RDFSyntax.byFileExtension(".html").get()); + assertEquals(RDFSyntax.RDFA_XHTML, RDFSyntax.byFileExtension(".xhtml").get()); + assertEquals(RDFSyntax.RDFXML, RDFSyntax.byFileExtension(".rdf").get()); + assertEquals(RDFSyntax.TRIG, RDFSyntax.byFileExtension(".trig").get()); + assertEquals(RDFSyntax.TURTLE, RDFSyntax.byFileExtension(".ttl").get()); + } + + @Test + public void byFileExtensionFailsWithoutDot() throws Exception { + assertEquals(Optional.empty(), RDFSyntax.byFileExtension("rdf")); + } + + @Test + public void byFileExtensionLowerCase() throws Exception { + assertEquals(RDFSyntax.TURTLE, RDFSyntax.byFileExtension(".TtL").get()); + } + + @Test + public void byFileExtensionUnknown() throws Exception { + assertEquals(Optional.empty(), RDFSyntax.byFileExtension(".tar")); + } + + @Test + public void byMediaType() throws Exception { + assertEquals(RDFSyntax.JSONLD, RDFSyntax.byMediaType("application/ld+json").get()); + assertEquals(RDFSyntax.NQUADS, RDFSyntax.byMediaType("application/n-quads").get()); + assertEquals(RDFSyntax.NTRIPLES, RDFSyntax.byMediaType("application/n-triples").get()); + assertEquals(RDFSyntax.RDFA_HTML, RDFSyntax.byMediaType("text/html").get()); + assertEquals(RDFSyntax.RDFA_XHTML, RDFSyntax.byMediaType("application/xhtml+xml").get()); + assertEquals(RDFSyntax.RDFXML, RDFSyntax.byMediaType("application/rdf+xml").get()); + assertEquals(RDFSyntax.TRIG, RDFSyntax.byMediaType("application/trig").get()); + assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle").get()); + } + + @Test + public void byMediaTypeContentType() throws Exception { + assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle; charset=\"UTF-8\"").get()); + assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle ; charset=\"UTF-8\"").get()); + assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle, text/plain").get()); + assertEquals(Optional.empty(), RDFSyntax.byMediaType(" text/turtle")); + } + + @Test + public void byMediaTypeLowerCase() throws Exception { + assertEquals(RDFSyntax.JSONLD, RDFSyntax.byMediaType("APPLICATION/ld+JSON").get()); + } + + @Test + public void byMediaTypeUnknown() throws Exception { + assertEquals(Optional.empty(), RDFSyntax.byMediaType("application/octet-stream")); + } + + @Test + public void fileExtension() throws Exception { + assertEquals(".jsonld", RDFSyntax.JSONLD.fileExtension); + assertEquals(".nq", RDFSyntax.NQUADS.fileExtension); + assertEquals(".nt", RDFSyntax.NTRIPLES.fileExtension); + assertEquals(".html", RDFSyntax.RDFA_HTML.fileExtension); + assertEquals(".xhtml", RDFSyntax.RDFA_XHTML.fileExtension); + assertEquals(".rdf", RDFSyntax.RDFXML.fileExtension); + assertEquals(".trig", RDFSyntax.TRIG.fileExtension); + assertEquals(".ttl", RDFSyntax.TURTLE.fileExtension); + } + + @Test + public void mediaType() throws Exception { + assertEquals("application/ld+json", RDFSyntax.JSONLD.mediaType); + assertEquals("application/n-quads", RDFSyntax.NQUADS.mediaType); + assertEquals("application/n-triples", RDFSyntax.NTRIPLES.mediaType); + assertEquals("text/html", RDFSyntax.RDFA_HTML.mediaType); + assertEquals("application/xhtml+xml", RDFSyntax.RDFA_XHTML.mediaType); + assertEquals("application/rdf+xml", RDFSyntax.RDFXML.mediaType); + assertEquals("application/trig", RDFSyntax.TRIG.mediaType); + assertEquals("text/turtle", RDFSyntax.TURTLE.mediaType); + } + + @Test + public void name() throws Exception { + assertEquals("JSON-LD 1.0", RDFSyntax.JSONLD.toString()); + assertEquals("RDF 1.1 Turtle", RDFSyntax.TURTLE.toString()); + } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/examples/src/example/UserGuideTest.java ---------------------------------------------------------------------- diff --git a/examples/src/example/UserGuideTest.java b/examples/src/example/UserGuideTest.java index fe884dd..f0ec618 100644 --- a/examples/src/example/UserGuideTest.java +++ b/examples/src/example/UserGuideTest.java @@ -42,225 +42,218 @@ import org.junit.Test; public class UserGuideTest { - private RDF factory; + private RDF factory; - @Before - public void factory() { - factory = new SimpleRDF(); - } + @Before + public void factory() { + factory = new SimpleRDF(); + } + + @Test + public void creating() throws Exception { + BlankNode aliceBlankNode = factory.createBlankNode(); + IRI nameIri = factory.createIRI("http://example.com/name"); + Literal aliceLiteral = factory.createLiteral("Alice"); + Triple triple = factory.createTriple(aliceBlankNode, nameIri, aliceLiteral); + + System.out.println(aliceBlankNode.ntriplesString()); + System.out.println(nameIri.ntriplesString()); + System.out.println(aliceLiteral.ntriplesString()); + + } + + @Test + public void ntriples() throws Exception { + IRI iri = factory.createIRI("http://example.com/alice"); + System.out.println(iri.getIRIString()); + + IRI iri2 = factory.createIRI("http://example.com/alice"); + System.out.println(iri.equals(iri2)); + + IRI iri3 = factory.createIRI("http://example.com/alice/./"); + System.out.println(iri.equals(iri3)); + + System.out.println(iri.equals("http://example.com/alice")); + System.out.println(iri.equals(factory.createLiteral("http://example.com/alice"))); + + } + + @Test + public void blanknode() throws Exception { + BlankNode bnode = factory.createBlankNode(); + System.out.println(bnode.equals(bnode)); + System.out.println(bnode.equals(factory.createBlankNode())); + + BlankNode b1 = factory.createBlankNode("b1"); + + System.out.println(b1.ntriplesString()); + + System.out.println(b1.equals(factory.createBlankNode("b1"))); + System.out.println(b1.equals(new SimpleRDF().createBlankNode("b1"))); - @Test - public void creating() throws Exception { - BlankNode aliceBlankNode = factory.createBlankNode(); - IRI nameIri = factory.createIRI("http://example.com/name"); - Literal aliceLiteral = factory.createLiteral("Alice"); - Triple triple = factory.createTriple(aliceBlankNode, nameIri, aliceLiteral); + System.out.println(bnode.uniqueReference()); + } + + @Test + public void literal() throws Exception { + Literal literal = factory.createLiteral("Hello world!"); + System.out.println(literal.ntriplesString()); - System.out.println(aliceBlankNode.ntriplesString()); - System.out.println(nameIri.ntriplesString()); - System.out.println(aliceLiteral.ntriplesString()); + String lexical = literal.getLexicalForm(); + System.out.println(lexical); - } + IRI datatype = literal.getDatatype(); + System.out.println(datatype.ntriplesString()); - @Test - public void ntriples() throws Exception { - IRI iri = factory.createIRI("http://example.com/alice"); - System.out.println(iri.getIRIString()); + IRI xsdDouble = factory.createIRI("http://www.w3.org/2001/XMLSchema#double"); + Literal literalDouble = factory.createLiteral("13.37", xsdDouble); + System.out.println(literalDouble.ntriplesString()); - IRI iri2 = factory.createIRI("http://example.com/alice"); - System.out.println(iri.equals(iri2)); - - IRI iri3 = factory.createIRI("http://example.com/alice/./"); - System.out.println(iri.equals(iri3)); - - System.out.println(iri.equals("http://example.com/alice")); - System.out.println(iri.equals(factory.createLiteral("http://example.com/alice"))); - - } - - @Test - public void blanknode() throws Exception { - BlankNode bnode = factory.createBlankNode(); - System.out.println(bnode.equals(bnode)); - System.out.println(bnode.equals(factory.createBlankNode())); - - BlankNode b1 = factory.createBlankNode("b1"); - - System.out.println(b1.ntriplesString()); - - System.out.println(b1.equals(factory.createBlankNode("b1"))); - System.out.println(b1.equals(new SimpleRDF().createBlankNode("b1"))); - - System.out.println(bnode.uniqueReference()); - } - - @Test - public void literal() throws Exception { - Literal literal = factory.createLiteral("Hello world!"); - System.out.println(literal.ntriplesString()); - - String lexical = literal.getLexicalForm(); - System.out.println(lexical); - - IRI datatype = literal.getDatatype(); - System.out.println(datatype.ntriplesString()); - - IRI xsdDouble = factory.createIRI("http://www.w3.org/2001/XMLSchema#double"); - Literal literalDouble = factory.createLiteral("13.37", xsdDouble); - System.out.println(literalDouble.ntriplesString()); - - Literal literalDouble2 = factory.createLiteral("13.37", Types.XSD_DOUBLE); - - System.out.println(Types.XSD_STRING.equals(literal.getDatatype())); - - Literal inSpanish = factory.createLiteral("¡Hola, Mundo!", "es"); - System.out.println(inSpanish.ntriplesString()); - System.out.println(inSpanish.getLexicalForm()); - System.out.println(inSpanish.getDatatype().ntriplesString()); - - Optional<String> tag = inSpanish.getLanguageTag(); - if (tag.isPresent()) { - System.out.println(tag.get()); - } - - System.out.println(literal.getLanguageTag().isPresent()); - System.out.println(literalDouble.getLanguageTag().isPresent()); - } - - @Test - public void triple() throws Exception { - BlankNodeOrIRI subject = factory.createBlankNode(); - IRI predicate = factory.createIRI("http://example.com/says"); - RDFTerm object = factory.createLiteral("Hello"); - Triple triple = factory.createTriple(subject, predicate, object); - - BlankNodeOrIRI subj = triple.getSubject(); - System.out.println(subj.ntriplesString()); - - IRI pred = triple.getPredicate(); - System.out.println(pred.getIRIString()); - - RDFTerm obj = triple.getObject(); - System.out.println(obj.ntriplesString()); - - if (subj instanceof IRI) { - String s = ((IRI) subj).getIRIString(); - System.out.println(s); - } - // .. - if (obj instanceof Literal) { - IRI type = ((Literal) obj).getDatatype(); - System.out.println(type); - } + Literal literalDouble2 = factory.createLiteral("13.37", Types.XSD_DOUBLE); - // Equal triples must have same s,p,o - System.out.println(triple.equals(factory.createTriple(subj, pred, obj))); - } + System.out.println(Types.XSD_STRING.equals(literal.getDatatype())); - @Test - public void quad() throws Exception { - BlankNodeOrIRI graph = factory.createIRI("http://example.com/graph"); - BlankNodeOrIRI subject = factory.createBlankNode(); - IRI predicate = factory.createIRI("http://example.com/says"); - RDFTerm object = factory.createLiteral("Hello"); - Quad quad = factory.createQuad(graph, subject, predicate, object); + Literal inSpanish = factory.createLiteral("¡Hola, Mundo!", "es"); + System.out.println(inSpanish.ntriplesString()); + System.out.println(inSpanish.getLexicalForm()); + System.out.println(inSpanish.getDatatype().ntriplesString()); - Optional<BlankNodeOrIRI> g = quad.getGraphName(); - if (g.isPresent()) { - System.out.println(g.get().ntriplesString()); - } - - BlankNodeOrIRI subj = quad.getSubject(); - System.out.println(subj.ntriplesString()); - - // null means default graph - Quad otherQuad = factory.createQuad(null, subject, predicate, object); - // Equal quads must have same g,s,p,o - System.out.println(quad.equals(otherQuad)); - - // all quads can be viewed as triples - "stripping" the graph - Triple asTriple = quad.asTriple(); - Triple otherAsTriple = quad.asTriple(); - System.out.println(asTriple.equals(otherAsTriple)); - - // NOTE: Quad does NOT extend Triple, however both Triple and Quad - // extend TripleLike - - TripleLike a = quad; - TripleLike b = quad.asTriple(); - // Unlike Triple and Quad, TripleLike does not mandate any .equals(), - // it just provides common access to getSubject(), getPredicate(), getObject() - - - // TripleLike supports generalized RDF - therefore all s/p/o are of type RDFTerm - RDFTerm s = a.getSubject(); - } - - - @Test - public void graph() throws Exception { - IRI nameIri = factory.createIRI("http://example.com/name"); - BlankNode aliceBlankNode = factory.createBlankNode(); - Literal aliceLiteral = factory.createLiteral("Alice"); - Triple triple = factory.createTriple(aliceBlankNode, nameIri, aliceLiteral); - - Graph graph = factory.createGraph(); - - graph.add(triple); - - IRI bob = factory.createIRI("http://example.com/bob"); - Literal bobName = factory.createLiteral("Bob"); - graph.add(bob, nameIri, bobName); - - - System.out.println(graph.contains(triple)); - - - System.out.println(graph.contains(null, nameIri, bobName)); - - System.out.println(graph.size()); - - for (Triple t : graph.iterate()) { - System.out.println(t.getObject()); - } - - - for (Triple t : graph.iterate(null, null, bobName)) { - System.out.println(t.getPredicate()); - } - - try (Stream<? extends Triple> triples = graph.stream()) { - Stream<RDFTerm> subjects = triples.map(t -> t.getObject()); - String s = subjects.map(RDFTerm::ntriplesString).collect(Collectors.joining(" ")); - System.out.println(s); - } - - try (Stream<? extends Triple> named = graph.stream(null, nameIri,null)) { - Stream<? extends Triple> namedB = named.filter( - t -> t.getObject().ntriplesString().contains("B")); - System.out.println(namedB.map(t -> t.getSubject()).findAny().get()); - } - - graph.remove(triple); - System.out.println(graph.contains(triple)); - - graph.remove(null, nameIri, null); - - graph.clear(); - System.out.println(graph.contains(null, null, null)); - - - } + Optional<String> tag = inSpanish.getLanguageTag(); + if (tag.isPresent()) { + System.out.println(tag.get()); + } + System.out.println(literal.getLanguageTag().isPresent()); + System.out.println(literalDouble.getLanguageTag().isPresent()); + } + + @Test + public void triple() throws Exception { + BlankNodeOrIRI subject = factory.createBlankNode(); + IRI predicate = factory.createIRI("http://example.com/says"); + RDFTerm object = factory.createLiteral("Hello"); + Triple triple = factory.createTriple(subject, predicate, object); + + BlankNodeOrIRI subj = triple.getSubject(); + System.out.println(subj.ntriplesString()); + + IRI pred = triple.getPredicate(); + System.out.println(pred.getIRIString()); + + RDFTerm obj = triple.getObject(); + System.out.println(obj.ntriplesString()); + + if (subj instanceof IRI) { + String s = ((IRI) subj).getIRIString(); + System.out.println(s); + } + // .. + if (obj instanceof Literal) { + IRI type = ((Literal) obj).getDatatype(); + System.out.println(type); + } + + // Equal triples must have same s,p,o + System.out.println(triple.equals(factory.createTriple(subj, pred, obj))); + } + + @Test + public void quad() throws Exception { + BlankNodeOrIRI graph = factory.createIRI("http://example.com/graph"); + BlankNodeOrIRI subject = factory.createBlankNode(); + IRI predicate = factory.createIRI("http://example.com/says"); + RDFTerm object = factory.createLiteral("Hello"); + Quad quad = factory.createQuad(graph, subject, predicate, object); + + Optional<BlankNodeOrIRI> g = quad.getGraphName(); + if (g.isPresent()) { + System.out.println(g.get().ntriplesString()); + } + + BlankNodeOrIRI subj = quad.getSubject(); + System.out.println(subj.ntriplesString()); + + // null means default graph + Quad otherQuad = factory.createQuad(null, subject, predicate, object); + // Equal quads must have same g,s,p,o + System.out.println(quad.equals(otherQuad)); + + // all quads can be viewed as triples - "stripping" the graph + Triple asTriple = quad.asTriple(); + Triple otherAsTriple = quad.asTriple(); + System.out.println(asTriple.equals(otherAsTriple)); + + // NOTE: Quad does NOT extend Triple, however both Triple and Quad + // extend TripleLike + + TripleLike a = quad; + TripleLike b = quad.asTriple(); + // Unlike Triple and Quad, TripleLike does not mandate any .equals(), + // it just provides common access to getSubject(), getPredicate(), + // getObject() + + // TripleLike supports generalized RDF - therefore all s/p/o are of type + // RDFTerm + RDFTerm s = a.getSubject(); + } + + @Test + public void graph() throws Exception { + IRI nameIri = factory.createIRI("http://example.com/name"); + BlankNode aliceBlankNode = factory.createBlankNode(); + Literal aliceLiteral = factory.createLiteral("Alice"); + Triple triple = factory.createTriple(aliceBlankNode, nameIri, aliceLiteral); + + Graph graph = factory.createGraph(); + + graph.add(triple); + + IRI bob = factory.createIRI("http://example.com/bob"); + Literal bobName = factory.createLiteral("Bob"); + graph.add(bob, nameIri, bobName); + + System.out.println(graph.contains(triple)); + + System.out.println(graph.contains(null, nameIri, bobName)); + + System.out.println(graph.size()); + + for (Triple t : graph.iterate()) { + System.out.println(t.getObject()); + } + + for (Triple t : graph.iterate(null, null, bobName)) { + System.out.println(t.getPredicate()); + } + + try (Stream<? extends Triple> triples = graph.stream()) { + Stream<RDFTerm> subjects = triples.map(t -> t.getObject()); + String s = subjects.map(RDFTerm::ntriplesString).collect(Collectors.joining(" ")); + System.out.println(s); + } + + try (Stream<? extends Triple> named = graph.stream(null, nameIri, null)) { + Stream<? extends Triple> namedB = named.filter(t -> t.getObject().ntriplesString().contains("B")); + System.out.println(namedB.map(t -> t.getSubject()).findAny().get()); + } + + graph.remove(triple); + System.out.println(graph.contains(triple)); + + graph.remove(null, nameIri, null); + + graph.clear(); + System.out.println(graph.contains(null, null, null)); + + } public static String tripleAsString(Triple t) { - return t.getSubject().ntriplesString() + " " - + t.getPredicate().ntriplesString() + " " + - t.getObject().ntriplesString() + " ."; + return t.getSubject().ntriplesString() + " " + t.getPredicate().ntriplesString() + " " + + t.getObject().ntriplesString() + " ."; } - public static void writeGraph(Graph graph, Path graphFile) throws Exception { + public static void writeGraph(Graph graph, Path graphFile) throws Exception { Stream<CharSequence> stream = graph.stream().map(UserGuideTest::tripleAsString); Files.write(graphFile, stream::iterator, StandardCharsets.UTF_8); - } + } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/integration-tests/src/test/java/org/apache/commons/rdf/integrationtests/AllToAllTest.java ---------------------------------------------------------------------- diff --git a/integration-tests/src/test/java/org/apache/commons/rdf/integrationtests/AllToAllTest.java b/integration-tests/src/test/java/org/apache/commons/rdf/integrationtests/AllToAllTest.java index 5a4cdcf..b6dce6f 100644 --- a/integration-tests/src/test/java/org/apache/commons/rdf/integrationtests/AllToAllTest.java +++ b/integration-tests/src/test/java/org/apache/commons/rdf/integrationtests/AllToAllTest.java @@ -44,133 +44,128 @@ import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class AllToAllTest { - private RDF nodeFactory; - private RDF graphFactory; - - - public AllToAllTest( - Class<? extends RDF> from, - Class<? extends RDF> to) throws InstantiationException, IllegalAccessException { - this.nodeFactory = from.newInstance(); - this.graphFactory = to.newInstance(); - } - - @SuppressWarnings("rawtypes") - @Parameters(name = "{index}: {0}->{1}") - public static Collection<Object[]> data() { - List<Class> factories = Arrays.asList( - SimpleRDF.class, - JenaRDF.class, - RDF4J.class, - JsonLdRDF.class); - Collection<Object[]> allToAll = new ArrayList<>(); - for (Class from : factories) { - for (Class to : factories) { - // NOTE: we deliberately include self-to-self here - // to test two instances of the same implementation - allToAll.add(new Object[]{from, to}); - } - } - return allToAll; - } - - /** - * This test creates a {@link Graph} with the first - * {@link RDFTermFactory}, then inserts/queries with - * triples using {@link RDFTerm}s created with the second factory. - * - * @throws Exception Just in case.. - */ - @Test - public void addTermsFromOtherFactory() throws Exception { - Graph g = graphFactory.createGraph(); - BlankNode s = nodeFactory.createBlankNode(); - IRI p = nodeFactory.createIRI("http://example.com/p"); - Literal o = nodeFactory.createLiteral("Hello"); - - g.add(s, p, o); - - // blankNode should still work with g.contains() - assertTrue(g.contains(s, p, o)); - Triple t1 = g.stream().findAny().get(); - - // Can't make assumptions about BlankNode equality - it might - // have been mapped to a different BlankNode.uniqueReference() - // assertEquals(s, t.getSubject()); - - assertEquals(p, t1.getPredicate()); - assertEquals(o, t1.getObject()); - - IRI s2 = nodeFactory.createIRI("http://example.com/s2"); - g.add(s2, p, s); - assertTrue(g.contains(s2, p, s)); - - // This should be mapped to the same BlankNode - // (even if it has a different identifier), e.g. - // we should be able to do: - - Triple t2 = g.stream(s2, p, null).findAny().get(); - - BlankNode bnode = (BlankNode) t2.getObject(); - // And that (possibly adapted) BlankNode object should - // match the subject of t1 statement - assertEquals(bnode, t1.getSubject()); - // And can be used as a key: - Triple t3 = g.stream(bnode, p, null).findAny().get(); - assertEquals(t1, t3); - } - - - /** - * This is a variation of {@link #addTermsFromOtherFactory()}, but here - * {@link Triple} is created in the "foreign" nodeFactory before adding to - * the graph. - * - * @throws Exception - * Just in case.. - */ - @Test - public void addTriplesFromOtherFactory() throws Exception { - Graph g = graphFactory.createGraph(); - BlankNode s = nodeFactory.createBlankNode(); - IRI p = nodeFactory.createIRI("http://example.com/p"); - Literal o = nodeFactory.createLiteral("Hello"); - - Triple srcT1 = nodeFactory.createTriple(s, p, o); - // This should work even with BlankNode as they are from the same factory - assertEquals(s, srcT1.getSubject()); - assertEquals(p, srcT1.getPredicate()); - assertEquals(o, srcT1.getObject()); - g.add(srcT1); - - // what about the blankNode within? - assertTrue(g.contains(srcT1)); - Triple t1 = g.stream().findAny().get(); - - // Can't make assumptions about BlankNode equality - it might - // have been mapped to a different BlankNode.uniqueReference() - //assertEquals(srcT1, t1); - //assertEquals(s, t1.getSubject()); - assertEquals(p, t1.getPredicate()); - assertEquals(o, t1.getObject()); - - IRI s2 = nodeFactory.createIRI("http://example.com/s2"); - Triple srcT2 = nodeFactory.createTriple(s2, p, s); - g.add(srcT2); - assertTrue(g.contains(srcT2)); - - // This should be mapped to the same BlankNode - // (even if it has a different identifier), e.g. - // we should be able to do: - - Triple t2 = g.stream(s2, p, null).findAny().get(); - - BlankNode bnode = (BlankNode) t2.getObject(); - // And that (possibly adapted) BlankNode object should - // match the subject of t1 statement - assertEquals(bnode, t1.getSubject()); - // And can be used as a key: - Triple t3 = g.stream(bnode, p, null).findAny().get(); - assertEquals(t1, t3); - } + private RDF nodeFactory; + private RDF graphFactory; + + public AllToAllTest(Class<? extends RDF> from, Class<? extends RDF> to) + throws InstantiationException, IllegalAccessException { + this.nodeFactory = from.newInstance(); + this.graphFactory = to.newInstance(); + } + + @SuppressWarnings("rawtypes") + @Parameters(name = "{index}: {0}->{1}") + public static Collection<Object[]> data() { + List<Class> factories = Arrays.asList(SimpleRDF.class, JenaRDF.class, RDF4J.class, JsonLdRDF.class); + Collection<Object[]> allToAll = new ArrayList<>(); + for (Class from : factories) { + for (Class to : factories) { + // NOTE: we deliberately include self-to-self here + // to test two instances of the same implementation + allToAll.add(new Object[] { from, to }); + } + } + return allToAll; + } + + /** + * This test creates a {@link Graph} with the first {@link RDFTermFactory}, + * then inserts/queries with triples using {@link RDFTerm}s created with the + * second factory. + * + * @throws Exception + * Just in case.. + */ + @Test + public void addTermsFromOtherFactory() throws Exception { + Graph g = graphFactory.createGraph(); + BlankNode s = nodeFactory.createBlankNode(); + IRI p = nodeFactory.createIRI("http://example.com/p"); + Literal o = nodeFactory.createLiteral("Hello"); + + g.add(s, p, o); + + // blankNode should still work with g.contains() + assertTrue(g.contains(s, p, o)); + Triple t1 = g.stream().findAny().get(); + + // Can't make assumptions about BlankNode equality - it might + // have been mapped to a different BlankNode.uniqueReference() + // assertEquals(s, t.getSubject()); + + assertEquals(p, t1.getPredicate()); + assertEquals(o, t1.getObject()); + + IRI s2 = nodeFactory.createIRI("http://example.com/s2"); + g.add(s2, p, s); + assertTrue(g.contains(s2, p, s)); + + // This should be mapped to the same BlankNode + // (even if it has a different identifier), e.g. + // we should be able to do: + + Triple t2 = g.stream(s2, p, null).findAny().get(); + + BlankNode bnode = (BlankNode) t2.getObject(); + // And that (possibly adapted) BlankNode object should + // match the subject of t1 statement + assertEquals(bnode, t1.getSubject()); + // And can be used as a key: + Triple t3 = g.stream(bnode, p, null).findAny().get(); + assertEquals(t1, t3); + } + + /** + * This is a variation of {@link #addTermsFromOtherFactory()}, but here + * {@link Triple} is created in the "foreign" nodeFactory before adding to + * the graph. + * + * @throws Exception + * Just in case.. + */ + @Test + public void addTriplesFromOtherFactory() throws Exception { + Graph g = graphFactory.createGraph(); + BlankNode s = nodeFactory.createBlankNode(); + IRI p = nodeFactory.createIRI("http://example.com/p"); + Literal o = nodeFactory.createLiteral("Hello"); + + Triple srcT1 = nodeFactory.createTriple(s, p, o); + // This should work even with BlankNode as they are from the same + // factory + assertEquals(s, srcT1.getSubject()); + assertEquals(p, srcT1.getPredicate()); + assertEquals(o, srcT1.getObject()); + g.add(srcT1); + + // what about the blankNode within? + assertTrue(g.contains(srcT1)); + Triple t1 = g.stream().findAny().get(); + + // Can't make assumptions about BlankNode equality - it might + // have been mapped to a different BlankNode.uniqueReference() + // assertEquals(srcT1, t1); + // assertEquals(s, t1.getSubject()); + assertEquals(p, t1.getPredicate()); + assertEquals(o, t1.getObject()); + + IRI s2 = nodeFactory.createIRI("http://example.com/s2"); + Triple srcT2 = nodeFactory.createTriple(s2, p, s); + g.add(srcT2); + assertTrue(g.contains(srcT2)); + + // This should be mapped to the same BlankNode + // (even if it has a different identifier), e.g. + // we should be able to do: + + Triple t2 = g.stream(s2, p, null).findAny().get(); + + BlankNode bnode = (BlankNode) t2.getObject(); + // And that (possibly adapted) BlankNode object should + // match the subject of t1 statement + assertEquals(bnode, t1.getSubject()); + // And can be used as a key: + Triple t3 = g.stream(bnode, p, null).findAny().get(); + assertEquals(t1, t3); + } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/jena/src/main/java/org/apache/commons/rdf/jena/ConversionException.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/ConversionException.java b/jena/src/main/java/org/apache/commons/rdf/jena/ConversionException.java index 29b9060..20bc44c 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/ConversionException.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/ConversionException.java @@ -28,21 +28,21 @@ package org.apache.commons.rdf.jena; */ public class ConversionException extends RuntimeException { - private static final long serialVersionUID = -898179977312382568L; + private static final long serialVersionUID = -898179977312382568L; - public ConversionException() { - super(); - } + public ConversionException() { + super(); + } - public ConversionException(String message) { - super(message); - } + public ConversionException(String message) { + super(message); + } - public ConversionException(Throwable cause) { - super(cause); - } + public ConversionException(Throwable cause) { + super(cause); + } - public ConversionException(String message, Throwable cause) { - super(message, cause); - } + public ConversionException(String message, Throwable cause) { + super(message, cause); + } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/jena/src/main/java/org/apache/commons/rdf/jena/JenaDataset.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaDataset.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaDataset.java index 4a85d34..bb8e39c 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaDataset.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaDataset.java @@ -29,29 +29,28 @@ import org.apache.jena.sparql.core.DatasetGraph; * {@link #asJenaDatasetGraph()}. */ public interface JenaDataset extends org.apache.commons.rdf.api.Dataset { - - /** - * Return the underlying Jena {@link DatasetGraph}. - * <p> - * Changes to the Jena <em>dataset graph</em> are reflected in the Commons - * RDF dataset and vice versa. - * - * @return A Jena {@link DatasetGraph} - */ - public DatasetGraph asJenaDatasetGraph(); - - /** - * Return a union graph view of this dataset. - * <p> - * The <em>union graph</em> contains triples in any graph (including the - * default graph). - * <p> - * Changes in the union graph are reflected in the Commons RDF dataset and - * vice versa. Triples added to the graph are added to the default graph. - * - * @return A union {@link Graph} - */ - public JenaGraph getUnionGraph(); + /** + * Return the underlying Jena {@link DatasetGraph}. + * <p> + * Changes to the Jena <em>dataset graph</em> are reflected in the Commons + * RDF dataset and vice versa. + * + * @return A Jena {@link DatasetGraph} + */ + public DatasetGraph asJenaDatasetGraph(); + + /** + * Return a union graph view of this dataset. + * <p> + * The <em>union graph</em> contains triples in any graph (including the + * default graph). + * <p> + * Changes in the union graph are reflected in the Commons RDF dataset and + * vice versa. Triples added to the graph are added to the default graph. + * + * @return A union {@link Graph} + */ + public JenaGraph getUnionGraph(); } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/jena/src/main/java/org/apache/commons/rdf/jena/JenaGeneralizedTripleLike.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaGeneralizedTripleLike.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaGeneralizedTripleLike.java index 634d05c..f65ed35 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaGeneralizedTripleLike.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaGeneralizedTripleLike.java @@ -34,6 +34,5 @@ import org.apache.commons.rdf.api.Triple; * * @see JenaGeneralizedQuadLike */ -public interface JenaGeneralizedTripleLike - extends JenaTripleLike { +public interface JenaGeneralizedTripleLike extends JenaTripleLike { } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/jena/src/main/java/org/apache/commons/rdf/jena/JenaGraph.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaGraph.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaGraph.java index 86b9f29..b605b4b 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaGraph.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaGraph.java @@ -29,27 +29,28 @@ import org.apache.jena.rdf.model.Model; * {@link #asJenaGraph()}. */ public interface JenaGraph extends org.apache.commons.rdf.api.Graph { - - /** - * Return the underlying Jena {@link org.apache.jena.graph.Graph}. - * <p> - * Changes to the Jena graph are reflected in the Commons RDF graph and vice versa. - * - * @return A Jena {@link org.apache.jena.graph.Graph} - */ - public org.apache.jena.graph.Graph asJenaGraph(); - /** - * Return the graph as a Jena {@link Model}. - * <p> - * Changes to the Jena model are reflected in the Commons RDF graph and vice - * versa. - * <p> - * Note that in some cases there is no underlying Jena {@link Model}, in - * which case this method will create one. Subsequent calls should return - * the same model. - * - * @return A Jena {@link Model} - */ - public Model asJenaModel(); + /** + * Return the underlying Jena {@link org.apache.jena.graph.Graph}. + * <p> + * Changes to the Jena graph are reflected in the Commons RDF graph and vice + * versa. + * + * @return A Jena {@link org.apache.jena.graph.Graph} + */ + public org.apache.jena.graph.Graph asJenaGraph(); + + /** + * Return the graph as a Jena {@link Model}. + * <p> + * Changes to the Jena model are reflected in the Commons RDF graph and vice + * versa. + * <p> + * Note that in some cases there is no underlying Jena {@link Model}, in + * which case this method will create one. Subsequent calls should return + * the same model. + * + * @return A Jena {@link Model} + */ + public Model asJenaModel(); } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/jena/src/main/java/org/apache/commons/rdf/jena/JenaIRI.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaIRI.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaIRI.java index bcc46c8..0edd1b5 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaIRI.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaIRI.java @@ -23,7 +23,7 @@ import org.apache.jena.graph.Node; /** * A Jena-backed {@link IRI}. * <p> - * The underlying Jena {@link Node} can be accessed from {@link #asJenaNode()}. + * The underlying Jena {@link Node} can be accessed from {@link #asJenaNode()}. * * @see Node#isURI() */ http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/jena/src/main/java/org/apache/commons/rdf/jena/JenaQuad.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaQuad.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaQuad.java index fffbef7..0004017 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaQuad.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaQuad.java @@ -19,12 +19,11 @@ package org.apache.commons.rdf.jena; import org.apache.commons.rdf.api.BlankNodeOrIRI; -/** +/** * A Jena-backed {@link org.apache.commons.rdf.api.Quad}. * <p> - * The underlying Jena {@link org.apache.jena.sparql.core.Quad} - * can be accessed with {@link #asJenaQuad()}. + * The underlying Jena {@link org.apache.jena.sparql.core.Quad} can be accessed + * with {@link #asJenaQuad()}. */ -public interface JenaQuad extends org.apache.commons.rdf.api.Quad, - JenaQuadLike<BlankNodeOrIRI> { +public interface JenaQuad extends org.apache.commons.rdf.api.Quad, JenaQuadLike<BlankNodeOrIRI> { } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/413dd09a/jena/src/main/java/org/apache/commons/rdf/jena/JenaQuadLike.java ---------------------------------------------------------------------- diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaQuadLike.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaQuadLike.java index 2e869c3..480bdb9 100644 --- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaQuadLike.java +++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaQuadLike.java @@ -32,13 +32,12 @@ import org.apache.jena.sparql.core.Quad; * @see JenaGeneralizedQuadLike * */ -public interface JenaQuadLike<G extends RDFTerm> - extends JenaTripleLike, QuadLike<G> { +public interface JenaQuadLike<G extends RDFTerm> extends JenaTripleLike, QuadLike<G> { - /** - * Return the adapted Jena quad - * - * @return Adapted Jena {@link Quad}. - */ - public Quad asJenaQuad(); + /** + * Return the adapted Jena quad + * + * @return Adapted Jena {@link Quad}. + */ + public Quad asJenaQuad(); }