clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From enrid...@apache.org
Subject git commit: Added Rdf IO tests. See CLEREZZA-863
Date Sun, 12 Jan 2014 23:55:28 GMT
Updated Branches:
  refs/heads/master 83fec5f9f -> 2cc95d0ce


Added Rdf IO tests. See CLEREZZA-863


Project: http://git-wip-us.apache.org/repos/asf/clerezza/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza/commit/2cc95d0c
Tree: http://git-wip-us.apache.org/repos/asf/clerezza/tree/2cc95d0c
Diff: http://git-wip-us.apache.org/repos/asf/clerezza/diff/2cc95d0c

Branch: refs/heads/master
Commit: 2cc95d0ce48fbf78a78b89803bf516009a2d1f18
Parents: 83fec5f
Author: enridaga <enridaga@apache.org>
Authored: Sun Jan 12 23:55:13 2014 +0000
Committer: enridaga <enridaga@apache.org>
Committed: Sun Jan 12 23:55:13 2014 +0000

----------------------------------------------------------------------
 .../rdf/virtuoso/storage/VirtuosoMGraph.java    | 174 ++++++++------
 .../java/rdf/virtuoso/storage/RdfIOTest.java    | 232 +++++++++++++++++++
 2 files changed, 330 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza/blob/2cc95d0c/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
index 8342fee..e2bc826 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
@@ -38,6 +38,7 @@ import org.apache.clerezza.rdf.core.UriRef;
 import org.apache.clerezza.rdf.core.access.LockableMGraph;
 import org.apache.clerezza.rdf.core.impl.AbstractMGraph;
 import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
 import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -554,92 +555,113 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
 			this.p = p;
 			this.o = o;
 		}
