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.Map;
029 import java.util.UUID;
030
031 import javax.mail.Folder;
032 import javax.mail.MessagingException;
033 import javax.mail.Quota;
034 import javax.mail.Session;
035 import javax.mail.URLName;
036 import javax.mail.event.ConnectionEvent;
037
038 import com.sun.mail.imap.IMAPStore;
039
040 public class IMAPMockStore extends IMAPStore {
041 private boolean connected;
042 // private IMAPMockFolder folder;
043 private MockMailbox mailbox;
044 private final UUID objectId = UUID.randomUUID();
045 protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
046
047 {
048 logger.warn("IMAP Mock Store in use");
049 System.out.println("IMAP Mock Store in use");
050 }
051
052 public IMAPMockStore(final Session session, final URLName urlname) {
053 this(session, urlname, "imap", false);
054
055 }
056
057 public IMAPMockStore(final Session session, final URLName url, final String name, final boolean isSSL) {
058 super(session, url, name, isSSL);
059
060 logger.debug("Created " + objectId);
061 }
062
063 @Override
064 public synchronized void close() throws MessagingException {
065 this.connected = false;
066 notifyConnectionListeners(ConnectionEvent.CLOSED);
067 logger.debug("Closed " + objectId);
068 }
069
070 @Override
071 public void connect() throws MessagingException {
072 if (isConnected()) {
073 throw new IllegalStateException("already connected");
074 }
075 super.connect(url.getHost(), url.getPort(), url.getUsername(), url.getPassword());
076 }
077
078 @Override
079 public void connect(final String user, final String password) throws MessagingException {
080 if (isConnected()) {
081 throw new IllegalStateException("already connected");
082 }
083 super.connect(url.getHost(), url.getPort(), user, password);
084 }
085
086 @Override
087 public void connect(final String host, final String user, final String password) throws MessagingException {
088 if (isConnected()) {
089 throw new IllegalStateException("already connected");
090 }
091 super.connect(host, url.getPort(), user, password);
092 }
093
094 @Override
095 public Folder getDefaultFolder() throws MessagingException {
096 checkConnected();
097
098 return new IMAPDefaultMockFolder(this, mailbox);
099 }
100
101 @Override
102 public Folder getFolder(final String name) throws MessagingException {
103 checkConnected();
104 logger.debug("getFolder(" + name + ")");
105 if ("inbox".equalsIgnoreCase(name)) {
106 return new IMAPMockFolder(this, mailbox.getInbox());
107 } else {
108
109 return new IMAPMockFolder(this, mailbox.getRoot().getOrAddSubFolder(name));
110 }
111 }
112
113 @Override
114 public Folder getFolder(final URLName url) throws MessagingException {
115 checkConnected();
116 return getFolder(url.getFile());
117 }
118
119 @Override
120 public synchronized Quota[] getQuota(final String root) throws MessagingException {
121 throw new MessagingException("QUOTA not supported");
122 }
123
124 @Override
125 public Folder[] getSharedNamespaces() throws MessagingException {
126 // TODO Auto-generated method stub
127 return super.getSharedNamespaces();
128 }
129
130 @Override
131 public Folder[] getUserNamespaces(final String user) throws MessagingException {
132 // TODO Auto-generated method stub
133 return super.getUserNamespaces(user);
134 }
135
136 /* (non-Javadoc)
137 * @see com.sun.mail.imap.IMAPStore#hasCapability(java.lang.String)
138 */
139 @Override
140 public synchronized boolean hasCapability(final String capability) throws MessagingException {
141 return capability != null && capability.toLowerCase().startsWith("imap4");
142 }
143
144 @Override
145 public synchronized Map<String, String> id(final Map<String, String> clientParams) throws MessagingException {
146 throw new MessagingException("ID not supported");
147 }
148
149 // /-------
150
151 @Override
152 public void idle() throws MessagingException {
153 throw new MessagingException("IDLE not supported");
154 }
155
156 @Override
157 public boolean isConnected() {
158 return this.connected;
159 }
160
161 @Override
162 public synchronized void setQuota(final Quota quota) throws MessagingException {
163
164 throw new MessagingException("QUOTA not supported");
165
166 }
167
168 protected void checkConnected() throws MessagingException {
169 if (!isConnected()) {
170 throw new MessagingException("Not connected");
171 }
172 }
173
174 @Override
175 protected boolean protocolConnect(final String host, final int port, final String user, final String password)
176 throws MessagingException {
177 logger.debug("Connect to " + user + " (" + objectId + ")");
178 mailbox = MockMailbox.get(user);
179 // folder = new IMAPMockFolder(this, mailbox.getInbox());
180 if (mailbox.getInbox().isSimulateError()) {
181 throw new MessagingException("Simulated error connecting to mailbox of " + user);
182 }
183
184 this.connected = true;
185
186 return true;
187 }
188
189 Session getSession() {
190 return session;
191 }
192
193 }