Commits

Micha Kops  committed 0a55b25

Initial import.

  • Participants

Comments (0)

Files changed (5)

+target
+.project
+.classpath
+.settings
+infinitest.filters
+# E-Mail Integration Testing with Greenmail Examples
+
+
+Examples how to run integration tests for SMTP, POP3, IMAP, IMAPS.. using the GreenMail library.
+
+For the full tutorial please feel free to visit my blog at [www.hascode.com].
+
+---
+
+*2012 Micha Kops / hasCode.com*
+
+  [www.hascode.com]:http://www.hascode.com/
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.hascode.tutorial</groupId>
+	<artifactId>mail-integration-test-tutorial</artifactId>
+	<version>0.0.1</version>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>com.icegreen</groupId>
+			<artifactId>greenmail</artifactId>
+			<version>1.3</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.10</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>javax.mail</groupId>
+			<artifactId>mail</artifactId>
+			<version>1.4.5</version>
+		</dependency>
+		<dependency>
+			<groupId>org.hamcrest</groupId>
+			<artifactId>hamcrest-all</artifactId>
+			<version>1.1</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>ch.qos.logback</groupId>
+			<artifactId>logback-core</artifactId>
+			<version>1.0.6</version>
+		</dependency>
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-api</artifactId>
+			<version>1.6.6</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>2.5.1</version>
+				<configuration>
+					<source>1.6</source>
+					<target>1.6</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+</project>

File src/test/java/it/Pop3IT.java

+package it;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Properties;
+
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.Store;
+import javax.mail.URLName;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.icegreen.greenmail.user.GreenMailUser;
+import com.icegreen.greenmail.user.UserException;
+import com.icegreen.greenmail.util.GreenMail;
+import com.icegreen.greenmail.util.ServerSetupTest;
+
+public class Pop3IT {
+	private static final String USER_PASSWORD = "abcdef123";
+	private static final String USER_NAME = "hascode";
+	private static final String EMAIL_USER_ADDRESS = "hascode@localhost";
+	private static final String EMAIL_TO = "someone@localhost.com";
+	private static final String EMAIL_SUBJECT = "Test E-Mail";
+	private static final String EMAIL_TEXT = "This is a test e-mail.";
+	private static final String LOCALHOST = "127.0.0.1";
+	private GreenMail mailServer;
+
+	@Before
+	public void setUp() {
+		mailServer = new GreenMail(ServerSetupTest.POP3);
+		mailServer.start();
+	}
+
+	@After
+	public void tearDown() {
+		mailServer.stop();
+	}
+
+	@Test
+	public void getMails() throws IOException, MessagingException,
+			UserException, InterruptedException {
+		GreenMailUser user = mailServer.setUser(EMAIL_USER_ADDRESS, USER_NAME,
+				USER_PASSWORD);
+		MimeMessage message = new MimeMessage((Session) null);
+		message.setFrom(new InternetAddress(EMAIL_TO));
+		message.addRecipient(Message.RecipientType.TO, new InternetAddress(
+				EMAIL_USER_ADDRESS));
+		message.setSubject(EMAIL_SUBJECT);
+		message.setText(EMAIL_TEXT);
+		user.deliver(message);
+
+		Properties props = new Properties();
+		props.setProperty("mail.pop3.connectiontimeout", "5000");
+		Session session = Session.getInstance(props);
+		URLName urlName = new URLName("pop3", LOCALHOST,
+				ServerSetupTest.POP3.getPort(), null, user.getLogin(),
+				user.getPassword());
+		Store store = session.getStore(urlName);
+		store.connect();
+
+		Folder folder = store.getFolder("INBOX");
+		folder.open(Folder.READ_ONLY);
+		Message[] messages = folder.getMessages();
+		assertNotNull(messages);
+		assertThat(1, equalTo(messages.length));
+		assertEquals(EMAIL_SUBJECT, messages[0].getSubject());
+		assertTrue(String.valueOf(messages[0].getContent())
+				.contains(EMAIL_TEXT));
+		assertEquals(EMAIL_TO, messages[0].getFrom()[0].toString());
+	}
+}

File src/test/java/it/Pop3SIT.java

+package it;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.security.Security;
+import java.util.Properties;
+
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.Store;
+import javax.mail.URLName;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.icegreen.greenmail.user.GreenMailUser;
+import com.icegreen.greenmail.user.UserException;
+import com.icegreen.greenmail.util.DummySSLSocketFactory;
+import com.icegreen.greenmail.util.GreenMail;
+import com.icegreen.greenmail.util.ServerSetupTest;
+
+public class Pop3SIT {
+	private static final String USER_PASSWORD = "abcdef123";
+	private static final String USER_NAME = "hascode";
+	private static final String EMAIL_USER_ADDRESS = "hascode@localhost";
+	private static final String EMAIL_TO = "someone@localhost.com";
+	private static final String EMAIL_SUBJECT = "Test E-Mail";
+	private static final String EMAIL_TEXT = "This is a test e-mail.";
+	private static final String LOCALHOST = "127.0.0.1";
+	private GreenMail mailServer;
+
+	@Before
+	public void setUp() {
+		Security.setProperty("ssl.SocketFactory.provider",
+				DummySSLSocketFactory.class.getName());
+		mailServer = new GreenMail(ServerSetupTest.POP3S);
+		mailServer.start();
+	}
+
+	@After
+	public void tearDown() {
+		mailServer.stop();
+	}
+
+	@Test
+	public void getMails() throws IOException, MessagingException,
+			UserException, InterruptedException {
+		GreenMailUser user = mailServer.setUser(EMAIL_USER_ADDRESS, USER_NAME,
+				USER_PASSWORD);
+		MimeMessage message = new MimeMessage((Session) null);
+		message.setFrom(new InternetAddress(EMAIL_TO));
+		message.addRecipient(Message.RecipientType.TO, new InternetAddress(
+				EMAIL_USER_ADDRESS));
+		message.setSubject(EMAIL_SUBJECT);
+		message.setText(EMAIL_TEXT);
+		user.deliver(message);
+
+		Properties props = new Properties();
+		props.setProperty("mail.pop3.connectiontimeout", "5000");
+		Session session = Session.getInstance(props);
+		URLName urlName = new URLName("pop3s", LOCALHOST,
+				ServerSetupTest.POP3S.getPort(), null, user.getLogin(),
+				user.getPassword());
+		Store store = session.getStore(urlName);
+		store.connect();
+
+		Folder folder = store.getFolder("INBOX");
+		folder.open(Folder.READ_ONLY);
+		Message[] messages = folder.getMessages();
+		assertNotNull(messages);
+		assertThat(1, equalTo(messages.length));
+		assertEquals(EMAIL_SUBJECT, messages[0].getSubject());
+		assertTrue(String.valueOf(messages[0].getContent())
+				.contains(EMAIL_TEXT));
+		assertEquals(EMAIL_TO, messages[0].getFrom()[0].toString());
+	}
+}