Test AbstractRDFParserBuilder
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/8dfc3f4e Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/8dfc3f4e Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/8dfc3f4e Branch: refs/heads/master Commit: 8dfc3f4eef9afda61cecf088be42c8a9ee547865 Parents: 8d548d5 Author: Stian Soiland-Reyes <st...@apache.org> Authored: Sun Apr 3 00:22:48 2016 +0100 Committer: Stian Soiland-Reyes <st...@apache.org> Committed: Sun Apr 3 00:22:48 2016 +0100 ---------------------------------------------------------------------- .../simple/AbstractRDFParserBuilderTest.java | 67 ++++++++++++++++++++ .../rdf/simple/DummyRDFParserBuilder.java | 60 ++++++++++++++++++ 2 files changed, 127 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/8dfc3f4e/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 new file mode 100644 index 0000000..1b751fa --- /dev/null +++ b/simple/src/test/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilderTest.java @@ -0,0 +1,67 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.rdf.simple; + +import static org.junit.Assert.*; + +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; +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.Triple; +import org.junit.Test; + +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 + // 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(); + assertTrue(triple.getSubject() instanceof IRI); + IRI iri = (IRI)triple.getSubject(); + assertEquals("http://example.com/test1", iri.getIRIString()); + + assertEquals("http://example.com/greeting", triple.getPredicate().getIRIString()); + + assertTrue(triple.getObject() instanceof Literal); + Literal literal = (Literal)triple.getObject(); + assertEquals("Hello world", literal.getLexicalForm()); + assertFalse(literal.getLanguageTag().isPresent()); + assertEquals(Types.XSD_STRING, literal.getDatatype()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/8dfc3f4e/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 new file mode 100644 index 0000000..38e6ed2 --- /dev/null +++ b/simple/src/test/java/org/apache/commons/rdf/simple/DummyRDFParserBuilder.java @@ -0,0 +1,60 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.rdf.simple; + +import java.io.IOException; +import java.text.ParseException; + +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. + * <p> + * This dummy RDF parser always sleeps for at least 1000 ms + * before inserting the triple: + * <pre> + * <http://example.com/test1> <http://example.com/greeting> "Hello world" . + * </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(); + + // 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); + } + +}