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        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        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        @Override
066        public synchronized void close() throws MessagingException {
067            connected = false;
068            mailbox = null;
069            notifyConnectionListeners(ConnectionEvent.CLOSED);
070            logger.debug("Closed " + objectId);
071        }
072    
073        @Override
074        public synchronized void connect() throws MessagingException {
075            if (isConnected()) {
076                throw new IllegalStateException("already connected");
077            }
078            super.connect(url.getHost(), url.getPort(), url.getUsername(), url.getPassword());
079        }
080    
081        @Override
082        public synchronized void connect(final String user, final String password) throws MessagingException {
083            if (isConnected()) {
084                throw new IllegalStateException("already connected");
085            }
086            super.connect(url.getHost(), url.getPort(), user, password);
087        }
088    
089        @Override
090        public synchronized void connect(final String host, final String user, final String password) throws MessagingException {
091            if (isConnected()) {
092                throw new IllegalStateException("already connected");
093            }
094            super.connect(host, url.getPort(), user, password);
095        }
096    
097        @Override
098        public synchronized Folder getDefaultFolder() throws MessagingException {
099            checkConnected();
100            return super.getDefaultFolder();
101        }
102    
103        @Override
104        public synchronized Folder getFolder(final String name) throws MessagingException {
105            checkConnected();
106            if ("inbox".equalsIgnoreCase(name)) {
107                return new POP3MockFolder(this, mailbox.getInbox());
108            }
109    
110            return new POP3MockFolder(this, new MailboxFolder(name, mailbox, false));
111    
112        }
113    
114        @Override
115        public synchronized Folder getFolder(final URLName url) throws MessagingException {
116            checkConnected();
117            return getFolder(url.getFile());
118        }
119    
120        @Override
121        public synchronized boolean isConnected() {
122            return connected;
123        }
124    
125        protected synchronized void checkConnected() throws MessagingException {
126            if (!isConnected()) {
127                throw new MessagingException("Not connected");
128            }
129        }
130    
131        @Override
132        protected synchronized boolean protocolConnect(final String host, final int port, final String user, final String password)
133                throws MessagingException {
134            logger.debug("Connect to " + user + " (" + objectId + ")");
135            mailbox = MockMailbox.get(user);
136            // folder = new POP3MockFolder(this, mailbox.getInbox());
137            if (mailbox.getInbox().isSimulateError()) {
138                throw new MessagingException("Simulated error connecting to mailbox of " + user);
139            }
140            this.connected = true;
141            return true;
142        }
143    
144        synchronized Session getSession() {
145            return session;
146        }
147    
148    }