KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > LocalDeliveryTest


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.transport.mailets;
21
22 import org.apache.avalon.framework.container.ContainerUtil;
23 import org.apache.james.Constants;
24 import org.apache.james.core.MailImpl;
25 import org.apache.james.services.MailRepository;
26 import org.apache.james.services.MailServer;
27 import org.apache.james.services.UsersRepository;
28 import org.apache.james.test.mock.avalon.MockServiceManager;
29 import org.apache.james.test.mock.james.InMemorySpoolRepository;
30 import org.apache.james.test.mock.james.MockMailServer;
31 import org.apache.james.test.mock.javaxmail.MockMimeMessage;
32 import org.apache.james.test.mock.mailet.MockMail;
33 import org.apache.james.test.mock.mailet.MockMailContext;
34 import org.apache.james.test.mock.mailet.MockMailetConfig;
35 import org.apache.james.userrepository.DefaultJamesUser;
36 import org.apache.james.userrepository.MockUsersRepository;
37 import org.apache.mailet.Mail;
38 import org.apache.mailet.MailAddress;
39 import org.apache.mailet.Mailet;
40
41 import javax.mail.MessagingException JavaDoc;
42 import javax.mail.internet.MimeMessage JavaDoc;
43 import javax.mail.internet.ParseException JavaDoc;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.Arrays JavaDoc;
47 import java.util.Collection JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Properties JavaDoc;
52
53 import junit.framework.TestCase;
54
55 /**
56  * Test LocalDelivery Mailet
57  */

