You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by th...@apache.org on 2009/05/07 12:10:10 UTC
svn commit: r772584 - in /jackrabbit/trunk/jackrabbit-spi-commons/src/test:
java/org/apache/jackrabbit/spi/commons/query/sql2/ resources/org/
resources/org/apache/ resources/org/apache/jackrabbit/
resources/org/apache/jackrabbit/spi/ resources/org/apac...
Author: thomasm
Date: Thu May 7 10:10:09 2009
New Revision: 772584
URL: http://svn.apache.org/viewvc?rev=772584&view=rev
Log:
JCR-1347: Move Jackrabbit Query Parser from core to spi-commons (move test cases)
Added:
jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/query/sql2/
jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/query/sql2/ParserTest.java
jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/
jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/apache/
jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/apache/jackrabbit/
jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/apache/jackrabbit/spi/
jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/apache/jackrabbit/spi/commons/
jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/apache/jackrabbit/spi/commons/query/
jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/apache/jackrabbit/spi/commons/query/sql2/
jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/apache/jackrabbit/spi/commons/query/sql2/test.sql2.txt
Added: jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/query/sql2/ParserTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/query/sql2/ParserTest.java?rev=772584&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/query/sql2/ParserTest.java (added)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/query/sql2/ParserTest.java Thu May 7 10:10:09 2009
@@ -0,0 +1,140 @@
+/*
+ * 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.jackrabbit.spi.commons.query.sql2;
+
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.util.Random;
+import javax.jcr.NamespaceException;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFactory;
+import javax.jcr.query.InvalidQueryException;
+import javax.jcr.query.qom.QueryObjectModel;
+import junit.framework.TestCase;
+import org.apache.jackrabbit.spi.commons.conversion.DefaultNamePathResolver;
+import org.apache.jackrabbit.spi.commons.conversion.DummyNamespaceResolver;
+import org.apache.jackrabbit.spi.commons.conversion.NamePathResolver;
+import org.apache.jackrabbit.spi.commons.query.qom.QueryObjectModelFactoryImpl;
+import org.apache.jackrabbit.spi.commons.query.qom.QueryObjectModelTree;
+import org.apache.jackrabbit.value.ValueFactoryImpl;
+
+/**
+ * This class runs function tests on the JCR-SQL2 parser.
+ */
+public class ParserTest extends TestCase {
+
+ protected Parser parser;
+
+ protected Random random = new Random();
+
+ static class QOMF extends QueryObjectModelFactoryImpl {
+
+ public QOMF(NamePathResolver resolver, ValueFactory factory) {
+ super(resolver, factory);
+ }
+
+ protected QueryObjectModel createQuery(QueryObjectModelTree qomTree) throws InvalidQueryException,
+ RepositoryException {
+ return null;
+ }
+
+ };
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ ValueFactory vf = ValueFactoryImpl.getInstance();
+ NamePathResolver resolver = new DefaultNamePathResolver(
+ new DummyNamespaceResolver());
+ QueryObjectModelFactoryImpl factory = new QOMF(resolver, vf);
+ parser = new Parser(factory, vf);
+ }
+
+ private LineNumberReader openScript(String name) {
+ return new LineNumberReader(new InputStreamReader(getClass()
+ .getResourceAsStream(name)));
+ }
+
+ public void testParseScript() throws Exception {
+ LineNumberReader reader = openScript("test.sql2.txt");
+ while (true) {
+ String line = reader.readLine();
+ if (line == null) {
+ break;
+ }
+ line = line.trim();
+ if (line.length() == 0 || line.startsWith("#")) {
+ continue;
+ }
+ // System.out.println(line);
+ String query = line;
+ try {
+ parser.createQueryObjectModel(line);
+ fuzz(line);
+ } catch (Exception e) {
+ line = reader.readLine();
+ if (line == null || !line.startsWith("> exception")) {
+ e.printStackTrace();
+ assertTrue("Unexpected exception for query " + query + ": "
+ + e, false);
+ }
+ }
+ }
+ reader.close();
+ }
+
+ public void fuzz(String query) throws Exception {
+ for (int i = 0; i < 100; i++) {
+ StringBuffer buff = new StringBuffer(query);
+ int changes = 1 + (int) Math.abs(random.nextGaussian() * 2);
+ for (int j = 0; j < changes; j++) {
+ char newChar;
+ if (random.nextBoolean()) {
+ String s = "<>_.+\"*%&/()=?[]{}_:;,.-1234567890.qersdf";
+ newChar = s.charAt(random.nextInt(s.length()));
+ } else {
+ newChar = (char) random.nextInt(255);
+ }
+ int pos = random.nextInt(buff.length());
+ if (random.nextBoolean()) {
+ // 50%: change one character
+ buff.setCharAt(pos, newChar);
+ } else {
+ if (random.nextBoolean()) {
+ // 25%: delete one character
+ buff.deleteCharAt(pos);
+ } else {
+ // 25%: insert one character
+ buff.insert(pos, newChar);
+ }
+ }
+ }
+ String q = buff.toString();
+ try {
+ parser.createQueryObjectModel(q);
+ } catch (InvalidQueryException e) {
+ // OK
+ } catch (NamespaceException e) {
+ // OK?
+ } catch (Throwable t) {
+ t.printStackTrace();
+ assertTrue("Unexpected exception for query " + q + ": " + t,
+ false);
+ }
+ }
+ }
+
+}
Added: jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/apache/jackrabbit/spi/commons/query/sql2/test.sql2.txt
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/apache/jackrabbit/spi/commons/query/sql2/test.sql2.txt?rev=772584&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/apache/jackrabbit/spi/commons/query/sql2/test.sql2.txt (added)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/test/resources/org/apache/jackrabbit/spi/commons/query/sql2/test.sql2.txt Thu May 7 10:10:09 2009
@@ -0,0 +1,217 @@
+# 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.
+
+# See also jcr-spec.pdf
+
+# 6.7.1 Query (p 99)
+select * from test
+select * from test where id=1
+select * from test where id=1 order by id
+select * from test order by id
+
+# 6.7.2 Source (p 99)
+# 6.7.3 Selector (p 100)
+# 6.7.4 Name (p 100)
+select * from test as t
+select * from ["Test"]
+select * from [test]
+select * from [test] as [t]
+select * from test as ["t"]
+select * from ["test"] as ["t"]
+
+# 6.7.5 Join (p 102)
+# 6.7.6 JoinType (p 102)
+# 6.7.7 JoinCondition (p 103)
+# 6.7.8 EquiJoinCondition (p 103)
+select * from parent inner join child on parent.id=child.parentid
+select * from parent as p inner join child as c on p.id=c.parentid
+select * from parent as p inner join child as c on p.id=c.parentid
+select * from parent as p left outer join child as c on p.id=c.parentid
+select * from parent as p right outer join child as c on p.id=c.parentid
+
+# 6.7.9 SameNodeJoinCondition (p 105)
+select * from parent as p inner join child as c on issamenode(p, c)
+select * from parent as p inner join child as c on issamenode(p, c, a)
+select * from parent as p inner join child as c on issamenode(p, c, [/a/b/c])
+select * from parent as p inner join child as c on issamenode(p, c, ['/a/b/c'])
+
+# 6.7.10 ChildNodeJoinCondition (p 106)
+select * from parent as p inner join child as c on ischildnode(p, c)
+
+# 6.7.11 DescendantNodeJoinCondition (p 107)
+select * from parent as p inner join child as c on isdescendantnode(p, c)
+select * from parent as p right outer join child as c on p.id=c.parentid inner join other as x on p.id = x.id
+
+# 6.7.12 Constraint (p 108)
+# 6.7.13 And (p 108)
+# 6.7.14 Or (p 109)
+select * from test where id<1 and id>1
+select * from test where id=2 or name='Hello'
+
+# 6.7.15 Not (p 110)
+select * from test where not id=2
+select * from test where not (id=2 and name='Hello')
+select * from test where id=2 or not (name='Hello' and id=3)
+
+# 6.7.16 Comparison (p 110)
+# 6.7.17 Operator (p 112)
+select * from test where id<=2 or id>=3 and name<'a' or name>'c'
+select * from test where id<>2
+select * from test where name like 'H%'
+
+# 6.7.18 PropertyExistence (p 113)
+select * from test where name is not null
+select * from test as t where t.name is not null and t.id<>0
+select * from test as t where not t.name is not null
+select * from test as t where t.name is null
+select * from test as t where not t.name is null
+
+# 6.7.19 FullTextSearch (p 113)
+select * from test where contains(name, 'hello -world')
+select * from test as t where contains(t.*, 'hello -world')
+select * from test as t where contains([t].name, 'hello -world')
+
+# 6.7.20 SameNode (p 115)
+select * from test where issamenode([/a/b/c])
+select * from test as a where issamenode(['/a'])
+select * from test as x where issamenode(x, ['/a[2]/b/c'])
+
+# 6.7.21 ChildNode (p 116)
+select * from test where ischildnode([/a[1]/b])
+select * from test as a where ischildnode(['/a'])
+select * from test as x where ischildnode(x, [/])
+select * from test as x where ischildnode(x, ['/a[1]'])
+
+# 6.7.22 DescendantNode (p 117)
+# 6.7.23 Path (p 118)
+select * from test where ISDESCENDANTNODE([/a[1]])
+select * from test as a where ISDESCENDANTNODE([/a])
+select * from test as x where ISDESCENDANTNODE(x, [/a/b/c])
+
+# 6.7.24 Operand (p 118)
+# 6.7.25 StaticOperand (p 119)
+# 6.7.26 DynamicOperand (p 119)
+# 6.7.27 PropertyValue (p 119)
+# 6.7.28 Length (p 120)
+select * from test where length(name)=5
+select * from test as t where length(t.name)=5
+
+# 6.7.29 NodeName (p 121)
+select * from test where name()='test'
+select * from test as x where name(x)='test'
+
+# 6.7.30 NodeLocalName (p 121)
+select * from test where localname()='test'
+select * from test as x where localname(x)='test'
+
+# 6.7.31 FullTextSearchScore (p 122)
+select * from test where score()>4
+select * from test as x where score(x)<1
+
+# 6.7.32 LowerCase (p 122)
+select * from test where lower(name)='test'
+select * from test where lower(upper(name))='test'
+select * from test where lower(localname(test))='test'
+select * from test where lower(name(test))='test'
+select * from test as x where lower(x.name)='test'
+
+# 6.7.33 UpperCase (p 123)
+select * from test where upper(name)='test'
+select * from test where upper(lower(name))='test'
+select * from test where upper(localname(test))='test'
+select * from test where upper(name(test))='test'
+select * from test as x where upper(x.name)='test'
+
+# 6.7.34 Literal (p 123)
+select * from test where amount=0.01
+select * from test where amount=10.
+select * from test where amount=.01
+select * from test where amount=.01e-1
+select * from test where amount=-.01e1
+select * from test where amount=-0.01e1
+select * from test where amount=+10
+select * from test where amount=-10e10
+select * from test where amount=+10e-10
+select * from test where amount=+10e+10
+select * from test where active=true
+select * from test where active=false
+select * from test where name='test''test'
+select * from test where name=''''
+select * from test where active="True"
+select * from test where active=""
+select * from test where a=cast('0.01' as string)
+select * from test where a=cast('abcdef' as binary)
+select * from test where a=cast('+2001-01-01T01:02:03-00:30' as date)
+select * from test where a=cast('10' as long)
+select * from test where a=cast('100.5' as double)
+select * from test where a=cast('3.11' as decimal)
+select * from test where a=cast('true' as boolean)
+select * from test where a=cast('firstName' as name)
+select * from test where a=cast('a/b/c' as path)
+select * from test where a=cast('[123]' as reference)
+select * from test where a=cast('[123]' as weakreference)
+select * from test where a=cast("x://y/z" as uri)
+
+# 6.7.35 BindVariable (p 124)
+select * from test where name=$name
+select * from test where name=$x and id=$y
+select * from test where name=$x14
+select * from test where name=$_
+select * from test where name=$äöü
+select * from test where name=$äöü
+
+# 6.7.36 Prefix (p 124)
+select * from test where name=$nt:name
+select * from test where name=$_:name
+select * from test where name=$_1:name
+select * from test where name=$_-.0:name
+select * from test where name=$x..:name
+
+# 6.7.37 Ordering (p 125)
+# 6.7.38 Order (p 126)
+select * from test order by name
+select * from test order by name asc
+select * from test order by name desc
+select * from test order by id, name
+select * from test order by id, name, id, name
+select * from test order by id desc, name asc, id, name desc
+select * from test order by id desc, name asc, id asc
+
+# 6.7.39 Column (p 127)
+select name from test
+select id, name from test
+select x.id from test as x
+select x.id, name from test as x
+select x.id, y.id from test as x inner join test as y on x.id=y.id
+select x.id as i from test as x inner join test as y on x.id=y.id
+select x.id as i, y.name as n from test as x inner join test as y on x.id=y.id
+select x.id, y.name as n from test as x inner join test as y on x.id=y.id
+select x.* from test as x
+select x.*, y.* from test as x inner join test as y on x.id=y.id
+select x.*, x.id as i, y.*, y.name from test as x inner join test as y on x.id=y.id
+
+#errors
+select * from parent as p inner join child as c on issamenode(p, c, a/b)
+> exception
+select * from parent as p inner join child as c on issamenode(p, c, d, e)
+> exception
+select * from
+> exception
+select * from parent as p inner join child as c on ischildnode(p, c, a)
+> exception
+select * from parent as p inner join child as c on isdescendantnode(p)
+> exception
+select * from parent as p inner join child as c on isdescendantnode(a, b, c)
+> exception