Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id E7A28200B9D for ; Wed, 28 Sep 2016 15:52:02 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id E67A3160AE9; Wed, 28 Sep 2016 13:52:02 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id BF6B1160ADD for ; Wed, 28 Sep 2016 15:52:01 +0200 (CEST) Received: (qmail 39789 invoked by uid 500); 28 Sep 2016 13:52:00 -0000 Mailing-List: contact commits-help@commonsrdf.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commonsrdf.incubator.apache.org Delivered-To: mailing list commits@commonsrdf.incubator.apache.org Received: (qmail 39731 invoked by uid 99); 28 Sep 2016 13:52:00 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Sep 2016 13:52:00 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 7DDDFCA866 for ; Wed, 28 Sep 2016 13:52:00 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.344 X-Spam-Level: X-Spam-Status: No, score=-4.344 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-1.124] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id 7H27SmFIV_18 for ; Wed, 28 Sep 2016 13:51:59 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id BB6DB5FB09 for ; Wed, 28 Sep 2016 13:51:54 +0000 (UTC) Received: (qmail 38467 invoked by uid 99); 28 Sep 2016 13:51:53 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Sep 2016 13:51:53 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id ABFD9EEE2A; Wed, 28 Sep 2016 13:51:53 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: stain@apache.org To: commits@commonsrdf.incubator.apache.org Date: Wed, 28 Sep 2016 13:52:03 -0000 Message-Id: <0ce2e67122ab41de9bdd34a5b2c392da@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [11/36] incubator-commonsrdf git commit: Some AbstractRDFParserBuilder tests archived-at: Wed, 28 Sep 2016 13:52:03 -0000 Some AbstractRDFParserBuilder tests Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/528af997 Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/528af997 Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/528af997 Branch: refs/heads/master Commit: 528af9976ce03280578f82531a95a49a3546b238 Parents: 478b20e Author: Stian Soiland-Reyes Authored: Sun Apr 3 01:47:16 2016 +0100 Committer: Stian Soiland-Reyes Committed: Sun Apr 3 02:32:47 2016 +0100 ---------------------------------------------------------------------- .../simple/AbstractRDFParserBuilderTest.java | 220 +++++++++++++++++-- .../rdf/simple/DummyRDFParserBuilder.java | 70 ++++-- 2 files changed, 250 insertions(+), 40 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/528af997/simple/src/test/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilderTest.java ---------------------------------------------------------------------- diff --git a/simple/src/test/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilderTest.java b/simple/src/test/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilderTest.java index 1b751fa..d23e1c8 100644 --- a/simple/src/test/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilderTest.java +++ b/simple/src/test/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilderTest.java @@ -19,9 +19,11 @@ package org.apache.commons.rdf.simple; import static org.junit.Assert.*; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.apache.commons.rdf.api.Graph; @@ -29,39 +31,211 @@ import org.apache.commons.rdf.api.IRI; import org.apache.commons.rdf.api.Literal; import org.apache.commons.rdf.api.RDFParserBuilder; import org.apache.commons.rdf.api.RDFSyntax; +import org.apache.commons.rdf.api.RDFTerm; +import org.apache.commons.rdf.api.RDFTermFactory; import org.apache.commons.rdf.api.Triple; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class AbstractRDFParserBuilderTest { - /** - * Test a basic parsing of an N-Triples-file - * - * @throws Exception - */ - @Test - public void parseFile() throws Exception { - Path file = Files.createTempFile("test", ".nt"); - // No need to populate the file as the dummy parser + private RDFTermFactory factory = new SimpleRDFTermFactory(); + + private DummyRDFParserBuilder dummyParser = new DummyRDFParserBuilder(); + private Path testNt; + private Path testTtl; + private Path testXml; + + @Before + public void createTempFile() throws IOException { + testNt = Files.createTempFile("test", ".nt"); + testTtl = Files.createTempFile("test", ".ttl"); + testXml = Files.createTempFile("test", ".xml"); + + // No need to populate the files as the dummy parser // doesn't actually read anything - - RDFParserBuilder parser = new DummyRDFParserBuilder() - .source(file).contentType(RDFSyntax.NTRIPLES); - Future f = parser.parse(); - Graph g = f.get(5, TimeUnit.SECONDS); - - assertEquals(1, g.size()); - Triple triple = g.getTriples().findAny().get(); + } + + @After + public void deleteTempFiles() throws IOException { + Files.deleteIfExists(testNt); + Files.deleteIfExists(testTtl); + Files.deleteIfExists(testXml); + } + + @Test + public void guessRDFSyntax() throws Exception { + assertEquals(RDFSyntax.NTRIPLES, AbstractRDFParserBuilder.guessRDFSyntax(testNt).get()); + assertEquals(RDFSyntax.TURTLE, AbstractRDFParserBuilder.guessRDFSyntax(testTtl).get()); + assertFalse(AbstractRDFParserBuilder.guessRDFSyntax(testXml).isPresent()); + } + + private void checkGraph(Graph g) throws Exception { + assertTrue(g.size() > 0); + IRI greeting = factory.createIRI("http://example.com/greeting"); + // Should only have parsed once! + assertEquals(1, g.getTriples(null, greeting, null).count()); + Triple triple = g.getTriples(null, greeting, null).findAny().get(); assertTrue(triple.getSubject() instanceof IRI); - IRI iri = (IRI)triple.getSubject(); - assertEquals("http://example.com/test1", iri.getIRIString()); - + IRI parsing = (IRI) triple.getSubject(); + assertTrue(parsing.getIRIString().startsWith("urn:uuid:")); + assertEquals("http://example.com/greeting", triple.getPredicate().getIRIString()); - + assertTrue(triple.getObject() instanceof Literal); - Literal literal = (Literal)triple.getObject(); + Literal literal = (Literal) triple.getObject(); assertEquals("Hello world", literal.getLexicalForm()); assertFalse(literal.getLanguageTag().isPresent()); assertEquals(Types.XSD_STRING, literal.getDatatype()); + + // Check uniqueness of properties that are always present + assertEquals(1, + g.getTriples(null, factory.createIRI("http://example.com/source"), null).count()); + + // Check optional properties that are unique + assertTrue(2 > g.getTriples(null, factory.createIRI("http://example.com/base"), null).count()); + assertTrue(2 > g.getTriples(null, factory.createIRI("http://example.com/contentType"), null).count()); + assertTrue(2 > g.getTriples(null, factory.createIRI("http://example.com/contentTypeSyntax"), null).count()); + } + + @Test + public void parseFile() throws Exception { + RDFParserBuilder parser = dummyParser.source(testNt); + Graph g = parser.parse().get(5, TimeUnit.SECONDS); + checkGraph(g); + // FIXME: this could potentially break if the equivalent of /tmp includes + // international characters + assertEquals("<" + testNt.toUri().toString() + ">", firstPredicate(g, "source")); + // Should be set to the file path + assertEquals("<" + testNt.toUri().toString() + ">", firstPredicate(g, "base")); + + // Should NOT have guessed the content type + assertNull(firstPredicate(g, "contentType")); + assertNull(firstPredicate(g, "contentTypeSyntax")); + } + + + @Test + public void parseNoSource() throws Exception { + thrown.expect(IllegalStateException.class); + dummyParser.parse(); } + + @Test + public void parseBaseAndContentTypeNoSource() throws Exception { + // Can set the other options, even without source() + IRI base = dummyParser.createRDFTermFactory().createIRI("http://www.example.org/test.rdf"); + RDFParserBuilder parser = dummyParser.base(base).contentType(RDFSyntax.RDFXML); + thrown.expect(IllegalStateException.class); + thrown.expectMessage("No source has been set"); + // but .parse() should fail + parser.parse(); + } + + @Test + public void parseFileMissing() throws Exception { + Files.delete(testNt); + // This should not fail yet + RDFParserBuilder parser = dummyParser.source(testNt); + // but here: + thrown.expect(IOException.class); + parser.parse(); + } + + + @Test + public void parseFileContentType() throws Exception { + RDFParserBuilder parser = dummyParser.source(testNt).contentType(RDFSyntax.NTRIPLES); + Graph g = parser.parse().get(5, TimeUnit.SECONDS); + checkGraph(g); + // FIXME: this could potentially break if the equivalent of /tmp includes + // international characters + assertEquals("<" + testNt.toUri().toString() + ">", firstPredicate(g, "source")); + assertEquals("<" + testNt.toUri().toString() + ">", firstPredicate(g, "base")); + assertEquals("\"NTRIPLES\"", firstPredicate(g, "contentTypeSyntax")); + assertEquals("\"application/n-triples\"", firstPredicate(g, "contentType")); + } + + private String firstPredicate(Graph g, String pred) { + return g.getTriples(null, factory.createIRI("http://example.com/" + pred), null) + .map(Triple::getObject).map(RDFTerm::ntriplesString).findAny().orElse(null); + } + + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void parseInputStreamFailsIfBaseMissing() throws Exception { + InputStream inputStream = new ByteArrayInputStream(new byte[0]); + // Should not fail at this point + RDFParserBuilder parser = dummyParser.source(inputStream); + // but here: + thrown.expect(IllegalStateException.class); + thrown.expectMessage("base iri required for inputstream source"); + parser.parse(); + } + + @Test + public void parseInputStreamWithBase() throws Exception { + InputStream inputStream = new ByteArrayInputStream(new byte[0]); + IRI base = dummyParser.createRDFTermFactory().createIRI("http://www.example.org/test.rdf"); + RDFParserBuilder parser = dummyParser.source(inputStream).base(base); + Graph g = parser.parse().get(5, TimeUnit.SECONDS); + checkGraph(g); + assertEquals("", firstPredicate(g, "base")); + // in our particular debug output, + // bnode source indicates InputStream + assertTrue(firstPredicate(g, "source").startsWith("_:")); + assertNull(firstPredicate(g, "contentType")); + assertNull(firstPredicate(g, "contentTypeSyntax")); + } + + @Test + public void parseInputStreamWithNQuads() throws Exception { + InputStream inputStream = new ByteArrayInputStream(new byte[0]); + RDFParserBuilder parser = dummyParser.source(inputStream).contentType(RDFSyntax.NQUADS); + Graph g = parser.parse().get(5, TimeUnit.SECONDS); + checkGraph(g); + assertNull(firstPredicate(g, "base")); + // in our particular debug output, + // bnode source indicates InputStream + assertTrue(firstPredicate(g, "source").startsWith("_:")); + assertEquals("\"application/n-quads\"", firstPredicate(g, "contentType")); + assertEquals("\"NQUADS\"", firstPredicate(g, "contentTypeSyntax")); + } + + @Test + public void parseIRI() throws Exception { + IRI iri = dummyParser.createRDFTermFactory().createIRI("http://www.example.net/test.ttl"); + RDFParserBuilder parser = dummyParser.source(iri); + Graph g = parser.parse().get(5, TimeUnit.SECONDS); + checkGraph(g); + assertEquals("", firstPredicate(g, "source")); + // No base - assuming the above IRI is always + // the base would break server-supplied base from + // any HTTP Location redirects and Content-Location header + assertNull(firstPredicate(g, "base")); + // ".ttl" in IRI string does not imply any content type + assertNull(firstPredicate(g, "contentType")); + assertNull(firstPredicate(g, "contentTypeSyntax")); + + } + + @Test + public void parseIRIBaseContentType() throws Exception { + IRI iri = dummyParser.createRDFTermFactory().createIRI("http://www.example.net/test.ttl"); + RDFParserBuilder parser = dummyParser.source(iri).base(iri).contentType(RDFSyntax.TURTLE); + Graph g = parser.parse().get(5, TimeUnit.SECONDS); + checkGraph(g); + assertEquals("", firstPredicate(g, "source")); + assertEquals("", firstPredicate(g, "base")); + assertEquals("\"TURTLE\"", firstPredicate(g, "contentTypeSyntax")); + assertEquals("\"text/turtle\"", firstPredicate(g, "contentType")); + } + + } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/528af997/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java ---------------------------------------------------------------------- diff --git a/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java b/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java index 38e6ed2..ddaf4ed 100644 --- a/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java +++ b/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java @@ -19,42 +19,78 @@ package org.apache.commons.rdf.simple; import java.io.IOException; import java.text.ParseException; +import java.util.UUID; import org.apache.commons.rdf.api.Graph; import org.apache.commons.rdf.api.IRI; -import org.apache.commons.rdf.api.Literal; import org.apache.commons.rdf.api.RDFParserBuilder; import org.apache.commons.rdf.api.RDFTermFactory; /** - * For test purposes - a {@link RDFParserBuilder} that always insert a single triple. + * For test purposes - a {@link RDFParserBuilder} that inserts information + * about what it has been asked to parse instead of actually parsing anything. *

- * This dummy RDF parser always sleeps for at least 1000 ms - * before inserting the triple: + * This always insert at least the triple equivalent to: *

- *      "Hello world" .
+ *      "Hello world" .
  * 
+ * Additional triples match the corresponding getter in AbstractRDFParserBuilder, + * e.g.: + *
+ *      .
+ *     "/tmp/file.ttl" .   
+ * 
+ * * */ public class DummyRDFParserBuilder extends AbstractRDFParserBuilder { - + @Override protected void parseSynchronusly() throws IOException, IllegalStateException, ParseException { // From parseSynchronusly both of these are always present RDFTermFactory factory = getRdfTermFactory().get(); Graph graph = getIntoGraph().get(); + + // well - each parsing is unique. This should hopefully + // catch any accidental double parsing + IRI parsing = factory.createIRI("urn:uuid:" + UUID.randomUUID()); + graph.add(parsing, factory.createIRI("http://example.com/greeting"), + factory.createLiteral("Hello world")); - // Let's always insert the same triple - IRI test1 = factory.createIRI("http://example.com/test1"); - IRI greeting = factory.createIRI("http://example.com/greeting"); - Literal hello = factory.createLiteral("Hello world"); - try { - // Pretend we take a while to parse - Thread.sleep(1000); - } catch (InterruptedException e) { - return; - } - graph.add(test1, greeting, hello); + // Now we'll expose the finalized AbstractRDFParserBuilder settings + // so they can be inspected by the junit test + + if (getSourceIri().isPresent()) { + graph.add(parsing, + factory.createIRI("http://example.com/source"), + getSourceIri().get()); + } + if (getSourceFile().isPresent()) { + graph.add(parsing, + factory.createIRI("http://example.com/source"), + factory.createIRI(getSourceFile().get().toUri().toString())); + } + if (getSourceInputStream().isPresent()) { + graph.add(parsing, + factory.createIRI("http://example.com/source"), + factory.createBlankNode()); + } + + if (getBase().isPresent()) { + graph.add(parsing, + factory.createIRI("http://example.com/base"), + getBase().get()); + } + if (getContentType().isPresent()) { + graph.add(parsing, + factory.createIRI("http://example.com/contentType"), + factory.createLiteral(getContentType().get())); + } + if (getContentTypeSyntax().isPresent()) { + graph.add(parsing, + factory.createIRI("http://example.com/contentTypeSyntax"), + factory.createLiteral(getContentTypeSyntax().get().name())); + } } }