Search in sources :

Example 11 with Session

use of javax.jcr.Session in project jackrabbit-oak by apache.

the class QueryTest method nodeType.

@Test
public void nodeType() throws Exception {
    Session session = createAdminSession();
    String xpath = "/jcr:root//element(*,rep:User)[xyz/@jcr:primaryType]";
    assertPlan(getPlan(session, xpath), "[rep:User] as [a] /* nodeType");
    session.getNode("/oak:index/nodetype").setProperty("declaringNodeTypes", new String[] { "oak:Unstructured" }, PropertyType.NAME);
    session.save();
    assertPlan(getPlan(session, xpath), "[rep:User] as [a] /* traverse ");
    xpath = "/jcr:root//element(*,oak:Unstructured)[xyz/@jcr:primaryType] option(traversal fail)";
    // the plan might still use traversal, so we can't just check the plan;
    // but using "option(traversal fail)" we have ensured that there is an index
    // (the nodetype index) that can serve this query
    getPlan(session, xpath);
    // and without the node type index, it is supposed to fail
    Node nodeTypeIndex = session.getRootNode().getNode("oak:index").getNode("nodetype");
    nodeTypeIndex.setProperty("declaringNodeTypes", new String[] {}, PropertyType.NAME);
    session.save();
    try {
        getPlan(session, xpath);
        fail();
    } catch (InvalidQueryException e) {
    // expected
    }
    session.logout();
}
Also used : Node(javax.jcr.Node) InvalidQueryException(javax.jcr.query.InvalidQueryException) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 12 with Session

use of javax.jcr.Session in project jackrabbit-oak by apache.

the class QueryTest method testOak1096.

@Test
public void testOak1096() throws RepositoryException {
    Session writer = createAdminSession();
    Session reader = createAdminSession();
    try {
        Node rootNode = writer.getRootNode();
        Node node = rootNode.addNode("test", "nt:unstructured");
        node.setProperty("text", "find me");
        writer.save();
        QueryManager qm = reader.getWorkspace().getQueryManager();
        Query q = qm.createQuery("select * from 'nt:base' where contains(*, 'find me')", Query.JCR_SQL2);
        NodeIterator res = q.execute().getNodes();
        assertEquals("False amount of hits", 1, res.getSize());
    } finally {
        if (reader != null) {
            reader.logout();
        }
        if (writer != null) {
            writer.logout();
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 13 with Session

use of javax.jcr.Session in project jackrabbit-oak by apache.

the class QueryTest method traversalOption.

@Test
public void traversalOption() throws Exception {
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    // for union queries:
    // both subqueries use an index
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where ischildnode('/') or [jcr:uuid] = 1 option(traversal fail)"));
    // no subquery uses an index
    assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where [x] = 1 or [y] = 2 option(traversal fail)"));
    // first one does not, second one does
    assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where [jcr:uuid] = 1 or [x] = 2 option(traversal fail)"));
    // first one does, second one does not
    assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] where [x] = 2 or [jcr:uuid] = 1 option(traversal fail)"));
    // queries that possibly use traversal (depending on the join order)
    assertTrue(isValidQuery(qm, "xpath", "/jcr:root/content//*/jcr:content[@jcr:uuid='1'] option(traversal fail)"));
    assertTrue(isValidQuery(qm, "xpath", "/jcr:root/content/*/jcr:content[@jcr:uuid='1'] option(traversal fail)"));
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] as [a] inner join [nt:base] as [b] on ischildnode(b, a) " + "where [a].[jcr:uuid] = 1 option(traversal fail)"));
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] as [a] inner join [nt:base] as [b] on ischildnode(a, b) " + "where [a].[jcr:uuid] = 1 option(traversal fail)"));
    // union with joins
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] as [a] inner join [nt:base] as [b] on ischildnode(a, b) " + "where ischildnode([a], '/') or [a].[jcr:uuid] = 1 option(traversal fail)"));
    assertFalse(isValidQuery(qm, "xpath", "//*[@test] option(traversal fail)"));
    assertFalse(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] option(traversal fail)"));
    assertTrue(isValidQuery(qm, "xpath", "//*[@test] option(traversal ok)"));
    assertTrue(isValidQuery(qm, "xpath", "//*[@test] option(traversal warn)"));
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] option(traversal ok)"));
    assertTrue(isValidQuery(qm, Query.JCR_SQL2, "select * from [nt:base] option(traversal warn)"));
    // the following is not really traversal, it is just listing child nodes:
    assertTrue(isValidQuery(qm, "xpath", "/jcr:root/*[@test] option(traversal fail)"));
    // the following is not really traversal; it is just one node:
    assertTrue(isValidQuery(qm, "xpath", "/jcr:root/oak:index[@test] option(traversal fail)"));
}
Also used : QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 14 with Session

