XPathConditionVisitor implements ConditionVisitor { @Override public void visit(@NotNull Condition.Node condition) { statement.append('(') .append("jcr:like(@") .append(QueryUtil.escapeForQuery(UserConstants.REP_AUTHORIZABLE_ID, namePathMapper)) .append(",'") .append(QueryUtil.escapeForQuery(condition.getPattern())) .append("')") .append(" or ") .append("jcr:like(@") .append(QueryUtil.escapeForQuery(UserConstants.REP_PRINCIPAL_NAME, namePathMapper)) .append(",'") .append(QueryUtil.escapeForQuery(condition.getPattern())) .append("')") .append(" or ") .append("jcr:like(fn:name(),'") .append(QueryUtil.escapeForQuery(QueryUtil.escapeNodeName(condition.getPattern()))) .append("')") .append(')'); } XPathConditionVisitor(@NotNull StringBuilder statement, @NotNull NamePathMapper namePathMapper,
                          @NotNull UserManager userMgr); @Override void visit(@NotNull Condition.Node condition); @Override void visit(@NotNull Condition.PropertyValue condition); @Override void visit(Condition.@NotNull PropertyLike condition); @Override void visit(Condition.@NotNull PropertyExists condition); @Override void visit(@NotNull Condition.Contains condition); @Override void visit(@NotNull Condition.Impersonation condition); @Override void visit(@NotNull Condition.Not condition); @Override void visit(@NotNull Condition.And condition); @Override void visit(@NotNull Condition.Or condition);  }
@Test public void testVisitNode() { visitor.visit(new Condition.Node(SERACH_EXPR)); String s = statement.toString(); assertFalse(s.contains(SERACH_EXPR)); assertTrue(s.contains(QueryUtils.escapeForQuery(SERACH_EXPR))); assertTrue(s.contains(QueryUtils.escapeForQuery(QueryUtils.escapeNodeName(SERACH_EXPR)))); }
@Test public void testVisitProperty() throws Exception { Value v = getValueFactory(root).createValue(SERACH_EXPR); for (RelationOp op : RelationOp.values()) { if (op == RelationOp.EX || op == RelationOp.LIKE) { continue; } visitor.visit(new Condition.PropertyValue(REL_PATH, op, v)); String s = statement.toString(); String expected = QueryUtils.escapeForQuery(REL_PATH) + op.getOp() + QueryUtil.format(v); assertEquals(expected, s); statement.delete(0, statement.length()); } }
@Test public void testVisitPropertyExists() { visitor.visit((Condition.PropertyExists) new XPathQueryBuilder().exists(REL_PATH)); assertEquals(QueryUtils.escapeForQuery(REL_PATH), statement.toString()); }
@Test public void testVisitPropertyLike() { visitor.visit((Condition.PropertyLike) new XPathQueryBuilder().like(REL_PATH, SERACH_EXPR)); String s = statement.toString(); assertTrue(s.startsWith("jcr:like(")); assertTrue(s.endsWith(")")); assertFalse(s.contains(REL_PATH)); assertTrue(s.contains(QueryUtils.escapeForQuery(REL_PATH))); assertFalse(s.contains(SERACH_EXPR)); assertTrue(s.contains(QueryUtils.escapeForQuery(SERACH_EXPR))); }
@Test public void testVisitContains() { visitor.visit(testCondition); String s = statement.toString(); assertTrue(s.startsWith("jcr:contains(")); assertTrue(s.endsWith(")")); assertFalse(s.contains(REL_PATH)); assertTrue(s.contains(QueryUtils.escapeForQuery(REL_PATH))); assertFalse(s.contains(SERACH_EXPR)); assertTrue(s.contains(QueryUtils.escapeForQuery(SERACH_EXPR))); }
@Test public void testVisitImpersonation() throws Exception { String principalName = getTestUser().getPrincipal().getName(); Condition.Impersonation c = new Condition.Impersonation(principalName); visitor.visit(c); String s = statement.toString(); assertTrue(s.contains(UserConstants.REP_IMPERSONATORS)); assertFalse(s.contains("@rcj:primaryType='" + UserConstants.NT_REP_USER + "'")); }
@Test public void testVisitImpersonationAdmin() throws Exception { String adminPrincipalName = getUserManager(root).getAuthorizable(getUserConfiguration().getParameters().getConfigValue(UserConstants.PARAM_ADMIN_ID, UserConstants.DEFAULT_ADMIN_ID)).getPrincipal().getName(); Condition.Impersonation c = new Condition.Impersonation(adminPrincipalName); visitor.visit(c); String s = statement.toString(); assertFalse(s.contains(UserConstants.REP_IMPERSONATORS)); assertTrue(s.contains("@rcj:primaryType='" + UserConstants.NT_REP_USER + "'")); }
@Test public void testVisitImpersonationGroup() throws Exception { UserManager uMgr = getUserManager(root); Group g = null; try { g = uMgr.createGroup("g"); root.commit(); Condition.Impersonation c = new Condition.Impersonation(g.getPrincipal().getName()); visitor.visit(c); String s = statement.toString(); assertTrue(s.contains(UserConstants.REP_IMPERSONATORS)); assertFalse(s.contains("@rcj:primaryType='" + UserConstants.NT_REP_USER + "'")); } finally { if (g != null) { g.remove(); root.commit(); } } }
@Test public void testVisitImpersonationNonExistingPrincipal() { Condition.Impersonation c = new Condition.Impersonation("nonExisting"); visitor.visit(c); String s = statement.toString(); assertTrue(s.contains(UserConstants.REP_IMPERSONATORS)); assertFalse(s.contains("@rcj:primaryType='" + UserConstants.NT_REP_USER + "'")); }
@Test public void testVisitNot() throws Exception { visitor.visit(new Condition.Not(testCondition)); assertTrue(statement.toString().startsWith("not(")); assertTrue(statement.toString().endsWith(")")); }
@Test public void testVisitAnd() throws Exception { visitor.visit(new Condition.And(testCondition, testCondition)); assertTrue(statement.toString().contains(" and ")); }
@Test public void testVisitAndSingle() throws Exception { Condition.And c = new Condition.And(testCondition, testCondition); reduceCompoundConditionToSingleTerm(c); visitor.visit(c); assertFalse(statement.toString().contains(" and ")); }
@Test public void testVisitOr() throws Exception { visitor.visit(new Condition.Or(testCondition, testCondition)); String s = statement.toString(); assertTrue(s.contains(" or ")); assertTrue(s.startsWith("(")); assertTrue(s.endsWith("))")); }
@Test public void testVisitOrSingle() throws Exception { Condition.Or c = new Condition.Or(testCondition, testCondition); reduceCompoundConditionToSingleTerm(c); visitor.visit(c); String s = statement.toString(); assertFalse(s.contains(" or ")); assertFalse(s.startsWith("(")); assertFalse(s.endsWith("))")); }