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 <st...@apache.org> Authored: Sun Apr 3 01:47:16 2016 +0100 Committer: Stian Soiland-Reyes <st...@apache.org> 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<Graph> 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("<http://www.example.org/test.rdf>", 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("<http://www.example.net/test.ttl>", 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("<http://www.example.net/test.ttl>", firstPredicate(g, "source")); + assertEquals("<http://www.example.net/test.ttl>", 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. * <p> - * This dummy RDF parser always sleeps for at least 1000 ms - * before inserting the triple: + * This always insert at least the triple equivalent to: * <pre> - * <http://example.com/test1> <http://example.com/greeting> "Hello world" . + * <urn:uuid:b7ac3fcc-4d86-4d28-8358-a1cd094974a6> <http://example.com/greeting> "Hello world" . * </pre> + * Additional triples match the corresponding getter in AbstractRDFParserBuilder, + * e.g.: + * <pre> + * <urn:uuid:b7ac3fcc-4d86-4d28-8358-a1cd094974a6> <http://example.com/base> <http://www.example.org/> . + * <urn:uuid:b7ac3fcc-4d86-4d28-8358-a1cd094974a6> <http://example.com/sourceFile> "/tmp/file.ttl" . + * </pre> + * * */ 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())); + } } }