RegExpConditionCheckerOperator implements ConditionCheckerOperator { @Override public boolean checkTarget(final String regExp, final String target) { boolean result = false; if (!"".equals(regExp)) { try { Pattern pattern = patternFactory.createPattern(regExp); Matcher matcher = pattern.matcher(target); if (matcher.find()) { result = true; } } catch (Exception e) { throw new RegularExpressionEvaluationException("Regular expression evaluation failed with the expression: " + regExp, e); } } return result; }  @Override boolean checkTarget(final String regExp, final String target);  }
@Test public void testCheckTargetShouldReturnFalseWhenRegExpIsEmptyString() { boolean actual = underTest.checkTarget("", EX_TARGET); assertFalse(actual); }
@Test(expectedExceptions = RegularExpressionEvaluationException.class) public void testCheckTargetShouldThrowExceptionWhenRegExpCompilationFailed() { given(patternFactory.createPattern(EX_REG_EXP)).willThrow(new PatternSyntaxException("", "", 0)); underTest.checkTarget(EX_REG_EXP, EX_TARGET); }
@Test public void testCheckTargetShouldReturnFalseWhenRegExpDoesNotMatch() { String regExp = "asdf[a-z]"; given(patternFactory.createPattern(regExp)).willReturn(Pattern.compile(regExp)); boolean actual = underTest.checkTarget(regExp, EX_TARGET); assertFalse(actual); }
@Test public void testCheckTargetShouldReturnTrueWhenRegExpMatches() { given(patternFactory.createPattern(EX_REG_EXP)).willReturn(Pattern.compile(EX_REG_EXP)); boolean actual = underTest.checkTarget(EX_REG_EXP, EX_TARGET); assertTrue(actual); }