jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r786480 - in /jackrabbit/trunk: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/ jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/ jackrabbit-jc...
Date Fri, 19 Jun 2009 12:36:22 GMT
Author: mreutegg
Date: Fri Jun 19 12:36:22 2009
New Revision: 786480

URL: http://svn.apache.org/viewvc?rev=786480&view=rev
Log:
JCR-2085: test case (TCK) maintenance for JCR 2.0
- more query tests
- storing of JQOM and SQL2 as node

Added:
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/CreateQueryTest.java
  (with props)
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/GetQueryTest.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SearchManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/AbstractQueryImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryManagerImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/AbstractQueryTest.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/GetLanguageTest.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/TestAll.java
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/TestAll.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/SQL2QOMBuilder.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/query/sql2/ParserTest.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SearchManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SearchManager.java?rev=786480&r1=786479&r2=786480&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SearchManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SearchManager.java
Fri Jun 19 12:36:22 2009
@@ -85,11 +85,6 @@
     public static final String NS_XS_URI = "http://www.w3.org/2001/XMLSchema";
 
     /**
-     * Name of the parameter that indicates the query implementation class.
-     */
-    private static final String PARAM_QUERY_IMPL = "queryClass";
-
-    /**
      * The search configuration.
      */
     private final SearchConfig config;
@@ -274,9 +269,12 @@
      *
      * @param session   the session of the user executing the query.
      * @param itemMgr   the item manager of the user executing the query. Needed
-     *                  to return <code>Node</code> instances in the result set.
+     *                  to return <code>Node</code> instances in the result
+     *                  set.
      * @param statement the actual query statement.
      * @param language  the syntax of the query statement.
+     * @param node      a nt:query node where the query was read from or
+     *                  <code>null</code> if it is not a stored query.
      * @return a <code>Query</code> instance to execute.
      * @throws InvalidQueryException if the query is malformed or the
      *                               <code>language</code> is unknown.
@@ -285,10 +283,11 @@
     public Query createQuery(SessionImpl session,
                              ItemManager itemMgr,
                              String statement,
-                             String language)
+                             String language,
+                             Node node)
             throws InvalidQueryException, RepositoryException {
         AbstractQueryImpl query = createQueryInstance();
-        query.init(session, itemMgr, handler, statement, language);
+        query.init(session, itemMgr, handler, statement, language, node);
         return query;
     }
 
@@ -298,6 +297,8 @@
      * @param session   the session of the user executing the query.
      * @param qomTree   the query object model tree, representing the query.
      * @param langugage the original language of the query statement.
+     * @param node      a nt:query node where the query was read from or
+     *                  <code>null</code> if it is not a stored query.
      * @return the query object model for the query.
      * @throws InvalidQueryException the the query object model tree is
      *                               considered invalid by the query handler
