Skip to content

Commit

Permalink
MAILET-140 Add a Builder to FakeMatcherConfig and modify all the impa…
Browse files Browse the repository at this point in the history
…cted classes
  • Loading branch information
Laura-Royet committed Nov 25, 2016
1 parent def450b commit c6855a2
Show file tree
Hide file tree
Showing 31 changed files with 431 additions and 157 deletions.
Expand Up @@ -22,39 +22,73 @@
import org.apache.mailet.MailetContext;
import org.apache.mailet.MatcherConfig;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;

/**
* MatcherConfig
*/
public class FakeMatcherConfig implements MatcherConfig {

private final String matcherName;
public static Builder builder() {
return new Builder();
}

private final MailetContext mc;
public static class Builder {

public FakeMatcherConfig(String matcherName, MailetContext mc) {
super();
this.matcherName = matcherName;
this.mc = mc;
}
private String matcherName;
private Optional<MailetContext> mailetContext;
private Optional<String> condition;

public String getCondition() {
if (matcherName.contains("=")) {
return matcherName.substring(getMatcherName().length() + 1);
} else {
return null;
private Builder() {
condition = Optional.absent();
mailetContext = Optional.absent();
}

public Builder matcherName(String matcherName) {
this.matcherName = matcherName;
return this;
}

public Builder mailetContext(MailetContext mailetContext) {
Preconditions.checkNotNull(mailetContext);
this.mailetContext = Optional.of(mailetContext);
return this;
}

public Builder condition(String condition) {
this.condition = Optional.fromNullable(condition);
return this;
}

public FakeMatcherConfig build() {
Preconditions.checkNotNull(matcherName, "'matcherName' is mandatory");
return new FakeMatcherConfig(matcherName, mailetContext.or(FakeMailContext.defaultContext()), condition);
}
}

public MailetContext getMailetContext() {
return mc;
private final String matcherName;
private final MailetContext mailetContext;
private final Optional<String> condition;

private FakeMatcherConfig(String matcherName, MailetContext mailetContext, Optional<String> condition) {
this.matcherName = matcherName;
this.mailetContext = mailetContext;
this.condition = condition;
}

@Override
public String getMatcherName() {
if (matcherName.contains("=")) {
return matcherName.split("=")[0];
} else {
return matcherName;
}
return matcherName;
}

@Override
public MailetContext getMailetContext() {
return mailetContext;
}

}
@Override
public String getCondition() {
return condition.orNull();
}
}
Expand Up @@ -28,7 +28,6 @@
import org.apache.mailet.Matcher;
import org.apache.mailet.base.GenericMatcher;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMatcherConfig;
import org.apache.mailet.base.test.MailUtil;

Expand Down Expand Up @@ -68,9 +67,13 @@ protected void setupMockedMail() throws MessagingException {

protected void setupMatcher() throws MessagingException {
matcher = createMatcher();
FakeMatcherConfig mci = new FakeMatcherConfig(getConfigOption()
+ getHasMailAttribute(), FakeMailContext.defaultContext());
FakeMatcherConfig mci = FakeMatcherConfig.builder()
.matcherName(getMatcherName())
.condition(getHasMailAttribute())
.build();

matcher.init(mci);

}

// test if the mail attribute was matched
Expand Down Expand Up @@ -109,5 +112,5 @@ public void testAttributeIsNotMatched() throws MessagingException {

protected abstract GenericMatcher createMatcher();

protected abstract String getConfigOption();
protected abstract String getMatcherName();
}
Expand Up @@ -29,7 +29,6 @@
import org.apache.mailet.Mail;
import org.apache.mailet.Matcher;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMatcherConfig;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -41,8 +40,10 @@ public class AllTest {
@Before
public void setupMatcher() throws MessagingException {
matcher = new All();
FakeMatcherConfig mci = new FakeMatcherConfig("All",
FakeMailContext.defaultContext());
FakeMatcherConfig mci = FakeMatcherConfig.builder()
.matcherName("All")
.build();

matcher.init(mci);
}

Expand Down
Expand Up @@ -27,7 +27,6 @@

import org.apache.mailet.Matcher;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMatcherConfig;
import org.apache.mailet.base.test.MailUtil;
import org.junit.Before;
Expand All @@ -42,7 +41,11 @@ public class FetchedFromTest {
@Before
public void setUp() throws MessagingException {
matcher = new FetchedFrom();
FakeMatcherConfig matcherConfig = new FakeMatcherConfig("FetchedFrom=" + EXPECTED_HEADER_VALUE, FakeMailContext.defaultContext());
FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
.matcherName("FetchedFrom")
.condition(EXPECTED_HEADER_VALUE)
.build();

matcher.init(matcherConfig);
}

Expand Down
Expand Up @@ -31,7 +31,6 @@
import org.apache.mailet.Mail;
import org.apache.mailet.Matcher;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMatcherConfig;
import org.apache.mailet.base.test.MailUtil;
import org.junit.Before;
Expand All @@ -56,72 +55,110 @@ public void setUp() throws Exception {

@Test
public void matchShouldReturnAddressesWhenRightHeaderNameWithoutValue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1, FakeMailContext.defaultContext()));

FakeMatcherConfig mci = FakeMatcherConfig.builder()
.matcherName("HasHeader")
.condition(HEADER_NAME_1)
.build();

matcher.init(mci);



assertThat(matcher.match(mockedMail)).containsAll(mockedMail.getRecipients());
}

@Test
public void matchShouldReturnNullWhenWrongHeaderNameWithoutValue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_2, FakeMailContext.defaultContext()));
matcher.init(FakeMatcherConfig.builder()
.matcherName("HasHeader")
.condition(HEADER_NAME_2)
.build());

assertThat(matcher.match(mockedMail)).isNull();
}

