RecipientIsRegex extends GenericRecipientMatcher { @Override public void init() throws javax.mail.MessagingException { String patternString = getCondition(); if (Strings.isNullOrEmpty(patternString)) { throw new MessagingException("Pattern is missing"); } patternString = patternString.trim(); try { pattern = Pattern.compile(patternString); } catch (PatternSyntaxException mpe) { throw new MessagingException("Malformed pattern: " + patternString, mpe); } }  @Override void init(); @Override boolean matchRecipient(MailAddress recipient);  }
@Test void shouldMatchOneAddress() throws MessagingException { matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIsRegex") .condition(".*@.*") .build()); FakeMail fakeMail = FakeMail.builder() .name("mail") .recipient(ANY_AT_JAMES) .build(); assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES); }
@Test void shouldNotMatchPartially() throws MessagingException { matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIsRegex") .condition("any") .build()); FakeMail fakeMail = FakeMail.builder() .name("mail") .recipient(ANY_AT_JAMES) .build(); assertThat(matcher.match(fakeMail)).isEmpty(); }
@Test void shouldMatchOnlyMatchingPatterns() throws MessagingException { matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIsRegex") .condition("^any@.*") .build()); FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients(ANY_AT_JAMES, OTHER_AT_JAMES) .build(); assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES); }
@Test void shouldNotMatchNonMatchingPatterns() throws MessagingException { matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIsRegex") .condition(".*\\+") .build()); FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients(ANY_AT_JAMES, OTHER_AT_JAMES) .build(); assertThat(matcher.match(fakeMail)).isEmpty(); }
@Test void testRegexIsNotMatchedCauseError() { assertThatThrownBy(() -> matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIsRegex") .condition("(.") .build())) .isInstanceOf(MessagingException.class); }
@Test void testThrowExceptionWithEmptyPattern() { assertThatThrownBy(() -> matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIsRegex") .build())) .isInstanceOf(MessagingException.class); }
@Test void testThrowExceptionWithNoCondition() { assertThatThrownBy(() -> matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIsRegex") .build())) .isInstanceOf(MessagingException.class); }