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