[Mulgara-svn] r767 - branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic
pag at mulgara.org
pag at mulgara.org
Thu Apr 10 23:19:06 UTC 2008
Author: pag
Date: 2008-04-10 16:19:05 -0700 (Thu, 10 Apr 2008)
New Revision: 767
Added:
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AddOperationUnitTest.java
Log:
Added passing tests. Value comparisons are being used, and we should probably add tests on the type IRIs. These new tests won't make the results better, but it will confirm what we expect. Need to check that I have the correct types first (eg. for float+long)
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AddOperationUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AddOperationUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AddOperationUnitTest.java 2008-04-10 23:19:05 UTC (rev 767)
@@ -0,0 +1,231 @@
+/**
+ * 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.arithmetic;
+
+import java.net.URI;
+
+import org.jrdf.graph.Literal;
+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 org.mulgara.query.filter.value.NumericLiteral;
+import org.mulgara.query.filter.value.SimpleLiteral;
+import org.mulgara.query.filter.value.TypedLiteral;
+import org.mulgara.query.filter.value.ValueLiteral;
+import org.mulgara.query.filter.value.Var;
+import static org.mulgara.query.filter.value.TypedLiteral.XSD_NS;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * Tests the numeric operation classes.
+ *
+ * @created Apr 10, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class AddOperationUnitTest extends TestCase {
+
+ private static final Integer fourI = 4;
+ private static final Integer fiveI = 5;
+ private static final Integer nineI = 9;
+
+ private static final Long fourL = 4L;
+ private static final Long fiveL = 5L;
+ private static final Long nineL = 9L;
+
+ private static final Float fourF = (float)4.0;
+ private static final Float fiveF = (float)5.0;
+ private static final Float nineF = (float)9.0;
+
+ private static final Double fourD = 4.0;
+ private static final Double fiveD = 5.0;
+ private static final Double nineD = 9.0;
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public AddOperationUnitTest(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 AddOperationUnitTest("testLiteral"));
+ suite.addTest(new AddOperationUnitTest("testVar"));
+ return suite;
+ }
+
+
+ public void testLiteral() throws Exception {
+ NumericLiteral i4 = new NumericLiteral(fourI);
+ NumericLiteral i5 = new NumericLiteral(fiveI);
+ NumericLiteral i9 = new NumericLiteral(nineI);
+ NumericLiteral l4 = new NumericLiteral(fourL);
+ NumericLiteral l5 = new NumericLiteral(fiveL);
+ NumericLiteral l9 = new NumericLiteral(nineL);
+ NumericLiteral f4 = new NumericLiteral(fourF);
+ NumericLiteral f5 = new NumericLiteral(fiveF);
+ NumericLiteral f9 = new NumericLiteral(nineF);
+ NumericLiteral d4 = new NumericLiteral(fourD);
+ NumericLiteral d5 = new NumericLiteral(fiveD);
+ NumericLiteral d9 = new NumericLiteral(nineD);
+
+ basicTest(i4, i5, i9);
+ basicTest(i4, l5, l9);
+ basicTest(i4, f5, f9);
+ basicTest(i4, d5, d9);
+ basicTest(l4, l5, l9);
+ basicTest(l4, f5, f9);
+ basicTest(l4, d5, d9);
+ basicTest(f4, f5, f9);
+ basicTest(f4, d5, d9);
+ basicTest(d4, d5, d9);
+ }
+
+ private void basicTest(NumericLiteral four, NumericLiteral five, NumericLiteral nine) throws Exception {
+ AddOperation op = new AddOperation(four, five);
+ assertTrue(op.equals(nine));
+ assertFalse(op.isBlank());
+ assertFalse(op.isIRI());
+ assertTrue(op.isLiteral());
+ assertFalse(op.isURI());
+ assertTrue(nine.getType().equals(op.getType()));
+ assertEquals(SimpleLiteral.EMPTY, op.getLang());
+ }
+
+ public void testVar() throws Exception {
+ Var x = new Var("x");
+ Var y = new Var("y");
+ AddOperation fn = new AddOperation(x, y);
+
+ URI xsdInt = URI.create(XSD_NS + "int");
+ URI xsdLong = URI.create(XSD_NS + "long");
+ URI xsdFloat = URI.create(XSD_NS + "float");
+ URI xsdDouble = URI.create(XSD_NS + "double");
+ URI fooBar = URI.create("foo:bar");
+ Literal i4 = new LiteralImpl(fourI.toString(), xsdInt);
+ Literal i5 = new LiteralImpl(fiveI.toString(), xsdInt);
+ Literal l4 = new LiteralImpl(fourL.toString(), xsdLong);
+ Literal l5 = new LiteralImpl(fiveL.toString(), xsdLong);
+ Literal f4 = new LiteralImpl(fourF.toString(), xsdFloat);
+ Literal f5 = new LiteralImpl(fiveF.toString(), xsdFloat);
+ Literal d4 = new LiteralImpl(fourD.toString(), xsdDouble);
+ Literal d5 = new LiteralImpl(fiveD.toString(), xsdDouble);
+ Node[][] rows = {
+ new Node[] {i4, i5},
+ new Node[] {i4, l5},
+ new Node[] {i4, f5},
+ new Node[] {i4, d5},
+ new Node[] {l4, l5},
+ new Node[] {l4, f5},
+ new Node[] {l4, d5},
+ new Node[] {f4, f5},
+ new Node[] {f4, d5},
+ new Node[] {d4, d5},
+ // The following are to fail
+ new Node[] {new LiteralImpl("foo", "en"), i5},
+ new Node[] {new LiteralImpl("foo", fooBar), i5},
+ new Node[] {new URIReferenceImpl(fooBar), i5},
+ new Node[] {new BlankNodeImpl(), i5},
+ new Node[] {null, i5}
+ };
+ TestContext c = new TestContext(new String[] {"x", "y"}, rows);
+ c.beforeFirst();
+ fn.setContextOwner(new TestContextOwner(c));
+
+ ValueLiteral lNineI = TypedLiteral.newLiteral((int)9);
+ ValueLiteral lNineL = TypedLiteral.newLiteral((long)9L);
+ ValueLiteral lNineF = TypedLiteral.newLiteral((float)9.0);
+ ValueLiteral lNineD = TypedLiteral.newLiteral((double)9.0);
+
+ // check the context setting
+ fn.setCurrentContext(c);
+
+ assertTrue(c.next());
+ assertTrue(lNineI.equals(fn)); // int + int
+
+ assertTrue(c.next());
+ assertTrue(lNineL.equals(fn)); // int + long
+
+ assertTrue(c.next());
+ assertTrue(lNineF.equals(fn)); // int + float
+
+ assertTrue(c.next());
+ assertTrue(lNineD.equals(fn)); // int + double
+
+ assertTrue(c.next());
+ assertTrue(lNineL.equals(fn)); // long + long
+
+ assertTrue(c.next());
+ assertTrue(lNineF.equals(fn)); // long + float
+
+ assertTrue(c.next());
+ assertTrue(lNineD.equals(fn)); // long + double
+
+ assertTrue(c.next());
+ assertTrue(lNineF.equals(fn)); // float + float
+
+ assertTrue(c.next());
+ assertTrue(lNineD.equals(fn)); // float + double
+
+ assertTrue(c.next());
+ assertTrue(lNineD.equals(fn)); // double + double
+
+ assertTrue(c.next());
+ try {
+ fn.getValue();
+ fail("Added a language string to an integer");
+ } catch (QueryException qe) { }
+
+ assertTrue(c.next());
+ try {
+ fn.getValue();
+ fail("Added a unknown typed literal to an integer");
+ } catch (QueryException qe) { }
+
+ assertTrue(c.next());
+ try {
+ fn.getValue();
+ fail("Added a uri to an integer");
+ } catch (QueryException qe) { }
+
+ assertTrue(c.next());
+ try {
+ fn.getValue();
+ fail("Added a blank node to an integer");
+ } catch (QueryException qe) { }
+
+ assertTrue(c.next());
+ try {
+ fn.getValue();
+ fail("Added an unbound to an integer");
+ } catch (QueryException qe) { }
+
+ assertFalse(c.next());
+ }
+
+}
More information about the Mulgara-svn
mailing list