/** * Executes the query based on QOM and then pops-up in the node hierarchy until returnItemType is found. If the result * is not returnItemType or none of its parents are then next node in result is checked. * Duplicate nodes are removed from result. * For date/time expressions use <code>DateUtil.create*Expression()</code> methods. * * @return Result as NodeIterator */ public static NodeIterator search(QueryObjectModel model, String returnItemType) throws InvalidQueryException, RepositoryException { return NodeUtil.filterDuplicates(NodeUtil.filterParentNodeType(model.execute().getNodes(), returnItemType)); }
public void testURILiteral() throws RepositoryException { Value literal = superuser.getValueFactory().createValue("http://example.com", PropertyType.URI); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with URI that cannot be converted to NAME must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
public void testStringLiteralInvalidName() throws RepositoryException { Value literal = superuser.getValueFactory().createValue("[" + nodeLocalName); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with STRING that cannot be converted to NAME must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
public void testDateLiteral() throws RepositoryException { Value literal = vf.createValue(Calendar.getInstance()); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with DATE must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } try { String stmt = "SELECT * FROM [" + testNodeType + "] AS s " + "WHERE NAME(s) = CAST('" + literal.getString() + "' AS DATE)"; qm.createQuery(stmt, Query.JCR_SQL2).execute(); fail("NAME() comparison with DATE must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
public void testDecimalLiteral() throws RepositoryException { Value literal = vf.createValue(new BigDecimal(283)); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with DECIMAL must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } try { String stmt = "SELECT * FROM [" + testNodeType + "] AS s " + "WHERE NAME(s) = CAST('" + literal.getString() + "' AS DECIMAL)"; qm.createQuery(stmt, Query.JCR_SQL2).execute(); fail("NAME() comparison with DECIMAL must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
public void testAnd() throws RepositoryException { Node n1 = testRootNode.addNode(nodeName1, testNodeType); n1.setProperty(propertyName1, "foo"); n1.setProperty(propertyName2, "bar"); Node n2 = testRootNode.addNode(nodeName2, testNodeType); n2.setProperty(propertyName2, "bar"); superuser.save(); QueryResult result = qf.createQuery( qf.selector(testNodeType, "s"), qf.and( qf.descendantNode("s", testRootNode.getPath()), qf.and( qf.propertyExistence("s", propertyName1), qf.propertyExistence("s", propertyName2) ) ), null, null ).execute(); checkResult(result, new Node[]{n1}); String stmt = "SELECT * FROM [" + testNodeType + "] AS s WHERE " + "ISDESCENDANTNODE(s, [" + testRootNode.getPath() + "]) " + "AND s.[" + propertyName1 + "] IS NOT NULL " + "AND s.[" + propertyName2 + "] IS NOT NULL"; result = qm.createQuery(stmt, Query.JCR_SQL2).execute(); checkResult(result, new Node[]{n1}); } }
public void testStringLiteralInvalidName() throws RepositoryException { Value literal = vf.createValue("[" + nodeName1); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with STRING that cannot be converted to NAME must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } try { String stmt = "SELECT * FROM [" + testNodeType + "] AS s " + "WHERE NAME(s) = '" + literal.getString() + "'"; qm.createQuery(stmt, Query.JCR_SQL2).execute(); fail("NAME() comparison with STRING that cannot be converted to NAME must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
public void testDoubleLiteral() throws RepositoryException { Value literal = vf.createValue(Math.PI); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with DOUBLE must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } try { String stmt = "SELECT * FROM [" + testNodeType + "] AS s " + "WHERE NAME(s) = CAST('" + literal.getString() + "' AS DOUBLE)"; qm.createQuery(stmt, Query.JCR_SQL2).execute(); fail("NAME() comparison with DOUBLE must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
protected void checkResultOrder(QueryObjectModel qom, String[] selectorNames, Node[][] nodes) throws RepositoryException { checkResultOrder(qom.execute(), selectorNames, nodes); checkResultOrder(qm.createQuery(qom.getStatement(), Query.JCR_SQL2).execute(), selectorNames, nodes); }
public void testReferenceLiteral() throws RepositoryException, NotExecutableException { ensureMixinType(node1, mixReferenceable); superuser.save(); Value literal = vf.createValue(node1); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with REFERENCE must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } try { String stmt = "SELECT * FROM [" + testNodeType + "] AS s " + "WHERE NAME(s) = CAST('" + literal.getString() + "' AS REFERENCE)"; qm.createQuery(stmt, Query.JCR_SQL2).execute(); fail("NAME() comparison with REFERENCE must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
public void testLongLiteral() throws RepositoryException { Value literal = vf.createValue(283); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with LONG must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } try { String stmt = "SELECT * FROM [" + testNodeType + "] AS s " + "WHERE NAME(s) = CAST(" + literal.getString() + " AS LONG)"; qm.createQuery(stmt, Query.JCR_SQL2).execute(); fail("NAME() comparison with LONG must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
public void testBooleanLiteral() throws RepositoryException { Value literal = vf.createValue(true); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with BOOLEAN must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } try { String stmt = "SELECT * FROM [" + testNodeType + "] AS s " + "WHERE NAME(s) = CAST(" + literal.getString() + " AS BOOLEAN)"; qm.createQuery(stmt, Query.JCR_SQL2).execute(); fail("NAME() comparison with BOOLEAN must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
public void testWeakReferenceLiteral() throws RepositoryException, NotExecutableException { ensureMixinType(node1, mixReferenceable); superuser.save(); Value literal = vf.createValue(node1, true); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with WEAKREFERENCE must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } try { String stmt = "SELECT * FROM [" + testNodeType + "] AS s " + "WHERE NAME(s) = CAST('" + literal.getString() + "' AS WEAKREFERENCE)"; qm.createQuery(stmt, Query.JCR_SQL2).execute(); fail("NAME() comparison with absolute WEAKREFERENCE must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
/** * Checks the query object model by executing it directly and matching the * result against the given <code>nodes</code>. Then the QOM is executed * again using {@link QueryObjectModel#getStatement()} with {@link * Query#JCR_SQL2}. * * @param qom the query object model to check. * @param nodes the result nodes. * @throws RepositoryException if an error occurs while executing the * query. */ protected void checkQOM(QueryObjectModel qom, Node[] nodes) throws RepositoryException { checkResult(qom.execute(), nodes); checkResult(qm.createQuery(qom.getStatement(), Query.JCR_SQL2).execute(), nodes); }
/** * Checks the query object model by executing it directly and matching the * result against the given <code>nodes</code>. Then the QOM is executed * again using {@link QueryObjectModel#getStatement()} with * {@link Query#JCR_SQL2}. * * @param qom the query object model to check. * @param selectorNames the selector names of the qom. * @param nodes the result nodes. * @throws RepositoryException if an error occurs while executing the * query. */ protected void checkQOM(QueryObjectModel qom, String[] selectorNames, Node[][] nodes) throws RepositoryException { checkResult(qom.execute(), selectorNames, nodes); checkResult(qm.createQuery(qom.getStatement(), Query.JCR_SQL2).execute(), selectorNames, nodes); }
public void testPathLiteral() throws RepositoryException { Value literal = superuser.getValueFactory().createValue( nodeLocalName, PropertyType.PATH); QueryObjectModel qom = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal); checkQOM(qom, new Node[]{node1}); literal = superuser.getValueFactory().createValue( node1.getPath(), PropertyType.PATH); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with absolute PATH must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } literal = superuser.getValueFactory().createValue( nodeName1 + "/" + nodeName1, PropertyType.PATH); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with PATH length >1 must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
public void testUnknownNodeType() throws RepositoryException { NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager(); String ntName = testNodeType; for (;;) { try { ntMgr.getNodeType(ntName); ntName += "x"; } catch (NoSuchNodeTypeException e) { break; } } try { qf.createQuery(qf.selector(ntName, "s"), null, null, null).execute(); fail("Selector with unknown node type must throw InvalidQueryException"); } catch (InvalidQueryException e) { // expected } try { String stmt = "SELECT * FROM [" + ntName + "] AS nt"; qm.createQuery(stmt, Query.JCR_SQL2).execute(); fail("Selector with unknown node type must throw InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
/** * will build a query directly via the api using a spaced path * * @throws Exception */ public void testGetChildrenApiDirect() throws Exception { QueryObjectModelFactory qomf = qm.getQOMFactory(); Source source1 = qomf.selector(NodeType.NT_BASE, "selector"); Column[] columns = new Column[] { qomf.column("selector", null, null) }; Constraint constraint2 = qomf.childNode("selector", n1.getPath()); QueryObjectModel qom = qomf.createQuery(source1, constraint2, null, columns); checkResult(qom.execute(), 2); }
private Row getRow() throws RepositoryException { QueryObjectModel qom = qf.createQuery( qf.selector(testNodeType, SELECTOR_NAME), qf.descendantNode(SELECTOR_NAME, testRoot), null, new Column[]{qf.column(SELECTOR_NAME, propertyName1, propertyName1)}); RowIterator rows = qom.execute().getRows(); assertTrue("empty result", rows.hasNext()); Row r = rows.nextRow(); assertFalse("result must not contain more than one row", rows.hasNext()); return r; } }
private QueryResult[] executeQueries(String propertyName, String operator, Value length) throws RepositoryException { QueryObjectModel qom = qf.createQuery( qf.selector(testNodeType, "s"), qf.and( qf.childNode("s", testRoot), qf.comparison( qf.length( qf.propertyValue( "s", propertyName)), operator, qf.literal(length)) ), null, null); QueryResult[] results = new QueryResult[2]; results[0] = qom.execute(); results[1] = qm.createQuery(qom.getStatement(), Query.JCR_SQL2).execute(); return results; } }