use of javax.jcr.ValueFactory in project jackrabbit-oak by apache.
the class QueryTest method noLiterals.
@Test
public void noLiterals() throws RepositoryException {
Session session = getAdminSession();
ValueFactory vf = session.getValueFactory();
QueryManager qm = session.getWorkspace().getQueryManager();
// insecure
try {
Query q = qm.createQuery("select text from [nt:base] where password = 'x'", Query.JCR_SQL2 + "-noLiterals");
q.execute();
fail();
} catch (InvalidQueryException e) {
assertTrue(e.toString(), e.toString().indexOf("literals of this type not allowed") > 0);
}
// secure
Query q = qm.createQuery("select text from [nt:base] where password = $p", Query.JCR_SQL2 + "-noLiterals");
q.bindValue("p", vf.createValue("x"));
q.execute();
}
use of javax.jcr.ValueFactory in project jackrabbit-oak by apache.
the class QueryTest method limit.
@Test
public void limit() throws RepositoryException {
Session session = getAdminSession();
Node hello1 = session.getRootNode().addNode("hello1");
hello1.setProperty("id", "1");
hello1.setProperty("data", "x");
session.save();
Node hello3 = session.getRootNode().addNode("hello3");
hello3.setProperty("id", "3");
hello3.setProperty("data", "z");
session.save();
Node hello2 = session.getRootNode().addNode("hello2");
hello2.setProperty("id", "2");
hello2.setProperty("data", "y");
session.save();
ValueFactory vf = session.getValueFactory();
QueryManager qm = session.getWorkspace().getQueryManager();
Query q = qm.createQuery("select id from [nt:base] where data >= $data order by id", Query.JCR_SQL2);
q.bindValue("data", vf.createValue("x"));
for (int limit = 0; limit < 5; limit++) {
q.setLimit(limit);
for (int offset = 0; offset < 3; offset++) {
q.setOffset(offset);
QueryResult r = q.execute();
RowIterator it = r.getRows();
int l = Math.min(Math.max(0, 3 - offset), limit);
assertEquals(l, r.getRows().getSize());
assertEquals(l, r.getNodes().getSize());
Row row;
for (int x = offset + 1, i = 0; i < limit && x < 4; i++, x++) {
assertTrue(it.hasNext());
row = it.nextRow();
assertEquals("" + x, row.getValue("id").getString());
}
assertFalse(it.hasNext());
}
}
}
use of javax.jcr.ValueFactory in project jackrabbit-oak by apache.
the class QueryTest method simple.
@SuppressWarnings("deprecation")
@Test
public void simple() throws RepositoryException {
Session session = getAdminSession();
Node hello = session.getRootNode().addNode("hello");
hello.setProperty("id", "1");
hello.setProperty("text", "hello_world");
session.save();
Node hello2 = session.getRootNode().addNode("hello2");
hello2.setProperty("id", "2");
hello2.setProperty("text", "hello world");
session.save();
ValueFactory vf = session.getValueFactory();
QueryManager qm = session.getWorkspace().getQueryManager();
// SQL-2
Query q = qm.createQuery("select text from [nt:base] where id = $id", Query.JCR_SQL2);
q.bindValue("id", vf.createValue("1"));
QueryResult r = q.execute();
RowIterator it = r.getRows();
assertTrue(it.hasNext());
Row row = it.nextRow();
assertEquals("hello_world", row.getValue("text").getString());
String[] columns = r.getColumnNames();
assertEquals(1, columns.length);
assertEquals("text", columns[0]);
assertFalse(it.hasNext());
r = q.execute();
NodeIterator nodeIt = r.getNodes();
assertTrue(nodeIt.hasNext());
Node n = nodeIt.nextNode();
assertEquals("hello_world", n.getProperty("text").getString());
assertFalse(it.hasNext());
// SQL
q = qm.createQuery("select text from [nt:base] where text like 'hello\\_world' escape '\\'", Query.SQL);
r = q.execute();
columns = r.getColumnNames();
assertEquals(3, columns.length);
assertEquals("text", columns[0]);
assertEquals("jcr:path", columns[1]);
assertEquals("jcr:score", columns[2]);
nodeIt = r.getNodes();
assertTrue(nodeIt.hasNext());
n = nodeIt.nextNode();
assertEquals("hello_world", n.getProperty("text").getString());
assertFalse(nodeIt.hasNext());
// XPath
q = qm.createQuery("//*[@id=1]", Query.XPATH);
r = q.execute();
assertEquals(newHashSet("jcr:path", "jcr:score", "jcr:primaryType"), newHashSet(r.getColumnNames()));
}
use of javax.jcr.ValueFactory in project jackrabbit-oak by apache.
the class QueryTest method testJoin.
public void testJoin() throws Exception {
// create a visible node /test/node1
// with an invisible child /test/node1/node2
// with an invisible child /test/node1/node2/node3
Node n = superuser.getNode(path);
Node visible = n.addNode(nodeName1, testNodeType);
allow(visible.getPath(), privilegesFromName(Privilege.JCR_READ));
Node invisible = visible.addNode(nodeName2, testNodeType);
Node invisible2 = invisible.addNode(nodeName3, testNodeType);
deny(invisible.getPath(), privilegesFromName(Privilege.JCR_READ));
deny(invisible2.getPath(), privilegesFromName(Privilege.JCR_READ));
superuser.save();
// test visibility
testSession.refresh(false);
testSession.checkPermission(visible.getPath(), Session.ACTION_READ);
try {
testSession.checkPermission(invisible.getPath(), Session.ACTION_READ);
fail();
} catch (AccessControlException e) {
// expected
}
Node x = testSession.getNode(visible.getPath());
ValueFactory vf = testSession.getValueFactory();
Query q;
QueryResult r;
NodeIterator ni;
// verify we can see the visible node
q = testSession.getWorkspace().getQueryManager().createQuery("select * from [nt:base] where [jcr:path]=$path", Query.JCR_SQL2);
q.bindValue("path", vf.createValue(visible.getPath()));
r = q.execute();
ni = r.getNodes();
assertTrue(ni.hasNext());
x = ni.nextNode();
assertTrue(x.getSession() == testSession);
// verify we cannot see the invisible node
q = testSession.getWorkspace().getQueryManager().createQuery("select * from [nt:base] where [jcr:path]=$path", Query.JCR_SQL2);
q.bindValue("path", vf.createValue(invisible.getPath()));
r = q.execute();
assertFalse(r.getNodes().hasNext());
// the superuser should see both nodes
q = superuser.getWorkspace().getQueryManager().createQuery("select a.* from [nt:base] as a " + "inner join [nt:base] as b on isdescendantnode(b, a) " + "where a.[jcr:path]=$path", Query.JCR_SQL2);
q.bindValue("path", vf.createValue(visible.getPath()));
r = q.execute();
assertTrue(r.getNodes().hasNext());
// but the testSession must not:
// verify we can not deduce existence of the invisible node
// using a join
q = testSession.getWorkspace().getQueryManager().createQuery("select a.* from [nt:base] as a " + "inner join [nt:base] as b on isdescendantnode(b, a) " + "where a.[jcr:path]=$path", Query.JCR_SQL2);
q.bindValue("path", vf.createValue(visible.getPath()));
r = q.execute();
assertFalse(r.getNodes().hasNext());
}
use of javax.jcr.ValueFactory in project jackrabbit by apache.
the class ReferencesTest method testRegularReference.
/**
* Regular references usage.
*
* @throws Exception on test error
*/
public void testRegularReference() throws Exception {
Session session1 = createSession();
Node bses1 = getTestRootNode(session1).getNode("A").getNode("B");
getTestRootNode(session1).getNode("A").setProperty("ref to B", bses1);
Session session2 = createSession();
ValueFactory valFac2 = session2.getValueFactory();
Node bses2 = getTestRootNode(session2).getNode("A").getNode("B");
getTestRootNode(session2).getNode("C").setProperty("ref to B", new Value[] { valFac2.createValue(bses2), valFac2.createValue(bses2) });
getTestRootNode(session2).getNode("C").setProperty("another ref to B", bses2);
saveAndlogout(session1, session2);
assertRemoveTestNodes();
}
Aggregations