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.io.IOException;
029 import java.io.InputStream;
030 import java.util.ArrayList;
031 import java.util.List;
032 import java.util.UUID;
033
034 import javax.mail.FetchProfile;
035 import javax.mail.FolderClosedException;
036 import javax.mail.Message;
037 import javax.mail.MessagingException;
038 import javax.mail.MethodNotSupportedException;
039 import javax.mail.event.ConnectionEvent;
040 import javax.mail.event.MessageChangedEvent;
041
042 import com.sun.mail.pop3.POP3MockFolder0;
043
044 import de.saly.javamail.mock2.MailboxFolder.MailboxEventListener;
045
046 public class POP3MockFolder extends POP3MockFolder0 implements MailboxEventListener {
047 protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
048 private final MailboxFolder mailboxFolder;
049 private final UUID objectId = UUID.randomUUID();
050 private volatile boolean opened;
051
052 {
053 logger.warn("POP3 Mock Store in use");
054 System.out.println("POP3 Mock Store in use");
055 }
056
057 protected POP3MockFolder(final POP3MockStore store, final MailboxFolder mailboxFolder) {
058 super(store);
059 this.mailboxFolder = mailboxFolder;
060 this.mailboxFolder.addMailboxEventListener(this);
061 }
062
063 /*
064 * TODO superclass ok?
065 public Folder getParent(){
066 return null;
067 }
068 */
069
070 protected synchronized void checkClosed() {
071 if (opened) {
072 throw new IllegalStateException("This operation is not allowed on an open folder " + objectId);
073 }
074 }
075
076 protected synchronized void checkOpened() throws FolderClosedException {
077
078 if (!opened) {
079
080 throw new IllegalStateException("This operation is not allowed on a closed folder " + objectId);
081
082 }
083 }
084
085 @Override
086 public synchronized void close(final boolean expunge) throws MessagingException {
087 checkOpened();
088
089 if (expunge) {
090 mailboxFolder.expunge();
091
092 }
093
094 opened = false;
095
096 logger.debug("Folder closed " + objectId);
097 notifyConnectionListeners(ConnectionEvent.CLOSED);
098 }
099
100 @Override
101 public void fetch(final Message[] msgs, final FetchProfile fp) throws MessagingException {
102 // just do nothing
103 }
104
105 @Override
106 public void folderCreated(final MailboxFolder mf) {
107 // not valid for pop3
108
109 }
110
111 @Override
112 public void folderDeleted(final MailboxFolder mf) {
113 // not valid for pop3
114
115 }
116
117 @Override
118 public void folderRenamed(final String from, final MailboxFolder to) {
119 // not valid for pop3
120
121 }
122
123 @Override
124 public synchronized Message getMessage(final int msgnum) throws MessagingException {
125 checkOpened();
126 return new MockMessage(mailboxFolder.getByMsgNum(msgnum), this);
127 }
128
129 @Override
130 public synchronized int getMessageCount() throws MessagingException {
131 return mailboxFolder.getMessageCount();
132 }
133
134 @Override
135 public synchronized Message[] getMessages() throws MessagingException {
136 checkOpened();
137 final List<Message> messages = new ArrayList<Message>();
138 for (int i = 1; i <= mailboxFolder.getMessageCount(); i++) {
139 final Message m = mailboxFolder.getByMsgNum(i);
140 messages.add(new MockMessage(m, this));
141 }
142 return messages.toArray(new Message[messages.size()]);
143 }
144
145 @Override
146 public synchronized Message[] getMessages(final int low, final int high) throws MessagingException {
147
148 checkOpened();
149 final List<Message> messages = new ArrayList<Message>();
150 for (int i = low; i <= high; i++) {
151 final Message m = mailboxFolder.getByMsgNum(i);
152 messages.add(new MockMessage(m, this));
153 }
154 return messages.toArray(new Message[messages.size()]);
155 }
156
157 @Override
158 public synchronized Message[] getMessages(final int[] msgnums) throws MessagingException {
159 checkOpened();
160
161 final List<Integer> idlist = new ArrayList<Integer>();
162 for (final int value : msgnums) {
163 idlist.add(value);
164 }
165
166 final List<Message> messages = new ArrayList<Message>();
167
168 for (int i = 1; i <= mailboxFolder.getMessageCount(); i++) {
169
170 if (!idlist.contains(new Integer(i))) {
171 continue;
172 }
173
174 final Message m = mailboxFolder.getByMsgNum(i);
175 messages.add(new MockMessage(m, this));
176 }
177 return messages.toArray(new Message[messages.size()]);
178 }
179
180 @Override
181 public synchronized int getSize() throws MessagingException {
182 checkOpened();
183 return mailboxFolder.getSizeInBytes();
184 }
185
186 @Override
187 public synchronized int[] getSizes() throws MessagingException {
188 checkOpened();
189 final int count = getMessageCount();
190 final int[] sizes = new int[count];
191
192 for (int i = 1; i <= count; i++) {
193 sizes[i - 1] = getMessage(i).getSize();
194 }
195
196 return sizes;
197
198 }
199
200 @Override
201 public synchronized String getUID(final Message msg) throws MessagingException {
202 checkOpened();
203 return String.valueOf(((MockMessage) msg).getMockid());
204 }
205
206 @Override
207 public boolean isOpen() {
208 return opened;
209 }
210
211 @Override
212 public InputStream listCommand() throws MessagingException, IOException {
213 throw new MethodNotSupportedException();
214 }
215
216 @Override
217 public void messageAdded(final MailboxFolder mf, final MockMessage msg) {
218 // ignore
219 // TODO JavaMail impl seems to not fire a event here for pop3, so we
220 // ignore it
221
222 }
223
224 @Override
225 public void messageChanged(final MailboxFolder mf, final MockMessage msg, final boolean headerChanged, final boolean flagsChanged) {
226 notifyMessageChangedListeners(MessageChangedEvent.FLAGS_CHANGED, msg);
227
228 }
229
230 @Override
231 public void messageExpunged(final MailboxFolder mf, final MockMessage msg, final boolean removed) {
232 // not valid for pop3
233
234 }
235
236 @Override
237 public synchronized void open(final int mode) throws MessagingException {
238 checkClosed();
239 opened = true;
240 logger.debug("Open " + objectId);
241 notifyConnectionListeners(ConnectionEvent.OPENED);
242 }
243
244 @Override
245 public void uidInvalidated() {
246 // not valid for pop3
247
248 }
249
250 }