Skip to content

Instantly share code, notes, and snippets.

@jcsirot
Created August 3, 2012 11:15

Revisions

  1. jcsirot renamed this gist Aug 3, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. jcsirot created this gist Aug 3, 2012.
    65 changes: 65 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    package com.chelonix.selenium;

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import com.dumbster.smtp.SimpleSmtpServer;
    import com.dumbster.smtp.SmtpMessage;
    import com.google.common.collect.Lists;
    import org.junit.rules.ExternalResource;

    /**
    * A JUnit Rule which runs a mock SMTP server
    *
    * @see org.junit.rules.TestRule
    */
    public class MockSMTPRule extends ExternalResource
    {
    private int port = SimpleSmtpServer.DEFAULT_SMTP_PORT;
    private SimpleSmtpServer smtp;

    /**
    * Creates a SMTP server listening on port 25.
    */
    public MockSMTPRule()
    {
    }

    /**
    * Creates a SMTP server listening on the given port.
    * @param port the SMTP port
    */
    public MockSMTPRule(int port)
    {
    this.port = port;
    }

    public synchronized int getReceivedEmailSize()
    {
    return smtp.getReceivedEmailSize();
    }

    public synchronized Iterator getReceivedEmail()
    {
    return smtp.getReceivedEmail();
    }

    public List<SmtpMessage> getReceivedEmailAsList()
    {
    return Lists.newArrayList((Iterator<SmtpMessage>)getReceivedEmail());
    }

    @Override
    protected void after()
    {
    smtp.stop();
    super.after();
    }

    @Override
    protected void before() throws Throwable
    {
    super.before();
    smtp = SimpleSmtpServer.start(port);
    }
    }