View Javadoc
1   /***********************************************************************************************************************
2    *
3    * JavaMail Mock2 Provider - open source mock classes for mock up JavaMail
4    * =======================================================================
5    *
6    * Copyright (C) 2014 by Hendrik Saly (http://saly.de)
7    * 
8    * Based on ideas from Kohsuke Kawaguchi's Mock-javamail (https://java.net/projects/mock-javamail)
9    *
10   ***********************************************************************************************************************
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13   * the License. You may obtain a copy of the License at
14   *
15   *     http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19   * specific language governing permissions and limitations under the License.
20   *
21   ***********************************************************************************************************************
22   *
23   * $Id:$
24   *
25   **********************************************************************************************************************/
26  package de.saly.javamail.mock2;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import javax.mail.Folder;
32  import javax.mail.MessagingException;
33  
34  import com.sun.mail.imap.DefaultFolder;
35  
36  public class IMAPDefaultMockFolder extends DefaultFolder {
37  
38      protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
39      private final MockMailbox mailbox;
40      // private final IMAPMockFolder[] children;
41      private final IMAPMockStore store;
42  
43      protected IMAPDefaultMockFolder(final IMAPMockStore store, final MockMailbox mailbox) {
44          super(store);
45          // this.children = children;
46          this.mailbox = mailbox;
47          this.store = store;
48          logger.debug("Created");
49      }
50  
51      @Override
52      public synchronized boolean create(final int type) throws MessagingException {
53          return true;
54      }
55  
56      @Override
57      public synchronized boolean exists() throws MessagingException {
58          return true;
59      }
60  
61      @Override
62      public Folder getFolder(final String name) throws MessagingException {
63  
64          logger.debug("getFolder(" + name + ") on " + getFullName());
65  
66          if ("inbox".equalsIgnoreCase(name)) {
67              return new IMAPMockFolder(store, mailbox.getInbox());
68          }
69  
70          return new IMAPMockFolder(store, mailbox.getRoot().getOrAddSubFolder(name));
71  
72      }
73  
74      @Override
75      public int getType() throws MessagingException {
76          checkExists();
77          return HOLDS_FOLDERS;
78      }
79  
80      @Override
81      public Folder[] list(final String pattern) throws MessagingException {
82  
83          final List<MailboxFolder> children = mailbox.getRoot().getChildren();
84          final List<Folder> ret = new ArrayList<Folder>();
85  
86          for (final MailboxFolder mf : children) {
87              if (mf.isExists()) {
88                  ret.add(new IMAPMockFolder(store, mf));
89              }
90          }
91  
92          logger.debug("Folder (Defaultroot) list return " + ret);
93  
94          return ret.toArray(new Folder[ret.size()]);
95  
96      }
97  
98      @Override
99      public synchronized Folder[] listSubscribed(final String pattern) throws MessagingException {
100         return new Folder[0];
101     }
102 
103 }