VisibilityExpression { public static boolean evaluate(final String expression, final Set<String> auths) { final String trimmed = expression.replaceAll("\\s+", ""); try { return expressionCache.get(trimmed).evaluate(auths); } catch (final ExecutionException e) { throw new RuntimeException(e.getCause()); } }  static boolean evaluate(final String expression, final Set<String> auths);  }
@Test public void testValidVisibilityExpressions() { final String EXPRESSION1 = "(a&b)|c"; assertTrue(VisibilityExpression.evaluate(EXPRESSION1, Sets.newHashSet("a", "b"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION1, Sets.newHashSet("a", "b", "c"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION1, Sets.newHashSet("c"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION1, Sets.newHashSet("a"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION1, Sets.newHashSet("b"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION1, Sets.newHashSet("d"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION1, Sets.newHashSet())); final String EXPRESSION2 = "((a & b) | c) & (d | e)"; assertTrue(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("a", "b", "d"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("a", "b", "e"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("c", "d"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("c", "e"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("a", "c", "d"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("b", "c", "e"))); assertTrue( VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("a", "b", "c", "d", "e"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("a"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("b"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("c"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("d"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("e"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("a", "b"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("a", "d"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("a", "e"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("a", "b", "c"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet())); final String EXPRESSION3 = "(a&b&c)|d|e"; assertTrue(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("a", "b", "c"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("a", "b", "e"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("c", "d"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("c", "e"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("a", "c", "d"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("d"))); assertTrue(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("e"))); assertTrue( VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("a", "b", "c", "d", "e"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("a"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("b"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("c"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("a", "b"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("a", "c"))); assertFalse(VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet())); final String EMPTY_EXPRESSION = ""; assertTrue(VisibilityExpression.evaluate(EMPTY_EXPRESSION, Sets.newHashSet("a", "b"))); assertTrue(VisibilityExpression.evaluate(EMPTY_EXPRESSION, Sets.newHashSet("a", "b", "c"))); assertTrue(VisibilityExpression.evaluate(EMPTY_EXPRESSION, Sets.newHashSet("c"))); assertTrue(VisibilityExpression.evaluate(EMPTY_EXPRESSION, Sets.newHashSet("a"))); assertTrue(VisibilityExpression.evaluate(EMPTY_EXPRESSION, Sets.newHashSet("b"))); assertTrue(VisibilityExpression.evaluate(EMPTY_EXPRESSION, Sets.newHashSet("d"))); assertTrue(VisibilityExpression.evaluate(EMPTY_EXPRESSION, Sets.newHashSet())); }
@Test public void testInvalidVisibilityExpressions() { final String EXPRESSION1 = "(a&b|c"; final String EXPRESSION2 = "a&b)|c"; final String EXPRESSION3 = "a&|b"; final String EXPRESSION4 = "(a)(b)"; final String EXPRESSION5 = "&b"; final String EXPRESSION6 = "a&"; try { VisibilityExpression.evaluate(EXPRESSION1, Sets.newHashSet("a")); fail(); } catch (final Exception e) { assertTrue(e.getCause() instanceof ParseException); assertEquals( "Left parenthesis found with no matching right parenthesis.", e.getCause().getMessage()); } try { VisibilityExpression.evaluate(EXPRESSION2, Sets.newHashSet("a")); fail(); } catch (final Exception e) { assertTrue(e.getCause() instanceof ParseException); assertEquals( "Right parenthesis found with no matching left parenthesis.", e.getCause().getMessage()); } try { VisibilityExpression.evaluate(EXPRESSION3, Sets.newHashSet("a")); fail(); } catch (final Exception e) { assertTrue(e.getCause() instanceof ParseException); assertEquals("Multiple sequential operators.", e.getCause().getMessage()); } try { VisibilityExpression.evaluate(EXPRESSION4, Sets.newHashSet("a")); fail(); } catch (final Exception e) { assertTrue(e.getCause() instanceof ParseException); assertEquals("Multiple sequential operands with no operator.", e.getCause().getMessage()); } try { VisibilityExpression.evaluate(EXPRESSION5, Sets.newHashSet("a")); fail(); } catch (final Exception e) { assertTrue(e.getCause() instanceof ParseException); assertEquals("Operator found with no left operand.", e.getCause().getMessage()); } try { VisibilityExpression.evaluate(EXPRESSION6, Sets.newHashSet("a")); fail(); } catch (final Exception e) { assertTrue(e.getCause() instanceof ParseException); assertEquals("Operator found with no right operand.", e.getCause().getMessage()); } }