001 /***********************************************************************************************************************
002 *
003 * JavaMail Mock2 Provider - open source mock classes for mock up JavaMail
004 * =======================================================================
005 *
006 * Copyright (C) 2014 by Hendrik Saly (http://saly.de)
007 *
008 * Based on ideas from Kohsuke Kawaguchi's Mock-javamail (https://java.net/projects/mock-javamail)
009 *
010 ***********************************************************************************************************************
011 *
012 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
013 * the License. You may obtain a copy of the License at
014 *
015 * http://www.apache.org/licenses/LICENSE-2.0
016 *
017 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
018 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
019 * specific language governing permissions and limitations under the License.
020 *
021 ***********************************************************************************************************************
022 *
023 * $Id:$
024 *
025 **********************************************************************************************************************/
026 package de.saly.javamail.mock2;
027
028 import javax.mail.Address;
029 import javax.mail.Message;
030 import javax.mail.MessagingException;
031 import javax.mail.Session;
032 import javax.mail.Transport;
033 import javax.mail.URLName;
034 import javax.mail.event.ConnectionEvent;
035 import javax.mail.event.TransportEvent;
036 import javax.mail.internet.MimeMessage;
037
038 public class MockTransport extends Transport {
039
040 protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
041
042 {
043 logger.warn("Mock Transport in use");
044 System.out.println("Mock Transport in use");
045 }
046
047 public MockTransport(final Session session, final URLName urlname) {
048 super(session, urlname);
049
050 }
051
052 @Override
053 public void connect(final String host, final int port, final String user, final String password) throws MessagingException {
054
055 if (isConnected()) {
056 throw new IllegalStateException("already connected");
057 }
058
059 setConnected(true);
060
061 notifyConnectionListeners(ConnectionEvent.OPENED);
062 }
063
064 @Override
065 public void sendMessage(final Message msg, final Address[] addresses) throws MessagingException {
066 for (final Address a : addresses) {
067 final MockMailbox mailbox = MockMailbox.get(a);
068 if (mailbox.getInbox().isSimulateError()) {
069
070 notifyTransportListeners(TransportEvent.MESSAGE_NOT_DELIVERED, new Address[0], addresses, new Address[0], msg);
071
072 throw new MessagingException("Simulated error sending message to " + a);
073
074 }
075
076 mailbox.getInbox().add(new MimeMessage((MimeMessage) msg));
077 notifyTransportListeners(TransportEvent.MESSAGE_DELIVERED, addresses, new Address[0], new Address[0], msg);
078 }
079 }
080
081 }