Repository: marmotta Updated Branches: refs/heads/ldp 8497e8419 -> e66e59d75
MARMOTTA-440: first working version of rdf-patch - requires more testing - inclusion into ldp pending Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/e66e59d7 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/e66e59d7 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/e66e59d7 Branch: refs/heads/ldp Commit: e66e59d75448920c413b08b95b637530bb673b08 Parents: 8497e84 Author: Jakob Frank <[email protected]> Authored: Tue Feb 25 11:56:55 2014 +0100 Committer: Jakob Frank <[email protected]> Committed: Tue Feb 25 11:56:55 2014 +0100 ---------------------------------------------------------------------- platform/marmotta-ldp/pom.xml | 2 +- .../marmotta/platform/ldp/api/LdpService.java | 17 +++ .../platform/ldp/patch/RdfPatchUtil.java | 112 +++++++++++++++++++ .../platform/ldp/patch/model/PatchLine.java | 20 +++- .../ldp/patch/model/WildcardStatement.java | 107 ++++++++++++++++++ .../platform/ldp/util/LdpWebServiceUtils.java | 17 +++ .../platform/ldp/webservices/PATCH.java | 17 +++ .../marmotta-ldp/src/main/javacc/rdf-patch.jj | 41 +++---- .../platform/ldp/patch/RdfPatchParserTest.java | 89 --------------- .../platform/ldp/patch/RdfPatchUtilTest.java | 98 ++++++++++++++++ .../ldp/patch/parser/RdfPatchParserTest.java | 89 +++++++++++++++ .../src/test/resources/illustrative.in.ttl | 17 +++ .../src/test/resources/illustrative.rdfp | 21 +++- 13 files changed, 535 insertions(+), 112 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/pom.xml ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/pom.xml b/platform/marmotta-ldp/pom.xml index 6f8bdff..f79cb9a 100644 --- a/platform/marmotta-ldp/pom.xml +++ b/platform/marmotta-ldp/pom.xml @@ -86,7 +86,7 @@ <goal>javacc</goal> </goals> <configuration> - <lookAhead>150</lookAhead> + <lookAhead>1</lookAhead> <isStatic>false</isStatic> </configuration> </execution> http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java index 9a746da..887aa8c 100644 --- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java +++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java @@ -1,3 +1,20 @@ +/* + * 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.marmotta.platform.ldp.api; import org.openrdf.model.Statement; http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java new file mode 100644 index 0000000..be43762 --- /dev/null +++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java @@ -0,0 +1,112 @@ +/* + * 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.marmotta.platform.ldp.patch; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.marmotta.platform.ldp.patch.model.PatchLine; +import org.apache.marmotta.platform.ldp.patch.parser.ParseException; +import org.apache.marmotta.platform.ldp.patch.parser.RdfPatchParser; +import org.openrdf.model.Resource; +import org.openrdf.model.Statement; +import org.openrdf.model.URI; +import org.openrdf.model.Value; +import org.openrdf.repository.Repository; +import org.openrdf.repository.RepositoryConnection; +import org.openrdf.repository.RepositoryException; + +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +/** + * Created by jakob on 2/25/14. + */ +public class RdfPatchUtil { + + public static void applyPatch(Repository repository, String patch, Resource... contexts) throws RepositoryException, ParseException { + applyPatch(repository, getPatch(patch), contexts); + } + + public static void applyPatch(Repository repository, InputStream patchSource, Resource... contexts) throws RepositoryException, ParseException { + applyPatch(repository, getPatch(patchSource), contexts); + } + public static void applyPatch(RepositoryConnection con, String patch, Resource... contexts) throws RepositoryException, ParseException { + applyPatch(con, getPatch(patch), contexts); + } + public static void applyPatch(RepositoryConnection con, InputStream patchSource, Resource... contexts) throws RepositoryException, ParseException { + applyPatch(con, getPatch(patchSource), contexts); + } + + public static void applyPatch(Repository repository, List<PatchLine> patch, Resource... contexts) throws RepositoryException { + RepositoryConnection con = repository.getConnection(); + try { + con.begin(); + applyPatch(con, patch, contexts); + con.commit(); + } catch (final Throwable t) { + con.rollback(); + throw t; + } finally { + con.close(); + } + } + + public static void applyPatch(RepositoryConnection con, List<PatchLine> patch, Resource... contexts) throws RepositoryException { + Resource subject = null; + URI predicate = null; + Value object = null; + + for (PatchLine patchLine : patch) { + final Statement statement = patchLine.getStatement(); + subject = statement.getSubject()!=null? statement.getSubject():subject; + predicate = statement.getPredicate()!=null?statement.getPredicate():predicate; + object = statement.getObject()!=null?statement.getObject():object; + + switch (patchLine.getOperator()) { + case ADD: + con.add(subject, predicate, object, contexts); + break; + case DEL: + con.remove(subject, predicate, object, contexts); + break; + default: + throw new IllegalArgumentException("Unknown patch operation: " + patchLine.getOperator()); + } + } + } + + private static List<PatchLine> getPatch(InputStream is) throws ParseException { + RdfPatchParser parser = new RdfPatchParser(is); + return parser.parsePatch(); + } + + private static List<PatchLine> getPatch(String patch) throws ParseException { + try (InputStream is = IOUtils.toInputStream(patch)) { + return getPatch(is); + } catch (IOException e) { + // You can always close an InputStream on a String + assert(false); + return null; + } + } + + private RdfPatchUtil() { + // static access only + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/PatchLine.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/PatchLine.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/PatchLine.java index 971b86a..4e0da28 100644 --- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/PatchLine.java +++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/PatchLine.java @@ -23,7 +23,25 @@ import org.openrdf.model.Statement; * Created by jakob on 2/24/14. */ public class PatchLine { - public enum Operator {DEL, ADD} + public enum Operator { + DEL("D"), + ADD("A"); + + private final String cmd; + + Operator(String cmd) { + this.cmd = cmd; + } + + public static Operator fromCommand(String cmd) { + for (Operator op: values()) { + if (op.cmd.equalsIgnoreCase(cmd)) { + return op; + } + } + throw new IllegalArgumentException("Unknown Operator: " + cmd); + } + } private final Operator operator; http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/WildcardStatement.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/WildcardStatement.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/WildcardStatement.java new file mode 100644 index 0000000..a243d34 --- /dev/null +++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/WildcardStatement.java @@ -0,0 +1,107 @@ +/* + * 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.marmotta.platform.ldp.patch.model; + +import org.apache.commons.lang3.ObjectUtils; +import org.openrdf.model.Resource; +import org.openrdf.model.Statement; +import org.openrdf.model.URI; +import org.openrdf.model.Value; + +/** + * Created by jakob on 2/25/14. + */ +public class WildcardStatement implements Statement { + + + private final Value object; + private final URI predicate; + private final Resource subject; + + public WildcardStatement(Resource subject, URI predicate, Value object) { + this.object = object; + this.predicate = predicate; + this.subject = subject; + } + + @Override + public Resource getSubject() { + return subject; + } + + @Override + public URI getPredicate() { + return predicate; + } + + @Override + public Value getObject() { + return object; + } + + @Override + public Resource getContext() { + return null; + } + + @Override + public int hashCode() { + return 961 * (subject!=null?subject.hashCode():0) + 31 * (predicate!=null?predicate.hashCode():0) + (object!=null?object.hashCode():0); + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + + if (other instanceof Statement) { + Statement otherSt = (Statement)other; + + // The object is potentially the cheapest to check, as types + // of these references might be different. + + // In general the number of different predicates in sets of + // statements is the smallest, so predicate equality is checked + // last. + return ObjectUtils.equals(object, otherSt.getObject()) && ObjectUtils.equals(subject, otherSt.getSubject()) + && ObjectUtils.equals(predicate, otherSt.getPredicate()); + } + + return false; + } + + /** + * Gives a String-representation of this Statement that can be used for + * debugging. + */ + @Override + public String toString() { + StringBuilder sb = new StringBuilder(256); + + sb.append("("); + sb.append(getSubject()); + sb.append(", "); + sb.append(getPredicate()); + sb.append(", "); + sb.append(getObject()); + sb.append(")"); + + return sb.toString(); + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpWebServiceUtils.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpWebServiceUtils.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpWebServiceUtils.java index f71dae3..a537538 100644 --- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpWebServiceUtils.java +++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpWebServiceUtils.java @@ -1,3 +1,20 @@ +/* + * 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.marmotta.platform.ldp.util; import info.aduna.iteration.CloseableIteration; http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/PATCH.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/PATCH.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/PATCH.java index 0ccc099..d9b65b0 100644 --- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/PATCH.java +++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/PATCH.java @@ -1,3 +1,20 @@ +/* + * 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.marmotta.platform.ldp.webservices; @java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD}) http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/main/javacc/rdf-patch.jj ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/javacc/rdf-patch.jj b/platform/marmotta-ldp/src/main/javacc/rdf-patch.jj index a8a685f..93047cf 100644 --- a/platform/marmotta-ldp/src/main/javacc/rdf-patch.jj +++ b/platform/marmotta-ldp/src/main/javacc/rdf-patch.jj @@ -16,18 +16,18 @@ options { STATIC=false; - LOOKAHEAD=150; + LOOKAHEAD=1; CACHE_TOKENS=true; // FORCE_LA_CHECK=true; // CHOICE_AMBIGUITY_CHECK=5; -// LOOKAHEAD=2147483647; -// DEBUG_PARSER=true; -// DEBUG_TOKEN_MANAGER=true; -// DEBUG_LOOKAHEAD=true; + //LOOKAHEAD=2147483647; + //DEBUG_PARSER=true; + //DEBUG_TOKEN_MANAGER=true; + //DEBUG_LOOKAHEAD=true; } PARSER_BEGIN(RdfPatchParser) -package org.apache.marmotta.platform.ldp.patch; +package org.apache.marmotta.platform.ldp.patch.parser; import org.openrdf.model.*; import org.openrdf.model.impl.*; @@ -56,7 +56,7 @@ public class RdfPatchParser { } private Literal createLiteral(String value, String lang, String type) { - value = TurtleUtil.decodeString(value.substring(1, value.length() -2)); + value = TurtleUtil.decodeString(value.substring(1, value.length() - 1)); if (lang != null) { return new LiteralImpl(value, lang); } else if (type != null) { @@ -67,7 +67,7 @@ public class RdfPatchParser { } private Literal createLongLiteral(String value, String lang, String type) { - value = value.substring(2, value.length() - 4); + value = value.substring(2, value.length() - 2); return createLiteral(value, lang, type); } @@ -114,8 +114,7 @@ MORE: { } TOKEN : { - < ADD: "A" > | - < DEL: "D" > | + < OP: ["A","D"] > | < REPEAT: "R" > | < UNDEFINED: "U" > | < PREFIX: "@prefix" > | @@ -135,25 +134,25 @@ SPECIAL_TOKEN : { public List<PatchLine> parsePatch() : { - Token id, prefix; + Token id, prefix, op; Statement statement; LinkedList<PatchLine> diff = new LinkedList<PatchLine>(); namespaces.clear(); } { - ( <PREFIX> id = <IDENT> <COLON> prefix = <URI> <DOT> { namespaces.put(id.image, unwrapUri(prefix.image));} )* {} + ( <PREFIX> id = <IDENT> <COLON> prefix = <URI> <DOT> { namespaces.put(id.image, unwrapUri(prefix.image));} )* ( - ( <ADD> statement = parseStatement() <DOT> { diff.add(new PatchLine(PatchLine.Operator.ADD, statement)); } ) | - ( <DEL> statement = parseStatement() <DOT> { diff.add(new PatchLine(PatchLine.Operator.DEL, statement)); } ) + op = <OP> statement = parseStatement() <DOT> { diff.add(new PatchLine(PatchLine.Operator.fromCommand(op.image), statement)); } )* <EOF> { - return diff; + return diff; } } private Statement parseStatement() : { Token t1 = null, t2 = null, t3 = null; + Token lVal = null, lLang = null, lType = null; Resource subject; URI predicate; Value object; @@ -165,17 +164,19 @@ private Statement parseStatement() : { <REPEAT> { subject = null; } ) ( t1 = <URI> { predicate = createURI(t1.image); } | + t1 = <IDENT> <COLON> t2 = <IDENT> { predicate = createURI(t1.image, t2.image); } | <REPEAT> { predicate = null; } ) ( t1 = <URI> { object = createURI(t1.image); } | + t1 = <IDENT> <COLON> t2 = <IDENT> { object = createURI(t1.image, t2.image); } | <BNODE> t1 = <IDENT> { object = createBNode(t1.image); } | - t1 = <STRLIT> (<LANG> t2 = <IDENT>)? (<TYPE> t3 = <URI>)? { - object = createLiteral(t1.image, t2!=null?t2.image:null, t3!=null?t3.image:null); + lVal = <STRLIT> (<LANG> lLang = <IDENT>)? (<TYPE> lType = <URI>)? { + object = createLiteral(lVal.image, lLang!=null?lLang.image:null, lType!=null?lType.image:null); } | - t1 = <STRLONGLIT> (<LANG> t2 = <IDENT>)? (<TYPE> t3 = <URI>)? { - object = createLongLiteral(t1.image, t2!=null?t2.image:null, t3!=null?t3.image:null); + lVal = <STRLONGLIT> (<LANG> lLang = <IDENT>)? (<TYPE> lType = <URI>)? { + object = createLongLiteral(lVal.image, lLang!=null?lLang.image:null, lType!=null?lType.image:null); } | <REPEAT> { object = null; } ) - { return new StatementImpl(subject, predicate, object); } + { return new WildcardStatement(subject, predicate, object); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchParserTest.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchParserTest.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchParserTest.java deleted file mode 100644 index 6c63013..0000000 --- a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchParserTest.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * 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.marmotta.platform.ldp.patch; - -import org.apache.marmotta.commons.vocabulary.FOAF; -import org.apache.marmotta.platform.ldp.patch.model.PatchLine; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.openrdf.model.*; -import org.openrdf.model.impl.LiteralImpl; -import org.openrdf.model.impl.URIImpl; - -import java.util.Iterator; -import java.util.List; - -/** - * Created by jakob on 2/24/14. - */ -public class RdfPatchParserTest { - - - private RdfPatchParser parser; - private URI alice, bob, charlie; - private Literal lcBob, ucBob; - - @Before - public void setUp() { - parser = new RdfPatchParser(this.getClass().getResourceAsStream("/illustrative.rdfp")); - - alice = new URIImpl("http://example/alice"); - bob = new URIImpl("http://example/bob"); - charlie = new URIImpl("http://example/charlie"); - - lcBob = new LiteralImpl("bob"); - ucBob = new LiteralImpl("Bob"); - } - - @After - public void tearDown() { - parser = null; - } - - - @Test - public void testParsing() throws ParseException { - List<PatchLine> patchLines = parser.parsePatch(); - - Iterator<PatchLine> it = patchLines.iterator(); - - Assert.assertTrue(it.hasNext()); - checkPatchLine(it.next(), PatchLine.Operator.DEL, bob, FOAF.name, lcBob); - - Assert.assertTrue(it.hasNext()); - checkPatchLine(it.next(), PatchLine.Operator.ADD, null, FOAF.name, lcBob); - - Assert.assertTrue(it.hasNext()); - checkPatchLine(it.next(), PatchLine.Operator.ADD, null, FOAF.knows, alice); - - Assert.assertTrue(it.hasNext()); - checkPatchLine(it.next(), PatchLine.Operator.DEL, null, null, charlie); - } - - private void checkPatchLine(PatchLine line, PatchLine.Operator operator, Resource subejct, URI predicate, Value object) { - Assert.assertEquals("Wrong patch operation", operator, line.getOperator()); - - Statement statement = line.getStatement(); - Assert.assertEquals("Wrong subject", subejct, statement.getSubject()); - Assert.assertEquals("Wrong predicate", predicate, statement.getPredicate()); - Assert.assertEquals("Wrong object", object, statement.getObject()); - } - -} http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java new file mode 100644 index 0000000..ba55337 --- /dev/null +++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java @@ -0,0 +1,98 @@ +/* + * 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.marmotta.platform.ldp.patch; + +import org.apache.marmotta.commons.vocabulary.FOAF; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openrdf.model.Literal; +import org.openrdf.model.URI; +import org.openrdf.model.impl.LiteralImpl; +import org.openrdf.model.impl.URIImpl; +import org.openrdf.repository.Repository; +import org.openrdf.repository.RepositoryConnection; +import org.openrdf.repository.sail.SailRepository; +import org.openrdf.rio.RDFFormat; +import org.openrdf.sail.memory.MemoryStore; + +/** + * Created by jakob on 2/25/14. + */ +public class RdfPatchUtilTest { + + public static final String BASE_URI = "http://example/"; + + private Repository repository; + private URI alice, bob, charlie; + private Literal lcBob, ucBob; + + @Before + public void setUp() throws Exception { + repository = new SailRepository(new MemoryStore()); + repository.initialize(); + + alice = repository.getValueFactory().createURI("http://example/alice"); + bob = repository.getValueFactory().createURI("http://example/bob"); + charlie = repository.getValueFactory().createURI("http://example/charlie"); + + lcBob = repository.getValueFactory().createLiteral("bob"); + ucBob = repository.getValueFactory().createLiteral("Bob"); + + RepositoryConnection con = repository.getConnection(); + try { + con.begin(); + con.add(this.getClass().getResourceAsStream("/illustrative.in.ttl"), BASE_URI, RDFFormat.TURTLE); + con.commit(); + } finally { + con.close(); + } + } + + @After + public void tearDown() throws Exception { + if (repository != null) { + repository.shutDown(); + } + } + + @Test + public void testApplyPatch() throws Exception { + RepositoryConnection con = repository.getConnection(); + try { + con.begin(); + + Assert.assertTrue(con.hasStatement(bob, FOAF.name, lcBob, false)); + Assert.assertFalse(con.hasStatement(bob, FOAF.name, ucBob, false)); + Assert.assertFalse(con.hasStatement(bob, FOAF.knows, alice, false)); + Assert.assertTrue(con.hasStatement(bob, FOAF.knows, charlie, false)); + + RdfPatchUtil.applyPatch(con, this.getClass().getResourceAsStream("/illustrative.rdfp")); + + Assert.assertFalse(con.hasStatement(bob, FOAF.name, lcBob, false)); + Assert.assertTrue(con.hasStatement(bob, FOAF.name, ucBob, false)); + Assert.assertTrue(con.hasStatement(bob, FOAF.knows, alice, false)); + Assert.assertFalse(con.hasStatement(bob, FOAF.knows, charlie, false)); + + con.commit(); + } finally { + con.close(); + } + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java new file mode 100644 index 0000000..6ebcc58 --- /dev/null +++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java @@ -0,0 +1,89 @@ +/* + * 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.marmotta.platform.ldp.patch.parser; + +import org.apache.marmotta.commons.vocabulary.FOAF; +import org.apache.marmotta.platform.ldp.patch.model.PatchLine; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openrdf.model.*; +import org.openrdf.model.impl.LiteralImpl; +import org.openrdf.model.impl.URIImpl; + +import java.util.Iterator; +import java.util.List; + +/** + * Created by jakob on 2/24/14. + */ +public class RdfPatchParserTest { + + + private RdfPatchParser parser; + private URI alice, bob, charlie; + private Literal lcBob, ucBob; + + @Before + public void setUp() { + parser = new RdfPatchParser(this.getClass().getResourceAsStream("/illustrative.rdfp")); + + alice = new URIImpl("http://example/alice"); + bob = new URIImpl("http://example/bob"); + charlie = new URIImpl("http://example/charlie"); + + lcBob = new LiteralImpl("bob"); + ucBob = new LiteralImpl("Bob"); + } + + @After + public void tearDown() { + parser = null; + } + + + @Test + public void testParsing() throws ParseException { + List<PatchLine> patchLines = parser.parsePatch(); + + Iterator<PatchLine> it = patchLines.iterator(); + + Assert.assertTrue(it.hasNext()); + checkPatchLine(it.next(), PatchLine.Operator.DEL, bob, FOAF.name, lcBob); + + Assert.assertTrue(it.hasNext()); + checkPatchLine(it.next(), PatchLine.Operator.ADD, bob, FOAF.name, ucBob); + + Assert.assertTrue(it.hasNext()); + checkPatchLine(it.next(), PatchLine.Operator.ADD, null, FOAF.knows, alice); + + Assert.assertTrue(it.hasNext()); + checkPatchLine(it.next(), PatchLine.Operator.DEL, null, null, charlie); + } + + private void checkPatchLine(PatchLine line, PatchLine.Operator operator, Resource subejct, URI predicate, Value object) { + Assert.assertEquals("Wrong patch operation", operator, line.getOperator()); + + Statement statement = line.getStatement(); + Assert.assertEquals("Wrong subject", subejct, statement.getSubject()); + Assert.assertEquals("Wrong predicate", predicate, statement.getPredicate()); + Assert.assertEquals("Wrong object", object, statement.getObject()); + } + +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/test/resources/illustrative.in.ttl ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/test/resources/illustrative.in.ttl b/platform/marmotta-ldp/src/test/resources/illustrative.in.ttl index be04c27..d52213a 100644 --- a/platform/marmotta-ldp/src/test/resources/illustrative.in.ttl +++ b/platform/marmotta-ldp/src/test/resources/illustrative.in.ttl @@ -1,3 +1,20 @@ +# +# 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. +# @prefix foaf: <http://xmlns.com/foaf/0.1/> . <http://example/bob> a foaf:Person; http://git-wip-us.apache.org/repos/asf/marmotta/blob/e66e59d7/platform/marmotta-ldp/src/test/resources/illustrative.rdfp ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/test/resources/illustrative.rdfp b/platform/marmotta-ldp/src/test/resources/illustrative.rdfp index 69ac715..e5c0ab9 100644 --- a/platform/marmotta-ldp/src/test/resources/illustrative.rdfp +++ b/platform/marmotta-ldp/src/test/resources/illustrative.rdfp @@ -1,6 +1,25 @@ +# +# 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. +# @prefix foaf: <http://xmlns.com/foaf/0.1/> . - +# Fix Bobs name D <http://example/bob> foaf:name "bob" . A <http://example/bob> foaf:name "Bob" . +# At ApacheCon EU, Bob met Alice A R foaf:knows <http://example/alice> . +# Bob and Charlie had a fight D R R <http://example/charlie> .
