Skip to content

Commit

Permalink
JAMES-1877 Tests then refactoring for RemoteDelivery
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Jan 10, 2017
1 parent 4a5a4ba commit 6774b87
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 81 deletions.
11 changes: 11 additions & 0 deletions mailet/base/pom.xml
Expand Up @@ -70,6 +70,17 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>1.7.5</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.assertj</groupId> <groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
Expand Down
Expand Up @@ -38,9 +38,12 @@
import org.apache.mailet.Mail; import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress; import org.apache.mailet.MailAddress;


import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;


Expand Down Expand Up @@ -176,6 +179,14 @@ public static FakeMail defaultFakeMail() throws MessagingException {
return FakeMail.builder().build(); return FakeMail.builder().build();
} }


private static Map<String, Serializable> attributes(Mail mail) {
ImmutableMap.Builder<String, Serializable> builder = ImmutableMap.builder();
for (String attributeName: ImmutableList.copyOf(mail.getAttributeNames())) {
builder.put(attributeName, mail.getAttribute(attributeName));
}
return builder.build();
}

private MimeMessage msg; private MimeMessage msg;
private Collection<MailAddress> recipients; private Collection<MailAddress> recipients;
private String name; private String name;
Expand All @@ -201,6 +212,11 @@ public FakeMail(MimeMessage msg, List<MailAddress> recipients, String name, Mail
this.remoteAddr = remoteAddr; this.remoteAddr = remoteAddr;
} }


public FakeMail(Mail mail) throws MessagingException {
this(mail.getMessage(), Lists.newArrayList(mail.getRecipients()), mail.getName(), mail.getSender(), mail.getState(), mail.getErrorMessage(),
mail.getLastUpdated(), attributes(mail), mail.getMessageSize(), mail.getRemoteAddr());
}

@Override @Override
public String getName() { public String getName() {
return name; return name;
Expand Down Expand Up @@ -322,4 +338,44 @@ public void setLastUpdated(Date lastUpdated) {
public void setMessageSize(long size) { public void setMessageSize(long size) {
this.size = size; this.size = size;
} }

@Override
public final boolean equals(Object o) {
if (o instanceof FakeMail) {
FakeMail that = (FakeMail) o;

return Objects.equal(this.size, that.size)
&& Objects.equal(this.msg, that.msg)
&& Objects.equal(this.recipients, that.recipients)
&& Objects.equal(this.name, that.name)
&& Objects.equal(this.sender, that.sender)
&& Objects.equal(this.state, that.state)
&& Objects.equal(this.errorMessage, that.errorMessage)
&& Objects.equal(this.lastUpdated, that.lastUpdated)
&& Objects.equal(this.attributes, that.attributes)
&& Objects.equal(this.remoteAddr, that.remoteAddr);
}
return false;
}

@Override
public final int hashCode() {
return Objects.hashCode(msg, name, sender, recipients, state, errorMessage, lastUpdated, attributes, size, recipients, remoteAddr);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("msg", msg)
.add("recipients", recipients)
.add("name", name)
.add("sender", sender)
.add("state", state)
.add("errorMessage", errorMessage)
.add("lastUpdated", lastUpdated)
.add("attributes", attributes)
.add("size", size)
.add("remoteAddr", remoteAddr)
.toString();
}
} }
@@ -0,0 +1,41 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/

package org.apache.mailet.base.test;

import static org.mockito.Mockito.mock;

import javax.mail.internet.MimeMessage;

import org.junit.Test;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;

public class FakeMailTest {

@Test
public void beanShouldRespectBeanContract() {
EqualsVerifier.forClass(FakeMail.class)
.suppress(Warning.NONFINAL_FIELDS)
.withPrefabValues(MimeMessage.class, mock(MimeMessage.class), mock(MimeMessage.class))
.verify();
}

}
Expand Up @@ -21,6 +21,7 @@
package org.apache.james.transport.mailets; package org.apache.james.transport.mailets;


import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;


Expand All @@ -30,6 +31,7 @@
import org.apache.mailet.MailAddress; import org.apache.mailet.MailAddress;
import org.apache.mailet.Mailet; import org.apache.mailet.Mailet;
import org.apache.mailet.MailetException; import org.apache.mailet.MailetException;
import org.apache.mailet.base.MailAddressFixture;
import org.apache.mailet.base.test.FakeMail; import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext; import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMailetConfig; import org.apache.mailet.base.test.FakeMailetConfig;
Expand Down Expand Up @@ -143,13 +145,13 @@ public void serviceShouldLogWhenDebug() throws MessagingException {
.build(); .build();
mailet.init(mailetConfig); mailet.init(mailetConfig);


Mail mail = FakeMail.builder()
.recipients(new MailAddress("test@james.apache.org"), new MailAddress("test2@james.apache.org"))
.build();
String initialErrorMessage = "first"; String initialErrorMessage = "first";
mail.setErrorMessage(initialErrorMessage); Mail mail = FakeMail.builder()
.recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES)
.errorMessage(initialErrorMessage)
.build();
mailet.service(mail); mailet.service(mail);


verify(logger).info("Sending mail " + mail +" to error"); verify(logger).info(anyString());
} }
} }

0 comments on commit 6774b87

Please sign in to comment.