Search in sources :

Example 96 with Query

use of javax.jcr.query.Query in project jackrabbit by apache.

the class OrderByMultiTypeTest method testMultipleOrder.

/**
 * Tests order by queries with a String property and a long property.
 */
public void testMultipleOrder() throws Exception {
    Node n1 = testRootNode.addNode(nodeName1, testNodeType);
    Node n2 = testRootNode.addNode(nodeName2, testNodeType);
    Node n3 = testRootNode.addNode(nodeName3, testNodeType);
    n1.setProperty(propertyName1, "aaa");
    n1.setProperty(propertyName2, 3);
    n2.setProperty(propertyName1, "bbb");
    n2.setProperty(propertyName2, 2);
    n3.setProperty(propertyName1, "ccc");
    n3.setProperty(propertyName2, 2);
    testRootNode.getSession().save();
    // both ascending
    String sql = "SELECT " + propertyName2 + " FROM " + testNodeType + " WHERE " + jcrPath + " LIKE '" + testRoot + "/%' ORDER BY " + propertyName2 + ", " + propertyName1;
    Query q;
    QueryResult result;
    if (checkSQL) {
        q = superuser.getWorkspace().getQueryManager().createQuery(sql, qsSQL);
        result = q.execute();
        checkResultOrder(result, new String[] { nodeName2, nodeName3, nodeName1 });
    }
    String xpath = "/" + jcrRoot + testRoot + "/*[@" + jcrPrimaryType + "='" + testNodeType + "'] order by @" + propertyName2 + ", @" + propertyName1;
    q = superuser.getWorkspace().getQueryManager().createQuery(xpath, qsXPATH);
    result = q.execute();
    checkResultOrder(result, new String[] { nodeName2, nodeName3, nodeName1 });
    // both descending
    sql = "SELECT " + propertyName2 + " FROM " + testNodeType + " WHERE " + jcrPath + " LIKE '" + testRoot + "/%' ORDER BY " + propertyName2 + " DESC, " + propertyName1 + " DESC";
    if (checkSQL) {
        q = superuser.getWorkspace().getQueryManager().createQuery(sql, qsSQL);
        result = q.execute();
        checkResultOrder(result, new String[] { nodeName1, nodeName3, nodeName2 });
    }
    xpath = "/" + jcrRoot + testRoot + "/*[@" + jcrPrimaryType + "='" + testNodeType + "'] order by @" + propertyName2 + " descending, @" + propertyName1 + " descending";
    q = superuser.getWorkspace().getQueryManager().createQuery(xpath, qsXPATH);
    result = q.execute();
    checkResultOrder(result, new String[] { nodeName1, nodeName3, nodeName2 });
    // mixed ascending and descending
    sql = "SELECT " + propertyName2 + " FROM " + testNodeType + " WHERE " + jcrPath + " LIKE '" + testRoot + "/%' ORDER BY " + propertyName2 + " DESC, " + propertyName1;
    if (checkSQL) {
        q = superuser.getWorkspace().getQueryManager().createQuery(sql, qsSQL);
        result = q.execute();
        checkResultOrder(result, new String[] { nodeName1, nodeName2, nodeName3 });
    }
    xpath = "/" + jcrRoot + testRoot + "/*[@" + jcrPrimaryType + "='" + testNodeType + "'] order by @" + propertyName2 + " descending, @" + propertyName1;
    q = superuser.getWorkspace().getQueryManager().createQuery(xpath, qsXPATH);
    result = q.execute();
    checkResultOrder(result, new String[] { nodeName1, nodeName2, nodeName3 });
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node)

Example 97 with Query

use of javax.jcr.query.Query in project jackrabbit by apache.

the class DescendantNodeTest method testNotASelectorName.

public void testNotASelectorName() throws RepositoryException {
    try {
        Query q = qf.createQuery(qf.selector(testNodeType, "s"), qf.descendantNode("x", testRoot), null, null);
        q.execute();
        fail("DescendantNode with an unknown selector name must throw InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
    try {
        String stmt = "SELECT * FROM [" + testNodeType + "] AS s WHERE " + "ISDESCENDANTNODE(x, [" + testRoot + "])";
        qm.createQuery(stmt, Query.JCR_SQL2).execute();
        fail("ISDESCENDANTNODE() with an unknown selector name must throw InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
}
Also used : Query(javax.jcr.query.Query) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 98 with Query

use of javax.jcr.query.Query in project jackrabbit by apache.

the class DescendantNodeTest method testRelativePath.

public void testRelativePath() throws RepositoryException {
    try {
        Query q = qf.createQuery(qf.selector(testNodeType, "s"), qf.descendantNode("s", testPath), null, null);
        q.execute();
        fail("DescendantNode with relative path argument must throw InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
    try {
        String stmt = "SELECT * FROM [" + testNodeType + "] AS s WHERE " + "ISDESCENDANTNODE(s, [" + testPath + "])";
        qm.createQuery(stmt, Query.JCR_SQL2).execute();
        fail("ISDESCENDANTNODE() with relative path argument must throw InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
}
Also used : Query(javax.jcr.query.Query) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 99 with Query

use of javax.jcr.query.Query in project jackrabbit by apache.

the class DescendantNodeTest method testSyntacticallyInvalidPath.

public void testSyntacticallyInvalidPath() throws RepositoryException {
    String invalidPath = testRoot + "/" + nodeName1 + "[";
    try {
        Query q = qf.createQuery(qf.selector(testNodeType, "s"), qf.descendantNode("s", invalidPath), null, null);
        q.execute();
        fail("DescendantNode with syntactically invalid path argument must throw InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
    try {
        String stmt = "SELECT * FROM [" + testNodeType + "] AS s WHERE " + "ISDESCENDANTNODE(s, [" + invalidPath + "])";
        qm.createQuery(stmt, Query.JCR_SQL2).execute();
        fail("ISDESCENDANTNODE() with syntactically invalid path argument must throw InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
}
Also used : Query(javax.jcr.query.Query) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 100 with Query

use of javax.jcr.query.Query in project jackrabbit by apache.

the class NodeLocalNameTest method testStringLiteral.

public void testStringLiteral() throws RepositoryException {
    Value literal = superuser.getValueFactory().createValue(nodeLocalName);
    Query q = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
    checkResult(q.execute(), new Node[] { node1 });
}
Also used : Query(javax.jcr.query.Query) Value(javax.jcr.Value)

Aggregations

Query (javax.jcr.query.Query)228 Node (javax.jcr.Node)141 QueryResult (javax.jcr.query.QueryResult)122 QueryManager (javax.jcr.query.QueryManager)83 Session (javax.jcr.Session)67 NodeIterator (javax.jcr.NodeIterator)51 Test (org.junit.Test)36 RowIterator (javax.jcr.query.RowIterator)35 RepositoryException (javax.jcr.RepositoryException)33 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)24 ArrayList (java.util.ArrayList)15 Row (javax.jcr.query.Row)15 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)15 InvalidQueryException (javax.jcr.query.InvalidQueryException)14 FacetResult (org.apache.jackrabbit.oak.query.facet.FacetResult)13 ValueFactory (javax.jcr.ValueFactory)12 Value (javax.jcr.Value)9 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)8 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)6 Calendar (java.util.Calendar)5