Source Code Cross Referenced for MimeMessageCopyOnWriteProxyTest.java in  » Net » james-2.3.1 » org » apache » james » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » james 2.3.1 » org.apache.james.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /****************************************************************
002:         * Licensed to the Apache Software Foundation (ASF) under one   *
003:         * or more contributor license agreements.  See the NOTICE file *
004:         * distributed with this work for additional information        *
005:         * regarding copyright ownership.  The ASF licenses this file   *
006:         * to you under the Apache License, Version 2.0 (the            *
007:         * "License"); you may not use this file except in compliance   *
008:         * with the License.  You may obtain a copy of the License at   *
009:         *                                                              *
010:         *   http://www.apache.org/licenses/LICENSE-2.0                 *
011:         *                                                              *
012:         * Unless required by applicable law or agreed to in writing,   *
013:         * software distributed under the License is distributed on an  *
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015:         * KIND, either express or implied.  See the License for the    *
016:         * specific language governing permissions and limitations      *
017:         * under the License.                                           *
018:         ****************************************************************/package org.apache.james.core;
019:
020:        import org.apache.avalon.framework.container.ContainerUtil;
021:        import org.apache.mailet.Mail;
022:        import org.apache.mailet.MailAddress;
023:
024:        import javax.mail.MessagingException;
025:        import javax.mail.Session;
026:        import javax.mail.internet.MimeMessage;
027:        import javax.mail.util.SharedByteArrayInputStream;
028:
029:        import java.util.ArrayList;
030:        import java.util.Properties;
031:
032:        public class MimeMessageCopyOnWriteProxyTest extends
033:                MimeMessageFromStreamTest {
034:
035:            String content = "Subject: foo\r\nContent-Transfer-Encoding2: plain";
036:            String sep = "\r\n\r\n";
037:            String body = "bar\r\n.\r\n";
038:
039:            protected MimeMessage getMessageFromSources(String sources)
040:                    throws Exception {
041:                MimeMessageInputStreamSource mmis = null;
042:                try {
043:                    mmis = new MimeMessageInputStreamSource("test",
044:                            new SharedByteArrayInputStream(sources.getBytes()));
045:                } catch (MessagingException e) {
046:                }
047:                return new MimeMessageCopyOnWriteProxy(mmis);
048:                //        return new MimeMessage(Session.getDefaultInstance(new Properties()),new ByteArrayInputStream(sources.getBytes()));
049:            }
050:
051:            public void testMessageCloning1() throws Exception {
052:                ArrayList r = new ArrayList();
053:                r.add(new MailAddress("recipient@test.com"));
054:                MimeMessageCopyOnWriteProxy messageFromSources = (MimeMessageCopyOnWriteProxy) getMessageFromSources(content
055:                        + sep + body);
056:                MailImpl mail = new MailImpl("test", new MailAddress(
057:                        "test@test.com"), r, messageFromSources);
058:                MailImpl m2 = (MailImpl) mail.duplicate();
059:                System.out.println("mail: " + getReferences(mail.getMessage())
060:                        + " m2: " + getReferences(m2.getMessage()));
061:                assertNotSame(m2, mail);
062:                assertNotSame(m2.getMessage(), mail.getMessage());
063:                // test that the wrapped message is the same
064:                assertTrue(isSameMimeMessage(m2.getMessage(), mail.getMessage()));
065:                // test it is the same after read only operations!
066:                mail.getMessage().getSubject();
067:                assertTrue(isSameMimeMessage(m2.getMessage(), mail.getMessage()));
068:                mail.getMessage().setText("new body");
069:                mail.getMessage().saveChanges();
070:                // test it is different after a write operation!
071:                mail.getMessage().setSubject("new Subject");
072:                assertTrue(!isSameMimeMessage(m2.getMessage(), mail
073:                        .getMessage()));
074:                ContainerUtil.dispose(mail);
075:                ContainerUtil.dispose(m2);
076:                ContainerUtil.dispose(messageFromSources);
077:            }
078:
079:            public void testMessageCloning2() throws Exception {
080:                ArrayList r = new ArrayList();
081:                r.add(new MailAddress("recipient@test.com"));
082:                MimeMessageCopyOnWriteProxy messageFromSources = (MimeMessageCopyOnWriteProxy) getMessageFromSources(content
083:                        + sep + body);
084:                MailImpl mail = new MailImpl("test", new MailAddress(
085:                        "test@test.com"), r, messageFromSources);
086:                MailImpl m2 = (MailImpl) mail.duplicate();
087:                System.out.println("mail: " + getReferences(mail.getMessage())
088:                        + " m2: " + getReferences(m2.getMessage()));
089:                assertNotSame(m2, mail);
090:                assertNotSame(m2.getMessage(), mail.getMessage());
091:                // test that the wrapped message is the same
092:                assertTrue(isSameMimeMessage(m2.getMessage(), mail.getMessage()));
093:                // test it is the same after real only operations!
094:                m2.getMessage().getSubject();
095:                assertTrue(isSameMimeMessage(m2.getMessage(), mail.getMessage()));
096:                m2.getMessage().setText("new body");
097:                m2.getMessage().saveChanges();
098:                // test it is different after a write operation!
099:                m2.getMessage().setSubject("new Subject");
100:                assertTrue(!isSameMimeMessage(m2.getMessage(), mail
101:                        .getMessage()));
102:                // check that the subjects are correct on both mails!
103:                assertEquals(m2.getMessage().getSubject(), "new Subject");
104:                assertEquals(mail.getMessage().getSubject(), "foo");
105:                // cloning again the messages
106:                Mail m2clone = m2.duplicate();
107:                assertTrue(isSameMimeMessage(m2clone.getMessage(), m2
108:                        .getMessage()));
109:                MimeMessage mm = getWrappedMessage(m2.getMessage());
110:                assertNotSame(m2.getMessage(), m2clone.getMessage());
111:                // test that m2clone has a valid wrapped message
112:                MimeMessage mm3 = getWrappedMessage(m2clone.getMessage());
113:                assertNotNull(mm3);
114:                // dispose m2 and check that the clone has still a valid message and it is the same!
115:                ((MailImpl) m2).dispose();
116:                assertEquals(mm3, getWrappedMessage(m2clone.getMessage()));
117:                // change the message that should be not referenced by m2 that has
118:                // been disposed, so it should not clone it!
119:                m2clone.getMessage().setSubject("new Subject 2");
120:                m2clone.getMessage().setText("new Body 3");
121:                assertTrue(isSameMimeMessage(m2clone.getMessage(), mm));
122:                ContainerUtil.dispose(mail);
123:                ContainerUtil.dispose(messageFromSources);
124:            }
125:
126:            /**
127:             * If I create a new MimeMessageCopyOnWriteProxy from another MimeMessageCopyOnWriteProxy,
128:             * I remove references to the first and I change the second, then it should not clone
129:             */
130:            public void testMessageAvoidCloning() throws Exception {
131:                ArrayList r = new ArrayList();
132:                r.add(new MailAddress("recipient@test.com"));
133:                MimeMessageCopyOnWriteProxy messageFromSources = (MimeMessageCopyOnWriteProxy) getMessageFromSources(content
134:                        + sep + body);
135:                MailImpl mail = new MailImpl("test", new MailAddress(
136:                        "test@test.com"), r, messageFromSources);
137:                // cloning the message
138:                Mail mailClone = mail.duplicate();
139:                assertTrue(isSameMimeMessage(mailClone.getMessage(), mail
140:                        .getMessage()));
141:                MimeMessage mm = getWrappedMessage(mail.getMessage());
142:                assertNotSame(mail.getMessage(), mailClone.getMessage());
143:                // dispose mail and check that the clone has still a valid message and it is the same!
144:                ((MailImpl) mail).dispose();
145:                ContainerUtil.dispose(messageFromSources);
146:                // need to add a gc and a wait, because the original mimemessage should be finalized before the test.
147:                System.gc();
148:                Thread.sleep(1000);
149:                // dumb test
150:                assertTrue(isSameMimeMessage(mailClone.getMessage(), mailClone
151:                        .getMessage()));
152:                // change the message that should be not referenced by mail that has
153:                // been disposed, so it should not clone it!
154:                mailClone.getMessage().setSubject("new Subject 2");
155:                mailClone.getMessage().setText("new Body 3");
156:                assertTrue(isSameMimeMessage(mailClone.getMessage(), mm));
157:                ContainerUtil.dispose(mailClone);
158:                ContainerUtil.dispose(mm);
159:            }
160:
161:            /**
162:             * If I create a new MimeMessageCopyOnWriteProxy from a MimeMessage and I change the new 
163:             * message, the original should be unaltered and the proxy should clone the message.
164:             */
165:            public void testMessageCloning3() throws Exception {
166:                ArrayList r = new ArrayList();
167:                r.add(new MailAddress("recipient@test.com"));
168:                MimeMessage m = new MimeMessage(Session
169:                        .getDefaultInstance(new Properties(null)));
170:                m.setText("CIPS");
171:                MailImpl mail = new MailImpl("test", new MailAddress(
172:                        "test@test.com"), r, m);
173:                assertTrue(isSameMimeMessage(m, mail.getMessage()));
174:                // change the message that should be not referenced by mail that has
175:                // been disposed, so it should not clone it!
176:                System.gc();
177:                Thread.sleep(100);
178:                mail.getMessage().setSubject("new Subject 2");
179:                mail.getMessage().setText("new Body 3");
180:                System.gc();
181:                Thread.sleep(100);
182:                assertFalse(isSameMimeMessage(m, mail.getMessage()));
183:                ContainerUtil.dispose(mail);
184:                ContainerUtil.dispose(m);
185:            }
186:
187:            public void testMessageDisposing() throws Exception {
188:                ArrayList r = new ArrayList();
189:                r.add(new MailAddress("recipient@test.com"));
190:                MimeMessageCopyOnWriteProxy messageFromSources = (MimeMessageCopyOnWriteProxy) getMessageFromSources(content
191:                        + sep + body);
192:                MailImpl mail = new MailImpl("test", new MailAddress(
193:                        "test@test.com"), r, messageFromSources);
194:                // cloning the message
195:                MailImpl mailClone = (MailImpl) mail.duplicate();
196:                mail.dispose();
197:
198:                assertNotNull(getWrappedMessage(mailClone.getMessage()));
199:                assertNull(mail.getMessage());
200:
201:                mailClone.dispose();
202:
203:                assertNull(mailClone.getMessage());
204:                assertNull(mail.getMessage());
205:                ContainerUtil.dispose(mail);
206:                ContainerUtil.dispose(messageFromSources);
207:            }
208:
209:            public void testNPE1() throws MessagingException,
210:                    InterruptedException {
211:                ArrayList recipients = new ArrayList();
212:                recipients.add(new MailAddress("recipient@test.com"));
213:                MimeMessageCopyOnWriteProxy mw = new MimeMessageCopyOnWriteProxy(
214:                        new MimeMessageInputStreamSource(
215:                                "test",
216:                                new SharedByteArrayInputStream(
217:                                        ("Return-path: return@test.com\r\n"
218:                                                + "Content-Transfer-Encoding: plain\r\n"
219:                                                + "Subject: test\r\n\r\n"
220:                                                + "Body Text\r\n").getBytes())));
221:
222:                MimeMessageCopyOnWriteProxy mw2 = new MimeMessageCopyOnWriteProxy(
223:                        mw);
224:                ContainerUtil.dispose(mw2);
225:                mw2 = null;
226:                System.gc();
227:                Thread.sleep(1000);
228:                // the NPE was inside this call
229:                mw.getMessageSize();
230:                ContainerUtil.dispose(mw);
231:            }
232:
233:            /**
234:             * This test throw a NullPointerException when the original message was created by
235:             * a MimeMessageInputStreamSource.
236:             */
237:            public void testMessageCloningViaCoW3() throws Exception {
238:                MimeMessage mmorig = getSimpleMessage();
239:
240:                MimeMessage mm = new MimeMessageCopyOnWriteProxy(mmorig);
241:
242:                ContainerUtil.dispose(mmorig);
243:                mmorig = null;
244:                System.gc();
245:                Thread.sleep(200);
246:
247:                try {
248:                    mm.writeTo(System.out);
249:                } catch (Exception e) {
250:                    e.printStackTrace();
251:                    fail("Exception while writing the message to output");
252:                }
253:
254:                ContainerUtil.dispose(mmorig);
255:            }
256:
257:            private static String getReferences(MimeMessage m) {
258:                StringBuffer ref = new StringBuffer("/");
259:                while (m instanceof  MimeMessageCopyOnWriteProxy) {
260:                    ref.append(((MimeMessageCopyOnWriteProxy) m).refCount
261:                            .getReferenceCount()
262:                            + "/");
263:                    m = ((MimeMessageCopyOnWriteProxy) m).getWrappedMessage();
264:                }
265:                if (m instanceof  MimeMessageWrapper) {
266:                    ref.append("W");
267:                } else if (m instanceof  MimeMessage) {
268:                    ref.append("M");
269:                } else {
270:                    ref.append(m.getClass());
271:                }
272:                return ref.toString();
273:            }
274:
275:            private static MimeMessage getWrappedMessage(MimeMessage m) {
276:                while (m instanceof  MimeMessageCopyOnWriteProxy) {
277:                    m = ((MimeMessageCopyOnWriteProxy) m).getWrappedMessage();
278:                }
279:                return m;
280:            }
281:
282:            private static boolean isSameMimeMessage(MimeMessage first,
283:                    MimeMessage second) {
284:                return getWrappedMessage(first) == getWrappedMessage(second);
285:
286:            }
287:
288:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.