RegexMatcher { public boolean validate(String dataStr, String regex) { if (regex == null || regex.equals("")) { throw new IllegalArgumentException("regex field cannot is be null or empty!"); } else { Pattern p = Pattern.compile(regex); return validate(dataStr, p); } }  boolean validate(String dataStr, String regex); boolean find(String dataStr, String regex); boolean validate(String dataStr, Pattern pattern); boolean find(String dataStr, Pattern pattern);  }
@Test public void validate_shouldThrowExeception_whenRegexStringIsNull() throws Exception { str = "abc"; boolean threwException = false; try { regexMatcher.validate(str, (String) null); } catch (IllegalArgumentException expectedException) { threwException = true; } assertEquals(true, threwException); }
@Test public void validate_shouldThrowExeception_whenRegexStringIsEmpty() throws Exception { str = "abc"; boolean threwException = false; try { regexMatcher.validate(str, ""); } catch (IllegalArgumentException expectedException) { threwException = true; } assertEquals(true, threwException); }
@Test public void validate_shouldReturnFalse_whenStringIsEmpty() throws Exception { str = ""; assertEquals(false, regexMatcher.validate(str, RegexPresetPattern.ALPHA)); }
@Test public void validate_shouldReturnFalse_whenStringIsNull() throws Exception { assertEquals(false, regexMatcher.validate(null, RegexPresetPattern.ALPHA)); }
@Test public void validate_shouldReturnTrue_whenStringIsValidAlpha() throws Exception { str = "abc"; assertEquals(true, regexMatcher.validate(str, RegexPresetPattern.ALPHA)); }
@Test public void validate_shouldReturnTrue_whenRegexStringIsValidString() throws Exception { str = "abc"; assertEquals(true, regexMatcher.validate(str, "[a-zA-Z]+")); }