1 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 ; 37 import java.util.HashMap ; 38 39 42 public class SpoolManagementTest extends TestCase { 43 44 protected static final String 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 { 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 { 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 { 73 String [] mailList = m_spoolManagement.listSpoolItems(OUTGOING_SPOOL, SpoolFilter.ERRORMAIL_FILTER); 74 assertEquals("emtpy spool", 0, mailList.length); 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 { 84 MockMail mockMail1 = createSpoolMail(Mail.DEFAULT, "subj1", 1); 85 String messageID1 = mockMail1.getName(); 86 87 String [] 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 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 { 106 107 MockMail mockMail1 = createSpoolMail(Mail.DEFAULT, "TestHeader", "value1", 1); 108 String messageID1 = mockMail1.getName(); 109 110 createSpoolMail(Mail.ERROR, "TestHeader", "value2", 2); 111 createSpoolMail(Mail.ERROR, "TestHeader", "another", 3); 112 113 String [] 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 headerMap = new HashMap (); 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 (); 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 { 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 [] 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 { 148 createSpoolMail(Mail.DEFAULT, "subj1", 1); 149 MockMail mockMail2 = createSpoolMail(Mail.ERROR, "subj2", 2); 150 String messageID2 = mockMail2.getName(); 151 MockMail mockMail3 = createSpoolMail(Mail.ERROR, "subj3", 3); 152 String messageID3 = mockMail3.getName(); 153 154 assertEquals("one mail removed", 1, m_spoolManagement.removeSpoolItems(OUTGOING_SPOOL, messageID3, SpoolFilter.ERRORMAIL_FILTER)); 155 String [] 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 state, String subject, int number) throws MessagingException { 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 state, String headerName, String headerRegex, int number) throws MessagingException { 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 |