1 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 ; 27 import javax.mail.Session ; 28 import javax.mail.internet.MimeMessage ; 29 import javax.mail.util.SharedByteArrayInputStream; 30 31 import java.util.ArrayList ; 32 import java.util.Properties ; 33 34 public class MimeMessageCopyOnWriteProxyTest extends MimeMessageFromStreamTest { 35 36 String content = "Subject: foo\r\nContent-Transfer-Encoding2: plain"; 37 String sep = "\r\n\r\n"; 38 String body = "bar\r\n.\r\n"; 39 40 protected MimeMessage getMessageFromSources(String sources) throws Exception { 41 MimeMessageInputStreamSource mmis = null; 42 try { 43 mmis = new MimeMessageInputStreamSource("test", new SharedByteArrayInputStream(sources.getBytes())); 44 } catch (MessagingException e) { 45 } 46 return new MimeMessageCopyOnWriteProxy(mmis); 47 } 49 50 public void testMessageCloning1() throws Exception { 51 ArrayList r = new ArrayList (); 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 assertTrue(isSameMimeMessage(m2.getMessage(),mail.getMessage())); 61 mail.getMessage().getSubject(); 63 assertTrue(isSameMimeMessage(m2.getMessage(),mail.getMessage())); 64 mail.getMessage().setText("new body"); 65 mail.getMessage().saveChanges(); 66 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 { 76 ArrayList r = new ArrayList (); 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 assertTrue(isSameMimeMessage(m2.getMessage(),mail.getMessage())); 86 m2.getMessage().getSubject(); 88 assertTrue(isSameMimeMessage(m2.getMessage(),mail.getMessage())); 89 m2.getMessage().setText("new body"); 90 m2.getMessage().saveChanges(); 91 m2.getMessage().setSubject("new Subject"); 93 assertTrue(!isSameMimeMessage(m2.getMessage(),mail.getMessage())); 94 assertEquals(m2.getMessage().getSubject(),"new Subject"); 96 assertEquals(mail.getMessage().getSubject(),"foo"); 97 Mail m2clone = m2.duplicate(); 99 assertTrue(isSameMimeMessage(m2clone.getMessage(),m2.getMessage())); 100 MimeMessage mm = getWrappedMessage(m2.getMessage()); 101 assertNotSame(m2.getMessage(),m2clone.getMessage()); 102 MimeMessage mm3 = getWrappedMessage(m2clone.getMessage()); 104 assertNotNull(mm3); 105 ContainerUtil.dispose(m2); 107 assertEquals(mm3,getWrappedMessage(m2clone.getMessage())); 108 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 121 public void testMessageAvoidCloning() throws Exception { 122 ArrayList r = new ArrayList (); 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 Mail mailClone = mail.duplicate(); 128 assertTrue(isSameMimeMessage(mailClone.getMessage(),mail.getMessage())); 129 MimeMessage mm = getWrappedMessage(mail.getMessage()); 130 assertNotSame(mail.getMessage(),mailClone.getMessage()); 131 ContainerUtil.dispose(mail); 133 ContainerUtil.dispose(messageFromSources); 134 System.gc(); 136 Thread.sleep(1000); 137 assertTrue(isSameMimeMessage(mailClone.getMessage(),mailClone.getMessage())); 139 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 153 public void testMessageCloning3() throws Exception { 154 ArrayList r = new ArrayList (); 155 r.add(new MailAddress("recipient@test.com")); 156 MimeMessage m = new MimeMessage (Session.getDefaultInstance(new Properties (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 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 { 175 ArrayList r = new ArrayList (); 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 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 , InterruptedException { 195 ArrayList recipients = new ArrayList (); 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 mw.getMessageSize(); 213 ContainerUtil.dispose(mw); 214 } 215 216 217 221 public void testMessageCloningViaCoW3() throws Exception { 222 MimeMessage mmorig = getSimpleMessage(); 223 224 MimeMessage 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 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 getReferences(MimeMessage m) { 242 StringBuffer ref = new StringBuffer ("/"); 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 ) { 250 ref.append("M"); 251 } else { 252 ref.append(m.getClass()); 253 } 254 return ref.toString(); 255 } 256 257 private static MimeMessage getWrappedMessage(MimeMessage m) { 258 while (m instanceof MimeMessageCopyOnWriteProxy) { 259 m = ((MimeMessageCopyOnWriteProxy) m).getWrappedMessage(); 260 } 261 return m; 262 } 263 264 private static boolean isSameMimeMessage(MimeMessage first, MimeMessage second) { 265 return getWrappedMessage(first) == getWrappedMessage(second); 266 267 } 268 269 } 270 | Popular Tags |