ExpressionAction extends AbstractSurvivorshipAction { @Override public boolean canHandle(ActionParameter actionParameter) { if (actionParameter.getExpression() == null) { return false; } ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("javascript"); try { if (actionParameter.getInputData() != null && actionParameter.getInputData() instanceof Number) { return (Boolean) engine .eval(actionParameter.getInputData().toString() + actionParameter.getExpression()); } else if (actionParameter.getInputData() != null && actionParameter.getInputData() instanceof Date) { String varName = actionParameter.getColumn() + "Date"; engine.put(varName, actionParameter.getInputData()); return (Boolean) engine.eval("" + varName + actionParameter.getExpression()); } else { String varName = actionParameter.getColumn() + "String"; engine.put(varName, actionParameter.getInputData()); return (Boolean) engine.eval("" + varName + actionParameter.getExpression()); } } catch (ScriptException e) { LOGGER.error(e.getMessage(), e); } return false; }  @Override boolean canHandle(ActionParameter actionParameter);  }
@Test public void testCheckCanHandleWithNumber() { DataSet dataset = null; Object inputData = 10; int rowNum = 0; String column = null; String ruleName = null; String expression = ">1"; boolean ignoreBlanks = false; ActionParameter actionParaneter = new ActionParameter(dataset, inputData, rowNum, column, ruleName, expression, ignoreBlanks); ExpressionAction expressionAction = new ExpressionAction(); boolean checkCanHandle = expressionAction.canHandle(actionParaneter); Assert.assertEquals("The result of checkCanHandle shuold be true", true, checkCanHandle); }
@Test public void testCheckCanHandleWithDate() { DataSet dataset = null; Object inputData = new Date(); int rowNum = 0; String column = null; String ruleName = null; String expression = ".getTime()<new Date().getTime()"; boolean ignoreBlanks = false; ActionParameter actionParaneter = new ActionParameter(dataset, inputData, rowNum, column, ruleName, expression, ignoreBlanks); ExpressionAction expressionAction = new ExpressionAction(); boolean checkCanHandle = expressionAction.canHandle(actionParaneter); Assert.assertEquals("The result of checkCanHandle shuold be true", true, checkCanHandle); }
@Test public void testCheckCanHandleWithString() { DataSet dataset = null; Object inputData = "string1"; int rowNum = 0; String column = null; String ruleName = null; String expression = ".equals(\"string1\")"; boolean ignoreBlanks = false; ActionParameter actionParaneter = new ActionParameter(dataset, inputData, rowNum, column, ruleName, expression, ignoreBlanks); ExpressionAction expressionAction = new ExpressionAction(); boolean checkCanHandle = expressionAction.canHandle(actionParaneter); Assert.assertEquals("The result of checkCanHandle shuold be true", true, checkCanHandle); }
@Test public void testCheckCanHandleInputDataNull() { DataSet dataset = null; Object inputData = null; int rowNum = 0; String column = null; String ruleName = null; String expression = ".equals(\"string1\")"; boolean ignoreBlanks = false; ActionParameter actionParaneter = new ActionParameter(dataset, inputData, rowNum, column, ruleName, expression, ignoreBlanks); ExpressionAction expressionAction = new ExpressionAction(); boolean checkCanHandle = expressionAction.canHandle(actionParaneter); Assert.assertEquals("The result of checkCanHandle shuold be false because of exception", false, checkCanHandle); }
@Test public void testCheckCanHandleExpressionNull() { DataSet dataset = null; Object inputData = null; int rowNum = 0; String column = null; String ruleName = null; String expression = ".equals(\"string1\")"; boolean ignoreBlanks = false; ActionParameter actionParaneter = new ActionParameter(dataset, inputData, rowNum, column, ruleName, expression, ignoreBlanks); ExpressionAction expressionAction = new ExpressionAction(); boolean checkCanHandle = expressionAction.canHandle(actionParaneter); Assert.assertEquals("The result of checkCanHandle shuold be false because of expression is null", false, checkCanHandle); }