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();
}
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();
}
}
}
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)"));
}
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));
}
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();
}
Aggregations