You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by en...@apache.org on 2014/03/16 19:51:36 UTC
[2/5] CLEREZZA-810 Packages renamed
http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/ThreadSafetyTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/ThreadSafetyTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/ThreadSafetyTest.java
new file mode 100644
index 0000000..fb418ab
--- /dev/null
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/ThreadSafetyTest.java
@@ -0,0 +1,341 @@
+/*
+ * 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.clerezza.rdf.virtuoso.storage;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.clerezza.rdf.virtuoso.storage.VirtuosoMGraph;
+import org.apache.clerezza.rdf.virtuoso.storage.access.DataAccess;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ *
+ * Credit: This code is largely cloned by the thread safe test in
+ * {@see org.apache.clerezza.rdf.sesame.storage.ThreadSafetyTest}
+ *
+ * @author enridaga
+ *
+ */
+public class ThreadSafetyTest {
+
+ private ExecutorService executor;
+ private VirtuosoMGraph mgraph;
+ private DataAccess da = null;
+ static Logger log = LoggerFactory.getLogger(ThreadSafetyTest.class);
+
+ @BeforeClass
+ public static void assume(){
+ org.junit.Assume.assumeTrue(!TestUtils.SKIP);
+ }
+
+ @Before
+ public void setUp() throws Exception {
+
+ da = TestUtils.getProvider().createDataAccess();
+ da.clearGraph("ThreadSafetyTest");
+ mgraph = new VirtuosoMGraph("ThreadSafetyTest",
+ da);
+ mgraph.clear();
+ executor = Executors.newCachedThreadPool();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ try {
+ executor.shutdown();
+ if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
+ fail("Timeout while waiting for termination");
+ }
+ } finally {
+ mgraph.clear();
+ mgraph = null;
+ da.close();
+ da = null;
+ }
+ }
+
+ @Test
+ public void testProduceFirstAndThenConsume() throws Exception {
+ log.info("testProduceFirstAndThenConsume()");
+ // Produce first...
+ Future<Result> fp = executor.submit(new Producer("A", 100));
+ fp.get().assertResult();
+
+ // ...and then consume
+ Future<Result> fc = executor.submit(new Consumer("A", 100));
+ fc.get().assertResult();
+ Iterator<Triple> it = mgraph.iterator();
+ while (it.hasNext()) {
+ TestUtils.stamp(it.next());
+ }
+ // test graph size
+ assertEquals(0, mgraph.size());
+ }
+
+ @Test
+ public void testProduceAndConsumeSingle() throws Exception {
+ log.info("testProduceAndConsumeSingle()");
+ List<Task> tasks = Arrays.asList(
+ new Consumer("A", 100), new Producer("A", 100));
+ List<Future<Result>> futures = executor.invokeAll(tasks);
+ for (Future<Result> future : futures) {
+ future.get().assertResult();
+ }
+ assertEquals(0, mgraph.size());
+ }
+
+ @Test
+ public void testProduceAndConsumeMultiple() throws Exception {
+ log.info("testProduceAndConsumeMultiple()");
+ List<Task> tasks = Arrays.asList(
+ new Consumer("A", 100), new Producer("A", 100),
+ new Consumer("B", 100), new Producer("B", 100),
+ new Consumer("C", 100), new Producer("C", 100),
+ new Consumer("D", 100), new Producer("D", 100));
+ List<Future<Result>> futures = executor.invokeAll(tasks);
+ for (Future<Result> future : futures) {
+ future.get().assertResult();
+ }
+ assertEquals(0, mgraph.size());
+ }
+
+ @Test
+ public void testProduceAndConsumeMixed() throws Exception {
+ log.info("testProduceAndConsumeMixed()");
+ List<? extends Task> tasks = Arrays.asList(
+ new Consumer("A", 110), new Consumer("A", 170),
+ new Consumer("B", 100), new Consumer("B", 500),
+ new Consumer("C", 230), new Consumer("C", 230),
+ new Consumer("D", 540), new Consumer("D", 10),
+ new Producer("D", 50), new Producer("D", 500),
+ new Producer("C", 400), new Producer("C", 60),
+ new Producer("B", 300), new Producer("B", 300),
+ new Producer("A", 200), new Producer("A", 80));
+ List<Future<Result>> futures = executor.invokeAll(tasks);
+ for (Future<Result> future : futures) {
+ future.get().assertResult();
+ }
+ assertEquals(0, mgraph.size());
+ }
+ /**
+ * The <code>Task</code> specifies a <code>Callable</code> whoes execution
+ * results in an <code>Integer</code>.
+ */
+ private abstract class Task implements Callable<Result> {
+
+ protected final String collectionName;
+ protected final UriRef subject;
+ protected final UriRef predicate;
+ protected final int numElements;
+
+ /**
+ * Creates a new <code>Task</code>.
+ *
+ * @param collectionName
+ * Name of the task's collection.
+ * @param numElements
+ * The number of elements to process.
+ */
+ protected Task(String collectionName, int numElements) {
+ if (collectionName == null) {
+ throw new IllegalArgumentException("Invalid name: null");
+ } else if (collectionName.length() <= 0) {
+ throw new IllegalArgumentException("Invalid name: '"
+ + collectionName + "'");
+ } else if (numElements < 0) {
+ throw new IllegalArgumentException("Invalid numElements: "
+ + numElements);
+ }
+ this.numElements = numElements;
+ this.collectionName = collectionName;
+ subject = new UriRef("http://example.org/" + "collection/"
+ + collectionName);
+ predicate = new UriRef("http://example.org/ontology/contains");
+ }
+
+ /**
+ * Causes the currently executing thread object to temporarily pause and
+ * allow other threads to execute.
+ */
+ protected void yield() {
+ try {
+ Thread.sleep(5);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
+
+ /**
+ * This class represents a task that produces triples that are stored in the
+ * graph.
+ */
+ private class Producer extends Task {
+
+ /**
+ * Creates a new <code>Producer</code> task.
+ *
+ * @param collectionName
+ * Name of the task's collection.
+ * @param numElements
+ * The number of elements to produce.
+ */
+ public Producer(String collectionName, int numElements) {
+ super(collectionName, numElements);
+ }
+
+ @Override
+ public Result call() throws InterruptedException {
+ int counter = 0;
+ for (int elementName = 1; counter < numElements; elementName++) {
+ yield();
+ final Triple t = createTriple(elementName);
+ if (mgraph.add(t)) {
+ counter++;
+ yield();
+ } else {
+ System.out.println("WARNING: element " + t + "NOT created");
+ }
+ }
+ return new Result(collectionName, "Produced elements", numElements,
+ counter);
+ }
+
+ /**
+ * Creates a new collection element triple.
+ *
+ * @param value
+ * Value of the collection element.
+ * @return A new triple representing the collection element.
+ */
+ protected Triple createTriple(int value) {
+ final UriRef object = new UriRef("http://example.org/"
+ + collectionName + "/" + Integer.valueOf(value)
+ + Math.random());
+ return new TripleImpl(subject, predicate, object);
+ }
+ }
+
+ /**
+ * This class represents a task that produces triples that are stored in the
+ * graph.
+ */
+ private class Consumer extends Task {
+
+ /**
+ * Creates a new <code>Consumer</code> task.
+ *
+ * @param collectionName
+ * Name of the task's collection.
+ * @param numElements
+ * The number of elements to consume.
+ */
+ public Consumer(String collectionName, int numElements) {
+ super(collectionName, numElements);
+ }
+
+ /**
+ * Performs this task.
+ *
+ * @return the number of elements successfully added to the graph.
+ */
+ @Override
+ public Result call() throws InterruptedException {
+ int counter = 0;
+ while (counter < numElements) {
+ yield();
+
+ Triple triple = null;
+ mgraph.getLock().writeLock().lock();
+ try {
+ // System.out.println("synchonized");
+ Iterator<Triple> i = mgraph
+ .filter(subject, predicate, null);
+ if (i.hasNext()) {
+ triple = i.next();
+ }
+
+ if (triple != null && mgraph.remove(triple)) {
+ counter++;
+ }
+ } finally {
+ mgraph.getLock().writeLock().unlock();
+ }
+ }
+ return new Result(collectionName, "Consumed elements", numElements,
+ counter);
+ }
+ }
+
+ /**
+ * Task result that asserts the number of processed elements.
+ */
+ private class Result {
+
+ private final int expected;
+ private final int actual;
+ private final String msg;
+ private final String cn;
+
+ /**
+ * Creates a new task result that asserts the element count.
+ *
+ * @param cn
+ * Name of the affected collection.
+ * @param msg
+ * Assertion message to print.
+ * @param expected
+ * Expected number of processed elements.
+ * @param actual
+ * Actual number of processed elements.
+ */
+ public Result(String cn, String msg, int expected, int actual) {
+ this.expected = expected;
+ this.actual = actual;
+ this.msg = msg;
+ this.cn = cn;
+ }
+
+ /**
+ * Asserts this result.
+ */
+ public void assertResult() {
+ assertEquals("[" + cn + "] " + msg, expected, actual);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/VirtuosoGraphTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/VirtuosoGraphTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/VirtuosoGraphTest.java
new file mode 100644
index 0000000..2280a3f
--- /dev/null
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/VirtuosoGraphTest.java
@@ -0,0 +1,32 @@
+/*
+ * 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.clerezza.rdf.virtuoso.storage;
+
+import org.apache.clerezza.rdf.core.Graph;
+import org.apache.clerezza.rdf.virtuoso.storage.VirtuosoGraph;
+
+/**
+ * Tests the {@link VirtuosoGraph} implementation of {@link Graph}
+ *
+ * @author enridaga
+ *
+ */
+public class VirtuosoGraphTest {
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/VirtuosoMGraphTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/VirtuosoMGraphTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/VirtuosoMGraphTest.java
new file mode 100644
index 0000000..0d7b1ec
--- /dev/null
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/VirtuosoMGraphTest.java
@@ -0,0 +1,526 @@
+/*
+ * 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.clerezza.rdf.virtuoso.storage;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Iterator;
+
+import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.rdf.core.Graph;
+import org.apache.clerezza.rdf.core.Language;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.clerezza.rdf.core.PlainLiteral;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.TypedLiteral;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
+import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
+import org.apache.clerezza.rdf.virtuoso.storage.VirtuosoMGraph;
+import org.apache.clerezza.rdf.virtuoso.storage.access.DataAccess;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import virtuoso.jdbc4.VirtuosoException;
+
+public class VirtuosoMGraphTest {
+ static VirtuosoMGraph mgraph = null;
+ static DataAccess da = null;
+ static final String TEST_GRAPH_NAME = "VirtuosoMGraphTest";
+
+ static Logger log = LoggerFactory.getLogger(VirtuosoMGraphTest.class);
+
+ @BeforeClass
+ public static void assume(){
+ org.junit.Assume.assumeTrue(!TestUtils.SKIP);
+ }
+
+ /**
+ * Clean before any test
+ *
+ * @throws ClassNotFoundException
+ * @throws SQLException
+ */
+ @Before
+ public void before() throws ClassNotFoundException, SQLException {
+
+ da = TestUtils.getProvider().createDataAccess();
+ mgraph = new VirtuosoMGraph(TEST_GRAPH_NAME, da);
+ mgraph.clear();
+ log.debug("Clearing graph <{}>", TEST_GRAPH_NAME);
+ }
+
+ final UriRef enridaga = new UriRef("enridaga");
+ final UriRef alexdma = new UriRef("alexdma");
+ final UriRef anuzzolese = new UriRef("anuzzolese");
+ final UriRef predicate = new UriRef("http://property/name");
+ final PlainLiteral object = new PlainLiteralImpl("Enrico Daga", new Language("it"));
+ final TypedLiteral objectTyped = new TypedLiteralImpl("Enrico Daga", new UriRef("http://www.w3.org/2001/XMLSchema#string"));
+ final TypedLiteral objectXml = new TypedLiteralImpl("<div>Enrico Daga</div>" ,
+ new UriRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"));
+ final UriRef knows = new UriRef(TestUtils.FOAF_NS + "knows");
+
+ @Test
+ public void testAddSingle() {
+ log.info("testAddSingle()");
+ Triple triple = new Triple() {
+
+ @Override
+ public NonLiteral getSubject() {
+ return enridaga;
+ }
+
+ @Override
+ public UriRef getPredicate() {
+ return predicate;
+ }
+
+ @Override
+ public Resource getObject() {
+ return object;
+ }
+ };
+ boolean success = mgraph.add(triple);
+ assertTrue(success);
+ assertTrue(mgraph.size() == 1);
+ assertTrue(mgraph.filter(enridaga, predicate, object).hasNext());
+ assertTrue(mgraph.filter(enridaga, predicate, object).next().equals(triple));
+ }
+
+ @Test
+ public void testAddSingleTypedLiteral() {
+ log.info("testAddSingleTypedLiteral()");
+ Triple triple = new Triple() {
+
+ @Override
+ public NonLiteral getSubject() {
+ return enridaga;
+ }
+
+ @Override
+ public UriRef getPredicate() {
+ return predicate;
+ }
+
+ @Override
+ public Resource getObject() {
+ return objectTyped;
+ }
+ };
+ boolean success = mgraph.add(triple);
+ assertTrue(success);
+ assertTrue(mgraph.size() == 1);
+ assertTrue(mgraph.filter(enridaga, predicate, objectTyped).hasNext());
+ assertTrue(mgraph.filter(enridaga, predicate, objectTyped).next().equals(triple));
+ }
+
+ @Ignore
+ @Test
+ public void testAddSingleXMLLiteral() {
+ log.info("testAddSingleXMLLiteral()");
+ Triple triple = new Triple() {
+
+ @Override
+ public NonLiteral getSubject() {
+ return enridaga;
+ }
+
+ @Override
+ public UriRef getPredicate() {
+ return predicate;
+ }
+
+ @Override
+ public Resource getObject() {
+ return objectXml;
+ }
+ };
+ boolean success = mgraph.add(triple);
+ assertTrue(success);
+ assertTrue(mgraph.size() == 1);
+ Triple rt = mgraph.filter(enridaga, predicate, null).next();
+ log.info(" > s: {} ", rt.getSubject());
+ log.info(" > p: {} ", rt.getPredicate());
+ log.info(" > o: {} ", rt.getObject());
+ log.info(" > tl?: {} ", rt.getObject() instanceof TypedLiteral);
+ assertTrue(mgraph.filter(enridaga, predicate, objectXml).hasNext());
+ assertTrue(mgraph.filter(enridaga, predicate, objectXml).next().equals(triple));
+ }
+
+
+ @Test
+ public void testFilter() {
+ log.info("testFilter(); Test filter(s,p,o)");
+ // We use testAdd to prepare this
+ testAddSingle();
+
+ Iterator<Triple> its = mgraph.filter(null, null, null);
+ while (its.hasNext()) {
+ Triple t = its.next();
+ log.info("Found --- triple: {}", t);
+ log.info("Found --- s: {} {}", t.getSubject(), t.getSubject().getClass());
+ log.info("Found --- p: {} {}", t.getPredicate(), t.getPredicate().getClass());
+ log.info("Found --- o: {} {}", t.getObject(), t.getObject().getClass());
+ assertEquals(t.getSubject(), enridaga);
+ assertEquals(t.getPredicate(), predicate);
+ assertEquals(t.getObject(), object);
+ }
+
+ Iterator<Triple> it = mgraph.filter(enridaga, predicate, object);
+ boolean found = false;
+ while (it.hasNext()) {
+ found = true;
+ Triple t = it.next();
+ log.debug("Found matching triple: {}", t);
+ assertEquals(t.getSubject(), enridaga);
+ assertEquals(t.getPredicate(), predicate);
+ assertEquals(t.getObject(), object);
+ }
+ assertTrue(found);
+ }
+
+ @Test
+ public void testFilterSubject() {
+ log.info("testFilterSubject(); Test filter(s,null,null)");
+ // We use testAdd to prepare this
+ testAddSingle();
+ Iterator<Triple> it = mgraph.filter(enridaga, null, null);
+ boolean found = false;
+ while (it.hasNext()) {
+ found = true;
+ Triple t = it.next();
+ if (log.isDebugEnabled()) {
+ log.debug("Found matching triple: {}", t);
+ TestUtils.stamp(t);
+ }
+ assertEquals(t.getSubject(), enridaga);
+ }
+ assertTrue(found);
+ }
+
+
+ @Test
+ public void testFilterSubjectBnode() throws VirtuosoException, SQLException, ClassNotFoundException {
+ log.info("testFilterSubjectBnode(); Test filter(s,null,null)");
+ final BNode bn = new BNode();
+ // We use testAdd to prepare this
+ Triple triple = new Triple() {
+
+ @Override
+ public NonLiteral getSubject() {
+ return bn;
+ }
+
+ @Override
+ public UriRef getPredicate() {
+ return predicate;
+ }
+
+ @Override
+ public Resource getObject() {
+ return new BNode();
+ }
+ };
+
+ boolean success = mgraph.add(triple);
+ assertTrue(success);
+ Iterator<Triple> it = mgraph.filter(bn, predicate, null);
+ boolean found = false;
+ Triple t = null; // we will use it to make a further query
+ while (it.hasNext()) {
+ found = true;
+ t = it.next();
+ if (log.isDebugEnabled()) {
+ log.debug("Found matching triple: {}", t);
+ TestUtils.stamp(t);
+ }
+ assertEquals(t.getPredicate(), predicate);
+ }
+ assertTrue(found);
+
+ assertNotNull(t);
+
+ log.info("{}",t.getSubject());
+ it = mgraph.filter(t.getSubject(), predicate, t.getObject());
+ found = false;
+ while (it.hasNext()) {
+ found = true;
+ t = it.next();
+ if (log.isDebugEnabled()) {
+ log.debug("Found matching triple: {}", t);
+ TestUtils.stamp(t);
+ }
+ assertEquals(t.getPredicate(), predicate);
+ }
+ assertTrue(found);
+ }
+
+ @Test
+ public void testFilterPredicate() {
+ log.info("testFilterPredicate(); Test filter(null,p,null)");
+ // We use testAdd to prepare this
+ testAddSingle();
+ Iterator<Triple> it = mgraph.filter(null, predicate, null);
+ boolean found = false;
+ while (it.hasNext()) {
+ found = true;
+ Triple t = it.next();
+ if (log.isDebugEnabled()) {
+ log.debug("Found matching triple: {}", t);
+ TestUtils.stamp(t);
+ }
+ assertEquals(t.getPredicate(), predicate);
+ }
+ assertTrue(found);
+ }
+
+ @Test
+ public void testFilterObject() {
+ log.info("testFilterObject(); Test filter(null,null,o)");
+ // We use testAdd to prepare this
+ testAddSingle();
+ Iterator<Triple> it = mgraph.filter(null, null, object);
+ boolean found = false;
+ while (it.hasNext()) {
+ found = true;
+ Triple t = it.next();
+ if (log.isDebugEnabled()) {
+ log.debug("Found matching triple: {}", t);
+ TestUtils.stamp(t);
+ }
+ assertEquals(t.getObject(), object);
+ }
+ assertTrue(found);
+ }
+
+ @Test
+ public void testFilterObjectTyped() {
+ log.info("testFilterObjectTyped(); Test filter(null,null,o)");
+ // We use testAdd to prepare this
+ testAddSingleTypedLiteral();
+ Iterator<Triple> it = mgraph.filter(null, null, objectTyped);
+ boolean found = false;
+ while (it.hasNext()) {
+ found = true;
+ Triple t = it.next();
+ if (log.isDebugEnabled()) {
+ log.debug("Found matching triple: {}", t);
+ TestUtils.stamp(t);
+ }
+ assertEquals(t.getObject(), objectTyped);
+ }
+ assertTrue(found);
+ }
+
+ @Ignore
+ @Test
+ public void testFilterObjectXml() {
+ log.info("testFilterObjectXml(); Test filter(null,null,o)");
+ // We use testAdd to prepare this
+ testAddSingleXMLLiteral();
+ Iterator<Triple> it = mgraph.filter(null, null, objectXml);
+ boolean found = false;
+ while (it.hasNext()) {
+ found = true;
+ Triple t = it.next();
+ if (log.isDebugEnabled()) {
+ log.debug("Found matching triple: {}", t);
+ TestUtils.stamp(t);
+ }
+ assertEquals(t.getObject(), objectXml);
+ }
+ assertTrue(found);
+ }
+
+ @Test
+ public void testSize() {
+ log.info("testSize()");
+ // We use testAdd to prepare this
+ testAddSingle();
+ // Should be 1 at this time
+ log.debug("How many triples are in graph <{}>? {}", TEST_GRAPH_NAME,
+ mgraph.size());
+ assertTrue(mgraph.size() > 0);
+ }
+
+ @Test
+ public void testIncreaseSize() {
+ log.info("testIncreaseSize()");
+ int beforeSize = mgraph.size();
+ Triple t = new Triple() {
+
+ @Override
+ public NonLiteral getSubject() {
+ return enridaga;
+ }
+
+ @Override
+ public UriRef getPredicate() {
+ return knows;
+ }
+
+ @Override
+ public Resource getObject() {
+ return alexdma;
+ }
+ };
+ assertTrue(mgraph.add(t));
+ int afterSize = mgraph.size();
+ assertEquals(beforeSize + 1, afterSize);
+ }
+
+ @Test
+ public void testAddRemoveSize() {
+ log.info("testAddRemoveSize()");
+ int beforeSize = mgraph.size();
+ Triple t = new Triple() {
+
+ @Override
+ public NonLiteral getSubject() {
+ return enridaga;
+ }
+
+ @Override
+ public UriRef getPredicate() {
+ return knows;
+ }
+
+ @Override
+ public Resource getObject() {
+ return alexdma;
+ }
+ };
+ assertTrue(mgraph.add(t));
+ assertTrue(mgraph.remove(t));
+ int afterSize = mgraph.size();
+ assertEquals(beforeSize, afterSize);
+ }
+
+ @Test
+ public void testGetGraphReadOnly() {
+ log.info("testGetGraphReadOnly()");
+ Graph g = mgraph.getGraph();
+ Triple t = new Triple() {
+
+ @Override
+ public NonLiteral getSubject() {
+ return enridaga;
+ }
+
+ @Override
+ public UriRef getPredicate() {
+ return knows;
+ }
+
+ @Override
+ public Resource getObject() {
+ return anuzzolese;
+ }
+ };
+ // This should not be allowed
+ boolean success;
+ try {
+ success = g.add(t);
+ } catch (UnsupportedOperationException e) {
+ success = false;
+ }
+ assertFalse(success);
+ }
+
+ @Test
+ public void testContains() {
+ log.info("testContains()");
+ Triple t = new Triple() {
+
+ @Override
+ public NonLiteral getSubject() {
+ return enridaga;
+ }
+
+ @Override
+ public UriRef getPredicate() {
+ return knows;
+ }
+
+ @Override
+ public Resource getObject() {
+ return anuzzolese;
+ }
+ };
+ boolean addWorks = mgraph.add(t);
+ assertTrue(addWorks);
+
+ // This second triple is equivalent
+ Triple t2 = new Triple() {
+
+ @Override
+ public NonLiteral getSubject() {
+ return enridaga;
+ }
+
+ @Override
+ public UriRef getPredicate() {
+ return knows;
+ }
+
+ @Override
+ public Resource getObject() {
+ return anuzzolese;
+ }
+ };
+ Iterator<Triple> it = mgraph.filter(t2.getSubject(), t2.getPredicate(),
+ t2.getObject());
+ while (it.hasNext()) {
+ if (log.isDebugEnabled()) {
+ log.debug("Found matching triple");
+ TestUtils.stamp(it.next());
+ }else{
+ it.next();
+ }
+ }
+ assertTrue(mgraph.contains(t2));
+ // Also the related read-only graph
+ assertTrue(mgraph.getGraph().contains(t2));
+ }
+
+ @After
+ public void clear() throws VirtuosoException, ClassNotFoundException,
+ SQLException {
+ log.info("Clearing graph <{}> of size {}", TEST_GRAPH_NAME,
+ mgraph.size());
+ log.debug("Removing graph <{}>", TEST_GRAPH_NAME);
+ da.close();
+ da = null;
+ mgraph = null;
+ Statement st = TestUtils.getConnection().createStatement();
+ st.execute("SPARQL CLEAR GRAPH <" + TEST_GRAPH_NAME + ">");
+ st.close();
+ }
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/access/DataAccessTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/access/DataAccessTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/access/DataAccessTest.java
new file mode 100644
index 0000000..ec24378
--- /dev/null
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/access/DataAccessTest.java
@@ -0,0 +1,111 @@
+package org.apache.clerezza.rdf.virtuoso.storage.access;
+
+import java.sql.SQLException;
+
+import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.clerezza.rdf.virtuoso.storage.TestUtils;
+import org.apache.clerezza.rdf.virtuoso.storage.access.DataAccess;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class DataAccessTest {
+
+ private static DataAccess da = null;
+
+ static Logger log = LoggerFactory.getLogger(DataAccessTest.class);
+
+ @BeforeClass
+ public static void assume(){
+ org.junit.Assume.assumeTrue(!TestUtils.SKIP);
+ }
+
+ @Before
+ public void before() throws ClassNotFoundException, SQLException {
+ da = TestUtils.getProvider().createDataAccess();
+ da.clearGraph( "urn:x-test:DataAccessTest" );
+ }
+
+ @After
+ public void after() {
+ da.clearGraph( "urn:x-test:DataAccessTest" );
+ da.close();
+ da = null;
+ }
+
+ private void testTriple(Triple t){
+ String g = "urn:x-test:DataAccessTest";
+ da.insertQuad(g, t);
+
+ Assert.assertTrue(da.filter(g, null, null, null).hasNext());
+
+ Assert.assertTrue(da.filter(g, t.getSubject(), null, null).hasNext());
+ Assert.assertTrue(da.filter(g, null, t.getPredicate(), null).hasNext());
+ Assert.assertTrue(da.filter(g, null, null, t.getObject()).hasNext());
+
+ Assert.assertTrue(da.filter(g, null, t.getPredicate(), t.getObject()).hasNext());
+ Assert.assertTrue(da.filter(g, t.getSubject(), null, t.getObject()).hasNext());
+ Assert.assertTrue(da.filter(g, t.getSubject(), t.getPredicate(), null).hasNext());
+ Assert.assertTrue(da.filter(g, t.getSubject(), null, t.getObject()).hasNext());
+
+ Assert.assertTrue(da.filter(g, t.getSubject(), t.getPredicate(), t.getObject()).hasNext());
+
+ Assert.assertTrue(da.size(g) == 1);
+
+ da.deleteQuad(g, t);
+
+ Assert.assertTrue(da.size(g) == 0);
+ }
+
+ @Test
+ public void test_Uri_Uri_Uri(){
+ Triple t = new TripleImpl(new UriRef("urn:subject"), new UriRef("urn:predicate"), new UriRef("urn:object"));
+ testTriple(t);
+ }
+
+ @Test
+ public void test_Uri_Uri_PlainLiteral(){
+ Triple t = new TripleImpl(new UriRef("urn:subject"), new UriRef("urn:predicate"), new PlainLiteralImpl("Lorem Ipsum"));
+ testTriple(t);
+ }
+
+ @Test
+ public void test_Uri_Uri_BNode(){
+ Triple t = new TripleImpl(new UriRef("urn:subject"), new UriRef("urn:predicate"), new BNode());
+ testTriple(t);
+ }
+
+ @Test
+ public void testRenew(){
+ int i = 100;
+ while(i>0){
+ test_Uri_Uri_Uri();
+ test_Uri_Uri_PlainLiteral();
+ i--;
+ }
+ da.renew();
+ i = 100;
+ while(i>0){
+ test_Uri_Uri_Uri();
+ test_Uri_Uri_PlainLiteral();
+ i--;
+ }
+ da.renew();
+ i = 100;
+ while(i>0){
+ test_Uri_Uri_Uri();
+ test_Uri_Uri_PlainLiteral();
+ i--;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/access/VirtuosoWeightedProviderTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/access/VirtuosoWeightedProviderTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/access/VirtuosoWeightedProviderTest.java
new file mode 100644
index 0000000..0ac3a27
--- /dev/null
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/access/VirtuosoWeightedProviderTest.java
@@ -0,0 +1,310 @@
+/*
+ * 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.clerezza.rdf.virtuoso.storage.access;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.sql.SQLException;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.clerezza.rdf.core.Graph;
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.clerezza.rdf.core.PlainLiteral;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.access.NoSuchEntityException;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
+import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.clerezza.rdf.virtuoso.storage.TestUtils;
+import org.apache.clerezza.rdf.virtuoso.storage.access.VirtuosoWeightedProvider;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import virtuoso.jdbc4.VirtuosoException;
+
+/**
+ *
+ * Tests the VirtuosoWeightedProvider
+ *
+ * @author enridaga
+ *
+ */
+public class VirtuosoWeightedProviderTest {
+ static final Logger log = LoggerFactory
+ .getLogger(VirtuosoWeightedProviderTest.class);
+
+ private static final String TEST_GRAPH_URI = "VirtuosoWeightedProviderTest";
+
+ private static VirtuosoWeightedProvider wp = null;
+
+ @BeforeClass
+ public static void before() throws ClassNotFoundException, SQLException {
+ org.junit.Assume.assumeTrue(!TestUtils.SKIP);
+
+ log.info("Preparing VirtuosoWeightedProvider for test");
+ wp = TestUtils.getProvider();
+ }
+
+ @Test
+ public void weight() {
+ log.info("Test setting the weight");
+ int w = 200;
+ wp.setWeight(w);
+ assertTrue(wp.getWeight() == w);
+ }
+
+ @Test
+ public void listGraphs() {
+ log.info("Test listGraphs()");
+ Set<UriRef> gs = wp.listGraphs();
+ Iterator<UriRef> it = gs.iterator();
+ log.debug("Graphs:");
+ while (it.hasNext()) {
+ UriRef r = it.next();
+ // Must not be null
+ assertNotNull(r);
+ // Must not be empty string
+ assertFalse(r.getUnicodeString().equals(""));
+ log.debug(" > {}", r.getUnicodeString());
+ }
+ }
+
+ @Test
+ public void listGraphsIsUnmodifiable() {
+ log.info("Test listGraphsIsUnmodifiable()");
+ Set<UriRef> gs = wp.listGraphs();
+ boolean exception = false;
+ try {
+ gs.add(new UriRef("example"));
+ } catch (UnsupportedOperationException e) {
+ log.debug(
+ "Great, we had an {} exception while modifying an immutable set!",
+ e.getClass());
+ exception = true;
+ }
+ assertTrue(exception);
+ }
+
+ @Test
+ public void listMGraphs() {
+ log.info("Test listMGraphs()");
+ Set<UriRef> mg = wp.listMGraphs();
+ log.debug("Graphs:");
+ for (UriRef r : mg) {
+ // Must not be null
+ assertNotNull(r);
+ // Must not be empty string
+ assertFalse(r.getUnicodeString().equals(""));
+ log.debug("MGraph iri: {}", r.getUnicodeString());
+ }
+ }
+
+ final UriRef enridaga = new UriRef("enridaga");
+ final UriRef alexdma = new UriRef("alexdma");
+ final UriRef anuzzolese = new UriRef("anuzzolese");
+ final UriRef predicate = new UriRef("http://property/name");
+ final PlainLiteral object = new PlainLiteralImpl("Enrico Daga");
+ final UriRef knows = new UriRef(TestUtils.FOAF_NS + "knows");
+
+ @Test
+ public void createMGraph() {
+ log.info("createMGraph()");
+ try {
+ MGraph mgraph = wp.createMGraph(new UriRef(TEST_GRAPH_URI));
+ assertNotNull(mgraph);
+ if (log.isDebugEnabled()) {
+ log.debug("Created mgraph, adding a triple");
+ log.debug("MGraph size is {}", mgraph.size());
+ }
+ mgraph.add(new Triple() {
+
+ @Override
+ public NonLiteral getSubject() {
+ return enridaga;
+ }
+
+ @Override
+ public UriRef getPredicate() {
+ return knows;
+ }
+
+ @Override
+ public Resource getObject() {
+ return anuzzolese;
+ }
+ });
+ log.debug("MGraph size is {}", mgraph.size());
+ assertTrue(mgraph.size() == 1);
+ } catch (RuntimeException re) {
+ log.error("ERROR! ", re);
+ assertFalse(true);
+ }
+ }
+
+ @Test
+ public void createGraph() throws VirtuosoException, ClassNotFoundException,
+ SQLException {
+ MGraph smg = new SimpleMGraph();
+ Triple t = new Triple() {
+
+ @Override
+ public NonLiteral getSubject() {
+ return enridaga;
+ }
+
+ @Override
+ public UriRef getPredicate() {
+ return knows;
+ }
+
+ @Override
+ public Resource getObject() {
+ return anuzzolese;
+ }
+ };
+ smg.add(t);
+ UriRef name = new UriRef(TEST_GRAPH_URI);
+ Graph g = wp.createGraph(name, smg);
+ // Graph must contain the triple
+ assertTrue(g.contains(t));
+ // Graph size must be 1
+ assertTrue(g.size() == 1);
+ // Graph retrieved by id must contain the triple
+ assertTrue(wp.getGraph(name).contains(t));
+ // Graph retrieved by id must be equal to g
+ assertTrue(wp.getGraph(name).equals(g));
+ }
+
+ @Test
+ public void testEquals() {
+ log.info("testEquals()");
+ UriRef name = new UriRef(TEST_GRAPH_URI);
+ MGraph g = wp.createMGraph(name);
+ // Equals
+ log.debug("Should be equal: {}", g.equals(wp.getMGraph(name)));
+ assertTrue(g.equals(wp.getMGraph(name)));
+ log.debug("{} <-> {}", g.getClass(), g.getGraph().getClass());
+ log.debug("Should not be equal: {}", g.equals(g.getGraph()));
+ // Not equals
+ assertFalse(g.equals(g.getGraph()));
+ }
+
+ @Before
+ @After
+ public void clear() throws VirtuosoException, ClassNotFoundException,
+ SQLException {
+ log.info("clear()");
+ log.debug("Removing test graphs <{}>", TEST_GRAPH_URI);
+ try {
+ wp.deleteTripleCollection(new UriRef(TEST_GRAPH_URI));
+ } catch (NoSuchEntityException nsee) {
+ // Nothing to do
+ }
+ try {
+ wp.deleteTripleCollection(new UriRef("urn:my-empty-graph"));
+ } catch (NoSuchEntityException nsee) {
+ // Nothing to do
+ }
+ try {
+ wp.deleteTripleCollection(new UriRef("urn:my-non-empty-graph"));
+ } catch (NoSuchEntityException nsee) {
+ // Nothing to do
+ }
+ }
+
+ @Test
+ public void testCreateEmptyMGraph(){
+ log.info("testCreateEmptyMGraph()");
+// try {
+ UriRef ur = new UriRef("urn:my-empty-graph");
+ Assert.assertFalse(wp.listGraphs().contains(ur));
+ Assert.assertFalse(wp.listMGraphs().contains(ur));
+ wp.createMGraph(ur);
+ Assert.assertTrue(wp.canRead(ur));
+ Assert.assertTrue(wp.canModify(ur));
+ Assert.assertTrue(wp.listGraphs().contains(ur));
+ Assert.assertTrue(wp.listMGraphs().contains(ur));
+ wp.deleteTripleCollection(ur);
+ Assert.assertFalse(wp.listGraphs().contains(ur));
+ Assert.assertFalse(wp.listMGraphs().contains(ur));
+// } catch (NoSuchEntityException nsee) {
+// // Nothing to do
+// }
+ }
+
+ @Test
+ public void testEmptyAndNonEmptyGraphs(){
+ log.info("testEmptyAndNonEmptyGraphs()");
+
+ UriRef ur = new UriRef("urn:my-empty-graph");
+ UriRef nur = new UriRef("urn:my-non-empty-graph");
+
+ Assert.assertFalse(wp.listGraphs().contains(ur));
+ Assert.assertFalse(wp.listMGraphs().contains(ur));
+ wp.createMGraph(ur);
+ Assert.assertTrue(wp.canRead(ur));
+ Assert.assertTrue(wp.canModify(ur));
+ wp.createMGraph(nur);
+ Assert.assertTrue(wp.canRead(nur));
+ Assert.assertTrue(wp.canModify(nur));
+ Assert.assertTrue(wp.listGraphs().contains(nur));
+ Assert.assertTrue(wp.listMGraphs().contains(nur));
+
+ // Add a triple to the non-empty graph
+ Triple t = new TripleImpl(new UriRef("urn:test:subject"), new UriRef("urn:test:predicate"),
+ new PlainLiteralImpl("A literal"));
+ wp.getMGraph(nur).add(t);
+ // Show inserted triple
+ Iterator<Triple> ti = wp.getMGraph(nur).iterator();
+ while(ti.hasNext()){
+ log.info(" > {}", ti.next());
+ }
+ Assert.assertTrue(wp.getMGraph(nur).contains(t));
+ Assert.assertTrue(wp.getMGraph(ur).isEmpty());
+ // We delete the empty graph
+ wp.deleteTripleCollection(ur);
+ Assert.assertFalse(wp.listGraphs().contains(ur));
+ Assert.assertFalse(wp.listMGraphs().contains(ur));
+ // But the other is still there
+ Assert.assertTrue(wp.listGraphs().contains(nur));
+ Assert.assertTrue(wp.listMGraphs().contains(nur));
+ // And it still contains its triple
+ Assert.assertTrue(wp.getMGraph(nur).contains(t));
+ // We delete the triple
+ wp.getMGraph(nur).remove(t);
+ Assert.assertFalse(wp.getMGraph(nur).contains(t));
+ Assert.assertTrue(wp.getMGraph(nur).isEmpty());
+ // We delete the non empty graph
+ wp.deleteTripleCollection(nur);
+ Assert.assertFalse(wp.listGraphs().contains(nur));
+ Assert.assertFalse(wp.listMGraphs().contains(nur));
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ConnectionTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ConnectionTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ConnectionTest.java
deleted file mode 100644
index cfe6aa4..0000000
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ConnectionTest.java
+++ /dev/null
@@ -1,138 +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 rdf.virtuoso.storage;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import java.sql.DatabaseMetaData;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.Map.Entry;
-import java.util.Properties;
-
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import virtuoso.jdbc4.VirtuosoConnection;
-import virtuoso.jdbc4.VirtuosoException;
-
-/**
- * Tests the connection to the Virtuoso DBMS
- *
- * @author enridaga
- */
-public class ConnectionTest {
-
- static final Logger log = LoggerFactory.getLogger(ConnectionTest.class);
-
- private static VirtuosoConnection connection = null;
-
- @BeforeClass
- public static void before() throws ClassNotFoundException, SQLException {
- org.junit.Assume.assumeTrue(!TestUtils.SKIP);
- connection = TestUtils.getConnection();
- }
-
- @Test
- public void testIsClosed() {
- assertFalse(connection.isClosed());
- }
-
- @Test
- public void testIsConnectionLost() {
- assertFalse(connection.isConnectionLost(0));
- }
-
- @Test
- public void testIsReadOnly() throws VirtuosoException {
- assertFalse(connection.isReadOnly());
- }
-
- @Test
- public void testConnection() {
- log.info("testConnection()");
- try {
-
- Statement st = connection.createStatement();
- log.debug("Populate graph <mytest>");
- String[] queries = {
- "sparql clear graph <mytest>",
- "sparql insert into graph <mytest> { <xxx> <P01> \"test1\"@en }",
- "sparql insert into graph <mytest> { <xxx> <P01> \"test2\"@it }",
- "sparql PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> insert into graph <mytest> { <xxx> <P01> \"test3\"^^xsd:string }",
- "sparql insert into graph <mytest> { <xxx> <P01> \"test4\" }",
- "sparql insert into graph <mytest> { <xxx> <P01> \"test5\" . <xxx> <P02> _:b1"
- + " . _:b1 <P03> <yyy> " + " . _:b3 <P05> <zzz> "
- + " . _:b3 <P05> <ppp> " +
- // This is to consider that we can force it
- " . <nodeID://b10005> <P05> <ooo> " + " }",
- "sparql insert into graph <mytest> { <nodeID://b10005> <property> <nodeID://b10007>}",
- "sparql insert into graph <mytest> { <enridaga> <property> \"Literal value\"^^<http://datatype#type>}",
- "sparql insert into graph <mytest> { <nodeID://b10005> <property> <nodeID://b10007>}" };
- for (String q : queries) {
- log.debug("Querying: {}", q);
- st.execute(q);
- }
-
- String query = "sparql SELECT * from <mytest> WHERE {?s ?p ?o}";
- log.debug("Querying: {}", query);
- ResultSet rs = st.executeQuery(query);
- TestUtils.stamp(rs);
- } catch (SQLException e) {
- log.error("SQL ERROR: ", e);
- assertTrue(false);
- } catch (Exception e) {
- log.error("SQL ERROR: ", e);
- assertTrue(false);
- }
- }
-
- @Test
- public void test() throws ClassNotFoundException, SQLException {
- DatabaseMetaData dm = connection.getMetaData();
- log.debug("Username is {}", dm.getUserName());
- Properties p = connection.getClientInfo();
- if (p == null) {
- log.warn("Client info is null...");
- } else
- for (Entry<Object, Object> e : p.entrySet()) {
- log.info("Client info property: {} => {}", e.getKey(),
- e.getValue());
- }
- String SQL = "SELECT DISTINCT id_to_iri(G) FROM DB.DBA.RDF_QUAD quad ";
- VirtuosoConnection cn = TestUtils.getConnection();
- long startAt = System.currentTimeMillis();
- // get the list of quad using SQL
- log.debug("Executing SQL: {}", SQL);
- cn.createStatement().executeQuery(SQL);
- long endAt = System.currentTimeMillis();
- log.debug("Using SQL: {}ms", endAt - startAt);
- SQL = "SPARQL SELECT DISTINCT ?G WHERE {GRAPH ?G {?S ?P ?O} }";
- startAt = System.currentTimeMillis();
- // get the list of quad using SQL+SPARQL
- log.debug("Executing SQL: {}", SQL);
- cn.createStatement().executeQuery(SQL);
- endAt = System.currentTimeMillis();
- log.debug("Using SQL+SPARQL: {}ms", endAt - startAt);
- }
-}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
deleted file mode 100644
index 4bd02ee..0000000
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
+++ /dev/null
@@ -1,350 +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 rdf.virtuoso.storage;
-
-import java.io.IOException;
-import java.net.URL;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.Iterator;
-
-import org.apache.clerezza.platform.config.SystemConfig;
-import org.apache.clerezza.rdf.core.BNode;
-import org.apache.clerezza.rdf.core.MGraph;
-import org.apache.clerezza.rdf.core.NonLiteral;
-import org.apache.clerezza.rdf.core.Resource;
-import org.apache.clerezza.rdf.core.Triple;
-import org.apache.clerezza.rdf.core.TypedLiteral;
-import org.apache.clerezza.rdf.core.UriRef;
-import org.apache.clerezza.rdf.core.impl.TripleImpl;
-import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
-import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
-import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
-import org.apache.clerezza.rdf.jena.parser.JenaParserProvider;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import rdf.virtuoso.storage.access.DataAccess;
-import virtuoso.jdbc4.VirtuosoException;
-
-public class RdfIOTest {
- static VirtuosoMGraph mgraph = null;
- static final String TEST_GRAPH_NAME = "RdfIOTest";
- static final String XSD = "http://www.w3.org/2001/XMLSchema#";
- static Logger log = LoggerFactory.getLogger(RdfIOTest.class);
- static DataAccess da;
-
- @BeforeClass
- public static void assume(){
- org.junit.Assume.assumeTrue(!TestUtils.SKIP);
- }
-
- /**
- * Clean before any test
- *
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- @Before
- public void before() throws ClassNotFoundException, SQLException {
-
- da = TestUtils.getProvider().createDataAccess();
- mgraph = new VirtuosoMGraph(TEST_GRAPH_NAME, da);
- mgraph.clear();
- log.debug("Clearing graph <{}>", TEST_GRAPH_NAME);
- }
-
- /**
- * Clean after a test
- *
- * @throws VirtuosoException
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- @After
- public void clear() throws VirtuosoException, ClassNotFoundException,
- SQLException {
- log.debug("Clearing graph <{}> of size {}", TEST_GRAPH_NAME,
- mgraph.size());
- // clear all resources
- da.close();
-
- da = null;
- mgraph = null;
-
- log.debug("Removing graph <{}>", TEST_GRAPH_NAME);
- Statement st = TestUtils.getConnection().createStatement();
- st.execute("SPARQL CLEAR GRAPH <" + TEST_GRAPH_NAME + ">");
- st.close();
- }
-
- @Test
- public void xsdString() throws ClassNotFoundException, SQLException {
- log.info("Text an xsd:string");
- TypedLiteral object = new TypedLiteralImpl("lorem ipsum", new UriRef(
- XSD + "string"));
- UriRef subject = new UriRef("urn:io-test:reto");
- UriRef predicate = new UriRef("urn:io-test:hasText");
-
- Triple read = writeAndRead(subject, predicate, object);
- Assert.assertTrue(read
- .equals(new TripleImpl(subject, predicate, object)));
- Assert.assertTrue(read.getObject() instanceof TypedLiteral);
- TypedLiteral l = (TypedLiteral) read.getObject();
- Assert.assertEquals(l.getLexicalForm(), "lorem ipsum");
- Assert.assertEquals(l.getDataType(), new UriRef(XSD + "string"));
-
- }
-
- @Test
- public void longString() throws ClassNotFoundException, SQLException, InterruptedException {
- log.info("Test a long xsd:string");
- StringBuilder longStr = new StringBuilder();
- int c = 250000;
- while (c > 0) {
- longStr.append(" lorem ipsum ");
- c--;
- }
- int size = longStr.length();
- TypedLiteral object = new TypedLiteralImpl(longStr.toString(),
- new UriRef(XSD + "string"));
- UriRef subject = new UriRef("urn:io-test:reto");
- UriRef predicate = new UriRef("urn:io-test:hasText");
- Triple read = writeAndRead(subject, predicate, object);
- Assert.assertTrue(read
- .equals(new TripleImpl(subject, predicate, object)));
- Assert.assertTrue(read.getObject() instanceof TypedLiteral);
- TypedLiteral l = (TypedLiteral) read.getObject();
- Assert.assertEquals(l.getDataType(), new UriRef(XSD + "string"));
- Assert.assertTrue(l.getLexicalForm().length() == size);
- }
-
- private Triple writeAndRead(NonLiteral subject, UriRef predicate,
- Resource object) throws ClassNotFoundException, SQLException {
- Triple t = new TripleImpl(subject, predicate, object);
- mgraph.add(t);
- Triple read = mgraph.getGraph().iterator().next();
- return read;
- }
-
- @Test
- public void subjectAsUriTest() throws ClassNotFoundException, SQLException {
- log.info("Test subject as UriRef");
-
- NonLiteral subject;
- UriRef predicate = new UriRef("urn:io-test:predicate");
- UriRef object = new UriRef("urn:io-test:object");
-
- // subject may be UriRef
- subject = new UriRef("urn:io-test:enridaga");
- Triple read = writeAndRead(subject, predicate, object);
- Assert.assertTrue(read.getSubject().equals(subject));
- Assert.assertEquals(read.getSubject(), new UriRef(
- "urn:io-test:enridaga"));
- Assert.assertNotSame(read.getSubject(), new UriRef(
- "urn:io-test:alexdma"));
- Assert.assertEquals(read, new TripleImpl(subject, predicate, object));
-
- }
-
- @Test
- public void subjectAsBnodeTest() throws ClassNotFoundException,
- SQLException {
- log.info("Test subject as BNode");
-
- NonLiteral subject;
- UriRef predicate = new UriRef("urn:io-test:predicate");
- UriRef object = new UriRef("urn:io-test:object");
-
- // subject may be BNode
- subject = new BNode();
- Triple read = writeAndRead(subject, predicate, object);
- // bnodes cannot be equals!
- Assert.assertFalse(read.getSubject().equals(subject));
- Assert.assertTrue(read.getSubject() instanceof BNode);
- Assert.assertNotSame(read.getSubject(), new UriRef(
- "urn:io-test:enridaga"));
- Assert.assertNotSame(read.getSubject(), new UriRef(
- "urn:io-test:alexdma"));
- // bnodes cannot be equals!
- Assert.assertNotSame(read, new TripleImpl(subject, predicate, object));
- }
-
- @Test
- public void objectAsUriTest() throws ClassNotFoundException, SQLException {
- log.info("Test object as UriRef");
-
- NonLiteral subject = new UriRef("urn:io-test:enridaga");
- UriRef predicate = new UriRef("urn:io-test:predicate");
- UriRef object = new UriRef("urn:io-test:object");
-
- Triple read = writeAndRead(subject, predicate, object);
- //
- Assert.assertTrue(read.getObject().equals(object));
- Assert.assertTrue(read.getObject() instanceof UriRef);
- Assert.assertEquals(read.getObject(), new UriRef("urn:io-test:object"));
- Assert.assertNotSame(read.getSubject(), new UriRef(
- "urn:io-test:alexdma"));
- Assert.assertEquals(read, new TripleImpl(subject, predicate, object));
- }
-
- @Test
- public void objectAsBnodeTest() throws ClassNotFoundException, SQLException {
- log.info("Test object as Bnode");
-
- NonLiteral subject = new UriRef("urn:io-test:subject");
- UriRef predicate = new UriRef("urn:io-test:predicate");
- Resource object;
-
- // subject may be BNode
- object = new BNode();
- Triple read = writeAndRead(subject, predicate, object);
- // bnodes cannot be equals!
- Assert.assertFalse(read.getObject().equals(object));
- Assert.assertTrue(read.getSubject().equals(subject));
-
- Assert.assertTrue(read.getObject() instanceof BNode);
- Assert.assertTrue(read.getObject() instanceof VirtuosoBNode);
-
- Assert.assertNotSame(read.getObject(), new UriRef(
- "urn:io-test:enridaga"));
- Assert.assertNotSame(read.getObject(),
- new UriRef("urn:io-test:alexdma"));
- // these bnodes cannot be equals!
- Assert.assertNotSame(read, new TripleImpl(subject, predicate, object));
- }
-
- @Test
- public void bnodesTest() throws ClassNotFoundException, SQLException {
- log.info("Test iterations and filter with bnode");
-
- NonLiteral s1 = new BNode();
- NonLiteral s2 = new BNode();
- NonLiteral s3 = new BNode();
- NonLiteral s4 = new BNode();
-
- UriRef p1 = new UriRef("p1");
- UriRef p2 = new UriRef("p2");
- UriRef p3 = new UriRef("p3");
-
- mgraph.add(new TripleImpl(s1, p1, s2));
- // Get the bnode of s1
- Triple first = mgraph.filter(null, p1, null).next();
-
- Assert.assertTrue(first.getSubject() instanceof VirtuosoBNode);
- Assert.assertTrue(first.getObject() instanceof VirtuosoBNode);
-
- BNode s1intern = (BNode) first.getSubject();
- BNode s2intern = (BNode) first.getObject();
-
- mgraph.add(new TripleImpl(s2intern, p1, s3));
- Triple second = mgraph.filter(s2intern, p1, null).next();
- Assert.assertTrue(second.getObject() instanceof VirtuosoBNode);
-
- mgraph.add(new TripleImpl(s1intern, p2, s4));
- Triple third = mgraph.filter(s1intern, p2, null).next();
- Assert.assertTrue(third.getObject() instanceof VirtuosoBNode);
- BNode s4intern = (BNode) third.getObject();
-
- mgraph.add(new TripleImpl(s1intern, p2, s4intern));
- mgraph.add(new TripleImpl(s4intern, p3, s1intern));
-
- Iterator<Triple> all = mgraph.iterator();
- while(all.hasNext()){
- Triple l = all.next();
- log.info("{} {} {}",new Object[]{ l.getSubject(), l.getPredicate(), l.getObject()});
- }
- Iterator<Triple> i = mgraph.filter(null, p2, null);
- int n = 0;
- while (i.hasNext()) {
- n++;
- Triple s1t = i.next();
- Iterator<Triple> s1i = mgraph.filter(s1t.getSubject(), null, null);
- boolean found = false;
- while (s1i.hasNext()) {
- Triple s1it = s1i.next();
- found = true;
- log.info("{} {}",s1it.getSubject(), s1t.getSubject());
- Assert.assertTrue(s1it.getSubject().equals(s1t.getSubject()));
- Assert.assertTrue(s1it.getPredicate().equals(p1)
- || s1it.getPredicate().equals(p2));
-
- }
- Assert.assertTrue(found);
- Assert.assertTrue(s1t.getObject() instanceof VirtuosoBNode);
- Assert.assertTrue(s1t.getSubject() instanceof VirtuosoBNode);
- Iterator<Triple> s4i = mgraph.filter((NonLiteral) s1t.getObject(),
- null, null);
- log.info("s4 {} ",s1t.getObject());
- while (s4i.hasNext()) {
- Triple s4it = s4i.next();
- log.info("{} {}",s4it.getSubject(), s1t.getObject());
- Assert.assertTrue(s4it.getSubject().equals(s1t.getObject()));
- Assert.assertTrue(s4it.getPredicate().equals(p3));
- }
- }
- Assert.assertEquals(n, 1);
-
- }
-
- @Test
- public void sysconfigTest(){
-
- SystemConfig sc = new SystemConfig();
- MGraph systemGraph = mgraph;
- URL config = sc.getClass().getResource(SystemConfig.CONFIG_FILE);
- if (config == null) {
- throw new RuntimeException("no config file found");
- }
- ParsingProvider parser = new JenaParserProvider();
- try {
- parser.parse(systemGraph, config.openStream(),
- SupportedFormat.RDF_XML, null);
- } catch (IOException ex) {
- log.warn("Cannot parse coniguration at URL: {}", config);
- throw new RuntimeException(ex);
- }
- }
-
- @Test
- public void testUnicodeChars() throws ClassNotFoundException, SQLException {
- log.info("Text an xsd:string");
-
- String s = "lorem ipsum è é £ ò ç à ù β ät ü ä";
- TypedLiteral object = new TypedLiteralImpl(s, new UriRef(
- XSD + "string"));
- UriRef subject = new UriRef("urn:io-test:reto");
- UriRef predicate = new UriRef("urn:io-test:hasText");
-
- Triple read = writeAndRead(subject, predicate, object);
- log.info("o: {} :: {}", object, read.getObject());
- Assert.assertTrue(read
- .equals(new TripleImpl(subject, predicate, object)));
- Assert.assertTrue(read.getObject() instanceof TypedLiteral);
- TypedLiteral l = (TypedLiteral) read.getObject();
- Assert.assertEquals(l.getLexicalForm(), s);
- Assert.assertEquals(l.getDataType(), new UriRef(XSD + "string"));
-
- }
-}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
deleted file mode 100644
index 2ca6d3c..0000000
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
+++ /dev/null
@@ -1,185 +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 rdf.virtuoso.storage;
-
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-
-import org.apache.clerezza.rdf.core.Triple;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import rdf.virtuoso.storage.access.VirtuosoWeightedProvider;
-import virtuoso.jdbc4.VirtuosoConnection;
-import virtuoso.jdbc4.VirtuosoExtendedString;
-import virtuoso.jdbc4.VirtuosoRdfBox;
-import virtuoso.jdbc4.VirtuosoResultSet;
-
-/**
- * Utilities for tests
- *
- * @author enridaga
- *
- */
-public class TestUtils {
-
- public static final String FOAF_NS = "http://xmlns.com/foaf/0.1/";
-
- private static VirtuosoWeightedProvider provider = null;
- private static String jdbcConnectionString = null;
- private static String jdbcUser = null;
- private static String jdbcPassword = null;
- private static String jdbcDriver = null;
-
- static Logger log = LoggerFactory.getLogger(TestUtils.class);
- public static boolean SKIP = false;
- static {
- String skipProperty = System.getProperty("virtuoso.test");
- if (skipProperty == null) {
- log.info("virtuoso.skip property is not set. We skip tests by default.");
- SKIP = true;
- } else
- SKIP = !skipProperty.equals("true");
- }
-
- public static VirtuosoConnection getConnection() throws SQLException, ClassNotFoundException{
- return getProvider().getConnection();
- }
- public static VirtuosoWeightedProvider getProvider()
- throws ClassNotFoundException, SQLException {
- if (provider == null) {
- initProvider();
- }
- return provider;
- }
-
- private static void initProvider() throws ClassNotFoundException,
- SQLException {
- if (SKIP) {
- log.warn("Skipping tests.");
- return;
- }
- String host = System.getProperty("virtuoso.host");
- String port = System.getProperty("virtuoso.port");
- jdbcUser = System.getProperty("virtuoso.user");
- jdbcPassword = System.getProperty("virtuoso.password");
- jdbcDriver = System.getProperty("virtuoso.driver");
- if (host == null) {
- host = "localhost";
- log.info("Missing param 'host', setting to default: {}", host);
- }
- if (port == null) {
- port = "1111";
- log.info("Missing param 'port', setting to default: {}", port);
- }
- if (jdbcUser == null) {
- jdbcUser = "dba";
- log.info("Missing param 'user', setting to default: {}", jdbcUser);
- }
- if (jdbcPassword == null) {
- jdbcPassword = "dba";
- log.info("Missing param 'password', setting to default: {}",
- jdbcPassword);
- }
- if (jdbcDriver == null) {
- jdbcDriver = "virtuoso.jdbc4.Driver";
- log.info("Missing param 'password', setting to default: {}",
- jdbcDriver);
- }
-
- StringBuilder cb = new StringBuilder();
- cb.append("jdbc:virtuoso://");
- cb.append(host);
- cb.append(":");
- cb.append(port).append("/CHARSET=UTF-8");
- jdbcConnectionString = cb.toString();
-// Class.forName(VirtuosoWeightedProvider.DRIVER);
- log.info("Create provider");
-// connection = (VirtuosoConnection) DriverManager.getConnection(
-// jdbcConnectionString, jdbcUser, jdbcPassword);
- provider = new VirtuosoWeightedProvider(jdbcConnectionString, jdbcUser, jdbcPassword);
- log.debug("Connection URL: {}", jdbcConnectionString);
- log.debug("Connection user: {}", jdbcUser);
- }
-
- public static void stamp(ResultSet rs) {
- try {
- ResultSetMetaData rsmd;
-
- StringBuilder output = new StringBuilder();
- output.append(System.getProperty("line.separator"));
- output.append("------------------------------");
- output.append(System.getProperty("line.separator"));
- rsmd = rs.getMetaData();
-
- while (rs.next()) {
- for (int i = 1; i <= rsmd.getColumnCount(); i++) {
- String s = rs.getString(i);
- Object o = ((VirtuosoResultSet) rs).getObject(i);
- if (o instanceof VirtuosoExtendedString) {
- // In case is IRI
- VirtuosoExtendedString vs = (VirtuosoExtendedString) o;
- if (vs.iriType == VirtuosoExtendedString.IRI
- && (vs.strType & 0x01) == 0x01) {
- output.append(" <" + vs.str + "> ");
- log.debug(" {} is IRI", vs.str);
- } else if (vs.iriType == VirtuosoExtendedString.BNODE) {
- log.debug(" {} is BNODE", vs.str);
- output.append(" <" + vs.str + "> ");
- } else {
- // In case is untyped literal
- log.debug(" {} is an untyped literal", vs.str);
- output.append(vs.str);
- }
- } else if (o instanceof VirtuosoRdfBox) {
- // In case is typed literal
- VirtuosoRdfBox rb = (VirtuosoRdfBox) o;
- output.append(rb.rb_box + " lang=" + rb.getLang()
- + " type=" + rb.getType());
- log.debug(" {} is an typed literal", rb.rb_box);
-
- } else if (rs.wasNull()) {
- log.debug(" NULL ");
- output.append("NULL");
- } else {
- // Is simple untyped string
- output.append(s);
- }
-
- }
- output.append("\n");
- }
-
- output.append(System.getProperty("line.separator"));
- output.append("------------------------------");
- output.append(System.getProperty("line.separator"));
- log.info(output.toString());
- } catch (Exception e) {
- log.error("ERROR", e);
- }
- }
-
- public static void stamp(Triple next) {
- log.info("> TRIPLE : "+next.getSubject().toString() + " "
- + next.getPredicate().toString() + " "
- + next.getObject().toString());
- }
-
-}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ThreadSafetyTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ThreadSafetyTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ThreadSafetyTest.java
deleted file mode 100644
index a3f30fd..0000000
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ThreadSafetyTest.java
+++ /dev/null
@@ -1,340 +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 rdf.virtuoso.storage;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
-
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.clerezza.rdf.core.Triple;
-import org.apache.clerezza.rdf.core.UriRef;
-import org.apache.clerezza.rdf.core.impl.TripleImpl;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import rdf.virtuoso.storage.access.DataAccess;
-
-/**
- *
- * Credit: This code is largely cloned by the thread safe test in
- * {@see org.apache.clerezza.rdf.sesame.storage.ThreadSafetyTest}
- *
- * @author enridaga
- *
- */
-public class ThreadSafetyTest {
-
- private ExecutorService executor;
- private VirtuosoMGraph mgraph;
- private DataAccess da = null;
- static Logger log = LoggerFactory.getLogger(ThreadSafetyTest.class);
-
- @BeforeClass
- public static void assume(){
- org.junit.Assume.assumeTrue(!TestUtils.SKIP);
- }
-
- @Before
- public void setUp() throws Exception {
-
- da = TestUtils.getProvider().createDataAccess();
- da.clearGraph("ThreadSafetyTest");
- mgraph = new VirtuosoMGraph("ThreadSafetyTest",
- da);
- mgraph.clear();
- executor = Executors.newCachedThreadPool();
- }
-
- @After
- public void tearDown() throws Exception {
- try {
- executor.shutdown();
- if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
- fail("Timeout while waiting for termination");
- }
- } finally {
- mgraph.clear();
- mgraph = null;
- da.close();
- da = null;
- }
- }
-
- @Test
- public void testProduceFirstAndThenConsume() throws Exception {
- log.info("testProduceFirstAndThenConsume()");
- // Produce first...
- Future<Result> fp = executor.submit(new Producer("A", 100));
- fp.get().assertResult();
-
- // ...and then consume
- Future<Result> fc = executor.submit(new Consumer("A", 100));
- fc.get().assertResult();
- Iterator<Triple> it = mgraph.iterator();
- while (it.hasNext()) {
- TestUtils.stamp(it.next());
- }
- // test graph size
- assertEquals(0, mgraph.size());
- }
-
- @Test
- public void testProduceAndConsumeSingle() throws Exception {
- log.info("testProduceAndConsumeSingle()");
- List<Task> tasks = Arrays.asList(
- new Consumer("A", 100), new Producer("A", 100));
- List<Future<Result>> futures = executor.invokeAll(tasks);
- for (Future<Result> future : futures) {
- future.get().assertResult();
- }
- assertEquals(0, mgraph.size());
- }
-
- @Test
- public void testProduceAndConsumeMultiple() throws Exception {
- log.info("testProduceAndConsumeMultiple()");
- List<Task> tasks = Arrays.asList(
- new Consumer("A", 100), new Producer("A", 100),
- new Consumer("B", 100), new Producer("B", 100),
- new Consumer("C", 100), new Producer("C", 100),
- new Consumer("D", 100), new Producer("D", 100));
- List<Future<Result>> futures = executor.invokeAll(tasks);
- for (Future<Result> future : futures) {
- future.get().assertResult();
- }
- assertEquals(0, mgraph.size());
- }
-
- @Test
- public void testProduceAndConsumeMixed() throws Exception {
- log.info("testProduceAndConsumeMixed()");
- List<? extends Task> tasks = Arrays.asList(
- new Consumer("A", 110), new Consumer("A", 170),
- new Consumer("B", 100), new Consumer("B", 500),
- new Consumer("C", 230), new Consumer("C", 230),
- new Consumer("D", 540), new Consumer("D", 10),
- new Producer("D", 50), new Producer("D", 500),
- new Producer("C", 400), new Producer("C", 60),
- new Producer("B", 300), new Producer("B", 300),
- new Producer("A", 200), new Producer("A", 80));
- List<Future<Result>> futures = executor.invokeAll(tasks);
- for (Future<Result> future : futures) {
- future.get().assertResult();
- }
- assertEquals(0, mgraph.size());
- }
- /**
- * The <code>Task</code> specifies a <code>Callable</code> whoes execution
- * results in an <code>Integer</code>.
- */
- private abstract class Task implements Callable<Result> {
-
- protected final String collectionName;
- protected final UriRef subject;
- protected final UriRef predicate;
- protected final int numElements;
-
- /**
- * Creates a new <code>Task</code>.
- *
- * @param collectionName
- * Name of the task's collection.
- * @param numElements
- * The number of elements to process.
- */
- protected Task(String collectionName, int numElements) {
- if (collectionName == null) {
- throw new IllegalArgumentException("Invalid name: null");
- } else if (collectionName.length() <= 0) {
- throw new IllegalArgumentException("Invalid name: '"
- + collectionName + "'");
- } else if (numElements < 0) {
- throw new IllegalArgumentException("Invalid numElements: "
- + numElements);
- }
- this.numElements = numElements;
- this.collectionName = collectionName;
- subject = new UriRef("http://example.org/" + "collection/"
- + collectionName);
- predicate = new UriRef("http://example.org/ontology/contains");
- }
-
- /**
- * Causes the currently executing thread object to temporarily pause and
- * allow other threads to execute.
- */
- protected void yield() {
- try {
- Thread.sleep(5);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
- }
- }
-
- /**
- * This class represents a task that produces triples that are stored in the
- * graph.
- */
- private class Producer extends Task {
-
- /**
- * Creates a new <code>Producer</code> task.
- *
- * @param collectionName
- * Name of the task's collection.
- * @param numElements
- * The number of elements to produce.
- */
- public Producer(String collectionName, int numElements) {
- super(collectionName, numElements);
- }
-
- @Override
- public Result call() throws InterruptedException {
- int counter = 0;
- for (int elementName = 1; counter < numElements; elementName++) {
- yield();
- final Triple t = createTriple(elementName);
- if (mgraph.add(t)) {
- counter++;
- yield();
- } else {
- System.out.println("WARNING: element " + t + "NOT created");
- }
- }
- return new Result(collectionName, "Produced elements", numElements,
- counter);
- }
-
- /**
- * Creates a new collection element triple.
- *
- * @param value
- * Value of the collection element.
- * @return A new triple representing the collection element.
- */
- protected Triple createTriple(int value) {
- final UriRef object = new UriRef("http://example.org/"
- + collectionName + "/" + Integer.valueOf(value)
- + Math.random());
- return new TripleImpl(subject, predicate, object);
- }
- }
-
- /**
- * This class represents a task that produces triples that are stored in the
- * graph.
- */
- private class Consumer extends Task {
-
- /**
- * Creates a new <code>Consumer</code> task.
- *
- * @param collectionName
- * Name of the task's collection.
- * @param numElements
- * The number of elements to consume.
- */
- public Consumer(String collectionName, int numElements) {
- super(collectionName, numElements);
- }
-
- /**
- * Performs this task.
- *
- * @return the number of elements successfully added to the graph.
- */
- @Override
- public Result call() throws InterruptedException {
- int counter = 0;
- while (counter < numElements) {
- yield();
-
- Triple triple = null;
- mgraph.getLock().writeLock().lock();
- try {
- // System.out.println("synchonized");
- Iterator<Triple> i = mgraph
- .filter(subject, predicate, null);
- if (i.hasNext()) {
- triple = i.next();
- }
-
- if (triple != null && mgraph.remove(triple)) {
- counter++;
- }
- } finally {
- mgraph.getLock().writeLock().unlock();
- }
- }
- return new Result(collectionName, "Consumed elements", numElements,
- counter);
- }
- }
-
- /**
- * Task result that asserts the number of processed elements.
- */
- private class Result {
-
- private final int expected;
- private final int actual;
- private final String msg;
- private final String cn;
-
- /**
- * Creates a new task result that asserts the element count.
- *
- * @param cn
- * Name of the affected collection.
- * @param msg
- * Assertion message to print.
- * @param expected
- * Expected number of processed elements.
- * @param actual
- * Actual number of processed elements.
- */
- public Result(String cn, String msg, int expected, int actual) {
- this.expected = expected;
- this.actual = actual;
- this.msg = msg;
- this.cn = cn;
- }
-
- /**
- * Asserts this result.
- */
- public void assertResult() {
- assertEquals("[" + cn + "] " + msg, expected, actual);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/VirtuosoGraphTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/VirtuosoGraphTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/VirtuosoGraphTest.java
deleted file mode 100644
index b6afa5b..0000000
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/VirtuosoGraphTest.java
+++ /dev/null
@@ -1,31 +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 rdf.virtuoso.storage;
-
-import org.apache.clerezza.rdf.core.Graph;
-
-/**
- * Tests the {@link VirtuosoGraph} implementation of {@link Graph}
- *
- * @author enridaga
- *
- */
-public class VirtuosoGraphTest {
-
-}