-
-		public Triple build() {
-			logger.debug("TripleBuilder.build()");
-			return new Triple() {
-				@Override
-				public NonLiteral getSubject() {
-					logger.debug("TripleBuilder.getSubject() : {}", s);
-					if (s instanceof VirtuosoExtendedString) {
-						VirtuosoExtendedString vs = (VirtuosoExtendedString) s;
-						if (vs.iriType == VirtuosoExtendedString.IRI
-								&& (vs.strType & 0x01) == 0x01) {
-							// Subject is IRI
-							return new UriRef(vs.str);
-						} else if (vs.iriType == VirtuosoExtendedString.BNODE) {
-							return VirtuosoMGraph.this.toBNode(vs.str);
-						} else {
-							// !Cannot happen
-							throw new IllegalStateException(
-									"Subject must be an IRI or a BNODE");
-						}
-					} else {
-						throw new IllegalStateException(
-								"Subject must be an instance of VirtuosoExtendedString");
-					}
+		
+		private NonLiteral buildSubject() {
+			logger.debug("TripleBuilder.getSubject() : {}", s);
+			if (s instanceof VirtuosoExtendedString) {
+				VirtuosoExtendedString vs = (VirtuosoExtendedString) s;
+				if (vs.iriType == VirtuosoExtendedString.IRI
+						&& (vs.strType & 0x01) == 0x01) {
+					// Subject is IRI
+					return new UriRef(vs.str);
+				} else if (vs.iriType == VirtuosoExtendedString.BNODE) {
+					return VirtuosoMGraph.this.toBNode(vs.str);
+				} else {
+					// !Cannot happen
+					throw new IllegalStateException(
+							"Subject must be an IRI or a BNODE");
 				}
+			} else {
+				throw new IllegalStateException(
+						"Subject must be an instance of VirtuosoExtendedString");
+			}
+		}
 
-				@Override
-				public UriRef getPredicate() {
-					logger.debug("TripleBuilder.getPredicate() : {}", p);
-					if (p instanceof VirtuosoExtendedString) {
-						VirtuosoExtendedString vs = (VirtuosoExtendedString) p;
-						if (vs.iriType == VirtuosoExtendedString.IRI
-								&& (vs.strType & 0x01) == 0x01) {
-							// Subject is IRI
-							return new UriRef(vs.str);
-						} else {
-							// !Cannot happen
-							throw new IllegalStateException(
-									"Predicate must be an IRI ");
-						}
+		private UriRef buildPredicate() {
+			logger.debug("TripleBuilder.getPredicate() : {}", p);
+			if (p instanceof VirtuosoExtendedString) {
+				VirtuosoExtendedString vs = (VirtuosoExtendedString) p;
+				if (vs.iriType == VirtuosoExtendedString.IRI
+						&& (vs.strType & 0x01) == 0x01) {
+					// Subject is IRI
+					return new UriRef(vs.str);
+				} else {
+					// !Cannot happen
+					throw new IllegalStateException(
+							"Predicate must be an IRI ");
+				}
+			} else {
+				throw new IllegalStateException(
+						"Predicate must be an IRI");
+			}
+		}
+		
+		Resource buildObject() {
+			logger.debug("TripleBuilder.getObject() : {}", o);
+			if (o instanceof VirtuosoExtendedString) {
+				// In case is IRI
+				VirtuosoExtendedString vs = (VirtuosoExtendedString) o;
+				if (vs.iriType == VirtuosoExtendedString.IRI
+						&& (vs.strType & 0x01) == 0x01) {
+					// Is IRI
+					return new UriRef(vs.str);
+				} else if (vs.iriType == VirtuosoExtendedString.BNODE) {
+					//
+					return VirtuosoMGraph.this.toBNode(vs.str);
+				} else {
+					// Is a plain literal
+					return new PlainLiteralImpl(vs.str);							
+				}
+			} else if (o instanceof VirtuosoRdfBox) {
+				// In case is typed literal
+				VirtuosoRdfBox rb = (VirtuosoRdfBox) o;
+				
+				String value;
+				if(rb.rb_box.getClass().isAssignableFrom(String.class)){
+					//logger.info("assignable from string: {}", rb.rb_box);
+					value = (String) rb.rb_box;
+					String lang = rb.getLang();
+					String type = rb.getType();
+					if (type == null) {
+						Language language = lang == null ? null
+								: new Language(lang);
+						return new PlainLiteralImpl(value, language);
 					} else {
-						throw new IllegalStateException(
-								"Predicate must be an IRI");
+						return new TypedLiteralImpl(value, new UriRef(type));
 					}
-				}
-
-				@Override
-				public Resource getObject() {
-					logger.debug("TripleBuilder.getObject() : {}", o);
-					if (o instanceof VirtuosoExtendedString) {
-						// In case is IRI
-						VirtuosoExtendedString vs = (VirtuosoExtendedString) o;
-						if (vs.iriType == VirtuosoExtendedString.IRI
-								&& (vs.strType & 0x01) == 0x01) {
-							// Is IRI
-							return new UriRef(vs.str);
-						} else if (vs.iriType == VirtuosoExtendedString.BNODE) {
-							//
-							return VirtuosoMGraph.this.toBNode(vs.str);
-						} else {
-							// Is a plain literal
-							return new PlainLiteralImpl(vs.str);
-						}
-					} else if (o instanceof VirtuosoRdfBox) {
-						// In case is typed literal
-						VirtuosoRdfBox rb = (VirtuosoRdfBox) o;
-						String value = (String) rb.rb_box;
-						String lang = rb.getLang();
+				}else if(rb.rb_box instanceof VirtuosoExtendedString){
+					//logger.info("VirtuosoExtendedString");
+					VirtuosoExtendedString vs = (VirtuosoExtendedString) rb.rb_box;
+					
+					if (vs.iriType == VirtuosoExtendedString.IRI
+							&& (vs.strType & 0x01) == 0x01) {
+						// Is IRI
+						return new UriRef(vs.str);
+					} else if (vs.iriType == VirtuosoExtendedString.BNODE) {
+						//
+						return VirtuosoMGraph.this.toBNode(vs.str);
+					} else {
 						String type = rb.getType();
 						if (type == null) {
-							Language language = lang == null ? null
-									: new Language(lang);
-							return new PlainLiteralImpl(value, language);
+							// Is a plain literal
+							return new PlainLiteralImpl(vs.str);							
 						} else {
-							return new TypedLiteralImpl(value, new UriRef(type));
+							return new TypedLiteralImpl(vs.str, new UriRef(type));
 						}
-					} else if (o == null) {
-						// Raise an exception
-						throw new IllegalStateException(
-								"Object cannot be NULL!");
-					} else {
-						// FIXME (not clear this...)
-						return new PlainLiteralImpl(o.toString());
 					}
 				}
-			};
+			} else if (o == null) {
+				// Raise an exception
+				throw new IllegalStateException(
+						"Object cannot be NULL!");
+			}
+			
+			// FIXME (not clear this...)
+			return new PlainLiteralImpl(o.toString());
+		}
+		public Triple build() {
+			logger.debug("TripleBuilder.build()");
+			return new TripleImpl(buildSubject(), buildPredicate(), buildObject());
 		}
 	}
 

http://git-wip-us.apache.org/repos/asf/clerezza/blob/2cc95d0c/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
new file mode 100644
index 0000000..ef73b2b
--- /dev/null
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
@@ -0,0 +1,232 @@
+/*
+ * 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.SQLException;
+import java.sql.Statement;
+
+import org.apache.clerezza.rdf.core.BNode;
+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.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+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);
+	
+	/**
+	 * Clean before any test
+	 * 
+	 * @throws ClassNotFoundException
+	 * @throws SQLException
+	 */
+	@Before
+	public void before() throws ClassNotFoundException, SQLException {
+		if (TestUtils.SKIP) {
+			log.warn("SKIPPED");
+			return;
+		}
+		mgraph = new VirtuosoMGraph(TEST_GRAPH_NAME, TestUtils.getConnection());
+		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 {
+		if (TestUtils.SKIP) {
+			log.warn("SKIPPED");
+			return;
+		}
+		log.debug("Clearing graph <{}> of size {}", TEST_GRAPH_NAME,
+				mgraph.size());
+		log.debug("Removing graph <{}>", TEST_GRAPH_NAME);
+		Statement st = TestUtils.getConnection().createStatement();
+		st.execute("SPARQL CLEAR GRAPH <" + TEST_GRAPH_NAME + ">");
+	}
+	
+	@Test
+	public void xsdString() throws ClassNotFoundException, SQLException{
+		if (TestUtils.SKIP) {
+			log.warn("SKIPPED");
+			return;
+		}
+		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.getDataType(), new UriRef(XSD + "string"));
+		
+	}
+	
+	@Test
+	public void longString() throws ClassNotFoundException, SQLException{
+		if (TestUtils.SKIP) {
+			log.warn("SKIPPED");
+			return;
+		}
+		log.info("Test a long xsd:string");	
+		StringBuilder longStr = new StringBuilder();
+		int c = 1000;
+		while (c>0){
+			longStr.append(" another piece of string ");
+			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{
+		VirtuosoMGraph graph = new VirtuosoMGraph(TEST_GRAPH_NAME, TestUtils.getConnection());
+		Triple t = new TripleImpl(subject, predicate, object);
+		graph.add(t);
+		Triple read = graph.getGraph().iterator().next();
+		
+		return read;
+	}
+	
+	@Test
+	public void subjectAsUriTest() throws ClassNotFoundException, SQLException{
+		if (TestUtils.SKIP) {
+			log.warn("SKIPPED");
+			return;
+		}
+		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{
+		if (TestUtils.SKIP) {
+			log.warn("SKIPPED");
+			return;
+		}
+		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{
+		if (TestUtils.SKIP) {
+			log.warn("SKIPPED");
+			return;
+		}
+		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{
+		if (TestUtils.SKIP) {
+			log.warn("SKIPPED");
+			return;
+		}
+		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.assertNotSame(read.getObject(), object);
+		Assert.assertNotSame(read.getObject(), new UriRef("urn:io-test:enridaga"));
+		Assert.assertNotSame(read.getObject(), new UriRef("urn:io-test:alexdma"));
+		// bnodes cannot be equals!
+		Assert.assertNotSame(read, new TripleImpl(subject, predicate, object));
+	}
+	
+}


Mime
View raw message