1 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 ; 42 import javax.mail.internet.MimeMessage ; 43 import javax.mail.internet.ParseException ; 44 45 import java.util.ArrayList ; 46 import java.util.Arrays ; 47 import java.util.Collection ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.List ; 51 import java.util.Properties ; 52 53 import junit.framework.TestCase; 54 55 58 public class LocalDeliveryTest extends TestCase { 59 60 private HashMap mailboxes; 61 private MockMailContext mockMailetContext; 62 private MockServiceManager mockServiceManager; 63 private MockMailServer mockMailServer; 64 private MockUsersRepository mockUsersRepository; 65 66 public void testUnknownUser() throws MessagingException { 67 Mailet m = getMailet(null); 69 70 Mail mail = createMail(new String [] {"unknownuser@ignoreddomain"}); 71 m.service(mail); 72 73 HashMap expectedMails = new HashMap (); 74 expectedMails.put("errors", new String [] {"unknownuser@ignoreddomain"}); 75 76 assertDeliveryWorked(mail, expectedMails); 77 } 78 79 80 public void testSimpleDelivery() throws MessagingException { 81 Mailet m = getMailet(null); 83 84 Mail mail = createMail(new String [] {"localuser@ignoreddomain"}); 85 m.service(mail); 86 87 HashMap expectedMails = new HashMap (); 88 expectedMails.put("localuser", new String [] {"localuser@ignoreddomain"}); 89 90 assertDeliveryWorked(mail, expectedMails); 91 } 92 93 public void testSimpleDeliveryCaseSensitiveNoMatch() throws MessagingException { 94 Mailet m = getMailet(null); 96 97 Mail mail = createMail(new String [] {"localUser@ignoreddomain"}); 98 m.service(mail); 99 100 HashMap expectedMails = new HashMap (); 101 expectedMails.put("errors", new String [] {"localUser@ignoreddomain"}); 102 103 assertDeliveryWorked(mail, expectedMails); 104 } 105 106 public void testSimpleDeliveryCaseSensitiveMatch() throws MessagingException { 107 mockUsersRepository.setIgnoreCase(true); 109 Mailet m = getMailet(null); 110 111 Mail mail = createMail(new String [] {"localUser@ignoreddomain"}); 112 m.service(mail); 113 114 HashMap expectedMails = new HashMap (); 115 expectedMails.put("localuser", new String [] {"localuser@ignoreddomain"}); 116 117 assertDeliveryWorked(mail, expectedMails); 118 } 119 120 public void testSimpleAliasDelivery() throws MessagingException { 121 mockUsersRepository.setEnableAliases(true); 122 Mailet m = getMailet(null); 124 125 Mail mail = createMail(new String [] {"aliasedUser@ignoreddomain"}); 126 m.service(mail); 127 128 HashMap expectedMails = new HashMap (); 129 expectedMails.put("localuser", new String [] {"localuser@ignoreddomain"}); 130 131 assertDeliveryWorked(mail, expectedMails); 132 } 133 134 public void testSimpleAliasWithDisabledAlias() throws MessagingException { 135 Mailet m = getMailet(null); 137 138 Mail mail = createMail(new String [] {"aliasedUser@ignoreddomain"}); 139 m.service(mail); 140 141 HashMap expectedMails = new HashMap (); 142 expectedMails.put("aliasedUser", new String [] {"aliasedUser@ignoreddomain"}); 143 144 assertDeliveryWorked(mail, expectedMails); 145 } 146 147 148 public void testForwardingWithForwardingDisabled() throws MessagingException { 149 Mailet m = getMailet(null); 151 152 Mail mail = createMail(new String [] {"forwardingUser@ignoreddomain"}); 153 m.service(mail); 154 155 HashMap expectedMails = new HashMap (); 156 expectedMails.put("errors", new String [] {"forwardingUser@ignoreddomain"}); 157 158 assertDeliveryWorked(mail, expectedMails); 159 } 160 161 162 public void testForwarding() throws MessagingException { 163 mockUsersRepository.setEnableForwarding(true); 164 Mailet m = getMailet(null); 166 167 Mail mail = createMail(new String [] {"forwardingUser@ignoreddomain"}); 168 m.service(mail); 169 170 HashMap expectedMails = new HashMap (); 171 expectedMails.put("resent", new String [] {"remoteuser@remotedomain"}); 172 173 assertDeliveryWorked(mail, expectedMails); 174 } 175 176 public void testAliasingForwarding() throws MessagingException { 177 mockUsersRepository.setEnableAliases(true); 178 mockUsersRepository.setEnableForwarding(true); 179 Mailet m = getMailet(null); 181 182 Mail mail = createMail(new String [] {"aliasForwardUser@ignoreddomain"}); 183 m.service(mail); 184 185 HashMap expectedMails = new HashMap (); 186 expectedMails.put("resent", new String [] {"remoteuser@remotedomain"}); 187 188 assertDeliveryWorked(mail, expectedMails); 189 } 190 191 public void testAliasingForwardingWithLocallyOverriddenForwarding() throws MessagingException { 192 mockUsersRepository.setEnableAliases(true); 193 mockUsersRepository.setEnableForwarding(false); 194 Mailet m = getMailet(null); 195 196 Mail mail = createMail(new String [] {"aliasForwardUser@ignoreddomain"}); 197 m.service(mail); 198 199 HashMap expectedMails = new HashMap (); 200 expectedMails.put("localuser", new String [] {"localuser@ignoreddomain"}); 201 202 assertDeliveryWorked(mail, expectedMails); 203 } 204 205 224 225 public void testSimpleDeliveryVirtualHosting() throws MessagingException { 226 mockMailServer.setVirtualHosting(true); 228 Mailet m = getMailet(null); 229 230 Mail mail = createMail(new String [] {"virtual@hosting"}); 231 m.service(mail); 232 233 HashMap expectedMails = new HashMap (); 234 expectedMails.put("virtual@hosting", new String [] {"virtual@hosting"}); 235 236 assertDeliveryWorked(mail, expectedMails); 237 } 238 239 243 public void setUp() throws ParseException { 244 mockServiceManager = new MockServiceManager(); 245 mockUsersRepository = new MockUsersRepository(); 246 mockUsersRepository.setForceUseJamesUser(); 247 mockUsersRepository.addUser("localuser", "password"); 248 mockUsersRepository.addUser("aliasedUser", "pass2"); 249 250 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 (); 273 mailboxes.put("localuser", new InMemorySpoolRepository()); 274 mailboxes.put("aliasedUser", new InMemorySpoolRepository()); 275 mailboxes.put("virtual@hosting",new InMemorySpoolRepository()); 276 Iterator mbi = mailboxes.keySet().iterator(); 277 while (mbi.hasNext()) { 278 String mboxName = (String ) 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 recipients, MimeMessage msg) throws MessagingException { 286 mockMailServer.sendMail(sender, recipients, msg); 287 } 288 289 public void sendMail(MailAddress sender, Collection recipients, MimeMessage msg, String state) throws MessagingException { 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 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 322 private void assertDeliveryWorked(Mail mail, HashMap expectedMails) throws MessagingException { 323 assertEquals(Mail.GHOST, mail.getState()); 324 Iterator mboxes = mailboxes.keySet().iterator(); 325 while (mboxes.hasNext()) { 326 String mboxName = (String ) mboxes.next(); 327 MailRepository inMemorySpoolRepository = (MailRepository) mailboxes.get(mboxName); 328 assertExpectedMailsInRepository(mail, (String []) expectedMails.get(mboxName), inMemorySpoolRepository); 329 330 } 331 332 MailRepository sentMailsRepository = mockMailServer.getSentMailsRepository(); 333 MailRepository errorsMailRepository = new InMemorySpoolRepository(); 334 335 Iterator keys = sentMailsRepository.list(); 336 while (keys.hasNext()) { 337 String nextKey = (String ) 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 []) expectedMails.get("resent"), sentMailsRepository); 350 assertExpectedMailsInRepository(mail, (String []) expectedMails.get("errors"), errorsMailRepository); 351 352 ContainerUtil.dispose(errorsMailRepository); 353 } 354 355 356 363 private void assertExpectedMailsInRepository(Mail mail, String [] expectedDeliveries, MailRepository inMemorySpoolRepository) throws MessagingException { 364 List c = expectedDeliveries != null ? new ArrayList (Arrays.asList(expectedDeliveries)) : new ArrayList (); 365 Iterator 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 next = (String ) 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 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 tooMuch = (String ) i.next(); 380 Mail m = inMemorySpoolRepository.retrieve(tooMuch); 381 fail("Found unexpected mail for recipient "+m.getRecipients().iterator().next().toString()+"!"); 382 } 383 } 384 385 389 private Mail createMail(String [] recipients) throws MessagingException { 390 Mail mail = new MockMail(); 391 ArrayList a = new ArrayList (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 p) throws MessagingException { 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 |