MARMOTTA-364: Started with some unit-tests... Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/1bb1783d Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/1bb1783d Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/1bb1783d
Branch: refs/heads/develop Commit: 1bb1783d1aa9993cd87341dc16c6466a337cd872 Parents: c75aa1a Author: Jakob Frank <[email protected]> Authored: Tue Nov 26 17:16:32 2013 +0100 Committer: Jakob Frank <[email protected]> Committed: Wed Nov 27 13:29:08 2013 +0100 ---------------------------------------------------------------------- commons/sesame-contextaware/pom.xml | 5 + .../commons/sesame/AbstractContextTest.java | 94 +++++++++++++ .../contextaware/ContextAwareSailTest.java | 138 +++++++++++++++++++ 3 files changed, 237 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/1bb1783d/commons/sesame-contextaware/pom.xml ---------------------------------------------------------------------- diff --git a/commons/sesame-contextaware/pom.xml b/commons/sesame-contextaware/pom.xml index b727e02..cd6900e 100644 --- a/commons/sesame-contextaware/pom.xml +++ b/commons/sesame-contextaware/pom.xml @@ -45,5 +45,10 @@ <groupId>org.openrdf.sesame</groupId> <artifactId>sesame-sail-api</artifactId> </dependency> + <dependency> + <groupId>org.openrdf.sesame</groupId> + <artifactId>sesame-sail-memory</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/marmotta/blob/1bb1783d/commons/sesame-contextaware/src/test/java/org/apache/marmotta/commons/sesame/AbstractContextTest.java ---------------------------------------------------------------------- diff --git a/commons/sesame-contextaware/src/test/java/org/apache/marmotta/commons/sesame/AbstractContextTest.java b/commons/sesame-contextaware/src/test/java/org/apache/marmotta/commons/sesame/AbstractContextTest.java new file mode 100644 index 0000000..8fd1404 --- /dev/null +++ b/commons/sesame-contextaware/src/test/java/org/apache/marmotta/commons/sesame/AbstractContextTest.java @@ -0,0 +1,94 @@ +/* + * 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.commons.sesame; + +import java.util.UUID; + +import org.junit.After; +import org.junit.Before; +import org.openrdf.model.Literal; +import org.openrdf.model.Resource; +import org.openrdf.model.URI; +import org.openrdf.model.ValueFactory; +import org.openrdf.sail.Sail; +import org.openrdf.sail.SailConnection; +import org.openrdf.sail.SailException; +import org.openrdf.sail.memory.MemoryStore; + +public abstract class AbstractContextTest { + + protected static final String NS = "http://test.marmotta.apache.org/resource/"; + + protected Sail sail; + + protected URI u1, u2, u3, u4; + protected URI p1, p2, p3, p4; + protected Resource c1, c2; + protected Literal l1, l2, l3, l4; + + @Before + public void setUp() throws SailException { + sail = new MemoryStore(); + sail.initialize(); + + final ValueFactory vf = sail.getValueFactory(); + u1 = vf.createURI(NS, UUID.randomUUID().toString()); + u2 = vf.createURI(NS, UUID.randomUUID().toString()); + u3 = vf.createURI(NS, UUID.randomUUID().toString()); + u4 = vf.createURI(NS, UUID.randomUUID().toString()); + + p1 = vf.createURI(NS, UUID.randomUUID().toString()); + p2 = vf.createURI(NS, UUID.randomUUID().toString()); + p3 = vf.createURI(NS, UUID.randomUUID().toString()); + p4 = vf.createURI(NS, UUID.randomUUID().toString()); + + c1 = vf.createURI(NS, UUID.randomUUID().toString()); + c2 = vf.createBNode(); + + l1 = vf.createLiteral(UUID.randomUUID().toString()); + l2 = vf.createLiteral(UUID.randomUUID().toString()); + l3 = vf.createLiteral(UUID.randomUUID().toString()); + l4 = vf.createLiteral(UUID.randomUUID().toString()); + + final SailConnection con = sail.getConnection(); + try { + con.begin(); + + con.addStatement(u1, p1, l1, c1); + con.addStatement(u2, p2, l2, c2); + con.addStatement(u3, p3, l3, c1); + con.addStatement(u4, p4, l4, c2); + + con.commit(); + } catch (final Throwable t) { + con.rollback(); + throw t; + } finally { + con.close(); + } + } + + @After + public void tearDown() throws SailException { + if (sail != null) { + sail.shutDown(); + } + sail = null; + } + +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/1bb1783d/commons/sesame-contextaware/src/test/java/org/apache/marmotta/commons/sesame/contextaware/ContextAwareSailTest.java ---------------------------------------------------------------------- diff --git a/commons/sesame-contextaware/src/test/java/org/apache/marmotta/commons/sesame/contextaware/ContextAwareSailTest.java b/commons/sesame-contextaware/src/test/java/org/apache/marmotta/commons/sesame/contextaware/ContextAwareSailTest.java new file mode 100644 index 0000000..77ceca6 --- /dev/null +++ b/commons/sesame-contextaware/src/test/java/org/apache/marmotta/commons/sesame/contextaware/ContextAwareSailTest.java @@ -0,0 +1,138 @@ +/* + * 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.commons.sesame.contextaware; + +import info.aduna.iteration.CloseableIteration; + +import org.apache.marmotta.commons.sesame.AbstractContextTest; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openrdf.model.Resource; +import org.openrdf.model.Statement; +import org.openrdf.model.URI; +import org.openrdf.model.Value; +import org.openrdf.sail.SailConnection; +import org.openrdf.sail.SailException; + +/** + * @author jakob + * + */ +public class ContextAwareSailTest extends AbstractContextTest { + + protected ContextAwareSail cas; + + @Override + @Before + public void setUp() throws SailException { + super.setUp(); + + cas = new ContextAwareSail(sail, c1); + } + + @Override + @After + public void tearDown() throws SailException { + super.tearDown(); + } + + + @Test + public void testGetStatements() throws SailException { + final SailConnection con = cas.getConnection(); + try { + con.begin(); + + Assert.assertTrue(hasStatement(con, u1, p1, l1)); + Assert.assertFalse(hasStatement(con, u2, p2, l2)); + + con.commit(); + } catch (final Throwable t) { + con.rollback(); + throw t; + } finally { + con.close(); + } + } + + @Test + public void testAddStatement() throws SailException { + final SailConnection con = cas.getConnection(); + try { + con.begin(); + con.addStatement(u1, p2, l3); + con.commit(); + } catch (final Throwable t) { + con.rollback(); + throw t; + } finally { + con.close(); + } + final SailConnection con2 = sail.getConnection(); + try { + con2.begin(); + + Assert.assertTrue(hasStatement(con2, u1, p2, l3, c1)); + Assert.assertFalse(hasStatement(con2, u1, p2, l3, c2)); + + con2.commit(); + } catch (final Throwable t) { + con2.rollback(); + throw t; + } finally { + con2.close(); + } + } + + @Test + public void testGetContextIDs() throws SailException { + final SailConnection con = cas.getConnection(); + try { + con.begin(); + + final CloseableIteration<? extends Resource, SailException> cid = con.getContextIDs(); + try { + Assert.assertTrue(cid.hasNext()); + Assert.assertThat(cid.next(), CoreMatchers.is(c1)); + Assert.assertFalse(cid.hasNext()); + } finally { + cid.close(); + } + + con.commit(); + } catch (final Throwable t) { + con.rollback(); + throw t; + } finally { + con.close(); + } + } + + protected static boolean hasStatement(SailConnection con, Resource subj, URI pred, Value object, Resource... contexts) throws SailException { + final CloseableIteration<? extends Statement, SailException> stmts = con.getStatements(subj, pred, object, true, contexts); + try { + return stmts.hasNext(); + } finally { + stmts.close(); + } + } + +}
