1 19 20 package org.apache.james.mailrepository; 21 22 import org.apache.avalon.framework.container.ContainerUtil; 23 import org.apache.james.core.MailImpl; 24 import org.apache.james.core.MimeMessageCopyOnWriteProxy; 25 import org.apache.james.core.MimeMessageInputStreamSource; 26 import org.apache.james.services.MailRepository; 27 import org.apache.mailet.Mail; 28 import org.apache.mailet.MailAddress; 29 30 import javax.mail.MessagingException ; 31 import javax.mail.internet.MimeMessage ; 32 import javax.mail.util.SharedByteArrayInputStream; 33 34 import java.io.IOException ; 35 import java.util.ArrayList ; 36 import java.util.Collection ; 37 import java.util.Iterator ; 38 39 import junit.framework.TestCase; 40 41 public abstract class AbstractMailRepositoryTest extends TestCase { 42 43 String content = "Subject: test\r\nAAAContent-Transfer-Encoding: text/plain"; 44 String sep = "\r\n\r\n"; 45 String body = "original body\r\n.\r\n"; 46 47 protected Mail mail; 48 protected MailRepository mailRepository; 49 protected MimeMessage mimeMessage; 50 51 public AbstractMailRepositoryTest() { 52 super(); 53 } 54 55 protected void setUp() throws Exception { 56 super.setUp(); 57 mailRepository = getMailRepository(); 58 MimeMessageInputStreamSource mmis = null; 59 try { 60 mmis = new MimeMessageInputStreamSource("test", new SharedByteArrayInputStream((content+sep+body).getBytes())); 61 } catch (MessagingException e) { 62 } 63 mimeMessage = new MimeMessageCopyOnWriteProxy(mmis); 64 Collection recipients = new ArrayList (); 65 recipients.add(new MailAddress("rec1","domain.com")); 66 recipients.add(new MailAddress("rec2","domain.com")); 67 mail = new MailImpl("mail1",new MailAddress("sender","domain.com"),recipients,mimeMessage); 68 mail.setAttribute("testAttribute", "testValue"); 69 } 70 71 protected abstract MailRepository getMailRepository() throws Exception ; 72 73 protected void tearDown() throws Exception { 74 for (Iterator i = mailRepository.list(); i.hasNext(); ) { 75 mailRepository.remove((String ) i.next()); 76 } 77 ContainerUtil.dispose(mail); 78 ContainerUtil.dispose(mimeMessage); 79 ContainerUtil.dispose(mailRepository); 80 super.tearDown(); 81 } 82 83 public void testStoreAndRetrieveMail() throws MessagingException , IOException { 84 try { 85 mailRepository.store(mail); 86 } catch (Exception e) { 87 fail("Failed to store mail"); 88 } 89 Mail m2 = mailRepository.retrieve((String ) mailRepository.list().next()); 90 91 assertEquals("stored and retrieved messages do not match", mail.getMessage().getContent().toString(),m2.getMessage().getContent().toString()); 92 assertEquals("stored and retrieved message sizes do not match", mail.getMessageSize(), m2.getMessageSize()); 93 assertEquals("stored and retrieved keys do not match", mail.getName(), m2.getName()); 94 assertEquals("stored and retrieved states do not match", mail.getState(), m2.getState()); 95 assertEquals("stored and retrieved attributes do not match", mail.getAttribute("testAttribute"), m2.getAttribute("testAttribute")); 96 97 ContainerUtil.dispose(m2); 98 } 99 100 public void testEmptyRepository() throws MessagingException { 101 assertFalse(mailRepository.list().hasNext()); 102 assertNull(mailRepository.retrieve("random")); 105 mailRepository.remove("random"); 107 } 108 109 public void testListMail() throws MessagingException { 110 mailRepository.store(mail); 111 mailRepository.store(mail); 112 Iterator i = mailRepository.list(); 113 assertTrue(i.hasNext()); 114 assertEquals(mail.getName(),i.next()); 115 assertFalse("Found more than one message after storing 2 times the SAME message: it MUST update the previous", i.hasNext()); 116 } 117 118 122 public void testJames559() throws Exception { 123 mailRepository.store(mail); 124 125 Mail m2 = mailRepository.retrieve("mail1"); 126 m2.getMessage().setHeader("X-Header", "foobar"); 127 m2.getMessage().saveChanges(); 128 129 mailRepository.store(m2); 130 ContainerUtil.dispose(m2); 132 133 m2 = mailRepository.retrieve("mail1"); 134 assertEquals(mail.getMessage().getContent().toString(),m2.getMessage().getContent().toString()); 135 136 ContainerUtil.dispose(mail); 137 mail = null; 138 ContainerUtil.dispose(m2); 139 140 mailRepository.remove("mail1"); 141 } 142 143 147 public void testJames559WithoutSaveChanges() throws Exception { 148 mailRepository.store(mail); 149 150 Mail m2 = mailRepository.retrieve("mail1"); 151 m2.getMessage().setHeader("X-Header", "foobar"); 152 153 mailRepository.store(m2); 154 ContainerUtil.dispose(m2); 156 157 m2 = mailRepository.retrieve("mail1"); 158 assertEquals(mail.getMessage().getContent().toString(),m2.getMessage().getContent().toString()); 159 160 ContainerUtil.dispose(mail); 161 mail = null; 162 163 ContainerUtil.dispose(m2); 164 165 mailRepository.remove("mail1"); 166 } 167 168 } 169 | Popular Tags |