KickJava   Java API By Example, From Geeks To Geeks.

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


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.RFC2822Headers;
24
25 import javax.mail.MessagingException JavaDoc;
26 import javax.mail.internet.MimeMessage JavaDoc;
27 import javax.mail.util.SharedByteArrayInputStream;
28
29 import java.io.BufferedReader JavaDoc;
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.util.Enumeration JavaDoc;
35
36 /**
37  * Test the subject folding issue.
38  */

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 JavaDoc {
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 JavaDoc {
75             if (headersLoadable) {
76                 super.loadHeaders();
77             } else {
78                 throw new IllegalStateException JavaDoc("headersLoadable disabled");
79             }
80         }
81
82         protected synchronized MailHeaders loadHeaders(InputStream is) throws MessagingException JavaDoc {
83             if (headersLoadable) {
84                 return (MailHeaders) super.createInternetHeaders(is);
85             } else {
86                 throw new IllegalStateException JavaDoc("headersLoadable disabled");
87             }
88         }
89
90         protected synchronized void loadMessage() throws MessagingException JavaDoc {
91             if (messageLoadable) {
92                 super.loadMessage();
93             } else {
94                 throw new IllegalStateException JavaDoc("messageLoadable disabled");
95             }
96         }
97         
98         
99         
100     }
101
102     TestableMimeMessageWrapper mw = null;
103     String JavaDoc content = "Subject: foo\r\nContent-Transfer-Encoding2: plain";
104     String JavaDoc sep = "\r\n\r\n";
105     String JavaDoc body = "bar\r\n";
106
107     protected MimeMessage JavaDoc getMessageFromSources(String JavaDoc sources) throws Exception JavaDoc {
108         MimeMessageInputStreamSource mmis = null;
109         try {
110             mmis = new MimeMessageInputStreamSource("test", new SharedByteArrayInputStream(sources.getBytes()));
111         } catch (MessagingException JavaDoc e) {
112         }
113         return new TestableMimeMessageWrapper(mmis);
114     }
115
116     protected void setUp() throws Exception JavaDoc {
117         mw = (TestableMimeMessageWrapper) getMessageFromSources(content+sep+body);
118     }
119
120     protected void tearDown() throws Exception JavaDoc {
121         ContainerUtil.dispose(mw);
122     }
123
124     
125     public void testDeferredMessageLoading() throws MessagingException JavaDoc, IOException JavaDoc {
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     /** this is commented out due optimisation reverts (JAMES-559)
134     public void testDeferredMessageLoadingWhileWriting() throws MessagingException, IOException {
135         mw.setMessageLoadable(false);
136         assertEquals("foo",mw.getSubject());
137         assertFalse(mw.isModified());
138         mw.setSubject("newSubject");
139         assertEquals("newSubject",mw.getSubject());
140         assertFalse(mw.messageParsed());
141         assertTrue(mw.isModified());
142         mw.setMessageLoadable(true);
143         
144     }
145     */

146
147     public void testDeferredHeaderLoading() throws MessagingException JavaDoc, IOException JavaDoc {
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 JavaDoc e) {
153             
154         }
155     }
156
157     /**
158      * See JAMES-474
159      * MimeMessageWrapper(MimeMessage) should clone the original message.
160      */

161     public void testMessageCloned() throws MessagingException JavaDoc, IOException JavaDoc, InterruptedException JavaDoc {
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     /*
171      * Class under test for String getSubject()
172      */

173     public void testGetSubjectFolding() {
174         try {
175             StringBuffer JavaDoc res = new StringBuffer JavaDoc();
176             BufferedReader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(mw.getInputStream()));
177             String JavaDoc 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 JavaDoc e) {
185             // TODO Auto-generated catch block
186
e.printStackTrace();
187         } catch (IOException JavaDoc e) {
188             // TODO Auto-generated catch block
189
e.printStackTrace();
190         }
191     }
192     
193     /*
194      * Class under test for String getSubject()
195      */

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 JavaDoc rawMessage = new ByteArrayOutputStream JavaDoc();
205             mw.writeTo(rawMessage);
206             
207             assertEquals("X-Value", mw.getHeader("X-Test")[0]);
208
209             String JavaDoc res = rawMessage.toString();
210             
211             boolean found = res.indexOf("X-Test: X-Value") > 0;
212             assertEquals(true,found);
213
214         } catch (MessagingException JavaDoc e) {
215             // TODO Auto-generated catch block
216
e.printStackTrace();
217         } catch (IOException JavaDoc e) {
218             // TODO Auto-generated catch block
219
e.printStackTrace();
220         }
221     }
222
223     
224     public void testReplaceReturnPathOnBadMessage() throws Exception JavaDoc {
225         MimeMessage JavaDoc message = getMessageWithBadReturnPath();
226         message.setHeader(RFC2822Headers.RETURN_PATH, "<test@test.de>");
227         Enumeration JavaDoc e = message.getMatchingHeaderLines(new String JavaDoc[] {"Return-Path"});
228         assertEquals("Return-Path: <test@test.de>",e.nextElement());
229         assertFalse(e.hasMoreElements());
230         Enumeration JavaDoc 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 JavaDoc {
237         MimeMessage JavaDoc message = getMessageWithBadReturnPath();
238         message.addHeader(RFC2822Headers.RETURN_PATH, "<test@test.de>");
239         // test that we have now 2 return-paths
240
Enumeration JavaDoc e = message.getMatchingHeaderLines(new String JavaDoc[] {"Return-Path"});
241         assertEquals("Return-Path: <test@test.de>",e.nextElement());
242         assertEquals("Return-Path: <mybadreturn@example.com>",e.nextElement());
243         // test that return-path is the first line
244
Enumeration JavaDoc h = message.getAllHeaderLines();
245         assertEquals("Return-Path: <test@test.de>",h.nextElement());
246         ContainerUtil.dispose(message);
247     }
248
249 }
250
Popular Tags