@@ -306,35 +307,15 @@
      */
     public QueryObjectModel createQueryObjectModel(SessionImpl session,
                                                    QueryObjectModelTree qomTree,
-                                                   String langugage)
+                                                   String langugage,
+                                                   Node node)
             throws InvalidQueryException, RepositoryException {
         QueryObjectModelImpl qom = new QueryObjectModelImpl();
-        qom.init(session, session.getItemManager(), handler, qomTree, langugage);
+        qom.init(session, session.getItemManager(), handler, qomTree, langugage, node);
         return qom;
     }
 
     /**
-     * Creates a query object from a node that can be executed on the workspace.
-     *
-     * @param session the session of the user executing the query.
-     * @param itemMgr the item manager of the user executing the query. Needed
-     *                to return <code>Node</code> instances in the result set.
-     * @param node a node of type nt:query.
-     * @return a <code>Query</code> instance to execute.
-     * @throws InvalidQueryException if <code>absPath</code> is not a valid
-     *                               persisted query (that is, a node of type nt:query)
-     * @throws RepositoryException   if any other error occurs.
-     */
-    public Query createQuery(SessionImpl session,
-                             ItemManager itemMgr,
-                             Node node)
-            throws InvalidQueryException, RepositoryException {
-        AbstractQueryImpl query = createQueryInstance();
-        query.init(session, itemMgr, handler, node);
-        return query;
-    }
-
-    /**
      * Checks if the given event should be excluded based on the
      * {@link #excludePath} setting.
      *
@@ -372,11 +353,11 @@
         long time = System.currentTimeMillis();
 
         // nodes that need to be removed from the index.
-        final Set removedNodes = new HashSet();
+        final Set<NodeId> removedNodes = new HashSet<NodeId>();
         // nodes that need to be added to the index.
-        final Map addedNodes = new HashMap();
+        final Map<NodeId, EventImpl> addedNodes = new HashMap<NodeId, EventImpl>();
         // property events
-        List propEvents = new ArrayList();
+        List<EventImpl> propEvents = new ArrayList<EventImpl>();
 
         while (events.hasNext()) {
             EventImpl e = (EventImpl) events.nextEvent();
@@ -407,8 +388,7 @@
         }
 
         // sort out property events
-        for (int i = 0; i < propEvents.size(); i++) {
-            EventImpl e = (EventImpl) propEvents.get(i);
+        for (EventImpl e : propEvents) {
             NodeId nodeId = e.getParentId();
             if (e.getType() == Event.PROPERTY_ADDED) {
                 if (addedNodes.put(nodeId, e) == null) {
@@ -452,7 +432,7 @@
                 } catch (ItemStateException ise) {
                     // check whether this item state change originated from
                     // an external event
-                    EventImpl e = (EventImpl) addedNodes.get(id);
+                    EventImpl e = addedNodes.get(id);
                     if (e == null || !e.isExternal()) {
                         log.error("Unable to index node " + id + ": does not exist");
                     } else {

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/AbstractQueryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/AbstractQueryImpl.java?rev=786480&r1=786479&r2=786480&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/AbstractQueryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/AbstractQueryImpl.java
Fri Jun 19 12:36:22 2009
@@ -17,13 +17,11 @@
 package org.apache.jackrabbit.core.query;
 
 import javax.jcr.Node;
-import javax.jcr.RepositoryException;
 import javax.jcr.query.InvalidQueryException;
 import javax.jcr.query.Query;
 
 import org.apache.jackrabbit.core.ItemManager;
 import org.apache.jackrabbit.core.SessionImpl;
-import org.apache.jackrabbit.spi.commons.query.qom.QueryObjectModelTree;
 
 /**
  * Defines common initialization methods for all query implementations.
@@ -38,6 +36,8 @@
      * @param handler   the query handler of the search index.
      * @param statement the query statement.
      * @param language  the syntax of the query statement.
+     * @param node      a nt:query node where the query was read from or
+     *                  <code>null</code> if it is not a stored query.
      * @throws InvalidQueryException if the query statement is invalid according
      *                               to the specified <code>language</code>.
      */
@@ -45,39 +45,6 @@
                               ItemManager itemMgr,
                               QueryHandler handler,
                               String statement,
-                              String language) throws InvalidQueryException;
-
-    /**
-     * Initializes a query instance from a nt:query node.
-     *
-     * @param session the session of the user executing this query.
-     * @param itemMgr the item manager of the session executing this query.
-     * @param handler the query handler of the search index.
-     * @param node    a node of type <code>nt:query</code>.
-     * @throws InvalidQueryException If <code>node</code> is not a valid persisted
query
-     *                               (that is, a node of type <code>nt:query</code>).
-     * @throws RepositoryException   if another error occurs
-     */
-    public abstract void init(SessionImpl session,
-                              ItemManager itemMgr,
-                              QueryHandler handler,
-                              Node node)
-            throws InvalidQueryException, RepositoryException;
-
-    /**
-     * Initializes a query instance from a query object model.
-     *
-     * @param session  the session of the user executing this query.
-     * @param itemMgr  the item manager of the session executing this query.
-     * @param handler  the query handler of the search index.
-     * @param qomTree  the query object model tree.
-     * @param language the original query syntax from where the JQOM was
-     *                 created.
-     * @throws RepositoryException if another error occurs
-     */
-    public abstract void init(SessionImpl session,
-                              ItemManager itemMgr,
-                              QueryHandler handler,
-                              QueryObjectModelTree qomTree,
-                              String language) throws RepositoryException;
+                              String language,
+                              Node node) throws InvalidQueryException;
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java?rev=786480&r1=786479&r2=786480&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
Fri Jun 19 12:36:22 2009
@@ -35,7 +35,6 @@
 import javax.jcr.query.InvalidQueryException;
 import javax.jcr.query.QueryResult;
 import org.apache.jackrabbit.spi.commons.name.NameConstants;
