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 java.util.HashMap;
029 import java.util.Map;
030 import java.util.UUID;
031
032 import javax.mail.Folder;
033 import javax.mail.MessagingException;
034 import javax.mail.Session;
035 import javax.mail.URLName;
036 import javax.mail.event.ConnectionEvent;
037
038 import com.sun.mail.pop3.POP3Store;
039
040 public class POP3MockStore extends POP3Store {
041
042 private volatile boolean connected;
043 protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
044 private MockMailbox mailbox;
045 private final UUID objectId = UUID.randomUUID();
046
047 public POP3MockStore(final Session session, final URLName urlname) {
048 this(session, urlname, "pop3", false);
049
050 }
051
052 public POP3MockStore(final Session session, final URLName url, final String name, final boolean isSSL) {
053 super(session, url, name, isSSL);
054
055 logger.debug("Created " + objectId);
056 }
057
058 @SuppressWarnings("rawtypes")
059 @Override
060 public Map capabilities() throws MessagingException {
061
062 return new HashMap();
063 }
064
065 protected synchronized void checkConnected() throws MessagingException {
066 if (!isConnected()) {
067 throw new MessagingException("Not connected");
068 }
069 }
070
071 @Override
072 public synchronized void close() throws MessagingException {
073 connected = false;
074 mailbox = null;
075 notifyConnectionListeners(ConnectionEvent.CLOSED);
076 logger.debug("Closed " + objectId);
077 }
078
079 @Override
080 public synchronized void connect() throws MessagingException {
081 if (isConnected()) {
082 throw new IllegalStateException("already connected");
083 }
084 super.connect(url.getHost(), url.getPort(), url.getUsername(), url.getPassword());
085 }
086
087 @Override
088 public synchronized void connect(final String user, final String password) throws MessagingException {
089 if (isConnected()) {
090 throw new IllegalStateException("already connected");
091 }
092 super.connect(url.getHost(), url.getPort(), user, password);
093 }
094
095 @Override
096 public synchronized void connect(final String host, final String user, final String password) throws MessagingException {
097 if (isConnected()) {
098 throw new IllegalStateException("already connected");
099 }
100 super.connect(host, url.getPort(), user, password);
101 }
102
103 @Override
104 public synchronized Folder getDefaultFolder() throws MessagingException {
105 checkConnected();
106 return super.getDefaultFolder();
107 }
108
109 @Override
110 public synchronized Folder getFolder(final String name) throws MessagingException {
111 checkConnected();
112 if ("inbox".equalsIgnoreCase(name)) {
113 return new POP3MockFolder(this, mailbox.getInbox());
114 }
115
116 return new POP3MockFolder(this, new MailboxFolder(name, mailbox, false));
117
118 }
119
120 @Override
121 public synchronized Folder getFolder(final URLName url) throws MessagingException {
122 checkConnected();
123 return getFolder(url.getFile());
124 }
125
126 synchronized Session getSession() {
127 return session;
128 }
129
130 @Override
131 public synchronized boolean isConnected() {
132 return connected;
133 }
134
135 @Override
136 protected synchronized boolean protocolConnect(final String host, final int port, final String user, final String password)
137 throws MessagingException {
138 logger.debug("Connect to " + user + " (" + objectId + ")");
139 mailbox = MockMailbox.get(user);
140 // folder = new POP3MockFolder(this, mailbox.getInbox());
141 if (mailbox.getInbox().isSimulateError()) {
142 throw new MessagingException("Simulated error connecting to mailbox of " + user);
143 }
144 this.connected = true;
145 return true;
146 }
147
148 }