@Test
public void matchShouldReturnAddressesWhenGoodHeaderNameAndValue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "=" + HEADER_VALUE_1, FakeMailContext.defaultContext()));
matcher.init(FakeMatcherConfig.builder()
.matcherName("HasHeader")
.condition(HEADER_NAME_1 + "=" + HEADER_VALUE_1)
.build());

assertThat(matcher.match(mockedMail)).containsAll(mockedMail.getRecipients());
}

@Test
public void matchShouldReturnNullWhenWrongValue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "=" + HEADER_VALUE_2, FakeMailContext.defaultContext()));
matcher.init(FakeMatcherConfig.builder()
.matcherName("HasHeader")
.condition(HEADER_NAME_1 + "=" + HEADER_VALUE_2)
.build());

assertThat(matcher.match(mockedMail)).isNull();
}

@Test
public void matchShouldReturnNullWhenWrongHeaderNameWithValueSpecified() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_2 + "=" + HEADER_VALUE_2, FakeMailContext.defaultContext()));
matcher.init(FakeMatcherConfig.builder()
.matcherName("HasHeader")
.condition(HEADER_NAME_2 + "=" + HEADER_VALUE_2)
.build());

assertThat(matcher.match(mockedMail)).isNull();
}

@Test
public void matchShouldIgnoreExtraEquals() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "=" + HEADER_VALUE_1 + "=any", FakeMailContext.defaultContext()));
matcher.init(FakeMatcherConfig.builder()
.matcherName("HasHeader")
.condition(HEADER_NAME_1 + "=" + HEADER_VALUE_1 + "=any")
.build());

assertThat(matcher.match(mockedMail)).containsAll(mockedMail.getRecipients());
}

@Test
public void matchShouldNotMatchMailsWithNoHeaderWhenValueSpecified() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "=" + HEADER_VALUE_1, FakeMailContext.defaultContext()));
matcher.init(FakeMatcherConfig.builder()
.matcherName("HasHeader")
.condition(HEADER_NAME_1 + "=" + HEADER_VALUE_1)
.build());

Mail mail = MailUtil.createMockMail2Recipients(MailUtil.createMimeMessage());

assertThat(matcher.match(mail)).isNull();
}

@Test
public void matchShouldNotMatchMailsWithNoHeaderWhenValueNotSpecified() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1, FakeMailContext.defaultContext()));
matcher.init(FakeMatcherConfig.builder()
.matcherName("HasHeader")
.condition(HEADER_NAME_1)
.build());

