[Mulgara-svn] r798 - branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter
pag at mulgara.org
pag at mulgara.org
Fri Apr 18 03:54:50 UTC 2008
Author: pag
Date: 2008-04-17 20:54:49 -0700 (Thu, 17 Apr 2008)
New Revision: 798
Added:
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFnUnitTest.java
Modified:
branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFn.java
Log:
Wrote tests, and fixed some typing bugs
Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFn.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFn.java 2008-04-18 02:31:42 UTC (rev 797)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFn.java 2008-04-18 03:54:49 UTC (rev 798)
@@ -20,6 +20,8 @@
/**
* The regular expression test for values.
+ * TODO: Move this on to Xalan Regex functions as these are fully compliant with SPARQL,
+ * while the Java ones are not.
*
* @created Mar 8, 2008
* @author Paul Gearon
@@ -56,6 +58,7 @@
*/
RegexFn(ValueLiteral str, ValueLiteral patternStr, ValueLiteral flagExpression) {
super(str, patternStr);
+ this.flagExpression = flagExpression;
flagExpression.setContextOwner(this);
}
@@ -70,7 +73,7 @@
* @throws QueryException If the expression for the string cannot be resolved.
*/
private String str() throws QueryException {
- if (!lhs.isLiteral()) throw new QueryException("Invalid type in regular expression. Need string, got: " + lhs.getClass().getSimpleName());
+ if (!lhs.isLiteral() || !((ValueLiteral)lhs).isSimple()) throw new QueryException("Type Error: Invalid type in regular expression. Need string, got: " + lhs.getClass().getSimpleName());
return ((ValueLiteral)lhs).getLexical();
}
@@ -81,10 +84,11 @@
* @throws QueryException If the pattern string or flags string cannot be resolved.
*/
private Pattern pattern() throws QueryException {
- if (!rhs.isLiteral()) throw new QueryException("Invalid pattern type in regular expression. Need string, got: " + rhs.getClass().getSimpleName());
+ if (!rhs.isLiteral() || !((ValueLiteral)rhs).isSimple()) throw new QueryException("Type Error: Invalid pattern type in regular expression. Need string, got: " + rhs.getClass().getSimpleName());
String patternStr = ((ValueLiteral)rhs).getLexical();
int oldFlags = flags;
- if (pattern == null || !patternStr.equals(pattern.pattern()) || oldFlags != flags()) {
+ // note that the call to flags has a side-effect
+ if (oldFlags != flags() || pattern == null || !patternStr.equals(pattern.pattern())) {
pattern = Pattern.compile(patternStr, flags);
}
return pattern;
@@ -103,6 +107,7 @@
*/
private int flags() throws QueryException {
if (flagExpression == null) return 0;
+ if (!flagExpression.isLiteral() || !((ValueLiteral)flagExpression).isSimple()) throw new QueryException("Type Error: Invalid flags in regular expression. Need string, got: " + rhs.getClass().getSimpleName());
String currentFlagStr = flagExpression.getLexical();
if (flagsStr == null || !flagsStr.equals(currentFlagStr)) {
flagsStr = currentFlagStr;
Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFnUnitTest.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFnUnitTest.java (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/RegexFnUnitTest.java 2008-04-18 03:54:49 UTC (rev 798)
@@ -0,0 +1,208 @@
+/**
+ * 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 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.Bool;
+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 junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * Tests the Regex function.
+ *
+ * @created Apr 17, 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 RegexFnUnitTest extends TestCase {
+
+ Bool t = Bool.TRUE;
+ Bool f = Bool.FALSE;
+
+ /**
+ * Build the unit test.
+ * @param name The name of the test
+ */
+ public RegexFnUnitTest(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 RegexFnUnitTest("testLiteral"));
+ suite.addTest(new RegexFnUnitTest("testVar"));
+ return suite;
+ }
+
+ public void testLiteral() throws Exception {
+ SimpleLiteral str = new SimpleLiteral("a foolish test");
+ SimpleLiteral diffStr = new SimpleLiteral("another test");
+ SimpleLiteral pattern = new SimpleLiteral(".*foo.*");
+ SimpleLiteral patternCaps = new SimpleLiteral(".*FOO.*");
+ SimpleLiteral pattern2 = new SimpleLiteral(".*foo.*test.*");
+ SimpleLiteral noTest = new SimpleLiteral("fred");
+ SimpleLiteral caseFlag = new SimpleLiteral("i");
+ ValueLiteral typed = TypedLiteral.newLiteral("a foolish test");
+ ValueLiteral typedPattern = TypedLiteral.newLiteral(".*foo.*");
+
+ RegexFn fn = new RegexFn(str, pattern);
+ assertTrue(t.equals(fn));
+ assertTrue(fn.equals(t));
+ fn = new RegexFn(str, pattern2);
+ assertTrue(t.equals(fn));
+ assertTrue(fn.equals(t));
+
+ fn = new RegexFn(diffStr, pattern);
+ assertTrue(f.equals(fn));
+ assertTrue(fn.equals(f));
+ fn = new RegexFn(diffStr, pattern2);
+ assertTrue(f.equals(fn));
+ assertTrue(fn.equals(f));
+
+ fn = new RegexFn(str, noTest);
+ assertTrue(f.equals(fn));
+
+ fn = new RegexFn(str, patternCaps);
+ assertTrue(f.equals(fn));
+ fn = new RegexFn(str, patternCaps, caseFlag);
+ assertTrue(t.equals(fn));
+
+ fn = new RegexFn(typed, pattern);
+ try {
+ assertTrue(f.equals(fn));
+ fail("Tested regex on a typed literal");
+ } catch (QueryException qe) {
+ assertTrue(qe.getMessage().startsWith("Type Error:"));
+ }
+
+ fn = new RegexFn(str, typedPattern);
+ try {
+ assertTrue(f.equals(fn));
+ fail("Tested regex on a typed literal");
+ } catch (QueryException qe) {
+ assertTrue(qe.getMessage().startsWith("Type Error:"));
+ }
+}
+
+ public void testVar() throws Exception {
+ RegexFn fn = new RegexFn(new Var("x"), new Var("y"), new Var("z"));
+
+ Literal noFlags = new LiteralImpl("");
+ Literal caseFlag = new LiteralImpl("i");
+
+ Literal str = new LiteralImpl("a foolish test");
+ Literal diffStr = new LiteralImpl("another test");
+ Literal pattern = new LiteralImpl(".*foo.*");
+ Literal patternCaps = new LiteralImpl(".*FOO.*");
+ Literal pattern2 = new LiteralImpl(".*foo.*test.*");
+ Literal noTest = new LiteralImpl("fred");
+ Literal typed = new LiteralImpl("a foolish test", SimpleLiteral.STRING_TYPE.getValue());
+ Literal typedPattern = new LiteralImpl(".*foo.*", SimpleLiteral.STRING_TYPE.getValue());
+ URIReferenceImpl xsdString = new URIReferenceImpl(SimpleLiteral.STRING_TYPE.getValue());
+ BlankNodeImpl bn = new BlankNodeImpl(101);
+ Node[][] rows = {
+ new Node[] {str, pattern, noFlags},
+ new Node[] {diffStr, pattern, noFlags},
+ new Node[] {str, pattern2, noFlags},
+ new Node[] {diffStr, pattern, noFlags},
+ new Node[] {diffStr, pattern2, noFlags},
+
+ new Node[] {str, noTest, noFlags},
+
+ new Node[] {str, patternCaps, noFlags},
+ new Node[] {str, patternCaps, caseFlag},
+
+ new Node[] {typed, pattern, noFlags},
+ new Node[] {str, typedPattern, caseFlag},
+
+ new Node[] {xsdString, pattern, noFlags},
+ new Node[] {str, xsdString, noFlags},
+ new Node[] {str, pattern, xsdString},
+
+ new Node[] {null, pattern, noFlags},
+ new Node[] {str, null, noFlags},
+ new Node[] {str, pattern, null},
+
+ new Node[] {bn, pattern, noFlags},
+ new Node[] {str, bn, noFlags},
+ new Node[] {str, pattern, bn},
+ };
+ TestContext c = new TestContext(new String[] {"x", "y", "z"}, rows);
+ c.beforeFirst();
+ fn.setContextOwner(new TestContextOwner(c));
+ // check the context setting
+ fn.setCurrentContext(c);
+
+ String results = "tftff f ft ee eee xxx eee";
+ runTests(c, fn, results);
+ }
+
+ private void runTests(TestContext c, AbstractFilterValue fn, String results) throws Exception {
+ c.beforeFirst();
+ for (char result: results.toCharArray()) {
+ if (result == ' ') continue;
+ assertTrue(c.next());
+ switch (result) {
+ case 't': // equal
+ assertTrue(t.equals(fn));
+ break;
+
+ case 'f': // unequal
+ assertTrue(f.equals(fn));
+ break;
+
+ case 'e': // typing error
+ try {
+ assertTrue(f.equals(fn));
+ fail("Successfully tested values that were not simple literals");
+ } catch (QueryException qe) {
+ assertTrue(qe.getMessage().startsWith("Type Error"));
+ }
+ break;
+
+ case 'x': // exception due to unbound
+ try {
+ assertTrue(f.equals(fn));
+ fail("No exception when testing an unbound value");
+ } catch (QueryException qe) {
+ assertTrue(qe.getMessage().startsWith("Unbound column"));
+ }
+ break;
+
+ default:
+ fail("Bad test data");
+ }
+ }
+ assertFalse(c.next());
+ }
+
+}
More information about the Mulgara-svn
mailing list