KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailboxmanager > mailstore > MailStoreMailboxCacheTest


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20 package org.apache.james.mailboxmanager.mailstore;
21
22 import java.util.Arrays JavaDoc;
23
24 import org.apache.avalon.cornerstone.services.store.Store;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.configuration.DefaultConfiguration;
27 import org.apache.james.mailboxmanager.MailboxManagerException;
28 import org.apache.james.mailboxmanager.mailbox.MailboxSession;
29 import org.apache.james.services.MailRepository;
30 import org.apache.james.test.mock.avalon.MockLogger;
31 import org.jmock.Mock;
32 import org.jmock.MockObjectTestCase;
33 import org.jmock.core.constraint.IsEqual;
34
35 public class MailStoreMailboxCacheTest extends MockObjectTestCase {
36
37     MailstoreMailboxCache mailstoreMailboxCache;
38
39     public void setUp() {
40         mailstoreMailboxCache = new MailstoreMailboxCache();
41         mailstoreMailboxCache.enableLogging(new MockLogger());
42         mailstoreMailboxCache.setMountPoint("#mail");
43         mailstoreMailboxCache.setDestinationURL("file://var/mail/inboxes/");
44     }
45
46     public void testBuildUrlNoInbox() {
47         assertEquals("file://var/mail/inboxes/t1", mailstoreMailboxCache
48                 .buildUrl("#mail.t1"));
49         assertEquals("file://var/mail/inboxes/t1.t2", mailstoreMailboxCache
50                 .buildUrl("#mail.t1.t2"));
51     }
52
53     public void testBuildUrlInbox() {
54         assertEquals("file://var/mail/inboxes/", mailstoreMailboxCache
55                 .buildUrl("#mail.INBOX"));
56         assertEquals("file://var/mail/inboxes/t1", mailstoreMailboxCache
57                 .buildUrl("#mail.t1.INBOX"));
58         assertEquals("file://var/mail/inboxes/t1.t2", mailstoreMailboxCache
59                 .buildUrl("#mail.t1.t2.INBOX"));
60     }
61
62     public void testGetMailboxSession() throws MailboxManagerException,
63             ConfigurationException {
64         
65         DefaultConfiguration repositoryConfiguration = new DefaultConfiguration(
66                 "repository");
67
68         DefaultConfiguration expectedConfiguration = new DefaultConfiguration(
69                 repositoryConfiguration);
70         expectedConfiguration.setAttribute("destinationURL",
71                 "file://var/mail/inboxes/t1");
72
73         Mock store = mock(Store.class);
74         Mock mockRepository = mock(MailRepository.class);
75         store.expects(exactly(2)).method("select").with(
76                 new IsEqual(expectedConfiguration)).will(
77                 returnValue(mockRepository.proxy()));
78
79         mailstoreMailboxCache.setMailStore((Store) store.proxy());
80         mailstoreMailboxCache.setRepositoryConf(repositoryConfiguration);
81         MailboxSession s1 = mailstoreMailboxCache.getMailboxSession("#mail.t1.INBOX");
82         MailboxSession s2 = mailstoreMailboxCache.getMailboxSession("#mail.t1.INBOX");
83         assertNotSame(s1, s2);
84         s1.close();
85         MailboxSession s3 = mailstoreMailboxCache.getMailboxSession("#mail.t1.INBOX");
86         s3.close();
87         s2.close();
88
89         MailboxSession s4 = mailstoreMailboxCache.getMailboxSession("#mail.t1.INBOX");
90
91         String JavaDoc[] keys = new String JavaDoc[] { "test" };
92         mockRepository.expects(once()).method("list").withNoArguments().will(
93                 returnIterator(keys));
94         assertEquals(Arrays.asList(keys), s4.list());
95     }
96
97 }
98
Popular Tags