KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > management > SpoolManagementTest


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
21
22 package org.apache.james.management;
23
24 import junit.framework.TestCase;
25 import org.apache.avalon.cornerstone.services.store.Store;
26 import org.apache.avalon.framework.container.ContainerUtil;
27 import org.apache.james.test.mock.avalon.MockLogger;
28 import org.apache.james.test.mock.avalon.MockServiceManager;
29 import org.apache.james.test.mock.avalon.MockStore;
30 import org.apache.james.test.mock.james.InMemorySpoolRepository;
31 import org.apache.james.test.mock.mailet.MockMail;
32 import org.apache.james.test.mock.javaxmail.MockMimeMessage;
33 import org.apache.james.test.util.Util;
34 import org.apache.mailet.Mail;
35
36 import javax.mail.MessagingException JavaDoc;
37 import java.util.HashMap JavaDoc;
38
39 /**
40  * Tests the SpoolManagement
41  */

42 public class SpoolManagementTest extends TestCase {
43
44     protected static final String JavaDoc OUTGOING_SPOOL = "file://target/var/mail/outgoing";
45
46     private SpoolManagement m_spoolManagement;
47     private MockStore m_mockStore;
48     private InMemorySpoolRepository m_mockSpoolRepository;
49
50     protected void setUp() throws Exception JavaDoc {
51         m_spoolManagement = new SpoolManagement();
52         ContainerUtil.enableLogging(m_spoolManagement, new MockLogger());
53         ContainerUtil.service(m_spoolManagement, setUpServiceManager());
54     }
55
56     protected void tearDown() throws Exception JavaDoc {
57         if (m_mockSpoolRepository != null) {
58             ContainerUtil.dispose(m_mockSpoolRepository);
59         }
60         super.tearDown();
61     }
62
63     private MockServiceManager setUpServiceManager() {
64         MockServiceManager serviceManager = new MockServiceManager();
65         m_mockStore = new MockStore();
66         serviceManager.put(Store.ROLE, m_mockStore);
67         m_mockSpoolRepository = new InMemorySpoolRepository();
68         m_mockStore.add("outgoing", m_mockSpoolRepository);
69         return serviceManager;
70     }
71
72     public void testListSpoolItemsDontFilter() throws SpoolManagementException, MessagingException JavaDoc {
73         String JavaDoc[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, SpoolFilter.ERRORMAIL_FILTER);
74         assertEquals("emtpy spool", 0, mailList.length); // no mail in spool
75

76         createSpoolMail(Mail.DEFAULT, "subj1", 1);
77         createSpoolMail(Mail.ERROR, "subj2", 2);
78
79         mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, new SpoolFilter(null));
80         assertEquals("finds all mails", 2, mailList.length);
81     }
82
83     public void testListSpoolItemsFilterByState() throws SpoolManagementException, MessagingException JavaDoc {
84         MockMail mockMail1 = createSpoolMail(Mail.DEFAULT, "subj1", 1);
85         String JavaDoc messageID1 = mockMail1.getName();
86
87         String JavaDoc[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, SpoolFilter.ERRORMAIL_FILTER);
88         assertEquals("no error mail in spool", 0, mailList.length);
89
90         MockMail mockMail2 = createSpoolMail(Mail.ERROR, "subj2", 2);
91         String JavaDoc messageID2 = mockMail2.getName();
92
93         mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, SpoolFilter.ERRORMAIL_FILTER);
94         assertEquals("finds only 1 error mail", 1, mailList.length);
95         assertTrue("finds only error mail: "+mailList[0], mailList[0].indexOf(messageID2) >= 0);
96
97         mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, new SpoolFilter(Mail.DEFAULT));
98         assertEquals("finds only 1 default mail", 1, mailList.length);
99         assertTrue("finds only default mail", mailList[0].indexOf(messageID1) >= 0);
100
101         mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, new SpoolFilter(Mail.DEFAULT.toUpperCase()));
102         assertEquals("finds default mail uppercase state", 1, mailList.length);
103     }
104
105     public void testListSpoolItemsFilterByHeader() throws SpoolManagementException, MessagingException JavaDoc {
106
107         MockMail mockMail1 = createSpoolMail(Mail.DEFAULT, "TestHeader", "value1", 1);
108         String JavaDoc messageID1 = mockMail1.getName();
109
110         createSpoolMail(Mail.ERROR, "TestHeader", "value2", 2);
111         createSpoolMail(Mail.ERROR, "TestHeader", "another", 3);
112
113         String JavaDoc[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, new SpoolFilter("TestHeader", "value[12]"));
114         assertEquals("find all mails", 2, mailList.length);
115
116         mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, new SpoolFilter("TestHeader", "(.*)1"));
117         assertEquals("find first mail", 1, mailList.length);
118         assertTrue("finds only default mail", mailList[0].indexOf(messageID1) >= 0);
119
120         HashMap JavaDoc headerMap = new HashMap JavaDoc();
121         headerMap.put("TestHeader", "(.*)1");
122         headerMap.put("to", "test2@james.apache.org");
123         SpoolFilter matchableFilterWith2Headers = new SpoolFilter(null, headerMap);
124         mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, matchableFilterWith2Headers);
125         assertEquals("find one mail", 1, mailList.length);
126         assertTrue("finds only default mail", mailList[0].indexOf(messageID1) >= 0);
127
128         headerMap = new HashMap JavaDoc();
129         headerMap.put("TestHeader", "(.*)1");
130         headerMap.put("UNmatchABLE", "test2@james.apache.org");
131         SpoolFilter unmatchableFilterWith2Headers = new SpoolFilter(null, headerMap);
132         mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, unmatchableFilterWith2Headers);
133         assertEquals("find no mail", 0, mailList.length);
134
135     }
136
137     public void testRemoveErrorMails() throws SpoolManagementException, MessagingException JavaDoc {
138         createSpoolMail(Mail.DEFAULT, "subj1", 1);
139         createSpoolMail(Mail.ERROR, "subj2", 2);
140         createSpoolMail(Mail.ERROR, "subj3", 3);
141
142         assertEquals("one mail removed", 2, m_spoolManagement.removeSpoolItems(OUTGOING_SPOOL, null, SpoolFilter.ERRORMAIL_FILTER));
143         String JavaDoc[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, SpoolFilter.ERRORMAIL_FILTER);
144         assertEquals("finds only left non-error mail", 0, mailList.length);
145     }
146
147     public void testRemoveErrorDedicatedMail() throws SpoolManagementException, MessagingException JavaDoc {
148         createSpoolMail(Mail.DEFAULT, "subj1", 1);
149         MockMail mockMail2 = createSpoolMail(Mail.ERROR, "subj2", 2);
150         String JavaDoc messageID2 = mockMail2.getName();
151         MockMail mockMail3 = createSpoolMail(Mail.ERROR, "subj3", 3);
152         String JavaDoc messageID3 = mockMail3.getName();
153
154         assertEquals("one mail removed", 1, m_spoolManagement.removeSpoolItems(OUTGOING_SPOOL, messageID3, SpoolFilter.ERRORMAIL_FILTER));
155         String JavaDoc[] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, SpoolFilter.ERRORMAIL_FILTER);
156         assertEquals("still finds 1 error mail", 1, mailList.length);
157         assertTrue("still finds error mail", mailList[0].indexOf(messageID2) >= 0);
158     }
159
160     private MockMail createSpoolMail(String JavaDoc state, String JavaDoc subject, int number) throws MessagingException JavaDoc {
161         MockMimeMessage mimeMessage = Util.createMimeMessage(subject, number);
162         MockMail mockMail = Util.createMockMail2Recipients(mimeMessage);
163         mockMail.setState(state);
164         m_mockSpoolRepository.store(mockMail);
165         return mockMail;
166     }
167
168     private MockMail createSpoolMail(String JavaDoc state, String JavaDoc headerName, String JavaDoc headerRegex, int number) throws MessagingException JavaDoc {
169         MockMimeMessage mimeMessage = Util.createMimeMessage(headerName, headerRegex, "test", number);
170         MockMail mockMail = Util.createMockMail2Recipients(mimeMessage);
171         mockMail.setState(state);
172         m_mockSpoolRepository.store(mockMail);
173         return mockMail;
174     }
175
176
177 }
178
Popular Tags