### Question:
TextMatchesCondition extends Condition<WebElement> { @Override public boolean apply(WebElement element) { try { Pattern pattern = flags == 0x00 ? Pattern.compile(regex) : Pattern.compile(regex, flags); Matcher matcher = pattern.matcher(webElementConverter.getTextFrom(element)); return matcher.matches(); } catch (NullPointerException | PatternSyntaxException | WebDriverException e) { return false; } } TextMatchesCondition(final String regex);  TextMatchesCondition(final int flags, final String regex);   TextMatchesCondition(final WebElementConverter webElementConverter, final int flags, final String regex); @Override boolean apply(WebElement element); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }### Answer:
@Test public void shouldMatchConditionWhenTextMatchesRegex() { when(mockWebElementConverter.getTextFrom(mockElement)).thenReturn("1234"); TextMatchesCondition condition = new TextMatchesCondition(mockWebElementConverter, 0x00, "(\\d+)"); assertThat(condition.apply(mockElement), equalTo(true)); }
@Test public void shouldNotMatchConditionWhenTextDoesNotMatchRegex() { when(mockWebElementConverter.getTextFrom(mockElement)).thenReturn("abcdef"); TextMatchesCondition condition = new TextMatchesCondition(mockWebElementConverter, 0x00, "^bcd"); assertThat(condition.apply(mockElement), equalTo(false)); }
@Test public void shouldHandleNullReturn() { when(mockWebElementConverter.getTextFrom(mockElement)).thenReturn(null); TextMatchesCondition condition = new TextMatchesCondition(mockWebElementConverter, 0x00, "(\\s+)"); assertThat(condition.apply(mockElement), equalTo(false)); }
@Test public void shouldHandleExceptionWhenElementHasStaleReference() { when(mockWebElementConverter.getTextFrom(mockElement)).thenThrow(new StaleElementReferenceException("stale")); TextMatchesCondition condition = new TextMatchesCondition(mockWebElementConverter, 0x00, "(\\s+)"); assertThat(condition.apply(mockElement), equalTo(false)); }