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 A2E15200BFF for ; Tue, 17 Jan 2017 17:16:46 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id A22E4160B58; Tue, 17 Jan 2017 16:16:46 +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 C87D1160B46 for ; Tue, 17 Jan 2017 17:16:45 +0100 (CET) Received: (qmail 76738 invoked by uid 500); 17 Jan 2017 16:16:44 -0000 Mailing-List: contact commits-help@jena.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jena.apache.org Delivered-To: mailing list commits@jena.apache.org Received: (qmail 76523 invoked by uid 99); 17 Jan 2017 16:16:43 -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; Tue, 17 Jan 2017 16:16:43 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7E216DFACD; Tue, 17 Jan 2017 16:16:43 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: andy@apache.org To: commits@jena.apache.org Date: Tue, 17 Jan 2017 16:16:49 -0000 Message-Id: <90e3d038745a41e1ab7dfa1a15152243@git.apache.org> In-Reply-To: <3f678fee97c140b1b886cf75ac00fbe4@git.apache.org> References: <3f678fee97c140b1b886cf75ac00fbe4@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [7/9] jena git commit: TestJsonLDREader: use RDFDataMgr.parse instead of Reader.read, and some cleaning archived-at: Tue, 17 Jan 2017 16:16:46 -0000 TestJsonLDREader: use RDFDataMgr.parse instead of Reader.read, and some cleaning Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/14f5bc61 Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/14f5bc61 Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/14f5bc61 Branch: refs/heads/master Commit: 14f5bc615d75de9e2d3730ee79d34dda139e5c84 Parents: 4cdb77f Author: François-Paul Servant Authored: Mon Jan 9 18:36:53 2017 +0100 Committer: François-Paul Servant Committed: Mon Jan 9 18:36:53 2017 +0100 ---------------------------------------------------------------------- .../org/apache/jena/riot/TestJsonLDReader.java | 76 ++++++++++++++------ 1 file changed, 53 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/14f5bc61/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java b/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java index b5f9373..c51a0fe 100644 --- a/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java +++ b/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java @@ -42,12 +42,12 @@ public class TestJsonLDReader { @Test public final void simpleReadTest() { try { - String jsonld = "{\"@id\":\"_:b0\",\"@type\":\"http://schema.org/Person\",\"name\":\"John Doe\",\"@context\":\"http://schema.org/\"}"; - StringReader reader = new StringReader(jsonld); + String jsonld = someSchemaDorOrgJsonld(); Model m = ModelFactory.createDefaultModel(); - m.read(reader, null, "JSON-LD"); - assertTrue(m.contains(null, RDF.type, m.createResource("http://schema.org/Person"))); - assertTrue(m.contains(null, m.createProperty("http://schema.org/name"), "John Doe")); + try (StringReader reader = new StringReader(jsonld)) { + m.read(reader, null, "JSON-LD"); + } + assertJohnDoeIsOK(m); } catch (RiotException e) { // cf. org.apache.jena.riot.RiotException: loading remote context failed: http://schema.org/ // There's a line printed anyway @@ -58,7 +58,7 @@ public class TestJsonLDReader { /** Test using the jena Context mechanism to pass the jsonld "@context" */ @Test public final void overrideAtContextTest() throws JsonGenerationException, IOException { // some jsonld using schema.org's URI as "@context" - String jsonld = "{\"@id\":\"_:b0\",\"@type\":\"Person\",\"name\":\"John Doe\",\"@context\":\"http://schema.org/\"}"; + String jsonld = someSchemaDorOrgJsonld(); // a subset of schema.org that can be used as @context for jsonld String jsonldContext = "{\"name\":{\"@id\":\"http://schema.org/name\"},\"Person\": {\"@id\": \"http://schema.org/Person\"}}"; @@ -69,15 +69,10 @@ public class TestJsonLDReader { jenaCtx.set(RIOT.JSONLD_CONTEXT, jsonldContextAsMap); // read the jsonld, replacing its "@context" - Dataset ds = DatasetFactory.create(); - ReaderRIOT reader = RDFDataMgr.createReader(Lang.JSONLD); - try (InputStream in = new ByteArrayInputStream(jsonld.getBytes(StandardCharsets.UTF_8))) { - reader.read(in, null, null, StreamRDFLib.dataset(ds.asDatasetGraph()), jenaCtx); - } - - Model m = ds.getDefaultModel(); - assertTrue(m.contains(null, RDF.type, m.createResource("http://schema.org/Person"))); - assertTrue(m.contains(null, m.createProperty("http://schema.org/name"), "John Doe")); + Dataset ds = jsonld2dataset(jsonld, jenaCtx); + + // check ds is correct + assertJohnDoeIsOK(ds.getDefaultModel()); } /** Not really useful, but one can replace the @context by a URI: in this case, this URI is used when expanding the json @@ -96,14 +91,12 @@ public class TestJsonLDReader { Object jsonldContextAsObject = JsonUtils.fromInputStream(new ByteArrayInputStream(jsonldContext.getBytes(StandardCharsets.UTF_8))); jenaCtx.set(RIOT.JSONLD_CONTEXT, jsonldContextAsObject); - // read the jsonld, replacing its "@context" - Dataset ds = DatasetFactory.create(); - ReaderRIOT reader = RDFDataMgr.createReader(Lang.JSONLD); - try (InputStream in = new ByteArrayInputStream(jsonld.getBytes(StandardCharsets.UTF_8))) { - reader.read(in, null, null, StreamRDFLib.dataset(ds.asDatasetGraph()), jenaCtx); - Model m = ds.getDefaultModel(); - assertTrue(m.contains(null, RDF.type, m.createResource("http://schema.org/Person"))); - assertTrue(m.contains(null, m.createProperty("http://schema.org/name"), "John Doe")); + try { + // read the jsonld, replacing its "@context" + Dataset ds = jsonld2dataset(jsonld, jenaCtx); + + // check ds is correct + assertJohnDoeIsOK(ds.getDefaultModel()); } catch (RiotException e) { // cf. org.apache.jena.riot.RiotException: loading remote context failed: http://schema.org/ // There's a line printed anyway @@ -111,4 +104,41 @@ public class TestJsonLDReader { } } + // + // + // + + /** + * Reading some jsonld String, using a Context + * @return a new Dataset + * @throws IOException + */ + private Dataset jsonld2dataset(String jsonld, Context jenaCtx) throws IOException { + Dataset ds = DatasetFactory.create(); + + // this is works too + // ReaderRIOT reader = RDFDataMgr.createReader(Lang.JSONLD); + // try (InputStream in = new ByteArrayInputStream(jsonld.getBytes(StandardCharsets.UTF_8))) { + // reader.read(in, null, null, StreamRDFLib.dataset(ds.asDatasetGraph()), jenaCtx); + // } + + try (InputStream in = new ByteArrayInputStream(jsonld.getBytes(StandardCharsets.UTF_8))) { + RDFDataMgr.parse(StreamRDFLib.dataset(ds.asDatasetGraph()), in, null, Lang.JSONLD, jenaCtx); + } + + return ds; + } + + /** Example data */ + private String someSchemaDorOrgJsonld() { + return "{\"@id\":\"_:b0\",\"@type\":\"Person\",\"name\":\"John Doe\",\"@context\":\"http://schema.org/\"}"; + } + + /** Checking that the data loaded from someSchemaDorOrgJsonld into a model, is OK */ + private void assertJohnDoeIsOK(Model m) { + assertTrue(m.contains(null, RDF.type, m.createResource("http://schema.org/Person"))); + assertTrue(m.contains(null, m.createProperty("http://schema.org/name"), "John Doe")); + } + + }