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.ArrayList;
029    import java.util.List;
030    
031    import javax.mail.Folder;
032    import javax.mail.MessagingException;
033    
034    import com.sun.mail.imap.DefaultFolder;
035    
036    public class IMAPDefaultMockFolder extends DefaultFolder {
037    
038        protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
039        private final MockMailbox mailbox;
040        // private final IMAPMockFolder[] children;
041        private final IMAPMockStore store;
042    
043        protected IMAPDefaultMockFolder(final IMAPMockStore store, final MockMailbox mailbox) {
044            super(store);
045            // this.children = children;
046            this.mailbox = mailbox;
047            this.store = store;
048            logger.debug("Created");
049        }
050    
051        @Override
052        public synchronized boolean create(final int type) throws MessagingException {
053            return true;
054        }
055    
056        @Override
057        public synchronized boolean exists() throws MessagingException {
058            return true;
059        }
060    
061        @Override
062        public Folder getFolder(final String name) throws MessagingException {
063    
064            logger.debug("getFolder(" + name + ") on " + getFullName());
065    
066            if ("inbox".equalsIgnoreCase(name)) {
067                return new IMAPMockFolder(store, mailbox.getInbox());
068            }
069    
070            return new IMAPMockFolder(store, mailbox.getRoot().getOrAddSubFolder(name));
071    
072        }
073    
074        @Override
075        public int getType() throws MessagingException {
076            checkExists();
077            return HOLDS_FOLDERS;
078        }
079    
080        @Override
081        public Folder[] list(final String pattern) throws MessagingException {
082    
083            final List<MailboxFolder> children = mailbox.getRoot().getChildren();
084            final List<Folder> ret = new ArrayList<Folder>();
085    
086            for (final MailboxFolder mf : children) {
087                if (mf.isExists()) {
088                    ret.add(new IMAPMockFolder(store, mf));
089                }
090            }
091    
092            logger.debug("Folder (Defaultroot) list return " + ret);
093    
094            return ret.toArray(new Folder[ret.size()]);
095    
096        }
097    
098        @Override
099        public synchronized Folder[] listSubscribed(final String pattern) throws MessagingException {
100            return new Folder[0];
101        }
102    
103    }