KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > core > MimeMessageCopyOnWriteProxyTest


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.core;
21
22 import org.apache.avalon.framework.container.ContainerUtil;
23 import org.apache.mailet.Mail;
24 import org.apache.mailet.MailAddress;
25
26 import javax.mail.MessagingException JavaDoc;
27 import javax.mail.Session JavaDoc;
28 import javax.mail.internet.MimeMessage JavaDoc;
29 import javax.mail.util.SharedByteArrayInputStream;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 public class MimeMessageCopyOnWriteProxyTest extends MimeMessageFromStreamTest {
35
36     String JavaDoc content = "Subject: foo\r\nContent-Transfer-Encoding2: plain";
37     String JavaDoc sep = "\r\n\r\n";
38     String JavaDoc body = "bar\r\n.\r\n";
39
40     protected MimeMessage JavaDoc getMessageFromSources(String JavaDoc sources) throws Exception JavaDoc {
41         MimeMessageInputStreamSource mmis = null;
42         try {
43             mmis = new MimeMessageInputStreamSource("test", new SharedByteArrayInputStream(sources.getBytes()));
44         } catch (MessagingException JavaDoc e) {
45         }
46         return new MimeMessageCopyOnWriteProxy(mmis);
47 // return new MimeMessage(Session.getDefaultInstance(new Properties()),new ByteArrayInputStream(sources.getBytes()));
48
}
49
50     public void testMessageCloning1() throws Exception JavaDoc {
51         ArrayList JavaDoc r = new ArrayList JavaDoc();
52         r.add(new MailAddress("recipient@test.com"));
53         MimeMessageCopyOnWriteProxy messageFromSources = (MimeMessageCopyOnWriteProxy) getMessageFromSources(content+sep+body);
54         MailImpl mail = new MailImpl("test",new MailAddress("test@test.com"),r,messageFromSources);
55         MailImpl m2 = (MailImpl) mail.duplicate();
56         System.out.println("mail: "+getReferences(mail.getMessage())+" m2: "+getReferences(m2.getMessage()));
57         assertNotSame(m2,mail);
58         assertNotSame(m2.getMessage(),mail.getMessage());
59         // test that the wrapped message is the same
60
assertTrue(isSameMimeMessage(m2.getMessage(),mail.getMessage()));
61         // test it is the same after read only operations!
62
mail.getMessage().getSubject();
63         assertTrue(isSameMimeMessage(m2.getMessage(),mail.getMessage()));
64         mail.getMessage().setText("new body");
65         mail.getMessage().saveChanges();
66         // test it is different after a write operation!
67
mail.getMessage().setSubject("new Subject");
68         assertTrue(!isSameMimeMessage(m2.getMessage(),mail.getMessage()));
69         ContainerUtil.dispose(mail);
70         ContainerUtil.dispose(m2);
71         ContainerUtil.dispose(messageFromSources);
72     }
73
74     
75     public void testMessageCloning2() throws Exception JavaDoc {
76         ArrayList JavaDoc r = new ArrayList JavaDoc();
77         r.add(new MailAddress("recipient@test.com"));
78         MimeMessageCopyOnWriteProxy messageFromSources = (MimeMessageCopyOnWriteProxy) getMessageFromSources(content+sep+body);
79         MailImpl mail = new MailImpl("test",new MailAddress("test@test.com"),r,messageFromSources);
80         MailImpl m2 = (MailImpl) mail.duplicate();
81         System.out.println("mail: "+getReferences(mail.getMessage())+" m2: "+getReferences(m2.getMessage()));
82         assertNotSame(m2,mail);
83         assertNotSame(m2.getMessage(),mail.getMessage());
84         // test that the wrapped message is the same
85
assertTrue(isSameMimeMessage(m2.getMessage(),mail.getMessage()));
86         // test it is the same after real only operations!
87
m2.getMessage().getSubject();
88         assertTrue(isSameMimeMessage(m2.getMessage(),mail.getMessage()));
89         m2.getMessage().setText("new body");
90         m2.getMessage().saveChanges();
91         // test it is different after a write operation!
92
m2.getMessage().setSubject("new Subject");
93         assertTrue(!isSameMimeMessage(m2.getMessage(),mail.getMessage()));
94         // check that the subjects are correct on both mails!
95
assertEquals(m2.getMessage().getSubject(),"new Subject");
96         assertEquals(mail.getMessage().getSubject(),"foo");
97         // cloning again the messages
98
Mail m2clone = m2.duplicate();
99         assertTrue(isSameMimeMessage(m2clone.getMessage(),m2.getMessage()));
100         MimeMessage JavaDoc mm = getWrappedMessage(m2.getMessage());
101         assertNotSame(m2.getMessage(),m2clone.getMessage());
102         // test that m2clone has a valid wrapped message
103
MimeMessage JavaDoc mm3 = getWrappedMessage(m2clone.getMessage());
104         assertNotNull(mm3);
105         // dispose m2 and check that the clone has still a valid message and it is the same!
106
ContainerUtil.dispose(m2);
107         assertEquals(mm3,getWrappedMessage(m2clone.getMessage()));
108         // change the message that should be not referenced by m2 that has
109
// been disposed, so it should not clone it!
110
m2clone.getMessage().setSubject("new Subject 2");
111         m2clone.getMessage().setText("new Body 3");
112         assertTrue(isSameMimeMessage(m2clone.getMessage(),mm));
113         ContainerUtil.dispose(mail);
114         ContainerUtil.dispose(messageFromSources);
115     }
116     
117     /**
118      * If I create a new MimeMessageCopyOnWriteProxy from another MimeMessageCopyOnWriteProxy,
119      * I remove references to the first and I change the second, then it should not clone
120      */

121     public void testMessageAvoidCloning() throws Exception JavaDoc {
122         ArrayList JavaDoc r = new ArrayList JavaDoc();
123         r.add(new MailAddress("recipient@test.com"));
124         MimeMessageCopyOnWriteProxy messageFromSources = (MimeMessageCopyOnWriteProxy) getMessageFromSources(content+sep+body);
125         MailImpl mail = new MailImpl("test",new MailAddress("test@test.com"),r,messageFromSources);
126         // cloning the message
127
Mail mailClone = mail.duplicate();
128         assertTrue(isSameMimeMessage(mailClone.getMessage(),mail.getMessage()));
129         MimeMessage JavaDoc mm = getWrappedMessage(mail.getMessage());
130         assertNotSame(mail.getMessage(),mailClone.getMessage());
131         // dispose mail and check that the clone has still a valid message and it is the same!
132
ContainerUtil.dispose(mail);
133         ContainerUtil.dispose(messageFromSources);
134         // need to add a gc and a wait, because the original mimemessage should be finalized before the test.
135
System.gc();
136         Thread.sleep(1000);
137         // dumb test
138
assertTrue(isSameMimeMessage(mailClone.getMessage(),mailClone.getMessage()));
139         // change the message that should be not referenced by mail that has
140
// been disposed, so it should not clone it!
141
mailClone.getMessage().setSubject("new Subject 2");
142         mailClone.getMessage().setText("new Body 3");
143         assertTrue(isSameMimeMessage(mailClone.getMessage(),mm));
144         ContainerUtil.dispose(mailClone);
145         ContainerUtil.dispose(mm);
146     }
147
148     
149     /**
150      * If I create a new MimeMessageCopyOnWriteProxy from a MimeMessage and I change the new
151      * message, the original should be unaltered and the proxy should clone the message.
152      */

153     public void testMessageCloning3() throws Exception JavaDoc {
154         ArrayList JavaDoc r = new ArrayList JavaDoc();
155         r.add(new MailAddress("recipient@test.com"));
156         MimeMessage JavaDoc m = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc(null)));
157         m.setText("CIPS");
158         MailImpl mail = new MailImpl("test",new MailAddress("test@test.com"),r,m);
159         assertTrue(isSameMimeMessage(m,mail.getMessage()));
160         // change the message that should be not referenced by mail that has
161
// been disposed, so it should not clone it!
162
System.gc();
163         Thread.sleep(100);
164         mail.getMessage().setSubject("new Subject 2");
165         mail.getMessage().setText("new Body 3");
166         System.gc();
167         Thread.sleep(100);
168         assertFalse(isSameMimeMessage(m,mail.getMessage()));
169         ContainerUtil.dispose(mail);
170         ContainerUtil.dispose(m);
171     }
172
173     
174     public void testMessageDisposing() throws Exception JavaDoc {
175         ArrayList JavaDoc r = new ArrayList JavaDoc();
176         r.add(new MailAddress("recipient@test.com"));
177         MimeMessageCopyOnWriteProxy messageFromSources = (MimeMessageCopyOnWriteProxy) getMessageFromSources(content+sep+body);
178         MailImpl mail = new MailImpl("test",new MailAddress("test@test.com"),r,messageFromSources);
179         // cloning the message
180
MailImpl mailClone = (MailImpl) mail.duplicate();
181         ContainerUtil.dispose(mail);
182
183         assertNotNull(getWrappedMessage(mailClone.getMessage()));
184         assertNull(mail.getMessage());
185
186         ContainerUtil.dispose(mailClone);
187         
188         assertNull(mailClone.getMessage());
189         assertNull(mail.getMessage());
190         ContainerUtil.dispose(mail);
191         ContainerUtil.dispose(messageFromSources);
192     }
193     
194     public void testNPE1() throws MessagingException JavaDoc, InterruptedException JavaDoc {
195         ArrayList JavaDoc recipients = new ArrayList JavaDoc();
196         recipients.add(new MailAddress("recipient@test.com"));
197         MimeMessageCopyOnWriteProxy mw = new MimeMessageCopyOnWriteProxy(
198                 new MimeMessageInputStreamSource(
199                         "test",
200                         new SharedByteArrayInputStream(
201                                 ("Return-path: return@test.com\r\n"+
202                                  "Content-Transfer-Encoding: plain\r\n"+
203                                  "Subject: test\r\n\r\n"+
204                                  "Body Text testNPE1\r\n").getBytes())));
205         
206         MimeMessageCopyOnWriteProxy mw2 = new MimeMessageCopyOnWriteProxy(mw);
207         ContainerUtil.dispose(mw2);
208         mw2 = null;
209         System.gc();
210         Thread.sleep(1000);
211         // the NPE was inside this call
212
mw.getMessageSize();
213         ContainerUtil.dispose(mw);
214     }
215
216     
217     /**
218      * This test throw a NullPointerException when the original message was created by
219      * a MimeMessageInputStreamSource.
220      */

