1 19 20 package org.apache.james.core; 21 22 import org.apache.avalon.framework.container.ContainerUtil; 23 import org.apache.mailet.RFC2822Headers; 24 25 import javax.mail.MessagingException ; 26 import javax.mail.internet.MimeMessage ; 27 import javax.mail.util.SharedByteArrayInputStream; 28 29 import java.io.BufferedReader ; 30 import java.io.ByteArrayOutputStream ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.InputStreamReader ; 34 import java.util.Enumeration ; 35 36 39 public class MimeMessageWrapperTest extends MimeMessageFromStreamTest { 40 41 private final class TestableMimeMessageWrapper extends MimeMessageWrapper { 42 43 boolean messageLoadable = true; 44 boolean headersLoadable = true; 45 46 private TestableMimeMessageWrapper(MimeMessageSource source) throws MessagingException { 47 super(source); 48 } 49 50 public boolean messageParsed() { 51 return messageParsed; 52 } 53 54 public MailHeaders getInnerHeaders() { 55 return (MailHeaders) headers; 56 } 57 58 public boolean isHeadersLoadable() { 59 return headersLoadable; 60 } 61 62 public void setHeadersLoadable(boolean headersLoadable) { 63 this.headersLoadable = headersLoadable; 64 } 65 66 public boolean isMessageLoadable() { 67 return messageLoadable; 68 } 69 70 public void setMessageLoadable(boolean messageLoadable) { 71 this.messageLoadable = messageLoadable; 72 } 73 74 protected synchronized void loadHeaders() throws MessagingException { 75 if (headersLoadable) { 76 super.loadHeaders(); 77 } else { 78 throw new IllegalStateException ("headersLoadable disabled"); 79 } 80 } 81 82 protected synchronized MailHeaders loadHeaders(InputStream is) throws MessagingException { 83 if (headersLoadable) { 84 return (MailHeaders) super.createInternetHeaders(is); 85 } else { 86 throw new IllegalStateException ("headersLoadable disabled"); 87 } 88 } 89 90 protected synchronized void loadMessage() throws MessagingException { 91 if (messageLoadable) { 92 super.loadMessage(); 93 } else { 94 throw new IllegalStateException ("messageLoadable disabled"); 95 } 96 } 97 98 99 100 } 101 102 TestableMimeMessageWrapper mw = null; 103 String content = "Subject: foo\r\nContent-Transfer-Encoding2: plain"; 104 String sep = "\r\n\r\n"; 105 String body = "bar\r\n"; 106 107 protected MimeMessage getMessageFromSources(String sources) throws Exception { 108 MimeMessageInputStreamSource mmis = null; 109 try { 110 mmis = new MimeMessageInputStreamSource("test", new SharedByteArrayInputStream(sources.getBytes())); 111 } catch (MessagingException e) { 112 } 113 return new TestableMimeMessageWrapper(mmis); 114 } 115 116 protected void setUp() throws Exception { 117 mw = (TestableMimeMessageWrapper) getMessageFromSources(content+sep+body); 118 } 119 120 protected void tearDown() throws Exception { 121 ContainerUtil.dispose(mw); 122 } 123 124 125 public void testDeferredMessageLoading() throws MessagingException , IOException { 126 assertEquals("foo",mw.getSubject()); 127 assertFalse(mw.messageParsed()); 128 assertEquals("bar\r\n",mw.getContent()); 129 assertTrue(mw.messageParsed()); 130 assertFalse(mw.isModified()); 131 } 132 133 146 147 public void testDeferredHeaderLoading() throws MessagingException , IOException { 148 mw.setHeadersLoadable(false); 149 try { 150 assertEquals("foo",mw.getSubject()); 151 fail("subject should not be loadable here, headers loading is disabled"); 152 } catch (IllegalStateException e) { 153 154 } 155 } 156 157 161 public void testMessageCloned() throws MessagingException , IOException , InterruptedException { 162 MimeMessageWrapper mmw = new MimeMessageWrapper(mw); 163 ContainerUtil.dispose(mw); 164 mw = null; 165 System.gc(); 166 Thread.sleep(200); 167 mmw.writeTo(System.out); 168 } 169 170 173 public void testGetSubjectFolding() { 174 try { 175 StringBuffer res = new StringBuffer (); 176 BufferedReader r = new BufferedReader (new InputStreamReader (mw.getInputStream())); 177 String line; 178 while (r.ready()) { 179 line = r.readLine(); 180 res.append(line+"\r\n"); 181 } 182 r.close(); 183 assertEquals(body,res.toString()); 184 } catch (MessagingException e) { 185 e.printStackTrace(); 187 } catch (IOException e) { 188 e.printStackTrace(); 190 } 191 } 192 193 196 public void testAddHeaderAndSave() { 197 try { 198 mw.addHeader("X-Test", "X-Value"); 199 200 assertEquals("X-Value", mw.getHeader("X-Test")[0]); 201 202 mw.saveChanges(); 203 204 ByteArrayOutputStream rawMessage = new ByteArrayOutputStream (); 205 mw.writeTo(rawMessage); 206 207 assertEquals("X-Value", mw.getHeader("X-Test")[0]); 208 209 String res = rawMessage.toString(); 210 211 boolean found = res.indexOf("X-Test: X-Value") > 0; 212 assertEquals(true,found); 213 214 } catch (MessagingException e) { 215 e.printStackTrace(); 217 } catch (IOException e) { 218 e.printStackTrace(); 220 } 221 } 222 223 224 public void testReplaceReturnPathOnBadMessage() throws Exception { 225 MimeMessage message = getMessageWithBadReturnPath(); 226 message.setHeader(RFC2822Headers.RETURN_PATH, "<test@test.de>"); 227 Enumeration e = message.getMatchingHeaderLines(new String [] {"Return-Path"}); 228 assertEquals("Return-Path: <test@test.de>",e.nextElement()); 229 assertFalse(e.hasMoreElements()); 230 Enumeration h = message.getAllHeaderLines(); 231 assertEquals("Return-Path: <test@test.de>",h.nextElement()); 232 assertFalse(h.nextElement().toString().startsWith("Return-Path:")); 233 ContainerUtil.dispose(message); 234 } 235 236 public void testAddReturnPathOnBadMessage() throws Exception { 237 MimeMessage message = getMessageWithBadReturnPath(); 238 message.addHeader(RFC2822Headers.RETURN_PATH, "<test@test.de>"); 239 Enumeration e = message.getMatchingHeaderLines(new String [] {"Return-Path"}); 241 assertEquals("Return-Path: <test@test.de>",e.nextElement()); 242 assertEquals("Return-Path: <mybadreturn@example.com>",e.nextElement()); 243 Enumeration h = message.getAllHeaderLines(); 245 assertEquals("Return-Path: <test@test.de>",h.nextElement()); 246 ContainerUtil.dispose(message); 247 } 248 249 } 250 | Popular Tags |