-import org.apache.jackrabbit.spi.commons.query.qom.QueryObjectModelTree;
 
 import javax.jcr.version.VersionException;
 import java.text.NumberFormat;
@@ -103,53 +102,19 @@
                      ItemManager itemMgr,
                      QueryHandler handler,
                      String statement,
-                     String language) throws InvalidQueryException {
+                     String language,
+                     Node node) throws InvalidQueryException {
         checkNotInitialized();
         this.session = session;
         this.statement = statement;
         this.language = language;
         this.handler = handler;
-        this.query = handler.createExecutableQuery(session, itemMgr, statement, language);
-        setInitialized();
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public void init(SessionImpl session,
-                     ItemManager itemMgr,
-                     QueryHandler handler,
-                     Node node)
-            throws InvalidQueryException, RepositoryException {
-        checkNotInitialized();
-        this.session = session;
         this.node = node;
-        this.handler = handler;
-
-        if (!node.isNodeType(session.getJCRName(NameConstants.NT_QUERY))) {
-            throw new InvalidQueryException("node is not of type nt:query");
-        }
-        statement = node.getProperty(session.getJCRName(NameConstants.JCR_STATEMENT)).getString();
-        language = node.getProperty(session.getJCRName(NameConstants.JCR_LANGUAGE)).getString();
-        query = handler.createExecutableQuery(session, itemMgr, statement, language);
+        this.query = handler.createExecutableQuery(session, itemMgr, statement, language);
         setInitialized();
     }
 
     /**
-     * @inheritDoc
-     * <p/>
-     * Throws an {@link UnsupportedOperationException}.
-     */
-    public void init(SessionImpl session,
-                     ItemManager itemMgr,
-                     QueryHandler handler,
-                     QueryObjectModelTree qomTree,
-                     String language)
-            throws InvalidQueryException, RepositoryException {
-        throw new UnsupportedOperationException("not a prepared query");
-    }
-
-    /**
      * This method simply forwards the <code>execute</code> call to the
      * {@link ExecutableQuery} object returned by
      * {@link QueryHandler#createExecutableQuery}.

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryManagerImpl.java?rev=786480&r1=786479&r2=786480&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryManagerImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryManagerImpl.java
Fri Jun 19 12:36:22 2009
@@ -32,6 +32,7 @@
 import org.apache.jackrabbit.core.SessionImpl;
 import org.apache.jackrabbit.spi.commons.query.qom.QueryObjectModelFactoryImpl;
 import org.apache.jackrabbit.spi.commons.query.qom.QueryObjectModelTree;
+import org.apache.jackrabbit.spi.commons.name.NameConstants;
 
 /**
  * This class implements the {@link QueryManager} interface.
@@ -59,12 +60,6 @@
     private final QueryObjectModelFactoryImpl qomFactory;
 
     /**
-     * The query factory which is responsible to create query instances base
-     * on the passed query language.
-     */
-    private final QueryFactory queryFactory;
-
-    /**
      * Creates a new <code>QueryManagerImpl</code> for the passed
      * <code>session</code>
      *
@@ -86,17 +81,9 @@
             protected QueryObjectModel createQuery(QueryObjectModelTree qomTree)
                     throws InvalidQueryException, RepositoryException {
                 return searchMgr.createQueryObjectModel(
-                        session, qomTree, Query.JCR_SQL2);
+                        session, qomTree, Query.JCR_JQOM, null);
             }
         };
-        this.queryFactory = new CompoundQueryFactory(Arrays.asList(
-                new QOMQueryFactory(qomFactory, session.getValueFactory()),
-                new AQTQueryFactory() {
-                    public Query createQuery(String statement, String language)
-                            throws InvalidQueryException, RepositoryException {
-                        return searchMgr.createQuery(session, itemMgr, statement, language);
-                    }
-                }));
     }
 
     /**
@@ -105,7 +92,8 @@
     public Query createQuery(String statement, String language)
             throws InvalidQueryException, RepositoryException {
         sanityCheck();
-        return queryFactory.createQuery(statement, language);
+        QueryFactory qf = new QueryFactoryImpl(language);
+        return qf.createQuery(statement, language);
     }
 
     /**
@@ -114,15 +102,21 @@
     public Query getQuery(Node node)
             throws InvalidQueryException, RepositoryException {
         sanityCheck();
-        // TODO: support SQL2 and QOM
-        return searchMgr.createQuery(session, itemMgr, node);
+        if (!node.isNodeType(session.getJCRName(NameConstants.NT_QUERY))) {
+            throw new InvalidQueryException("node is not of type nt:query");
+        }
+        String statement = node.getProperty(session.getJCRName(NameConstants.JCR_STATEMENT)).getString();
+        String language = node.getProperty(session.getJCRName(NameConstants.JCR_LANGUAGE)).getString();
+
+        QueryFactory qf = new QueryFactoryImpl(node, language);
+        return qf.createQuery(statement, language);
     }
 
     /**
      * {@inheritDoc}
      */
     public String[] getSupportedQueryLanguages() throws RepositoryException {
-        List<String> languages = queryFactory.getSupportedLanguages();
+        List<String> languages = new QueryFactoryImpl(Query.JCR_JQOM).getSupportedLanguages();
         return languages.toArray(new String[languages.size()]);
     }
 
@@ -162,4 +156,30 @@
             throw new RepositoryException("corresponding session has been closed");
         }
     }
