git clone of logicmail with some fixes/features added
1/*-
2 * Copyright (c) 2009, Derek Konigsberg
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the project nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31package org.logicprobe.LogicMail.model;
32
33import java.util.Calendar;
34
35import org.logicprobe.LogicMail.message.FolderMessage;
36import org.logicprobe.LogicMail.message.MimeMessageContent;
37import org.logicprobe.LogicMail.message.MessageEnvelope;
38import org.logicprobe.LogicMail.message.MimeMessagePart;
39import org.logicprobe.LogicMail.message.TextContent;
40import org.logicprobe.LogicMail.message.TextPart;
41import org.logicprobe.LogicMail.util.StringParser;
42
43import j2meunit.framework.Test;
44import j2meunit.framework.TestCase;
45import j2meunit.framework.TestMethod;
46import j2meunit.framework.TestSuite;
47
48public class MessageNodeTest extends TestCase {
49 private MessageNode instance;
50
51 public MessageNodeTest() {
52 }
53
54 public MessageNodeTest(String testName, TestMethod testMethod) {
55 super(testName, testMethod);
56 }
57
58 public void setUp() {
59 }
60
61 public void tearDown() {
62 instance = null;
63 }
64
65 public void testToReplyMessage() {
66 String sampleText =
67 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\r\n" +
68 "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim\r\n" +
69 "ad minim veniam, quis nostrud exercitation ullamco laboris\r\n" +
70 "nisi ut aliquip ex ea commodo consequat.";
71 TextPart part = new TextPart("plain", "", "", "", "", "", -1);
72 TextContent content = new TextContent(part, sampleText);
73
74 MessageEnvelope env = new MessageEnvelope();
75 env.subject = "This is the subject";
76 env.sender = new String[1];
77 env.sender[0] = "\"John Doe\" <jdoe@generic.org>";
78 env.date = Calendar.getInstance().getTime();
79
80 instance = new MessageNode(new FolderMessage(null, env, 0, 0, -1));
81 instance.setMessageStructure(part);
82 instance.putMessageContent(content);
83
84 String expectedText =
85 "\r\n" +
86 "On " + StringParser.createDateString(env.date) + ", John Doe wrote:\r\n" +
87 "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\r\n" +
88 "> eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim\r\n" +
89 "> ad minim veniam, quis nostrud exercitation ullamco laboris\r\n" +
90 "> nisi ut aliquip ex ea commodo consequat.";
91
92 MessageNode result = instance.toReplyMessage();
93
94 MimeMessagePart resultPart = result.getMessageStructure();
95 assertNotNull("Null result", resultPart);
96 assertTrue("Bad type", resultPart instanceof TextPart);
97
98 MimeMessageContent resultContent = result.getMessageContent(resultPart);
99 assertNotNull("Null result", resultContent);
100 assertTrue("Bad type", resultContent instanceof TextContent);
101 assertEquals("Content mismatch", expectedText, ((TextContent)resultContent).getText());
102 }
103
104 public void testToForwardMessage() {
105 String sampleText =
106 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\r\n" +
107 "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim\r\n" +
108 "ad minim veniam, quis nostrud exercitation ullamco laboris\r\n" +
109 "nisi ut aliquip ex ea commodo consequat.";
110 TextPart part = new TextPart("plain", "", "", "", "", "", -1);
111 TextContent content = new TextContent(part, sampleText);
112
113 MessageEnvelope env = new MessageEnvelope();
114 env.from = new String[] { "\"John Doe\" <jdoe@generic.org>" };
115 env.to = new String[] { "\"Jim Smith\" <jsmith@something.net>",
116 "\"Jane Doe\" <jane.doe@things.org>" };
117 env.date = Calendar.getInstance().getTime();
118 env.subject = "The Subject";
119
120 instance = new MessageNode(new FolderMessage(null, env, 0, 0, -1));
121 instance.setMessageStructure(part);
122 instance.putMessageContent(content);
123
124 String expectedText =
125 "\r\n" +
126 "----Original Message----\r\n"+
127 "Subject: The Subject\r\n"+
128 "Date: "+StringParser.createDateString(env.date)+"\r\n"+
129 "From: \"John Doe\" <jdoe@generic.org>\r\n"+
130 "To: \"Jim Smith\" <jsmith@something.net>, \"Jane Doe\" <jane.doe@things.org>\r\n"+
131 "\r\n" +
132 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\r\n" +
133 "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim\r\n" +
134 "ad minim veniam, quis nostrud exercitation ullamco laboris\r\n" +
135 "nisi ut aliquip ex ea commodo consequat.\r\n"+
136 "------------------------";
137
138 MessageNode result = instance.toForwardMessage();
139
140 MimeMessagePart resultPart = result.getMessageStructure();
141 assertNotNull("Null result", resultPart);
142 assertTrue("Bad type", resultPart instanceof TextPart);
143
144 MimeMessageContent resultContent = result.getMessageContent(resultPart);
145 assertNotNull("Null result", resultContent);
146 assertTrue("Bad type", resultContent instanceof TextContent);
147 assertEquals("Content mismatch", expectedText, ((TextContent)resultContent).getText());
148 }
149
150 public Test suite() {
151 TestSuite suite = new TestSuite("MessageNodeTest");
152
153 suite.addTest(new MessageNodeTest("toReplyMessage", new TestMethod()
154 { public void run(TestCase tc) {((MessageNodeTest)tc).testToReplyMessage(); } }));
155 suite.addTest(new MessageNodeTest("toForwardMessage", new TestMethod()
156 { public void run(TestCase tc) {((MessageNodeTest)tc).testToForwardMessage(); } }));
157
158 return suite;
159 }
160}