ExpressionUtils { public static boolean isTrue( String expression, Map<String, Object> vars ) { Object result = evaluate( expression, vars ); return ( result != null && result instanceof Boolean ) ? (Boolean) result : false; }  static Object evaluate( String expression, Map<String, Object> vars ); static Double evaluateToDouble( String expression, Map<String, Object> vars ); static boolean isTrue( String expression, Map<String, Object> vars ); static boolean isBoolean( String expression, Map<String, Object> vars ); static boolean isValid( String expression, Map<String, Object> vars ); static boolean isNumeric( String value ); static String asSql( String expression ); static Object evaluate( String expression );  }
@Test public void testIsTrue() { assertTrue( ExpressionUtils.isTrue( "2 > 1", null ) ); assertTrue( ExpressionUtils.isTrue( "(2 * 3) == 6", null ) ); assertTrue( ExpressionUtils.isTrue( "\"a\" == \"a\"", null ) ); assertTrue( ExpressionUtils.isTrue( "'b' == 'b'", null ) ); assertTrue( ExpressionUtils.isTrue( "('b' == 'b') && ('c' == 'c')", null ) ); assertTrue( ExpressionUtils.isTrue( "'goat' == 'goat'", null ) ); assertFalse( ExpressionUtils.isTrue( "2 < 1", null ) ); assertFalse( ExpressionUtils.isTrue( "(2 * 3) == 8", null ) ); assertFalse( ExpressionUtils.isTrue( "\"a\" == \"b\"", null ) ); assertFalse( ExpressionUtils.isTrue( "'b' == 'c'", null ) ); assertFalse( ExpressionUtils.isTrue( "'goat' == 'cow'", null ) ); }
@Test public void testIsTrueWithVars() { Map<String, Object> vars = new HashMap<>(); vars.put( "v1", "4" ); vars.put( "v2", "12" ); vars.put( "v3", "goat" ); vars.put( "v4", "horse" ); assertTrue( ExpressionUtils.isTrue( "v1 > 1", vars ) ); assertTrue( ExpressionUtils.isTrue( "v2 < 18", vars ) ); assertTrue( ExpressionUtils.isTrue( "v2 < '23'", vars ) ); assertTrue( ExpressionUtils.isTrue( "v3 == 'goat'", vars ) ); assertTrue( ExpressionUtils.isTrue( "v4 == 'horse'", vars ) ); assertTrue( ExpressionUtils.isTrue( "v4 == \"horse\"", vars ) ); assertFalse( ExpressionUtils.isTrue( "v1 < 1", vars ) ); assertFalse( ExpressionUtils.isTrue( "v2 > 18", vars ) ); assertFalse( ExpressionUtils.isTrue( "v2 > '23'", vars ) ); assertFalse( ExpressionUtils.isTrue( "v3 == 'cow'", vars ) ); assertFalse( ExpressionUtils.isTrue( "v4 == 'goat'", vars ) ); assertFalse( ExpressionUtils.isTrue( "v4 == \"goat\"", vars ) ); }