Mail mail = MailUtil.createMockMail2Recipients(MailUtil.createMimeMessage());

assertThat(matcher.match(mail)).isNull();
}

@Test
public void matchShouldReturnNullWhenOneConditionIsNotTrue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "+" + HEADER_NAME_2, FakeMailContext.defaultContext()));
matcher.init(FakeMatcherConfig.builder()
.matcherName("HasHeader")
.condition(HEADER_NAME_1 + "+" + HEADER_NAME_2)
.build());

assertThat(matcher.match(mockedMail)).isNull();
}

@Test
public void matchShouldReturnAddressesWhenAllConditionsMatch() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "+" + HEADER_NAME_2, FakeMailContext.defaultContext()));
matcher.init(FakeMatcherConfig.builder()
.matcherName("HasHeader")
.condition(HEADER_NAME_1 + "+" + HEADER_NAME_2)
.build());

MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
mimeMessage.addHeader(HEADER_NAME_1, HEADER_VALUE_1);
mimeMessage.addHeader(HEADER_NAME_2, HEADER_VALUE_2);
Expand All @@ -133,7 +170,11 @@ public void matchShouldReturnAddressesWhenAllConditionsMatch() throws MessagingE

@Test
public void matchShouldFindTheRightHeaderLineWhenUsedWithValue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "=" + HEADER_VALUE_2, FakeMailContext.defaultContext()));
matcher.init(FakeMatcherConfig.builder()
.matcherName("HasHeader")
.condition(HEADER_NAME_1 + "=" + HEADER_VALUE_2)
.build());

MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
mimeMessage.addHeader(HEADER_NAME_1, HEADER_VALUE_1);
mimeMessage.addHeader(HEADER_NAME_1, HEADER_VALUE_2);
Expand Down
Expand Up @@ -36,8 +36,8 @@ protected String getHasMailAttribute() {
return MAIL_ATTRIBUTE_NAME;
}

protected String getConfigOption() {
return "HasMailAttribute=";
protected String getMatcherName() {
return "HasMailAttribute";
}

}
Expand Up @@ -102,7 +102,7 @@ public void testHeaderIsNotMatchedCauseValue() throws MessagingException {
}
}

protected String getConfigOption() {
return "HasMailAttributeWithValueRegex=";
protected String getMatcherName() {
return "HasMailAttributeWithValueRegex";
}
}
Expand Up @@ -52,7 +52,7 @@ public void testAttributeIsNotMatchedCauseValue() throws MessagingException {
assertNull(matchedRecipients);
}

protected String getConfigOption() {
return "HasMailAttributeWithValue=";
protected String getMatcherName() {
return "HasMailAttributeWithValue";
}
}
Expand Up @@ -20,11 +20,11 @@

package org.apache.james.transport.matchers;

import static org.apache.mailet.base.MailAddressFixture.ANY_AT_JAMES;
import static org.apache.mailet.base.MailAddressFixture.ANY_AT_JAMES2;
import static org.apache.mailet.base.MailAddressFixture.JAMES2_APACHE_ORG;
import static org.apache.mailet.base.MailAddressFixture.JAMES_APACHE_ORG;
import static org.apache.mailet.base.MailAddressFixture.ANY_AT_JAMES;
import static org.apache.mailet.base.MailAddressFixture.OTHER_AT_JAMES;
import static org.apache.mailet.base.MailAddressFixture.ANY_AT_JAMES2;
import static org.apache.mailet.base.MailAddressFixture.OTHER_AT_JAMES2;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
Expand All @@ -51,7 +51,11 @@ public void setUp() throws Exception {
when(mailContext.isLocalServer(JAMES2_APACHE_ORG)).thenReturn(false);

matcher = new HostIsLocal();
FakeMatcherConfig mci = new FakeMatcherConfig("HostIsLocal", mailContext);
FakeMatcherConfig mci = FakeMatcherConfig.builder()
.matcherName("HostIsLocal")
.mailetContext(mailContext)
.build();

matcher.init(mci);
}

Expand Down

0 comments on commit c6855a2

Please sign in to comment.