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
031 import javax.mail.Address;
032 import javax.mail.internet.AddressException;
033 import javax.mail.internet.InternetAddress;
034
035 public class MockMailbox {
036
037 private static final Map<Address, MockMailbox> mailboxes = new HashMap<Address, MockMailbox>();
038
039 public synchronized static MockMailbox get(final Address a) {
040 MockMailbox mb = mailboxes.get(a);
041 if (mb == null) {
042 mailboxes.put(a, mb = new MockMailbox(a));
043 }
044 return mb;
045 }
046
047 public static MockMailbox get(final String address) throws AddressException {
048 return get(new InternetAddress(address));
049 }
050
051 public static void resetAll() {
052
053 mailboxes.clear();
054
055 }
056
057 private final Address address;
058 private final MailboxFolder inbox;
059
060 private final MailboxFolder root = new MailboxFolder("", this, true);
061
062 private MockMailbox(final Address address) {
063 super();
064 this.address = address;
065 inbox = root.addSpecialSubFolder("INBOX");
066 }
067
068 private MockMailbox(final String address) throws AddressException {
069 this(new InternetAddress(address));
070 }
071
072 /**
073 * @return the address
074 */
075 public Address getAddress() {
076 return address;
077 }
078
079 public MailboxFolder getInbox() {
080 return inbox;
081 }
082
083 public MailboxFolder getRoot() {
084 return root;
085 }
086
087 }