jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r643367 - in /jackrabbit/trunk/jackrabbit-core/src: main/java/org/apache/jackrabbit/core/query/lucene/JQOM2LuceneQueryBuilder.java test/java/org/apache/jackrabbit/core/query/qom/UpperLowerCaseTest.java
Date Tue, 01 Apr 2008 11:37:06 GMT
Author: mreutegg
Date: Tue Apr  1 04:36:38 2008
New Revision: 643367

URL: http://svn.apache.org/viewvc?rev=643367&view=rev
Log:
JCR-1104: JSR 283 support
- query (work in progress)

Added:
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/qom/UpperLowerCaseTest.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/JQOM2LuceneQueryBuilder.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/JQOM2LuceneQueryBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/JQOM2LuceneQueryBuilder.java?rev=643367&r1=643366&r2=643367&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/JQOM2LuceneQueryBuilder.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/JQOM2LuceneQueryBuilder.java
Tue Apr  1 04:36:38 2008
@@ -404,16 +404,7 @@
 
     public Object visit(LowerCaseImpl node, Object data) throws Exception {
         Object obj = ((DynamicOperandImpl) node.getOperand()).accept(this, data);
-        if (obj instanceof Transformable) {
-            ((Transformable) obj).setTransformation(TransformConstants.TRANSFORM_LOWER_CASE);
-            return obj;
-        } else if (obj instanceof TermQuery) {
-            return transformTermQuery((TermQuery) obj, false);
-        } else {
-            throw new InvalidQueryException(
-                    "lower-case not supported on operand "
-                    + node.getOperand().getClass().getName());
-        }
+        return transformCase(obj, data, false);
     }
 
     public Object visit(NodeLocalNameImpl node, Object data) {
@@ -679,16 +670,7 @@
 
     public Object visit(UpperCaseImpl node, Object data) throws Exception {
         Object obj = ((DynamicOperandImpl) node.getOperand()).accept(this, data);
-        if (obj instanceof Transformable) {
-            ((Transformable) obj).setTransformation(TransformConstants.TRANSFORM_UPPER_CASE);
-            return obj;
-        } else if (obj instanceof TermQuery) {
-            return transformTermQuery((TermQuery) obj, true);
-        } else {
-            throw new InvalidQueryException(
-                    "upper-case not supported on operand "
-                    + node.getOperand().getClass().getName());
-        }
+        return transformCase(obj, data, true);
     }
 
     //------------------------------< internal >--------------------------------
@@ -734,6 +716,43 @@
             throw new InvalidQueryException(
                     "Upper/LowerCase not supported on field "
                     + query.getTerm().field());
+        }
+    }
+
+    private Object transformCase(Object operand, Object data, boolean toUpperCase)
+            throws InvalidQueryException, Exception {
+        if (operand instanceof Transformable) {
+            ((Transformable) operand).setTransformation(toUpperCase ?
+                    TransformConstants.TRANSFORM_UPPER_CASE :
+                    TransformConstants.TRANSFORM_LOWER_CASE);
+            return operand;
+        } else if (operand instanceof TermQuery) {
+            return transformTermQuery((TermQuery) operand, toUpperCase);
+        } else if (operand instanceof LowerCaseImpl) {
+            LowerCaseImpl lc = (LowerCaseImpl) operand;
+            if (toUpperCase) {
+                // upper-case operand, ignore lower-case
+                return transformCase(lc.getOperand(), data, true);
+            } else {
+                // lower-cased twice
+                return ((DynamicOperandImpl) lc.getOperand()).accept(this, data);
+            }
+        } else if (operand instanceof UpperCaseImpl) {
+            UpperCaseImpl oc = (UpperCaseImpl) operand;
+            if (toUpperCase) {
+                // upper-cased twice
+                return ((DynamicOperandImpl) oc.getOperand()).accept(this, data);
+            } else {
+                // lower-case operand, ignore upper-case
+                return transformCase(oc.getOperand(), data, false);
+            }
+        } else if (operand instanceof CaseTermQuery) {
+            CaseTermQuery ctq = (CaseTermQuery) operand;
+            return transformTermQuery(new TermQuery(ctq.getTerm()), toUpperCase);
+        } else {
+            throw new InvalidQueryException(
+                    "lower/upper-case not supported on operand "
+                    + operand.getClass().getName());
         }
     }
 }

