KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailboxmanager > torque > repository > TorqueMailboxManagerMailRepositoryNativeTestCase


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.torque.repository;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.mail.MessagingException JavaDoc;
32 import javax.mail.internet.MimeMessage JavaDoc;
33
34 import org.apache.avalon.framework.configuration.Configuration;
35 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
36 import org.apache.james.mailboxmanager.MailboxManagerException;
37 import org.apache.james.mailboxmanager.MessageResult;
38 import org.apache.james.mailboxmanager.impl.GeneralMessageSetImpl;
39 import org.apache.james.mailboxmanager.mailbox.GeneralMailboxSession;
40 import org.apache.james.mailboxmanager.manager.MailboxManager;
41 import org.apache.james.mailboxmanager.mock.MockUser;
42 import org.apache.james.mailboxmanager.mock.TorqueMailboxManagerProviderSingleton;
43 import org.apache.james.mailboxmanager.redundant.AbstractMailRepositoryNativeTestCase;
44 import org.apache.james.mailboxmanager.repository.MailboxManagerMailRepository;
45
46 public class TorqueMailboxManagerMailRepositoryNativeTestCase extends
47         AbstractMailRepositoryNativeTestCase {
48
49     private static final String JavaDoc TUSER_INBOX = "#mail.tuser.INBOX";
50     GeneralMailboxSession shadowMailbox = null;
51
52     protected void configureRepository() throws Exception JavaDoc {
53         TorqueMailboxManagerProviderSingleton
54                 .getTorqueMailboxManagerProviderInstance().deleteEverything();
55         MailboxManagerMailRepository mailboxManagerMailRepository = new MailboxManagerMailRepository();
56
57         DefaultConfigurationBuilder db = new DefaultConfigurationBuilder();
58
59         Configuration conf = db
60                 .build(
61                         new ByteArrayInputStream JavaDoc(
62                                 ("<repository destinationURL=\"mailboxmanager://#mail/tuser\" postfix=\".INBOX\" translateDelimiters=\"true\" type=\"MAIL\"></repository>")
63                                         .getBytes()), "repository");
64         mailboxManagerMailRepository.configure(conf);
65         mailboxManagerMailRepository.initialize();
66         mailboxManagerMailRepository.setMailboxManagerProvider(TorqueMailboxManagerProviderSingleton
67                 .getTorqueMailboxManagerProviderInstance());
68         mailRepository = mailboxManagerMailRepository;
69
70     }
71
72     protected void destroyRepository() throws IOException JavaDoc, MessagingException JavaDoc {
73     }
74
75     protected void assertNativeMessageCountEquals(int count) {
76         assertEquals(count, getNativeMessageCount());
77     }
78
79     
80     protected void assertNativeMessagesEqual(Collection JavaDoc expectedMessages)
81             throws IOException JavaDoc, MessagingException JavaDoc {
82         Collection JavaDoc existing = getNativeMessages();
83         Set JavaDoc existingSet = new HashSet JavaDoc();
84         for (Iterator JavaDoc iter = existing.iterator(); iter.hasNext();) {
85             MimeMessage JavaDoc mm = (MimeMessage JavaDoc) iter.next();
86             existingSet.add(new Integer JavaDoc(messageHashSum(mm)));
87         }
88         Set JavaDoc expectedSet = new HashSet JavaDoc();
89         for (Iterator JavaDoc iter = expectedMessages.iterator(); iter.hasNext();) {
90             MimeMessage JavaDoc mm = (MimeMessage JavaDoc) iter.next();
91             expectedSet.add(new Integer JavaDoc(messageHashSum(mm)));
92         }
93         assertEquals(expectedSet.size(), existingSet.size());
94         assertTrue(expectedSet.equals(existingSet));
95
96     }
97
98     protected int getNativeMessageCount() {
99         try {
100             return getShadowMailbox().getMessageCount();
101         } catch (MailboxManagerException e) {
102             throw new RuntimeException JavaDoc(e);
103         }
104     }
105
106     public void testLock() throws MessagingException JavaDoc {
107         super.testLock();
108     }
109
110     
111     protected Collection JavaDoc getNativeMessages() {
112         final MessageResult[] mr;
113         try {
114             mr = getShadowMailbox().getMessages(GeneralMessageSetImpl.all(),
115                     MessageResult.MIME_MESSAGE);
116
117         } catch (MailboxManagerException e) {
118             throw new RuntimeException JavaDoc(e);
119         }
120         Collection JavaDoc existing = new ArrayList JavaDoc();
121         for (int i = 0; i < mr.length; i++) {
122             existing.add(mr[i].getMimeMessage());
123         }
124         return existing;
125     }
126
127     protected void nativeStoreMessage(MimeMessage JavaDoc mm) {
128         try {
129             getShadowMailbox().appendMessage(mm, new Date JavaDoc(),
130                     MessageResult.NOTHING);
131         } catch (MailboxManagerException e) {
132             throw new RuntimeException JavaDoc(e);
133         }
134
135     }
136
137     protected GeneralMailboxSession getShadowMailbox() {
138         if (shadowMailbox == null) {
139             try {
140                 MailboxManager mailboxManager= TorqueMailboxManagerProviderSingleton
141                 .getTorqueMailboxManagerProviderInstance()
142                 .getMailboxManagerInstance(new MockUser());
143                 if (!mailboxManager.existsMailbox(TUSER_INBOX)) {
144                     mailboxManager.createMailbox(TUSER_INBOX);
145                 }
146                 shadowMailbox = mailboxManager.getGeneralMailboxSession(TUSER_INBOX);
147             } catch (Exception JavaDoc e) {
148                 throw new RuntimeException JavaDoc(e);
149             }
150         }
151
152         return shadowMailbox;
153     }
154
155 }
156
Popular Tags