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.HashMap;
29 import java.util.Map;
30
31 import javax.mail.Address;
32 import javax.mail.internet.AddressException;
33 import javax.mail.internet.InternetAddress;
34
35 public class MockMailbox {
36
37 private static final Map<Address, MockMailbox> mailboxes = new HashMap<Address, MockMailbox>();
38
39 public synchronized static MockMailbox get(final Address a) {
40 MockMailbox mb = mailboxes.get(a);
41 if (mb == null) {
42 mailboxes.put(a, mb = new MockMailbox(a));
43 }
44 return mb;
45 }
46
47 public static MockMailbox get(final String address) throws AddressException {
48 return get(new InternetAddress(address));
49 }
50
51 public static void resetAll() {
52
53 mailboxes.clear();
54
55 }
56
57 private final Address address;
58 private final MailboxFolder inbox;
59
60 private final MailboxFolder root = new MailboxFolder("", this, true);
61
62 private MockMailbox(final Address address) {
63 super();
64 this.address = address;
65 inbox = root.addSpecialSubFolder("INBOX");
66 }
67
68 private MockMailbox(final String address) throws AddressException {
69 this(new InternetAddress(address));
70 }
71
72 /**
73 * @return the address
74 */
75 public Address getAddress() {
76 return address;
77 }
78
79 public MailboxFolder getInbox() {
80 return inbox;
81 }
82
83 public MailboxFolder getRoot() {
84 return root;
85 }
86
87 }