Added: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/qom/UpperLowerCaseTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/qom/UpperLowerCaseTest.java?rev=643367&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/qom/UpperLowerCaseTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/qom/UpperLowerCaseTest.java
Tue Apr  1 04:36:38 2008
@@ -0,0 +1,195 @@
+/*
+ * 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.core.query.qom;
+
+import org.apache.jackrabbit.spi.commons.query.jsr283.qom.DynamicOperand;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.Node;
+import javax.jcr.ValueFactory;
+import javax.jcr.PropertyType;
+import javax.jcr.query.Query;
+
+/**
+ * <code>UpperLowerCaseTest</code> performs tests with upper- and lower-case
+ * operands.
+ */
+public class UpperLowerCaseTest extends AbstractQOMTest {
+
+    private ValueFactory vf;
+
+    private Node node;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        vf = superuser.getValueFactory();
+        node = testRootNode.addNode(nodeName1);
+        node.setProperty(propertyName1, "abc");
+        node.setProperty(propertyName2, "ABC");
+        testRootNode.save();
+    }
+
+    protected void tearDown() throws Exception {
+        vf = null;
+        node = null;
+        super.tearDown();
+    }
+
+    public void testFullTextSearchScore() throws RepositoryException {
+        // TODO
+    }
+
+    public void testLength() throws RepositoryException {
+        // TODO
+    }
+
+    public void testNodeLocalName() throws RepositoryException {
+        // TODO
+    }
+
+    public void testNodeName() throws RepositoryException {
+        node.setProperty(propertyName1, "abc", PropertyType.NAME);
+        node.setProperty(propertyName2, "ABC", PropertyType.NAME);
+        node.save();
+
+        // upper case
+        checkQueries(qomFactory.propertyValue("s", propertyName1),
+                true, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.NAME,
+                new boolean[]{false, false, false, false, true});
+
+        checkQueries(qomFactory.propertyValue("s", propertyName2),
+                true, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.NAME,
+                new boolean[]{false, false, false, false, true});
+
+        // lower case
+        checkQueries(qomFactory.propertyValue("s", propertyName1),
+                false, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.NAME,
+                new boolean[]{true, false, false, false, false});
+
+        checkQueries(qomFactory.propertyValue("s", propertyName2),
+                false, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.NAME,
+                new boolean[]{true, false, false, false, false});
+    }
+
+    public void testPropertyValue() throws RepositoryException {
+        // upper case
+        checkQueries(qomFactory.propertyValue("s", propertyName1),
+                true, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.STRING,
+                new boolean[]{false, false, false, false, true});
+
+        checkQueries(qomFactory.propertyValue("s", propertyName2),
+                true, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.STRING,
+                new boolean[]{false, false, false, false, true});
+
+        // lower case
+        checkQueries(qomFactory.propertyValue("s", propertyName1),
+                false, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.STRING,
+                new boolean[]{true, false, false, false, false});
+
+        checkQueries(qomFactory.propertyValue("s", propertyName2),
+                false, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.STRING,
+                new boolean[]{true, false, false, false, false});
+    }
+
+    public void testUpperLowerCase() throws RepositoryException {
+        // first upper case, then lower case again
+        checkQueries(qomFactory.upperCase(qomFactory.propertyValue("s", propertyName1)),
+                false, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.STRING,
+                new boolean[]{true, false, false, false, false});
+    }
+
+    public void testUpperCaseTwice() throws RepositoryException {
+        // upper case twice
+        checkQueries(qomFactory.upperCase(qomFactory.propertyValue("s", propertyName1)),
+                true, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.STRING,
+                new boolean[]{false, false, false, false, true});
+    }
+
+    public void testLowerUpperCase() throws RepositoryException {
+        // first lower case, then upper case again
+        checkQueries(qomFactory.lowerCase(qomFactory.propertyValue("s", propertyName1)),
+                true, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.STRING,
+                new boolean[]{false, false, false, false, true});
+    }
+
+    public void testLowerCaseTwice() throws RepositoryException {
+        // lower case twice
+        checkQueries(qomFactory.lowerCase(qomFactory.propertyValue("s", propertyName1)),
+                false, OPERATOR_EQUAL_TO,
+                new String[]{"abc", "Abc", "aBc", "abC", "ABC"},
+                PropertyType.STRING,
+                new boolean[]{true, false, false, false, false});
+    }
+
+    //-------------------------------< internal >-------------------------------
+
+    private void checkQueries(DynamicOperand operand,
+                              boolean toUpper,
+                              int operator,
+                              String[] literals,
+                              int type,
+                              boolean[] matches) throws RepositoryException {
+        for (int i = 0; i < literals.length; i++) {
+            Query query = createQuery(operand, toUpper, operator, vf.createValue(literals[i],
type));
+            checkResult(query.execute(), matches[i] ? new Node[]{node} : new Node[0]);
+        }
+    }
+    
+    private Query createQuery(DynamicOperand operand,
+                              boolean toUpper,
+                              int operator,
+                              Value literal) throws RepositoryException {
+        if (toUpper) {
+            operand = qomFactory.upperCase(operand);
+        } else {
+            operand = qomFactory.lowerCase(operand);
+        }
+        return qomFactory.createQuery(
+                qomFactory.selector(testNodeType, "s"),
+                qomFactory.and(
+                        qomFactory.childNode("s", testRoot),
+                        qomFactory.comparison(
+                                operand,
+                                operator,
+                                qomFactory.literal(literal)
+                        )
+                ), null, null);
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/qom/UpperLowerCaseTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message