View Javadoc
1   /***********************************************************************************************************************
2    *
3    * JavaMail Mock2 Provider - open source mock classes for mock up JavaMail
4    * =======================================================================
5    *
6    * Copyright (C) 2014 by Hendrik Saly (http://saly.de)
7    * 
8    * Based on ideas from Kohsuke Kawaguchi's Mock-javamail (https://java.net/projects/mock-javamail)
9    *
10   ***********************************************************************************************************************
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13   * the License. You may obtain a copy of the License at
14   *
15   *     http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19   * specific language governing permissions and limitations under the License.
20   *
21   ***********************************************************************************************************************
22   *
23   * $Id:$
24   *
25   **********************************************************************************************************************/
26  package de.saly.javamail.mock2;
27  
28  import javax.mail.Address;
29  import javax.mail.Message;
30  import javax.mail.MessagingException;
31  import javax.mail.Session;
32  import javax.mail.Transport;
33  import javax.mail.URLName;
34  import javax.mail.event.ConnectionEvent;
35  import javax.mail.event.TransportEvent;
36  import javax.mail.internet.MimeMessage;
37  
38  public class MockTransport extends Transport {
39  
40      protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
41  
42      {
43          logger.warn("Mock Transport in use");
44          System.out.println("Mock Transport in use");
45      }
46  
47      public MockTransport(final Session session, final URLName urlname) {
48          super(session, urlname);
49  
50      }
51  
52      @Override
53      public void connect(final String host, final int port, final String user, final String password) throws MessagingException {
54  
55          if (isConnected()) {
56              throw new IllegalStateException("already connected");
57          }
58  
59          setConnected(true);
60  
61          notifyConnectionListeners(ConnectionEvent.OPENED);
62      }
63  
64      @Override
65      public void sendMessage(final Message msg, final Address[] addresses) throws MessagingException {
66          for (final Address a : addresses) {
67              final MockMailbox mailbox = MockMailbox.get(a);
68              if (mailbox.getInbox().isSimulateError()) {
69  
70                  notifyTransportListeners(TransportEvent.MESSAGE_NOT_DELIVERED, new Address[0], addresses, new Address[0], msg);
71  
72                  throw new MessagingException("Simulated error sending message to " + a);
73  
74              }
75  
76              mailbox.getInbox().add(new MimeMessage((MimeMessage) msg));
77              notifyTransportListeners(TransportEvent.MESSAGE_DELIVERED, addresses, new Address[0], new Address[0], msg);
78          }
79      }
80  
81  }