58 public class LocalDeliveryTest extends TestCase {
59
60     private HashMap JavaDoc mailboxes;
61     private MockMailContext mockMailetContext;
62     private MockServiceManager mockServiceManager;
63     private MockMailServer mockMailServer;
64     private MockUsersRepository mockUsersRepository;
65
66     public void testUnknownUser() throws MessagingException JavaDoc {
67         //mockMailetConfig.setProperty(key, value)
68
Mailet m = getMailet(null);
69         
70         Mail mail = createMail(new String JavaDoc[] {"unknownuser@ignoreddomain"});
71         m.service(mail);
72         
73         HashMap JavaDoc expectedMails = new HashMap JavaDoc();
74         expectedMails.put("errors", new String JavaDoc[] {"unknownuser@ignoreddomain"});
75         
76         assertDeliveryWorked(mail, expectedMails);
77     }
78
79
80     public void testSimpleDelivery() throws MessagingException JavaDoc {
81         //mockMailetConfig.setProperty(key, value)
82
Mailet m = getMailet(null);
83         
84         Mail mail = createMail(new String JavaDoc[] {"localuser@ignoreddomain"});
85         m.service(mail);
86         
87         HashMap JavaDoc expectedMails = new HashMap JavaDoc();
88         expectedMails.put("localuser", new String JavaDoc[] {"localuser@ignoreddomain"});
89         
90         assertDeliveryWorked(mail, expectedMails);
91     }
92
93     public void testSimpleDeliveryCaseSensitiveNoMatch() throws MessagingException JavaDoc {
94         //mockMailetConfig.setProperty(key, value)
95
Mailet m = getMailet(null);
96         
97         Mail mail = createMail(new String JavaDoc[] {"localUser@ignoreddomain"});
98         m.service(mail);
99         
100         HashMap JavaDoc expectedMails = new HashMap JavaDoc();
101         expectedMails.put("errors", new String JavaDoc[] {"localUser@ignoreddomain"});
102         
103         assertDeliveryWorked(mail, expectedMails);
104     }
105
106     public void testSimpleDeliveryCaseSensitiveMatch() throws MessagingException JavaDoc {
107         //mockMailetConfig.setProperty(key, value)
108
mockUsersRepository.setIgnoreCase(true);
109         Mailet m = getMailet(null);
110         
111         Mail mail = createMail(new String JavaDoc[] {"localUser@ignoreddomain"});
112         m.service(mail);
113         
114         HashMap JavaDoc expectedMails = new HashMap JavaDoc();
115         expectedMails.put("localuser", new String JavaDoc[] {"localuser@ignoreddomain"});
116         
117         assertDeliveryWorked(mail, expectedMails);
118     }
119
120     public void testSimpleAliasDelivery() throws MessagingException JavaDoc {
121         mockUsersRepository.setEnableAliases(true);
122         //mockMailetConfig.setProperty(key, value)
123
Mailet m = getMailet(null);
124         
125         Mail mail = createMail(new String JavaDoc[] {"aliasedUser@ignoreddomain"});
126         m.service(mail);
127         
128         HashMap JavaDoc expectedMails = new HashMap JavaDoc();
129         expectedMails.put("localuser", new String JavaDoc[] {"localuser@ignoreddomain"});
130         
131         assertDeliveryWorked(mail, expectedMails);
132     }
133
134     public void testSimpleAliasWithDisabledAlias() throws MessagingException JavaDoc {
135         //mockMailetConfig.setProperty(key, value)
136
Mailet m = getMailet(null);
137         
138         Mail mail = createMail(new String JavaDoc[] {"aliasedUser@ignoreddomain"});
139         m.service(mail);
140         
141         HashMap JavaDoc expectedMails = new HashMap JavaDoc();
142         expectedMails.put("aliasedUser", new String JavaDoc[] {"aliasedUser@ignoreddomain"});
143         
144         assertDeliveryWorked(mail, expectedMails);
145     }
146
147
148     public void testForwardingWithForwardingDisabled() throws MessagingException JavaDoc {
149         //mockMailetConfig.setProperty(key, value)
150
Mailet m = getMailet(null);
151         
152         Mail mail = createMail(new String JavaDoc[] {"forwardingUser@ignoreddomain"});
153         m.service(mail);
154         
155         HashMap JavaDoc expectedMails = new HashMap JavaDoc();
156         expectedMails.put("errors", new String JavaDoc[] {"forwardingUser@ignoreddomain"});
157         
158         assertDeliveryWorked(mail, expectedMails);
159     }
160
161
162     public void testForwarding() throws MessagingException JavaDoc {
163         mockUsersRepository.setEnableForwarding(true);
164         //mockMailetConfig.setProperty(key, value)
165
Mailet m = getMailet(null);
166         
167         Mail mail = createMail(new String JavaDoc[] {"forwardingUser@ignoreddomain"});
168         m.service(mail);
169         
170         HashMap JavaDoc expectedMails = new HashMap JavaDoc();
171         expectedMails.put("resent", new String JavaDoc[] {"remoteuser@remotedomain"});
172         
173         assertDeliveryWorked(mail, expectedMails);
174     }
175
176     public void testAliasingForwarding() throws MessagingException JavaDoc {
177         mockUsersRepository.setEnableAliases(true);
178         mockUsersRepository.setEnableForwarding(true);
179         //mockMailetConfig.setProperty(key, value)
180
Mailet m = getMailet(null);
181         
182         Mail mail = createMail(new String JavaDoc[] {"aliasForwardUser@ignoreddomain"});
183         m.service(mail);
184         
185         HashMap JavaDoc expectedMails = new HashMap JavaDoc();
186         expectedMails.put("resent", new String JavaDoc[] {"remoteuser@remotedomain"});
187         
188         assertDeliveryWorked(mail, expectedMails);
189     }
190
191     public void testAliasingForwardingWithLocallyOverriddenForwarding() throws MessagingException JavaDoc {
192         mockUsersRepository.setEnableAliases(true);
193         mockUsersRepository.setEnableForwarding(false);
194         Mailet m = getMailet(null);
195
196         Mail mail = createMail(new String JavaDoc[] {"aliasForwardUser@ignoreddomain"});
197         m.service(mail);
198         
199         HashMap JavaDoc expectedMails = new HashMap JavaDoc();
200         expectedMails.put("localuser", new String JavaDoc[] {"localuser@ignoreddomain"});
201         
202         assertDeliveryWorked(mail, expectedMails);
203     }
204
205     /* Commented out because "forwarding" to local is no more available
206      * under the new structure.
207      * If we'll ever change VirtualUserTable to return different collections
208      * for local and remote addresses this will be enabled again.
209     public void testForwardingToLocal() throws MessagingException {
210         mockUsersRepository.setEnableAliases(true);
211         mockUsersRepository.setEnableForwarding(true);
212         //mockMailetConfig.setProperty(key, value)
213         Mailet m = getMailet(null);
214         
215         Mail mail = createMail(new String[] {"forwardToLocal@ignoreddomain"});
216         m.service(mail);
217         
218         HashMap expectedMails = new HashMap();
219         expectedMails.put("resent", new String[] {"localuser@ignoreddomain"});
220         
221         assertDeliveryWorked(mail, expectedMails);
222     }
223     */

224     
225     public void testSimpleDeliveryVirtualHosting() throws MessagingException JavaDoc {
226         //enable virtual hosting
227
mockMailServer.setVirtualHosting(true);
228         Mailet m = getMailet(null);
229            
230         Mail mail = createMail(new String JavaDoc[] {"virtual@hosting"});
231         m.service(mail);
232             
233         HashMap JavaDoc expectedMails = new HashMap JavaDoc();
234         expectedMails.put("virtual@hosting", new String JavaDoc[] {"virtual@hosting"});
235            
236         assertDeliveryWorked(mail, expectedMails);
237     }
238
239     /**
240      * @throws ParseException
241      *
242      */

243     public void setUp() throws ParseException JavaDoc {
244         mockServiceManager = new MockServiceManager();
245         mockUsersRepository = new MockUsersRepository();
246         mockUsersRepository.setForceUseJamesUser();
247         mockUsersRepository.addUser("localuser", "password");
248         mockUsersRepository.addUser("aliasedUser", "pass2");
249         
250         // VirtualHosting
251
mockUsersRepository.addUser("virtual@hosting","pass");
252
253         DefaultJamesUser u = (DefaultJamesUser) mockUsersRepository.getUserByName("aliasedUser");
254         u.setAliasing(true);
255         u.setAlias("localuser");
256         mockUsersRepository.addUser("forwardingUser", "pass2");
257         u = (DefaultJamesUser) mockUsersRepository.getUserByName("forwardingUser");
258         u.setForwarding(true);
259         u.setForwardingDestination(new MailAddress("remoteuser@remotedomain"));
260         mockUsersRepository.addUser("aliasForwardUser", "pass2");
261         u = (DefaultJamesUser) mockUsersRepository.getUserByName("aliasForwardUser");
262         u.setAliasing(true);
263         u.setAlias("localuser");
264         u.setForwarding(true);
265         u.setForwardingDestination(new MailAddress("remoteuser@remotedomain"));
266         mockUsersRepository.addUser("forwardToLocal", "pass2");
267         u = (DefaultJamesUser) mockUsersRepository.getUserByName("forwardToLocal");
268         u.setForwarding(true);
269         u.setForwardingDestination(new MailAddress("localuser@ignoreddomain"));
270         mockServiceManager.put(UsersRepository.ROLE,mockUsersRepository);
271         mockMailServer = new MockMailServer();
272         mailboxes = new HashMap JavaDoc();
273         mailboxes.put("localuser", new InMemorySpoolRepository());
274         mailboxes.put("aliasedUser", new InMemorySpoolRepository());
275         mailboxes.put("virtual@hosting",new InMemorySpoolRepository());
276         Iterator JavaDoc mbi = mailboxes.keySet().iterator();
277         while (mbi.hasNext()) {
278             String JavaDoc mboxName = (String JavaDoc) mbi.next();
279             mockMailServer.setUserInbox(mboxName, (MailRepository) mailboxes.get(mboxName));
280         }
281         mockServiceManager.put(MailServer.ROLE, mockMailServer);
282
283         mockMailetContext = new MockMailContext() {
284
285             public void sendMail(MailAddress sender, Collection JavaDoc recipients, MimeMessage msg) throws MessagingException JavaDoc {
286                 mockMailServer.sendMail(sender, recipients, msg);
287             }
288
289             public void sendMail(MailAddress sender, Collection JavaDoc recipients, MimeMessage msg, String JavaDoc state) throws MessagingException JavaDoc {
290                 MailImpl m = new MailImpl(MockMailServer.newId(), sender, recipients, msg);
291                 m.setState(state);
292                 mockMailServer.sendMail(m);
293                 m.dispose();
294             }
295
296             public boolean isLocalServer(String JavaDoc serverName) {
297                 if ("ignoreddomain".equals(serverName)) {
298                     return true;
299                 }
300                 return super.isLocalServer(serverName);
301             }
302             
303             
304         };
305         mockUsersRepository.setEnableAliases(false);
306         mockUsersRepository.setEnableForwarding(false);
307         mockUsersRepository.setIgnoreCase(false);
308         mockMailetContext.setAttribute(Constants.AVALON_COMPONENT_MANAGER, mockServiceManager);
309     }
310     
311     public void tearDown() {
312         mockMailetContext = null;
313         mailboxes = null;
314         mockServiceManager = null;
315     }
316
317     /**
318      * @param mail
319      * @param expectedMails
320      * @throws MessagingException
321      */

322     private void assertDeliveryWorked(Mail mail, HashMap JavaDoc expectedMails) throws MessagingException JavaDoc {
323         assertEquals(Mail.GHOST, mail.getState());
324         Iterator JavaDoc mboxes = mailboxes.keySet().iterator();
325         while (mboxes.hasNext()) {
326             String JavaDoc mboxName = (String JavaDoc) mboxes.next();
327             MailRepository inMemorySpoolRepository = (MailRepository) mailboxes.get(mboxName);
328             assertExpectedMailsInRepository(mail, (String JavaDoc[]) expectedMails.get(mboxName), inMemorySpoolRepository);
329             
330         }
331         
332         MailRepository sentMailsRepository = mockMailServer.getSentMailsRepository();
333         MailRepository errorsMailRepository = new InMemorySpoolRepository();
334         
335         Iterator JavaDoc keys = sentMailsRepository.list();
336         while (keys.hasNext()) {
337             String JavaDoc nextKey = (String JavaDoc) keys.next();
338             Mail m = sentMailsRepository.retrieve(nextKey);
339             if (Mail.ERROR.equals(m.getState())) {
340                 errorsMailRepository.store(m);
341                 sentMailsRepository.remove(nextKey);
342             } else {
343                 assertEquals("Found a mail in outgoing having a state different from ERROR or ROOT", Mail.DEFAULT, m.getState());
344             }
345             
346             ContainerUtil.dispose(m);
347         }
348         
349         assertExpectedMailsInRepository(mail, (String JavaDoc[]) expectedMails.get("resent"), sentMailsRepository);
350         assertExpectedMailsInRepository(mail, (String JavaDoc[]) expectedMails.get("errors"), errorsMailRepository);
351         
352         ContainerUtil.dispose(errorsMailRepository);
353     }
354
355
356     /**
357      * @param mail
358      * @param expectedMails
359      * @param mboxName
360      * @param inMemorySpoolRepository
361      * @throws MessagingException
362      */

363     private void assertExpectedMailsInRepository(Mail mail, String JavaDoc[] expectedDeliveries, MailRepository inMemorySpoolRepository) throws MessagingException JavaDoc {
364         List JavaDoc c = expectedDeliveries != null ? new ArrayList JavaDoc(Arrays.asList(expectedDeliveries)) : new ArrayList JavaDoc();
365         Iterator JavaDoc i = inMemorySpoolRepository.list();
366         System.out.println("check: "+c.size()+"|"+inMemorySpoolRepository);
367         for (int j = 0; j < c.size(); j++) {
368             assertTrue("No mails have been found in the repository", i.hasNext());
369             String JavaDoc next = (String JavaDoc) i.next();
370             assertNotNull("Mail has not been stored", next);
371             Mail storedMail = inMemorySpoolRepository.retrieve(next);
372             assertNotNull("Mail cannot be retrieved", storedMail);
373             String JavaDoc recipient = storedMail.getRecipients().iterator().next().toString();
374             assertTrue("Message has been delivered to the wrong user", c.contains(recipient));
375             c.remove(recipient);
376         }
377         assertEquals("Message has not been delivered to all expected recipients", c.size(), 0);
378         if (i.hasNext()) {
379             String JavaDoc tooMuch = (String JavaDoc) i.next();
380             Mail m = inMemorySpoolRepository.retrieve(tooMuch);
381             fail("Found unexpected mail for recipient "+m.getRecipients().iterator().next().toString()+"!");
382         }
383     }
384
385     /**
386      * @return
387      * @throws MessagingException
388      */

389     private Mail createMail(String JavaDoc[] recipients) throws MessagingException JavaDoc {
390         Mail mail = new MockMail();
391         ArrayList JavaDoc a = new ArrayList JavaDoc(recipients.length);
392         for (int i = 0; i < recipients.length; i++) {
393             a.add(new MailAddress(recipients[i]));
394         }
395         mail.setRecipients(a);
396         mail.setMessage(new MockMimeMessage());
397         return mail;
398     }
399     
400     public Mailet getMailet(Properties JavaDoc p) throws MessagingException JavaDoc {
401         MockMailetConfig mockMailetConfig = new MockMailetConfig("TestedLocalDelivery", mockMailetContext, p);
402         Mailet m = new LocalDelivery();
403         m.init(mockMailetConfig);
404         return m;
405     }
406 }
407
Popular Tags