[Mulgara-svn] r728 - in branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter: . value
pag at mulgara.org
pag at mulgara.org
Tue Apr 1 03:29:46 UTC 2008
Author: pag
Date: 2008-03-31 20:29:44 -0700 (Mon, 31 Mar 2008)
New Revision: 728
Added:
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/TestContext.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/TestContextOwner.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BlankNodeValueUnitTest.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BoolUnitTest.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFnUnitTest.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DateTimeUnitTest.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/NumericLiteralUnitTest.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteralUnitTest.java
Modified:
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/Context.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparableLiteral.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Bool.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFn.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/NumericLiteral.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/SimpleLiteral.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Var.java
Log:
Added a series of value tests
Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/Context.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/Context.java 2008-03-31 06:50:37 UTC (rev 727)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/Context.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -25,6 +25,9 @@
*/
public interface Context {
+ /** An invalid index to indicate that a variable is not bound */
+ static final int NOT_BOUND = -1;
+
/**
* Gets the current binding to a local value (a long) for a given internal column number.
* @return the value in the column number specified in the current context.
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/TestContext.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/TestContext.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/TestContext.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -0,0 +1,123 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.jrdf.graph.Node;
+import org.mulgara.query.QueryException;
+
+
+/**
+ * A simple Context used for testing purposes.
+ * @created Mar 31, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class TestContext implements Context {
+
+ /** The current internal row for the data */
+ private int rowNumber = -1;
+
+ /** The names of the columns */
+ private List<String> columnNames;
+
+ /** The rows of data. Now row can exceed the size of columnNames */
+ private Node[][] rows;
+
+ /** The map for converting IDs into Nodes stored against them */
+ private Map<Long,Node> globalizer = new HashMap<Long,Node>();
+
+ /**
+ * Empty constructor used for tests which have no data.
+ */
+ public TestContext() {
+ columnNames = Collections.emptyList();
+ rows = new Node[][] { new Node[] {} };
+ }
+
+ /**
+ * Creates a test context.
+ * @param columnNames The names of the columns for the virtual tuples.
+ * @param rows An array of rows, where each row is Node[]. All rows should be
+ * the same width, which is equal to columnNames.length.
+ */
+ public TestContext(String[] columnNames, Node[][] rows) {
+ this.columnNames = Arrays.asList(columnNames);
+ this.rows = rows;
+ mapGlobalizer(rows);
+ }
+
+ /**
+ * Reset the position to the start.
+ */
+ public void beforeFirst() {
+ rowNumber = -1;
+ }
+
+ /**
+ * Move to the next row of data.
+ * @return <code>true</code> if the new row contains data, or <code>false</code>
+ * if this object has moved past the last row.
+ */
+ public boolean next() {
+ return ++rowNumber < rows.length;
+ }
+
+ /**@see org.mulgara.query.filter.Context#getColumnValue(int) */
+ public long getColumnValue(int columnNumber) throws QueryException {
+ if (columnNumber >= columnNames.size()) throw new QueryException("Unexpected column: " + columnNumber);
+ Node v = rows[rowNumber][columnNumber];
+ if (v == null) throw new QueryException("Unbound column: " + columnNumber);
+ return v.hashCode();
+ }
+
+ /** @see org.mulgara.query.filter.Context#getInternalColumnIndex(java.lang.String) */
+ public int getInternalColumnIndex(String name) {
+ return columnNames.contains(name) ? columnNames.indexOf(name) : NOT_BOUND;
+ }
+
+ /** @see org.mulgara.query.filter.Context#globalize(long) */
+ public Node globalize(long node) throws QueryException {
+ Node n = globalizer.get(node);
+ if (n == null) throw new QueryException("Unable to globalize id <" + node + ">");
+ return n;
+ }
+
+ /** @see org.mulgara.query.filter.Context#isBound(int) */
+ public boolean isBound(int columnNumber) throws QueryException {
+ if (columnNumber >= columnNames.size()) throw new QueryException("Unexpected column: " + columnNumber);
+ return rows[rowNumber][columnNumber] != null;
+ }
+
+ /**
+ * Map node hash codes to the nodes. This is using hash codes as pseudo-gNodes
+ * for the sake of testing. If there is a hash collision, then either update this
+ * class or change the test to use non-conflicting test values.
+ * @param rows An array of node arrays.
+ */
+ private void mapGlobalizer(Node[][] rows) {
+ for (Node[] row: rows) {
+ assert row.length == columnNames.size();
+ for (Node v: row) {
+ Node stored = globalizer.get((long)v.hashCode());
+ if (stored == null) globalizer.put((long)v.hashCode(), v);
+ else assert stored == v : "Unexpected hash collision in test";
+ }
+ }
+ }
+}
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/TestContextOwner.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/TestContextOwner.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/TestContextOwner.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -0,0 +1,42 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter;
+
+
+/**
+ * A test class for emulating a context ownership.
+ *
+ * @created Mar 31, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class TestContextOwner implements ContextOwner {
+ /** The owned context */
+ private Context ctx;
+
+ /**
+ * Create the test ownership.
+ * @param ctx The context to own.
+ */
+ public TestContextOwner(Context ctx) { this.ctx = ctx; }
+
+ /**
+ * Updates the owned context.
+ * @param ctx The context to update to.
+ */
+ public void setCurrentContext(Context ctx) { this.ctx = ctx; }
+
+ /** @return the current context. */
+ public Context getCurrentContext() { return ctx; }
+
+}
Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparableLiteral.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparableLiteral.java 2008-03-31 06:50:37 UTC (rev 727)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/AbstractComparableLiteral.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -163,11 +163,11 @@
*/
int compare(Object left, Object right) throws QueryException;
/**
- * Creates a new ComparableLiteral compatible with this comparison type, from given data
+ * Creates a new ValueLiteral compatible with this comparison type, from given data
* @param data The data in the correct native {@link java.lang.Class} to be converted.
* @return A new literal containing the data.
*/
- ComparableExpression newLiteral(Object data);
+ ValueLiteral newLiteral(Object data);
}
/** Implements string comparisons */
@@ -176,7 +176,7 @@
if (!(right instanceof String)) throw new QueryException("Cannot compare a String to a: " + right.getClass());
return ((String)left).compareTo((String)right);
}
- public ComparableExpression newLiteral(Object data) { return new SimpleLiteral((String)data); }
+ public ValueLiteral newLiteral(Object data) { return new TypedLiteral((String)data, SimpleLiteral.STRING_TYPE.getValue()); }
}
/** Implements string comparisons */
@@ -185,7 +185,7 @@
if (!(right instanceof Date)) throw new QueryException("Cannot compare a Date to a: " + right.getClass());
return ((Date)left).compareTo((Date)right);
}
- public ComparableExpression newLiteral(Object data) { return new DateTime((Date)data); }
+ public ValueLiteral newLiteral(Object data) { return new DateTime((Date)data); }
}
/** Implements boolean comparisons */
@@ -194,7 +194,7 @@
if (!(right instanceof Boolean)) throw new QueryException("Cannot compare a boolean to a: " + right.getClass());
return ((Boolean)left).compareTo((Boolean)right);
}
- public ComparableExpression newLiteral(Object data) { return new Bool((Boolean)data); }
+ public ValueLiteral newLiteral(Object data) { return new Bool((Boolean)data); }
}
/** Implements floating point comparisons, or double comparisons if the rhs parameter is a double */
@@ -206,7 +206,7 @@
if (right instanceof Double) return -((Double)right).compareTo(fleft.doubleValue());
return fleft.compareTo(((Number)right).floatValue());
}
- public ComparableExpression newLiteral(Object data) { return new NumericLiteral((Float)data); }
+ public ValueLiteral newLiteral(Object data) { return new NumericLiteral((Float)data); }
}
/** Implements double precision floating point comparisons */
@@ -215,7 +215,7 @@
if (!(right instanceof Number)) throw new QueryException("Cannot compare a double to a: " + right.getClass());
return ((Double)left).compareTo(((Number)right).doubleValue());
}
- public ComparableExpression newLiteral(Object data) { return new NumericLiteral((Double)data); }
+ public ValueLiteral newLiteral(Object data) { return new NumericLiteral((Double)data); }
}
/** Implements integer comparisons */
@@ -225,7 +225,7 @@
Long lleft = ((Number)left).longValue();
return lleft.compareTo(((Number)right).longValue());
}
- public ComparableExpression newLiteral(Object data) { return new NumericLiteral((Number)data); }
+ public ValueLiteral newLiteral(Object data) { return new NumericLiteral((Number)data); }
}
}
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BlankNodeValueUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BlankNodeValueUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BlankNodeValueUnitTest.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -0,0 +1,74 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.value;
+
+import org.mulgara.query.rdf.BlankNodeImpl;
+import org.jrdf.graph.BlankNode;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * Tests the BlankNode value class.
+ *
+ * @created Mar 31, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class BlankNodeValueUnitTest extends TestCase {
+
+ private BlankNode b = new BlankNodeImpl();
+
+ private BlankNode b2 = new BlankNodeImpl();
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public BlankNodeValueUnitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Hook for test runner to obtain a test suite from.
+ * @return The test suite
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new BlankNodeValueUnitTest("testValues"));
+ suite.addTest(new BlankNodeValueUnitTest("testProperties"));
+ return suite;
+ }
+
+
+ public void testValues() throws Exception {
+ BlankNodeValue bv = new BlankNodeValue(b);
+ assertEquals(b, bv.getValue());
+ assertFalse(b2.equals(bv.getValue()));
+ BlankNodeValue bv2 = new BlankNodeValue(b2);
+ assertTrue(bv.equals(bv));
+ assertTrue(bv.notEquals(bv2));
+ assertFalse(bv.notEquals(bv));
+ assertFalse(bv.equals(bv2));
+ }
+
+ public void testProperties() throws Exception {
+ BlankNodeValue bv = new BlankNodeValue(b);
+ assertTrue(bv.isBlank());
+ assertFalse(bv.isIRI());
+ assertFalse(bv.isLiteral());
+ assertFalse(bv.isURI());
+ }
+}
Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Bool.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Bool.java 2008-03-31 06:50:37 UTC (rev 727)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Bool.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -14,6 +14,7 @@
import java.net.URI;
import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.Context;
/**
* A boolean value.
@@ -37,7 +38,7 @@
}
/** {@inheritDoc} */
- public boolean test() throws QueryException {
+ public boolean test(Context context) throws QueryException {
return (Boolean)value;
}
}
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BoolUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BoolUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/BoolUnitTest.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -0,0 +1,82 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.value;
+
+import org.mulgara.query.filter.Context;
+import org.mulgara.query.filter.TestContext;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * Tests the Bool literal class.
+ *
+ * @created Mar 31, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class BoolUnitTest extends TestCase {
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public BoolUnitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Hook for test runner to obtain a test suite from.
+ * @return The test suite
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new BoolUnitTest("testValues"));
+ suite.addTest(new BoolUnitTest("testFilter"));
+ suite.addTest(new BoolUnitTest("testType"));
+ suite.addTest(new BoolUnitTest("testProperties"));
+ return suite;
+ }
+
+
+ public void testValues() throws Exception {
+ Bool b = new Bool(true);
+ assertTrue((Boolean)b.getValue());
+ b = new Bool(false);
+ assertFalse((Boolean)b.getValue());
+ }
+
+ public void testFilter() throws Exception {
+ Context c = new TestContext();
+ Bool b = new Bool(true);
+ assertTrue(b.test(c));
+ b = new Bool(false);
+ assertFalse(b.test(c));
+ }
+
+ public void testType() throws Exception {
+ Bool b = new Bool(true);
+ assertTrue(b.getType().isIRI());
+ assertEquals(b.getType().getValue(), Bool.TYPE);
+ }
+
+ public void testProperties() throws Exception {
+ Bool b = new Bool(true);
+ assertFalse(b.isBlank());
+ assertFalse(b.isIRI());
+ assertTrue(b.isLiteral());
+ assertFalse(b.isURI());
+ }
+}
Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFn.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFn.java 2008-03-31 06:50:37 UTC (rev 727)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFn.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -39,7 +39,7 @@
}
/** @see org.mulgara.query.filter.value.RDFTerm#isIri() */
- public boolean isIri() throws QueryException {
+ public boolean isIRI() throws QueryException {
return true;
}
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFnUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFnUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DataTypeFnUnitTest.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -0,0 +1,155 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.value;
+
+import static org.mulgara.query.filter.value.TypedLiteral.XSD_NS;
+
+import java.net.URI;
+
+import org.jrdf.graph.Node;
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.TestContext;
+import org.mulgara.query.filter.TestContextOwner;
+import org.mulgara.query.rdf.BlankNodeImpl;
+import org.mulgara.query.rdf.LiteralImpl;
+import org.mulgara.query.rdf.URIReferenceImpl;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * Tests the data type function class.
+ *
+ * @created Mar 31, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class DataTypeFnUnitTest extends TestCase {
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public DataTypeFnUnitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Hook for test runner to obtain a test suite from.
+ * @return The test suite
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new DataTypeFnUnitTest("testValues"));
+ suite.addTest(new DataTypeFnUnitTest("testVar"));
+ return suite;
+ }
+
+
+ public void testValues() throws Exception {
+ String str = "test";
+ URI t = URI.create(XSD_NS + "string");
+ TypedLiteral l = (TypedLiteral) TypedLiteral.newLiteral(str, t, null);
+ DataTypeFn fn = new DataTypeFn(l);
+ IRI i = new IRI(t);
+ assertTrue(fn.equals(i));
+ assertFalse(fn.isBlank());
+ assertTrue(fn.isIRI());
+ assertFalse(fn.isLiteral());
+ assertTrue(fn.isURI());
+ try {
+ fn.getType();
+ fail("Should not be able to get a type for an IRI");
+ } catch (QueryException qe) { }
+ try {
+ fn.getLang();
+ fail("Should not be able to get a lang for an IRI");
+ } catch (QueryException qe) { }
+
+ l = (TypedLiteral)TypedLiteral.newLiteral(str);
+ fn = new DataTypeFn(l);
+ i = new IRI(t);
+ assertTrue(fn.equals(i));
+ assertFalse(fn.isBlank());
+ assertTrue(fn.isIRI());
+ assertFalse(fn.isLiteral());
+ assertTrue(fn.isURI());
+
+ String s2 = "foobar";
+ URI t2 = URI.create(XSD_NS + "foo:bar");
+ l = (TypedLiteral)TypedLiteral.newLiteral(s2, t2, null);
+ fn = new DataTypeFn(l);
+ i = new IRI(t2);
+ assertTrue(fn.equals(i));
+ assertFalse(fn.isBlank());
+ assertTrue(fn.isIRI());
+ assertFalse(fn.isLiteral());
+ assertTrue(fn.isURI());
+
+ Long v = Long.valueOf(5);
+ l = (TypedLiteral)TypedLiteral.newLiteral(v);
+ fn = new DataTypeFn(l);
+ i = new IRI(URI.create(XSD_NS + "long"));
+ assertTrue(fn.equals(i));
+ }
+
+ public void testVar() throws Exception {
+ String vName = "foo";
+ Var v = new Var(vName);
+ DataTypeFn fn = new DataTypeFn(v);
+
+ URI xsdString = URI.create(XSD_NS + "string");
+ URI xsdDouble = URI.create(XSD_NS + "double");
+ URI fooBar = URI.create("foo:bar");
+ Node[][] rows = {
+ new Node[] {new LiteralImpl("foo")},
+ new Node[] {new LiteralImpl("foo", xsdString)},
+ new Node[] {new LiteralImpl("5.0", xsdDouble)},
+ new Node[] {new LiteralImpl("foo", "en")},
+ new Node[] {new LiteralImpl("foo", fooBar)},
+ new Node[] {new URIReferenceImpl(fooBar)},
+ new Node[] {new BlankNodeImpl()},
+ };
+ TestContext c = new TestContext(new String[] {vName}, rows);
+ c.beforeFirst();
+ fn.setContextOwner(new TestContextOwner(c));
+
+ // check the context setting
+ fn.setCurrentContext(c);
+
+ assertTrue(c.next());
+ assertEquals(xsdString, fn.getValue());
+ assertTrue(c.next());
+ assertEquals(xsdString, fn.getValue());
+ assertTrue(c.next());
+ assertEquals(xsdDouble, fn.getValue());
+ assertTrue(c.next());
+ assertEquals(SimpleLiteral.STRING_TYPE.getValue(), fn.getValue());
+ assertTrue(c.next());
+ assertEquals(fooBar, fn.getValue());
+ assertTrue(c.next());
+ try {
+ Object o = fn.getValue();
+ fail("Got the type of a URI reference: " + o);
+ } catch (QueryException qe) { }
+ assertTrue(c.next());
+ try {
+ Object o = fn.getValue();
+ fail("Got the type of a Blank Node: " + o);
+ } catch (QueryException qe) { }
+ assertFalse(c.next());
+ }
+
+}
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DateTimeUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DateTimeUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/DateTimeUnitTest.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -0,0 +1,87 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.value;
+
+import java.util.Date;
+
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.Context;
+import org.mulgara.query.filter.TestContext;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * Tests the DataTime literal class
+ *
+ * @created Mar 31, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class DateTimeUnitTest extends TestCase {
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public DateTimeUnitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Hook for test runner to obtain a test suite from.
+ * @return The test suite
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new DateTimeUnitTest("testValues"));
+ suite.addTest(new DateTimeUnitTest("testFilter"));
+ suite.addTest(new DateTimeUnitTest("testType"));
+ suite.addTest(new DateTimeUnitTest("testProperties"));
+ return suite;
+ }
+
+ public void testValues() throws Exception {
+ Date date = new Date();
+ DateTime dt = new DateTime(date);
+ assertEquals(dt.getValue(), date);
+ Date d2 = new Date(date.getTime() + 1);
+ assertFalse(d2.equals(dt.getValue()));
+ }
+
+ public void testFilter() throws Exception {
+ Context c = new TestContext();
+ DateTime dt = new DateTime(new Date());
+ try {
+ dt.test(c);
+ fail("DateTime effective boolean value should not return a value");
+ } catch (QueryException qe) { }
+ }
+
+ public void testType() throws Exception {
+ DateTime dt = new DateTime(new Date());
+ assertTrue(dt.getType().isIRI());
+ assertEquals(dt.getType().getValue(), DateTime.TYPE);
+ }
+
+
+ public void testProperties() throws Exception {
+ DateTime dt = new DateTime(new Date());
+ assertFalse(dt.isBlank());
+ assertFalse(dt.isIRI());
+ assertTrue(dt.isLiteral());
+ assertFalse(dt.isURI());
+ }
+}
Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/NumericLiteral.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/NumericLiteral.java 2008-03-31 06:50:37 UTC (rev 727)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/NumericLiteral.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -30,7 +30,7 @@
* @param n The number to wrap
*/
public NumericLiteral(Number n) {
- super(n, typeMap.get(n));
+ super(n, typeMap.get(n.getClass()));
}
/**
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/NumericLiteralUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/NumericLiteralUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/NumericLiteralUnitTest.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -0,0 +1,107 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.value;
+
+import java.net.URI;
+
+import org.mulgara.query.filter.Context;
+import org.mulgara.query.filter.TestContext;
+import static org.mulgara.query.filter.value.TypedLiteral.XSD_NS;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * Tests the NumericLiteral class
+ *
+ * @created Mar 31, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class NumericLiteralUnitTest extends TestCase {
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public NumericLiteralUnitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Hook for test runner to obtain a test suite from.
+ * @return The test suite
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new NumericLiteralUnitTest("testValues"));
+ suite.addTest(new NumericLiteralUnitTest("testFilter"));
+ suite.addTest(new NumericLiteralUnitTest("testType"));
+ suite.addTest(new NumericLiteralUnitTest("testProperties"));
+ return suite;
+ }
+
+ public void testValues() throws Exception {
+ Integer five = Integer.valueOf(5);
+ NumericLiteral n = new NumericLiteral(five);
+ assertEquals(n.getValue(), five);
+ assertFalse(Integer.valueOf(4).equals(n.getValue()));
+
+ Double six = Double.valueOf(6.0);
+ n = new NumericLiteral(six);
+ assertEquals(n.getValue(), six);
+ assertFalse(Double.valueOf(4).equals(n.getValue()));
+ }
+
+ public void testFilter() throws Exception {
+ Context c = new TestContext();
+ NumericLiteral n = new NumericLiteral(Integer.valueOf(5));
+ assertTrue(n.test(c));
+ n = new NumericLiteral(Integer.valueOf(0));
+ assertFalse(n.test(c));
+
+ n = new NumericLiteral(Double.valueOf(5.0));
+ assertTrue(n.test(c));
+ n = new NumericLiteral(Double.valueOf(0));
+ assertFalse(n.test(c));
+ }
+
+ public void testType() throws Exception {
+ NumericLiteral n = new NumericLiteral(Integer.valueOf(5));
+ assertTrue(n.getType().isIRI());
+ assertEquals(n.getType().getValue(), URI.create(XSD_NS + "int"));
+
+ n = new NumericLiteral(Long.valueOf(5));
+ assertTrue(n.getType().isIRI());
+ assertEquals(n.getType().getValue(), URI.create(XSD_NS + "long"));
+
+ n = new NumericLiteral(Double.valueOf(5));
+ assertTrue(n.getType().isIRI());
+ assertEquals(n.getType().getValue(), URI.create(XSD_NS + "double"));
+
+ n = new NumericLiteral(Float.valueOf(5));
+ assertTrue(n.getType().isIRI());
+ assertEquals(n.getType().getValue(), URI.create(XSD_NS + "float"));
+ }
+
+
+ public void testProperties() throws Exception {
+ NumericLiteral n = new NumericLiteral(Integer.valueOf(5));
+ assertFalse(n.isBlank());
+ assertFalse(n.isIRI());
+ assertTrue(n.isLiteral());
+ assertFalse(n.isURI());
+ }
+}
Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/SimpleLiteral.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/SimpleLiteral.java 2008-03-31 06:50:37 UTC (rev 727)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/SimpleLiteral.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -32,7 +32,7 @@
/** The language tag for a simple literal */
private SimpleLiteral lang = EMPTY;
- /** The language tag for a simple literal */
+ /** The type used for strings */
static final IRI STRING_TYPE = new IRI(new TypedLiteral.XSDString().getTypeURI());
/**
@@ -68,7 +68,7 @@
/**
* Gets the type of this literal
- * @return Always null.
+ * @return Always the string type.
*/
public IRI getType() {
return STRING_TYPE;
Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java 2008-03-31 06:50:37 UTC (rev 727)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -58,13 +58,13 @@
* @param type The type of the literal. May be null.
* @param lang The language code of the literal. May be null.
*/
- public static ComparableExpression newTypedLiteral(String value, URI type, String lang) {
+ public static ValueLiteral newLiteral(String value, URI type, String lang) {
if (type != null) {
// get the info registered for this type URI
TypeInfo info = infoMap.get(type);
- if (info != null) {
+ if (info != null && info.delegatedConstruction()) {
try {
- return newTypedLiteral(info.toData(value));
+ return newLiteral(info.toData(value));
} catch (QueryException e) { // should not happen
throw new AssertionError("Internal type maps are inconsistent for: " + info.getClass().getSimpleName());
}
@@ -82,7 +82,7 @@
* from literal numbers parsed by a SPARQL parser.
* @param value The data as an object. May be a String, or some kind of {@link java.lang.Number}
*/
- public static ComparableExpression newTypedLiteral(Object value) throws QueryException {
+ public static ValueLiteral newLiteral(Object value) throws QueryException {
DataCompare dc = typeMap.get(value.getClass());
if (dc == null) throw new QueryException("Unrecognized data type: " + value.getClass().getSimpleName());
return dc.newLiteral(value);
@@ -127,7 +127,7 @@
public boolean test(Context context) throws QueryException {
if (type == null) return ((String)value).length() != 0;
TypeInfo test = infoMap.get(type);
- return test != null && test.ebv((String)value);
+ return test != null && test.ebv(value.toString());
}
/** A map of XSD datatypes onto the tests for their types */
@@ -143,6 +143,8 @@
public Object toData(String representation);
/** Returns the URI for this type */
public URI getTypeURI();
+ /** Indicates if construction should be delegated */
+ public boolean delegatedConstruction();
/** All the registered types */
static final List<TypeInfo> types = new ArrayList<TypeInfo>();
}
@@ -152,6 +154,7 @@
private final URI typeURI;
AbstractXSD(String fragment) { typeURI = xsd(fragment); }
public URI getTypeURI() { return typeURI; }
+ public boolean delegatedConstruction() { return true; }
}
/**
@@ -193,6 +196,7 @@
XSDString() { super("string"); }
public boolean ebv(String data) { return data != null && data.length() != 0; }
public Object toData(String r) { return r; }
+ public boolean delegatedConstruction() { return false; }
}
private static class XSDBoolean extends AbstractXSD {
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteralUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteralUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteralUnitTest.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -0,0 +1,120 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter.value;
+
+import java.net.URI;
+
+import org.mulgara.query.filter.Context;
+import org.mulgara.query.filter.TestContext;
+import static org.mulgara.query.filter.value.TypedLiteral.XSD_NS;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * Tests the typed literal class.
+ *
+ * @created Mar 31, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class TypedLiteralUnitTest extends TestCase {
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public TypedLiteralUnitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Hook for test runner to obtain a test suite from.
+ * @return The test suite
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new TypedLiteralUnitTest("testValues"));
+ suite.addTest(new TypedLiteralUnitTest("testFilter"));
+ suite.addTest(new TypedLiteralUnitTest("testType"));
+ suite.addTest(new TypedLiteralUnitTest("testProperties"));
+ return suite;
+ }
+
+
+ public void testValues() throws Exception {
+ String str = "test";
+ TypedLiteral l = (TypedLiteral)TypedLiteral.newLiteral(str, URI.create(XSD_NS + "string"), null);
+ assertEquals(str, l.getValue());
+
+ l = (TypedLiteral)TypedLiteral.newLiteral(str);
+ assertEquals(str, l.getValue());
+
+ String s2 = "foobar";
+ l = (TypedLiteral)TypedLiteral.newLiteral(s2, URI.create("foo:bar"), null);
+ assertEquals(s2, l.getValue());
+
+ Long v = Long.valueOf(5);
+ l = (TypedLiteral)TypedLiteral.newLiteral(v);
+ assertEquals(v, l.getValue());
+ }
+
+ public void testFilter() throws Exception {
+ Context c = new TestContext();
+ TypedLiteral l = (TypedLiteral)TypedLiteral.newLiteral("test", URI.create(XSD_NS + "string"), null);
+ assertTrue(l.test(c));
+
+ l = (TypedLiteral)TypedLiteral.newLiteral("test");
+ assertTrue(l.test(c));
+ l = (TypedLiteral)TypedLiteral.newLiteral("");
+ assertFalse(l.test(c));
+
+ l = (TypedLiteral)TypedLiteral.newLiteral("foobar", URI.create("foo:bar"), null);
+ assertFalse(l.test(c));
+
+ l = (TypedLiteral)TypedLiteral.newLiteral(Long.valueOf(5));
+ assertTrue(l.test(c));
+ l = (TypedLiteral)TypedLiteral.newLiteral(Long.valueOf(0));
+ assertFalse(l.test(c));
+ }
+
+ public void testType() throws Exception {
+ String str = "test";
+ URI t = URI.create(XSD_NS + "string");
+ TypedLiteral l = (TypedLiteral) TypedLiteral.newLiteral(str, t, null);
+ assertTrue(l.getType().isIRI());
+ assertEquals(t, l.getType().getValue());
+
+ l = (TypedLiteral)TypedLiteral.newLiteral(str);
+ assertEquals(t, l.getType().getValue());
+
+ String s2 = "foobar";
+ URI t2 = URI.create(XSD_NS + "foo:bar");
+ l = (TypedLiteral)TypedLiteral.newLiteral(s2, t2, null);
+ assertEquals(t2, l.getType().getValue());
+
+ Long v = Long.valueOf(5);
+ l = (TypedLiteral)TypedLiteral.newLiteral(v);
+ assertEquals(URI.create(XSD_NS + "long"), l.getType().getValue());
+ }
+
+ public void testProperties() throws Exception {
+ TypedLiteral l = (TypedLiteral) TypedLiteral.newLiteral("test", URI.create(XSD_NS + "string"), null);
+ assertFalse(l.isBlank());
+ assertFalse(l.isIRI());
+ assertTrue(l.isLiteral());
+ assertFalse(l.isURI());
+ }
+}
Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Var.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Var.java 2008-03-31 06:50:37 UTC (rev 727)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/value/Var.java 2008-04-01 03:29:44 UTC (rev 728)
@@ -223,7 +223,7 @@
if (!(node instanceof Literal)) throw new QueryException("Unknown type for: " + node);
Literal l = (Literal)node;
- return TypedLiteral.newTypedLiteral(l.getLexicalForm(), l.getDatatypeURI(), l.getLanguage());
+ return TypedLiteral.newLiteral(l.getLexicalForm(), l.getDatatypeURI(), l.getLanguage());
}
/**
More information about the Mulgara-svn
mailing list