221     public void testMessageCloningViaCoW3() throws Exception JavaDoc {
222         MimeMessage JavaDoc mmorig = getSimpleMessage();
223         
224         MimeMessage JavaDoc mm = new MimeMessageCopyOnWriteProxy(mmorig);
225         
226         ContainerUtil.dispose(mmorig);
227         mmorig = null;
228         System.gc();
229         Thread.sleep(200);
230
231         try {
232             mm.writeTo(System.out);
233         } catch (Exception JavaDoc e) {
234             e.printStackTrace();
235             fail("Exception while writing the message to output");
236         }
237         
238         ContainerUtil.dispose(mm);
239     }
240
241     private static String JavaDoc getReferences(MimeMessage JavaDoc m) {
242         StringBuffer JavaDoc ref = new StringBuffer JavaDoc("/");
243         while (m instanceof MimeMessageCopyOnWriteProxy) {
244             ref.append(((MimeMessageCopyOnWriteProxy) m).refCount.getReferenceCount()+"/");
245             m = ((MimeMessageCopyOnWriteProxy) m).getWrappedMessage();
246         }
247         if (m instanceof MimeMessageWrapper) {
248             ref.append("W");
249         } else if (m instanceof MimeMessage JavaDoc) {
250             ref.append("M");
251         } else {
252             ref.append(m.getClass());
253         }
254         return ref.toString();
255     }
256     
257     private static MimeMessage JavaDoc getWrappedMessage(MimeMessage JavaDoc m) {
258         while (m instanceof MimeMessageCopyOnWriteProxy) {
259           m = ((MimeMessageCopyOnWriteProxy) m).getWrappedMessage();
260         }
261         return m;
262     }
263     
264     private static boolean isSameMimeMessage(MimeMessage JavaDoc first, MimeMessage JavaDoc second) {
265         return getWrappedMessage(first) == getWrappedMessage(second);
266         
267     }
268
269 }
270
Popular Tags