1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package de.saly.javamail.mock2;
27
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.UUID;
31
32 import javax.mail.Folder;
33 import javax.mail.MessagingException;
34 import javax.mail.Session;
35 import javax.mail.URLName;
36 import javax.mail.event.ConnectionEvent;
37
38 import com.sun.mail.pop3.POP3Store;
39
40 public class POP3MockStore extends POP3Store {
41
42 private volatile boolean connected;
43 protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
44 private MockMailbox mailbox;
45 private final UUID objectId = UUID.randomUUID();
46
47 public POP3MockStore(final Session session, final URLName urlname) {
48 this(session, urlname, "pop3", false);
49
50 }
51
52 public POP3MockStore(final Session session, final URLName url, final String name, final boolean isSSL) {
53 super(session, url, name, isSSL);
54
55 logger.debug("Created " + objectId);
56 }
57
58 @SuppressWarnings("rawtypes")
59 @Override
60 public Map capabilities() throws MessagingException {
61
62 return new HashMap();
63 }
64
65 protected synchronized void checkConnected() throws MessagingException {
66 if (!isConnected()) {
67 throw new MessagingException("Not connected");
68 }
69 }
70
71 @Override
72 public synchronized void close() throws MessagingException {
73 connected = false;
74 mailbox = null;
75 notifyConnectionListeners(ConnectionEvent.CLOSED);
76 logger.debug("Closed " + objectId);
77 }
78
79 @Override
80 public synchronized void connect() throws MessagingException {
81 if (isConnected()) {
82 throw new IllegalStateException("already connected");
83 }
84 super.connect(url.getHost(), url.getPort(), url.getUsername(), url.getPassword());
85 }
86
87 @Override
88 public synchronized void connect(final String user, final String password) throws MessagingException {
89 if (isConnected()) {
90 throw new IllegalStateException("already connected");
91 }
92 super.connect(url.getHost(), url.getPort(), user, password);
93 }
94
95 @Override
96 public synchronized void connect(final String host, final String user, final String password) throws MessagingException {
97 if (isConnected()) {
98 throw new IllegalStateException("already connected");
99 }
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
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 }