+
+    private class QueryFactoryImpl extends CompoundQueryFactory {
+
+        public QueryFactoryImpl(String language) {
+            this(null, language);
+        }
+
+        public QueryFactoryImpl(final Node node, final String language) {
+            super(Arrays.asList(
+                new QOMQueryFactory(new QueryObjectModelFactoryImpl(
+                        session, session.getValueFactory()) {
+                    protected QueryObjectModel createQuery(QueryObjectModelTree qomTree)
+                            throws InvalidQueryException, RepositoryException {
+                        return searchMgr.createQueryObjectModel(
+                                session, qomTree, language, node);
+                    }
+                }, session.getValueFactory()),
+                new AQTQueryFactory() {
+                    public Query createQuery(String statement,
+                                             String language)
+                            throws InvalidQueryException, RepositoryException {
+                        return searchMgr.createQuery(session, itemMgr, statement, language,
node);
+                    }
+                }));
+        }
+    }
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java?rev=786480&r1=786479&r2=786480&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
Fri Jun 19 12:36:22 2009
@@ -27,12 +27,11 @@
 
 import org.apache.jackrabbit.core.ItemManager;
 import org.apache.jackrabbit.core.SessionImpl;
-import org.apache.jackrabbit.spi.commons.name.NameConstants;
 import org.apache.jackrabbit.spi.commons.query.qom.QueryObjectModelTree;
 import org.apache.jackrabbit.spi.commons.query.QueryObjectModelBuilderRegistry;
 
 /**
- * <code>QueryObjectModelImpl</code>...
+ * <code>QueryObjectModelImpl</code> implements the query object model.
  */
 public class QueryObjectModelImpl extends QueryImpl implements QueryObjectModel {
 
@@ -42,65 +41,51 @@
     protected QueryObjectModelTree qomTree;
 
     /**
-     * @inheritDoc
+     * {@inheritDoc}
+     * @throws UnsupportedOperationException always.
      */
     public void init(SessionImpl session,
                      ItemManager itemMgr,
                      QueryHandler handler,
                      String statement,
-                     String language) throws InvalidQueryException {
-        checkNotInitialized();
-        this.session = session;
-        this.language = language;
-        this.handler = handler;
-        this.statement = statement;
-        this.query = handler.createExecutableQuery(session, itemMgr,
-                createQOMTree(statement, language));
-        setInitialized();
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public void init(SessionImpl session,
-                     ItemManager itemMgr,
-                     QueryHandler handler,
-                     Node node) throws InvalidQueryException, RepositoryException {
-        checkNotInitialized();
-        this.session = session;
-        this.node = node;
-        this.handler = handler;
-
-        if (!node.isNodeType(session.getJCRName(NameConstants.NT_QUERY))) {
-            throw new InvalidQueryException("node is not of type nt:query");
-        }
-        this.statement = node.getProperty(session.getJCRName(NameConstants.JCR_STATEMENT)).getString();
-        this.language = node.getProperty(session.getJCRName(NameConstants.JCR_LANGUAGE)).getString();
-        this.query = handler.createExecutableQuery(session, itemMgr,
-                createQOMTree(statement, language));
-        setInitialized();
+                     String language,
+                     Node node) throws InvalidQueryException {
+        throw new UnsupportedOperationException();
     }
 
     /**
-     * @inheritDoc
+     * Initializes a query instance from a query object model.
+     *
+     * @param session  the session of the user executing this query.
+     * @param itemMgr  the item manager of the session executing this query.
+     * @param handler  the query handler of the search index.
+     * @param qomTree  the query object model tree.
+     * @param language the original query syntax from where the JQOM was
+     *                 created.
+     * @param node     a nt:query node where the query was read from or
+     *                 <code>null</code> if it is not a stored query.
+     * @throws InvalidQueryException if the qom tree cannot be serialized
+     *                               according to the given language.
+     * @throws RepositoryException   if another error occurs
      */
     public void init(SessionImpl session,
                      ItemManager itemMgr,
                      QueryHandler handler,
                      QueryObjectModelTree qomTree,
-                     String language)
+                     String language,
+                     Node node)
             throws InvalidQueryException, RepositoryException {
         checkNotInitialized();
         this.session = session;
         this.language = language;
         this.handler = handler;
         this.qomTree = qomTree;
+        this.node = node;
         this.statement = QueryObjectModelBuilderRegistry.getQueryObjectModelBuilder(language).toString(this);
         this.query = handler.createExecutableQuery(session, itemMgr, qomTree);
         setInitialized();
     }
 
-
     //-------------------------< QueryObjectModel >-----------------------------
 
     /**
@@ -138,22 +123,4 @@
     public Column[] getColumns() {
         return qomTree.getColumns();
     }
-
-    //------------------------------< internal >--------------------------------
-
-    /**
-     * Creates a {@link QueryObjectModelTree} representation for the query
-     * <code>statement</code>.
-     *
-     * @param statement the query statement.
-     * @param language  the language of the query statement.
-     * @return the {@link QueryObjectModelTree} representation.
-     * @throws InvalidQueryException if the query statement is malformed.
-     */
-    private QueryObjectModelTree createQOMTree(String statement,
-                                               String language)
-            throws InvalidQueryException {
-        // TODO: implement
-        return null;
-    }
 }

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/AbstractQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/AbstractQueryTest.java?rev=786480&r1=786479&r2=786480&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/AbstractQueryTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/AbstractQueryTest.java
Fri Jun 19 12:36:22 2009
@@ -34,6 +34,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Arrays;
 
 /**
  * Abstract base class for query test cases.
@@ -341,4 +342,15 @@
             return '"' + identifier + '"';
         }
     }
+
+    /**
+     * @param language a query language.
+     * @return <code>true</code> if <code>language</code> is supported;
+     *         <code>false</code> otherwise.
+     * @throws RepositoryException if an error occurs.
+     */
+    protected boolean isSupportedLanguage(String language)
+            throws RepositoryException {
+        return Arrays.asList(qm.getSupportedQueryLanguages()).contains(language);
+    }
 }