use of javax.jcr.Session in project jackrabbit-oak by apache.

the class QueryTest method twoSelectors.

@Test
public void twoSelectors() throws Exception {
    Session session = getAdminSession();
    Node root = session.getRootNode();
    Node test = root.addNode("test");
    test.addNode("testNode", "oak:Unstructured");
    session.save();
    assertEquals("/test/testNode", getNodeList(session, "select b.[jcr:path] as [jcr:path], b.[jcr:score] as [jcr:score], b.* " + "from [nt:base] as a " + "inner join [nt:base] as b " + "on ischildnode(b, a) " + "where issamenode(a, '/test')", Query.JCR_SQL2));
    assertEquals("/test/testNode", getNodeList(session, "select b.[jcr:path] as [jcr:path], b.[jcr:score] as [jcr:score], b.* " + "from [nt:base] as b " + "inner join [nt:base] as a " + "on ischildnode(b, a) " + "where issamenode(b, '/test/testNode')", Query.JCR_SQL2));
    assertEquals("/test", getNodeList(session, "select a.[jcr:path] as [jcr:path], a.[jcr:score] as [jcr:score], a.* " + "from [nt:base] as a " + "inner join [nt:base] as b " + "on ischildnode(b, a) " + "where issamenode(a, '/test')", Query.JCR_SQL2));
    assertEquals("/test", getNodeList(session, "select a.[jcr:path] as [jcr:path], a.[jcr:score] as [jcr:score], a.* " + "from [nt:base] as b " + "inner join [nt:base] as a " + "on ischildnode(b, a) " + "where issamenode(b, '/test/testNode')", Query.JCR_SQL2));
}
Also used : Node(javax.jcr.Node) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 15 with Session

use of javax.jcr.Session in project jackrabbit-oak by apache.

the class QueryTest method testOak1171.

@Test
public void testOak1171() throws RepositoryException {
    Session session = createAdminSession();
    Node p = session.getRootNode().addNode("etc");
    p.addNode("p1").setProperty("title", "test");
    p.addNode("p2").setProperty("title", 1);
    session.save();
    Query q = session.getWorkspace().getQueryManager().createQuery("//*[@title = 'test']", "xpath");
    QueryResult qr = q.execute();
    NodeIterator ni = qr.getNodes();
    assertTrue(ni.hasNext());
    Node n = ni.nextNode();
    assertEquals("/etc/p1", n.getPath());
    assertFalse(ni.hasNext());
    session.logout();
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Aggregations

Session (javax.jcr.Session)1393 Node (javax.jcr.Node)761 Test (org.junit.Test)387 RepositoryException (javax.jcr.RepositoryException)304 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)162 Property (javax.jcr.Property)90 SimpleCredentials (javax.jcr.SimpleCredentials)88 Privilege (javax.jcr.security.Privilege)84 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)77 Value (javax.jcr.Value)71 Query (javax.jcr.query.Query)69 NodeIterator (javax.jcr.NodeIterator)68 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)65 QueryManager (javax.jcr.query.QueryManager)63 HashMap (java.util.HashMap)57 IOException (java.io.IOException)56 ArrayList (java.util.ArrayList)55 AccessControlManager (javax.jcr.security.AccessControlManager)55 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)52 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)49