MatchRegexAction 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) { engine.put("inputData", actionParameter.getInputData()); Object eval = engine.eval("inputData.match(\"" + actionParameter.getExpression() + "\")"); return eval != null; } } catch (ScriptException e) { LOGGER.error(e.getMessage(), e); } return false; }  @Override boolean canHandle(ActionParameter actionParameter);  }
@Test public void testCheckCanHandleSucc() { DataSet dataset = null; Object inputData = "Silly"; int rowNum = 0; String column = null; String ruleName = null; String expression = "^[A-Z]\\\\w*$"; boolean ignoreBlanks = false; ActionParameter actionParaneter = new ActionParameter(dataset, inputData, rowNum, column, ruleName, expression, ignoreBlanks); MatchRegexAction matchRegexAction = new MatchRegexAction(); boolean checkCanHandle = matchRegexAction.canHandle(actionParaneter); Assert.assertEquals("The result of checkCanHandle shuold be true", true, checkCanHandle); }
@Test public void testCheckCanHandleFail() { DataSet dataset = null; Object inputData = "silly"; int rowNum = 0; String column = null; String ruleName = null; String expression = "^[A-Z]\\\\w*$"; boolean ignoreBlanks = false; ActionParameter actionParaneter = new ActionParameter(dataset, inputData, rowNum, column, ruleName, expression, ignoreBlanks); MatchRegexAction matchRegexAction = new MatchRegexAction(); boolean checkCanHandle = matchRegexAction.canHandle(actionParaneter); Assert.assertEquals("The result of checkCanHandle shuold be false", false, checkCanHandle); }
@Test public void testCheckCanHandleInputNull() { DataSet dataset = null; Object inputData = null; int rowNum = 0; String column = null; String ruleName = null; String expression = "^[A-Z]\\\\w*$"; boolean ignoreBlanks = false; ActionParameter actionParaneter = new ActionParameter(dataset, inputData, rowNum, column, ruleName, expression, ignoreBlanks); MatchRegexAction matchRegexAction = new MatchRegexAction(); boolean checkCanHandle = matchRegexAction.canHandle(actionParaneter); Assert.assertEquals("The result of checkCanHandle shuold be false", false, checkCanHandle); }
@Test public void testCheckCanHandleExpressionNull() { DataSet dataset = null; Object inputData = "Silly"; int rowNum = 0; String column = null; String ruleName = null; String expression = null; boolean ignoreBlanks = false; ActionParameter actionParaneter = new ActionParameter(dataset, inputData, rowNum, column, ruleName, expression, ignoreBlanks); MatchRegexAction matchRegexAction = new MatchRegexAction(); boolean checkCanHandle = matchRegexAction.canHandle(actionParaneter); Assert.assertEquals("The result of checkCanHandle shuold be false", false, checkCanHandle); }