ExpressionUtils { public static boolean isValid( String expression, Map<String, Object> vars ) { try { Object result = evaluate( expression, vars, true ); return result != null; } catch ( JexlException | NumberFormatException ex ) { return ex.getMessage().contains( "divide error" ); } }  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 testIsValid() { Map<String, Object> vars = new HashMap<>(); vars.put( "v1", "12" ); assertTrue( ExpressionUtils.isValid( "2 + 8", null ) ); assertTrue( ExpressionUtils.isValid( "3 - v1", vars ) ); assertTrue( ExpressionUtils.isValid( "d2:zing(1)", null ) ); assertTrue( ExpressionUtils.isValid( "d2:oizp(1)", null ) ); assertTrue( ExpressionUtils.isValid( "d2:oizp(d2:zing(1))", null ) ); assertTrue( ExpressionUtils.isValid( "d2:daysBetween('2015-02-01','2015-04-02')", null ) ); assertTrue( ExpressionUtils.isValid( "(d2:zing(1)+d2:zing(1))*50/1", null ) ); assertTrue( ExpressionUtils.isValid( "d2:condition('1 > 100',5,100)", null ) ); assertTrue( ExpressionUtils.isValid( "1/(1/100)", null ) ); assertTrue( ExpressionUtils.isValid( "SUM(1)", null ) ); assertTrue( ExpressionUtils.isValid( "avg(2+1)", null ) ); assertFalse( ExpressionUtils.isValid( "2 a 3", null ) ); assertFalse( ExpressionUtils.isValid( "4 b", vars ) ); assertFalse( ExpressionUtils.isValid( "4 + A", vars ) ); assertFalse( ExpressionUtils.isValid( "4 + someunkownvar", vars ) ); assertFalse( ExpressionUtils.isValid( "aver(2+1)", null ) ); }