KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailrepository > AbstractMailRepositoryTest


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.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 JavaDoc;
31 import javax.mail.internet.MimeMessage JavaDoc;
32 import javax.mail.util.SharedByteArrayInputStream;
33
34 import java.io.IOException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Iterator JavaDoc;
38
39 import junit.framework.TestCase;
40
41 public abstract class AbstractMailRepositoryTest extends TestCase {
42
43     String JavaDoc content = "Subject: test\r\nAAAContent-Transfer-Encoding: text/plain";
44     String JavaDoc sep = "\r\n\r\n";
45     String JavaDoc body = "original body\r\n.\r\n";
46     
47     protected Mail mail;
48     protected MailRepository mailRepository;
49     protected MimeMessage JavaDoc mimeMessage;
50
51     public AbstractMailRepositoryTest() {
52         super();
53     }
54
55     protected void setUp() throws Exception JavaDoc {
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 JavaDoc e) {
62         }
63         mimeMessage = new MimeMessageCopyOnWriteProxy(mmis);
64         Collection JavaDoc recipients = new ArrayList JavaDoc();
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 JavaDoc;
72     
73     protected void tearDown() throws Exception JavaDoc {
74         for (Iterator JavaDoc i = mailRepository.list(); i.hasNext(); ) {
75             mailRepository.remove((String JavaDoc) 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 JavaDoc, IOException JavaDoc {
84         try {
85             mailRepository.store(mail);
86         } catch (Exception JavaDoc e) {
87             fail("Failed to store mail");
88         }
89         Mail m2 = mailRepository.retrieve((String JavaDoc) 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 JavaDoc {
101         assertFalse(mailRepository.list().hasNext());
102         // locking does not check for the existence of the file
103
// assertFalse(mailRepository.lock("random"));
104
assertNull(mailRepository.retrieve("random"));
105         // removing an unexisting message does not throw/return errors
106
mailRepository.remove("random");
107     }
108     
109     public void testListMail() throws MessagingException JavaDoc {
110         mailRepository.store(mail);
111         mailRepository.store(mail);
112         Iterator JavaDoc 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     /**
119      * This test has been written as a proof to:
120      * http://issues.apache.org/jira/browse/JAMES-559
121      */

122     public void testJames559() throws Exception JavaDoc {
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         // ALWAYS remember to dispose mails!
131
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     /**
144      * This test has been written as a proof to:
145      * http://issues.apache.org/jira/browse/JAMES-559
146      */

147     public void testJames559WithoutSaveChanges() throws Exception JavaDoc {
148         mailRepository.store(mail);
149         
150         Mail m2 = mailRepository.retrieve("mail1");
151         m2.getMessage().setHeader("X-Header", "foobar");
152         
153         mailRepository.store(m2);
154         // ALWAYS remember to dispose mails!
155
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