KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailboxmanager > impl > VirtualMailboxManagerIntegrationTest


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.impl;
21
22 import java.io.IOException JavaDoc;
23
24 import javax.mail.MessagingException JavaDoc;
25 import javax.mail.internet.MimeMessage JavaDoc;
26
27 import org.apache.avalon.cornerstone.services.store.Store;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
31 import org.apache.avalon.framework.service.ServiceManager;
32 import org.apache.james.core.AvalonMailStore;
33 import org.apache.james.imapserver.util.MessageGenerator;
34 import org.apache.james.mailboxmanager.TestUtil;
35 import org.apache.james.mailboxmanager.mailbox.MailboxSession;
36 import org.apache.james.services.FileSystem;
37 import org.apache.james.test.mock.avalon.MockLogger;
38 import org.apache.james.test.mock.james.MockFileSystem;
39 import org.apache.james.userrepository.DefaultJamesUser;
40 import org.jmock.Mock;
41 import org.jmock.MockObjectTestCase;
42 import org.xml.sax.SAXException JavaDoc;
43
44 public class VirtualMailboxManagerIntegrationTest extends MockObjectTestCase {
45
46     Configuration confFile;
47
48     public void testMixedMailstores() throws Exception JavaDoc {
49         Mock mockService = mock(ServiceManager.class);
50         mockService.expects(exactly(2)).method("lookup").with(eq(Store.ROLE))
51                 .will(returnValue(getMailStore()));
52
53         DefaultMailboxManagerProvider mailboxManagerProvider = new DefaultMailboxManagerProvider();
54         mailboxManagerProvider.enableLogging(new MockLogger());
55         mailboxManagerProvider.configure(getConfFile().getChild(
56                 "mailboxmanager", false));
57         mailboxManagerProvider.service((ServiceManager) mockService.proxy());
58         mailboxManagerProvider.initialize();
59
60         MailboxSession session;
61
62         session = mailboxManagerProvider.getInboxSession(new DefaultJamesUser(
63                 "user1", "none"));
64         doMiniTestOnSession(session);
65         session = mailboxManagerProvider.getInboxSession(new DefaultJamesUser(
66                 "user2", "none"));
67         doMiniTestOnSession(session);
68     }
69
70     protected void doMiniTestOnSession(MailboxSession session)
71             throws MessagingException JavaDoc, IOException JavaDoc {
72         MimeMessage JavaDoc origMessage = MessageGenerator.generateSimpleMessage();
73         String JavaDoc key = session.store(origMessage);
74         System.out.println("Key: " + key);
75         assertNotNull(key);
76         assertEquals(key, session.list().iterator().next());
77
78         MimeMessage JavaDoc fetchedMessage = session.retrieve(key);
79         assertTrue(TestUtil.contentEquals(origMessage, fetchedMessage, true));
80
81         session.remove(key);
82
83         assertEquals(0, session.list().size());
84         session.close();
85     }
86
87     protected Store getMailStore() throws Exception JavaDoc {
88         Mock mockService = mock(ServiceManager.class);
89         mockService.expects(atLeastOnce()).method("lookup").with(
90                 eq(FileSystem.ROLE)).will(returnValue(new MockFileSystem()));
91
92         AvalonMailStore mailStore = new AvalonMailStore();
93         mailStore.enableLogging(new MockLogger());
94         mailStore.configure(getConfFile().getChild("mailstore", false));
95         mailStore.service((ServiceManager) mockService.proxy());
96         mailStore.initialize();
97         return mailStore;
98     }
99
100     protected Configuration getConfFile() throws ConfigurationException,
101             SAXException JavaDoc, IOException JavaDoc {
102         if (confFile == null) {
103             confFile = new DefaultConfigurationBuilder()
104                     .build(getClass()
105                             .getResourceAsStream(
106                                     "/org/apache/james/mailboxmanager/testdata/MixedMailstores.xml"));
107         }
108         return confFile;
109     }
110
111 }
112
Popular Tags