PredicateParser { public Predicate parse(String predicateString, Object... parameters) { if (predicateString == null) { return null; } Queue<String> tokens = new LinkedList<String>(); char[] predicateChars = predicateString.toCharArray(); for (int i = 0, length = predicateChars.length; i < length; ++ i) { char c = predicateChars[i]; if (Character.isWhitespace(c)) { continue; } StringBuilder tokenBuilder = new StringBuilder(); if ("()[],".indexOf(c) > -1) { tokenBuilder.append(c); } else if ("'\"".indexOf(c) > -1) { char quote = c; for (++ i; i < length; ++ i) { c = predicateChars[i]; if (c == '\\') { ++ i; if (i < length) { tokenBuilder.append(predicateChars[i]); } } else if (c == quote) { break; } else { tokenBuilder.append(c); } } } else { tokenBuilder.append(c); for (++ i; i < length; ++ i) { c = predicateChars[i]; if ("),".indexOf(c) > -1) { -- i; break; } else if (Character.isWhitespace(c)) { break; } else { tokenBuilder.append(c); } } } tokens.add(tokenBuilder.toString()); } return readPredicate(tokens, new ParameterList(parameters)); }  Map<String, String> getCompoundOperators(); Map<String, String> getComparisonOperators(); Map<String, Evaluator> getEvaluators(); Predicate parse(String predicateString, Object... parameters); boolean evaluate(Object object, Predicate predicate); static final String AND_OPERATOR; static final String OR_OPERATOR; static final String NOT_OPERATOR; static final String EQUALS_ANY_OPERATOR; static final String NOT_EQUALS_ALL_OPERATOR; static final String LESS_THAN_OPERATOR; static final String LESS_THAN_OR_EQUALS_OPERATOR; static final String GREATER_THAN_OPERATOR; static final String GREATER_THAN_OR_EQUALS_OPERATOR; static final String STARTS_WITH_OPERATOR; static final String CONTAINS_OPERATOR; static final String MATCHES_ANY_OPERATOR; static final String MATCHES_ALL_OPERATOR; static final String MATCHES_EXACT_ANY_OPERATOR; static final String MATCHES_EXACT_ALL_OPERATOR; }
@Test public void parse_empty() { assertEquals(null, parser.parse("")); }
@Test public void parse_empty_2() { assertEquals(null, parser.parse(" ")); }
@Test public void parse_null() { assertEquals(null, parser.parse(null)); }
@Test public void parse_simple() { Predicate pred = parser.parse("a = 1"); Predicate expect = new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "a", Arrays.asList("1")); assertEquals(expect, pred); }
@Test (expected=IllegalArgumentException.class) public void parse_nospace() { parser.parse("a=1"); }
@Test public void parse_compound() { Predicate pred = parser.parse("a = 1 and b != 2"); Predicate expect = CompoundPredicate.combine( PredicateParser.AND_OPERATOR, new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "a", Arrays.asList("1")), new ComparisonPredicate(PredicateParser.NOT_EQUALS_ALL_OPERATOR, false, "b", Arrays.asList("2")) ); assertEquals(expect, pred); }
@Test public void parse_noparens() { Predicate pred = parser.parse("a = 1 and b = 2 or c = 3"); Predicate expect = CompoundPredicate.combine( PredicateParser.OR_OPERATOR, CompoundPredicate.combine( PredicateParser.AND_OPERATOR, new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "a", Arrays.asList("1")), new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "b", Arrays.asList("2")) ), new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "c", Arrays.asList("3")) ); assertEquals(expect, pred); }
@Test public void parse_parens() { Predicate pred = parser.parse("a = 1 and (b = 2 or c = 3)"); Predicate expect = CompoundPredicate.combine( PredicateParser.AND_OPERATOR, new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "a", Arrays.asList("1")), CompoundPredicate.combine( PredicateParser.OR_OPERATOR, new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "b", Arrays.asList("2")), new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "c", Arrays.asList("3")) ) ); assertEquals(expect, pred); }
@Test public void parse_key_canonical_id() { Predicate expect = new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "_id", Arrays.asList("1")); assertEquals(expect, parser.parse("id = 1")); }
@Test (expected=IllegalArgumentException.class) public void parse_key_missing() { parser.parse(" = 1 "); }
@Test public void parse_operator_negative() { Predicate expect = new ComparisonPredicate(PredicateParser.NOT_EQUALS_ALL_OPERATOR, false, "token1", Arrays.asList("1")); assertEquals(expect, parser.parse("token1 != 1")); }
@Test (expected=IllegalArgumentException.class) public void parse_operator_missing() { parser.parse("a 1"); }
@Test (expected=IllegalArgumentException.class) public void parse_error_value_missing() { parser.parse("a = "); }
@Test public void parse_value_boolean_true() { ComparisonPredicate expect = new ComparisonPredicate( PredicateParser.EQUALS_ANY_OPERATOR, false, "token1", Arrays.asList(true)); assertEquals(expect, parser.parse("token1 = true")); }
@Test public void parse_value_boolean_false() { Predicate expect = new ComparisonPredicate( PredicateParser.EQUALS_ANY_OPERATOR, false, "token1", Arrays.asList(false)); assertEquals(expect, parser.parse("token1 = false")); }
@Test public void parse_value_null() { Predicate expect = new ComparisonPredicate( PredicateParser.EQUALS_ANY_OPERATOR, false, "token1", null); assertEquals(expect, parser.parse("token1 = null")); }
@Test public void parse_params() { Predicate pred = parser.parse("b = ?", "1"); Predicate expect = new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "b", Arrays.asList("1")); assertEquals(expect, pred); }
@Test public void parse_params_multivalue() { Predicate pred = parser.parse("a in ?", Arrays.asList(1,2,3)); Predicate expect = new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "a", Arrays.asList(1,2,3)); assertEquals(expect, pred); }
@Test public void parse_params_notstring() { Predicate pred = parser.parse("b = ?", 1); Predicate expect = new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "b", Arrays.asList(1)); assertEquals(expect, pred); }
@Test public void parse_params_null() { Predicate pred = parser.parse("b = ?", (Object)null); Predicate expect = new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "b", null); assertEquals(expect, pred); }
@Test public void parse_params_missing() { Predicate pred = parser.parse("b = ?"); Predicate expect = new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "b", null); assertEquals(expect, pred); }
@Test public void parse_extra() { Predicate pred = parser.parse("b = ?", "1", "2"); Predicate expect = new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "b", Arrays.asList("1")); assertEquals(expect, pred); }
@Test (expected=IllegalArgumentException.class) public void parse_parens_empty() { assertEquals(null, parser.parse(" ( ) ")); }
@Test public void parse_identity_state() { for (Database database : DATABASES) { TestRecord current = TestRecord.getInstance(database); Predicate predicate = parser.parse("record = ?", current.getState()); Predicate expect = new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "record", Arrays.asList(current.getId())); assertEquals(expect, predicate); } }
@Test public void parse_name_spaced_field_recordable() { for (Database database : DATABASES) { TestRecord current = TestRecord.getInstance(database); current.setName("testName"); Predicate predicate = parser.parse("record = ?/" + TestRecord.class.getName() + "/name", current); Predicate expect = new ComparisonPredicate(PredicateParser.EQUALS_ANY_OPERATOR, false, "record", Arrays.asList(current.getName())); assertEquals(expect, predicate); } }