[jboss-cvs] jboss-seam/src/main/org/jboss/seam/mail ...

Peter Muir peter at bleepbleep.org.uk
Tue Oct 9 09:58:50 EDT 2007


  User: pmuir   
  Date: 07/10/09 09:58:50

  Modified:    src/main/org/jboss/seam/mail   MailSession.java
  Added:       src/main/org/jboss/seam/mail   MockTransport.java
  Log:
  Initial support for integration testing Seam Mail - yay ( JBSEAM-1833) and a start at tests for the mail example
  
  Revision  Changes    Path
  1.15      +40 -10    jboss-seam/src/main/org/jboss/seam/mail/MailSession.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: MailSession.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/mail/MailSession.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -b -r1.14 -r1.15
  --- MailSession.java	8 Oct 2007 20:40:51 -0000	1.14
  +++ MailSession.java	9 Oct 2007 13:58:50 -0000	1.15
  @@ -36,7 +36,6 @@
               .getLogProvider(MailSession.class);
   
       private Session session;
  -
       private String host = "localhost";
       private Integer port;
       private String username;
  @@ -45,6 +44,15 @@
       private String sessionJndiName;
       private boolean ssl;
       private boolean tls = true;
  +    private String transport;
  +    
  +    public MailSession() {}
  +    
  +    // Only used for tests
  +    public MailSession(String transport)
  +    {
  +        this.transport = transport;
  +    }
   
       @Unwrap
       public Session getSession() throws NamingException
  @@ -68,12 +76,13 @@
        * components.xml
        */
       @Create
  -    public void create()
  +    public MailSession create()
       {
           if (getSessionJndiName() == null)
           {
               createSession();
           }
  +        return this;
       }
   
       private void createSession()
  @@ -128,14 +137,7 @@
               }
           }
   
  -        if (isSsl())
  -        {
  -            properties.put("mail.transport.protocol", "smtps");
  -        } 
  -        else
  -        {
  -            properties.put("mail.transport.protocol", "smtp");
  -        }
  +        properties.put("mail.transport.protocol", getTransport());
   
           // Authentication if required
           Authenticator authenticator = null;
  @@ -269,6 +271,34 @@
           this.tls = tls;
       }
   
  +    /**
  +     * Get the transport to used. If the not explicitly specified smtp or smtps
  +     * is used
  +     */
  +    public String getTransport()
  +    {
  +        if (transport != null)
  +        {
  +            return transport;
  +        }
  +        if (isSsl())
  +        {
  +            return "smtps";
  +        } 
  +        else
  +        {
  +            return "smtp";
  +        }
  +    }
  +    
  +    /**
  +     * Explicitly set the transport to use
  +     */
  +    public void setTransport(String transport)
  +    {
  +        this.transport = transport;
  +    }
  +
       public static Session instance()
       {
           return (Session) Component.getInstance(MailSession.class);
  
  
  
  1.1      date: 2007/10/09 13:58:50;  author: pmuir;  state: Exp;jboss-seam/src/main/org/jboss/seam/mail/MockTransport.java
  
  Index: MockTransport.java
  ===================================================================
  package org.jboss.seam.mail;
  
  import javax.mail.Address;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.URLName;
  import javax.mail.internet.MimeMessage;
  
  import org.jboss.seam.contexts.Contexts;
  
  /**
   * Provides a MockTransport for integration testing Seam Mail.
   * {@link MockTransport#getMailMessage()} returns the most recently rendered 
   * message sent using the MockTransport.
   * 
   * To enable the mock transport, set the mailSession.transport property to mock
   * in components.properties.
   * 
   * @see javax.mail.Transport
   *
   * @author Pete Muir
   *
   */
  public class MockTransport extends Transport 
  {
  
      private static final String VAR_NAME = "org.jboss.seam.mock.mailMessage";
      
      public MockTransport(Session session, URLName urlname) 
      {
          super(session, urlname);
      }
  
      @Override
      public void sendMessage(Message message, Address[] recipients)
              throws MessagingException 
      {
          Contexts.getEventContext().set(VAR_NAME, message);
      }
      
      /**
       * Get the most recently rendered message sent using the MockTransport.
       */
      public static MimeMessage getMailMessage()
      {
          return (MimeMessage) Contexts.getEventContext().get(VAR_NAME);
      }
      
      @Override
      public void connect() throws MessagingException
      {
          // No-op
      }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list