Added: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/CreateQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/CreateQueryTest.java?rev=786480&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/CreateQueryTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/CreateQueryTest.java
Fri Jun 19 12:36:22 2009
@@ -0,0 +1,45 @@
+/*
+ * 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.test.api.query;
+
+import java.util.List;
+import java.util.Arrays;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.query.QueryManager;
+import javax.jcr.query.InvalidQueryException;
+
+/**
+ * <code>CreateQueryTest</code> checks if {@link QueryManager#createQuery(String,
String)}
+ * throws an {@link InvalidQueryException} for an unknown query language.
+ */
+public class CreateQueryTest extends AbstractQueryTest {
+
+    public void testUnknownQueryLanguage() throws RepositoryException {
+        List supported = Arrays.asList(qm.getSupportedQueryLanguages());
+        String language;
+        do {
+            language = createRandomString(5);
+        } while (supported.contains(language));
+        try {
+            qm.createQuery("foo", language);
+            fail("createQuery() must throw for unknown query language: " + language);
+        } catch (InvalidQueryException e) {
+            // expected
+        }
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/CreateQueryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/GetLanguageTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/GetLanguageTest.java?rev=786480&r1=786479&r2=786480&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/GetLanguageTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/GetLanguageTest.java
Fri Jun 19 12:36:22 2009
@@ -17,9 +17,12 @@
 package org.apache.jackrabbit.test.api.query;
 
 import javax.jcr.query.Query;
+import javax.jcr.query.qom.QueryObjectModel;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import org.apache.jackrabbit.test.NotExecutableException;
+
 /**
  * Test the method {@link Query#getLanguage()}.
  *
@@ -63,4 +66,40 @@
         Query q = session.getWorkspace().getQueryManager().createQuery(statement, Query.XPATH);
         assertEquals("Query returns wrong language.", Query.XPATH, q.getLanguage());
     }
+
+    /**
+     * Tests if a SQL query returns {@link Query#SQL} when calling
+     * {@link Query#getLanguage()}.
+     */
+    public void testSQL() throws RepositoryException, NotExecutableException {
+        if (isSupportedLanguage(Query.SQL)) {
+            String stmt = "select * from " + testNodeType;
+            Query q = session.getWorkspace().getQueryManager().createQuery(stmt, Query.SQL);
+            assertEquals("Query returns wrong language.", Query.SQL, q.getLanguage());
+        } else {
+            throw new NotExecutableException("SQL not supported");
+        }
+    }
+
+    /**
+     * Tests if a JCR_SQL2 query returns {@link Query#JCR_SQL2} when calling
+     * {@link Query#getLanguage()}.
+     */
+    public void testJCRSQL2() throws RepositoryException {
+        String stmt = "SELECT * FROM [" + testNodeType + "]";
+        Query q = session.getWorkspace().getQueryManager().createQuery(stmt, Query.JCR_SQL2);
+        assertEquals("Query returns wrong language.", Query.JCR_SQL2, q.getLanguage());
+    }
+
+    /**
+     * Tests if a query object model returns {@link Query#JCR_JQOM} when calling
+     * {@link Query#getLanguage()}.
+     */
+    public void testJCRQOM() throws RepositoryException {
+        QueryObjectModel qom = qf.createQuery(
+                qf.selector(testNodeType, "s"),
+                null, null, null
+        );
+        assertEquals("Query returns wrong language.", Query.JCR_JQOM, qom.getLanguage());
+    }
 }

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/TestAll.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/TestAll.java?rev=786480&r1=786479&r2=786480&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/TestAll.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/TestAll.java
Fri Jun 19 12:36:22 2009
@@ -57,6 +57,7 @@
         suite.addTestSuite(GetPersistentQueryPathTest.class);
         suite.addTestSuite(GetStatementTest.class);
         suite.addTestSuite(GetSupportedQueryLanguagesTest.class);
+        suite.addTestSuite(CreateQueryTest.class);
 
         suite.addTestSuite(QueryResultNodeIteratorTest.class);
         suite.addTestSuite(GetPropertyNamesTest.class);

Added: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/GetQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/GetQueryTest.java?rev=786480&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/GetQueryTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/GetQueryTest.java
Fri Jun 19 12:36:22 2009
@@ -0,0 +1,97 @@
+/*
+ * 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.test.api.query.qom;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Iterator;
+
+import javax.jcr.query.QueryManager;
+import javax.jcr.query.Query;
+import javax.jcr.query.InvalidQueryException;
+import javax.jcr.query.qom.QueryObjectModel;
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.nodetype.NoSuchNodeTypeException;
+
+import org.apache.jackrabbit.test.NotExecutableException;
+
+/**
+ * <code>GetQueryTest</code> contains test cases that check
+ * {@link QueryManager#getQuery(Node)}.
+ */
+public class GetQueryTest extends AbstractQOMTest {
+
+    public void testGetQuery() throws RepositoryException, NotExecutableException {
+        checkNtQuery();
+        Node n = testRootNode.addNode(nodeName1, testNodeType);
+        superuser.save();
+        List queries = new ArrayList();
+        QueryObjectModel qom = qf.createQuery(
+                qf.selector(testNodeType, "s"),
+                qf.childNode("s", testRoot),
+                null,
+                null
+        );
+        queries.add(qom);
+        queries.add(qm.createQuery(qom.getStatement(), Query.JCR_SQL2));
+        if (isSupportedLanguage(Query.XPATH)) {
+            String xpath = testPath + "/element(*, " + testNodeType + ")";
+            queries.add(qm.createQuery(xpath, Query.XPATH));
+        }
+        if (isSupportedLanguage(Query.SQL)) {
+            String sql = "select * from " + testNodeType + " where jcr:path like '" + testRoot
+ "/%'";
+            queries.add(qm.createQuery(sql, Query.SQL));
+        }
+        for (Iterator it = queries.iterator(); it.hasNext(); ) {
+            Query q = (Query) it.next();
+            String lang = q.getLanguage();
+            checkResult(q.execute(), new Node[]{n});
+
+            Node stored = q.storeAsNode(testRoot + "/" + nodeName1);
+            q = qm.getQuery(stored);
+            assertEquals("language of stored query does not match", lang, q.getLanguage());
+            checkResult(q.execute(), new Node[]{n});
+            stored.remove();
+        }
+    }
+
+    public void testInvalidQueryException() throws RepositoryException {
+        try {
+            qm.getQuery(testRootNode);
+            fail("getQuery() must throw InvalidQueryException when node is not of type nt:query");
+        } catch (InvalidQueryException e) {
+            // expected
+        }
+    }
+
+    /**
+     * Checks if the repository supports the nt:query node type otherwise throws
+     * a <code>NotExecutableException</code>.
+     *
+     * @throws NotExecutableException if nt:query is not supported.
+     * @throws RepositoryException if another error occurs.
+     */
+    private void checkNtQuery() throws RepositoryException, NotExecutableException {
+        try {
+            superuser.getWorkspace().getNodeTypeManager().getNodeType(ntQuery);
+        } catch (NoSuchNodeTypeException e) {
+            // not supported
+            throw new NotExecutableException("repository does not support nt:query");
+        }
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/GetQueryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/TestAll.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/TestAll.java?rev=786480&r1=786479&r2=786480&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/TestAll.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/query/qom/TestAll.java
Fri Jun 19 12:36:22 2009
@@ -38,6 +38,7 @@
         suite.addTestSuite(DescendantNodeTest.class);
         suite.addTestSuite(EquiJoinConditionTest.class);
         suite.addTestSuite(FullTextSearchScoreTest.class);
+        suite.addTestSuite(GetQueryTest.class);
         suite.addTestSuite(LengthTest.class);
         suite.addTestSuite(NodeLocalNameTest.class);
         suite.addTestSuite(NodeNameTest.class);

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/SQL2QOMBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/SQL2QOMBuilder.java?rev=786480&r1=786479&r2=786480&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/SQL2QOMBuilder.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/query/sql2/SQL2QOMBuilder.java
Fri Jun 19 12:36:22 2009
@@ -16,6 +16,10 @@
  */
 package org.apache.jackrabbit.spi.commons.query.sql2;
 
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Arrays;
+
 import javax.jcr.query.qom.QueryObjectModel;
 import javax.jcr.query.qom.QueryObjectModelFactory;
 import javax.jcr.query.InvalidQueryException;
@@ -27,11 +31,21 @@
 
 /**
  * <code>SQL2QOMBuilder</code> implements QOM builder that understands
- * {@link Query#JCR_SQL2}.
+ * {@link Query#JCR_SQL2} and {@link Query#JCR_JQOM}. <code>JCR_JQOM</code>
+ * might be surprising, but JSR 283 says that the serialization format of
+ * <code>JCR_JQOM</code> is <code>JCR_SQL2</code>. This is important
when
+ * a JQOM is stored on a node as a serialized String and a language property
+ * set to <code>JCR_JQOM</code>.
  */
 public class SQL2QOMBuilder implements QueryObjectModelBuilder {
 
     /**
+     * Supports {@link Query#JCR_JQOM} and {@link Query#JCR_SQL2}.
+     */
+    private static final List<String> SUPPORTED = new ArrayList<String>(
+            Arrays.asList(Query.JCR_JQOM, Query.JCR_SQL2));
+
+    /**
      * {@inheritDoc}
      */
     public QueryObjectModel createQueryObjectModel(String statement,
@@ -45,14 +59,14 @@
      * {@inheritDoc}
      */
     public boolean canHandle(String language) {
-        return Query.JCR_SQL2.equals(language);
+        return SUPPORTED.contains(language);
     }
 
     /**
      * {@inheritDoc}
      */
     public String[] getSupportedLanguages() {
-        return new String[]{Query.JCR_SQL2};
+        return SUPPORTED.toArray(new String[SUPPORTED.size()]);
     }
 
     /**

Modified: 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=786480&r1=786479&r2=786480&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/query/sql2/ParserTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/query/sql2/ParserTest.java
Fri Jun 19 12:36:22 2009
@@ -55,7 +55,7 @@
             return null;
         }
 
-    };
+    }
 
     protected void setUp() throws Exception {
         super.setUp();



Mime
View raw message