| 1 | /*- |
|---|
| 2 | * Copyright (c) 2007, 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 | */ |
|---|
| 31 | |
|---|
| 32 | package org.logicprobe.LogicMail.message; |
|---|
| 33 | |
|---|
| 34 | import j2meunit.framework.Test; |
|---|
| 35 | import j2meunit.framework.TestCase; |
|---|
| 36 | import j2meunit.framework.TestMethod; |
|---|
| 37 | import j2meunit.framework.TestSuite; |
|---|
| 38 | import java.util.Calendar; |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Unit test for MessageReplyConverter |
|---|
| 42 | */ |
|---|
| 43 | public class MessageTest extends TestCase { |
|---|
| 44 | private String sampleText; |
|---|
| 45 | private MessageEnvelope envelope; |
|---|
| 46 | private MimeMessagePart structure; |
|---|
| 47 | private TextContent textContent; |
|---|
| 48 | private Message message; |
|---|
| 49 | |
|---|
| 50 | public MessageTest() { |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | public MessageTest(String testName, TestMethod testMethod) { |
|---|
| 54 | super(testName, testMethod); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | public void setUp() { |
|---|
| 58 | sampleText = |
|---|
| 59 | "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\r\n" + |
|---|
| 60 | "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim\r\n" + |
|---|
| 61 | "ad minim veniam, quis nostrud exercitation ullamco laboris\r\n" + |
|---|
| 62 | "nisi ut aliquip ex ea commodo consequat."; |
|---|
| 63 | |
|---|
| 64 | structure = new TextPart("plain", "", "", "", "", "", -1); |
|---|
| 65 | textContent = new TextContent((TextPart)structure, sampleText); |
|---|
| 66 | |
|---|
| 67 | envelope = new MessageEnvelope(); |
|---|
| 68 | envelope.from = new String[] { "John Doe <jdoe@generic.org>" }; |
|---|
| 69 | envelope.sender = new String[] { "John Doe <jdoe@generic.org>" }; |
|---|
| 70 | envelope.to = new String[] { "Jim Smith <jsmith@random.net>", |
|---|
| 71 | "Jane Doe <jdoe@generic.org>"}; |
|---|
| 72 | envelope.cc = new String[] { "Jane Smith <jane.smith@random.net>" }; |
|---|
| 73 | envelope.bcc = new String[] { "Jane Smith <smith12@otherplace.com>" }; |
|---|
| 74 | |
|---|
| 75 | envelope.date = Calendar.getInstance().getTime(); |
|---|
| 76 | envelope.subject = "The subject"; |
|---|
| 77 | envelope.messageId = "1234567890"; |
|---|
| 78 | |
|---|
| 79 | message = new Message(structure); |
|---|
| 80 | message.putContent(structure, textContent); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | public void tearDown() { |
|---|
| 84 | sampleText = null; |
|---|
| 85 | envelope = null; |
|---|
| 86 | structure = null; |
|---|
| 87 | message = null; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | public void testMessage() { |
|---|
| 91 | assertEquals(structure, message.getStructure()); |
|---|
| 92 | assertEquals(textContent, message.getContent(structure)); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | // TODO: Convert these into tests for MessageNode |
|---|
| 96 | // public void testToReplyMessage() { |
|---|
| 97 | // Message replyMessage = message.toReplyMessage(); |
|---|
| 98 | // MessageEnvelope replyEnvelope = replyMessage.getEnvelope(); |
|---|
| 99 | // assertNotNull(replyEnvelope); |
|---|
| 100 | // MessagePart replyBody = replyMessage.getBody(); |
|---|
| 101 | // assertNotNull(replyBody); |
|---|
| 102 | // |
|---|
| 103 | // // Perform a simple test to ensure that the reply converter executed |
|---|
| 104 | // assertTrue(replyBody instanceof TextPart); |
|---|
| 105 | // String expectedText = |
|---|
| 106 | // "On "+StringParser.createDateString(envelope.date)+", John Doe wrote:\r\n" + |
|---|
| 107 | // "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\r\n" + |
|---|
| 108 | // "> eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim\r\n" + |
|---|
| 109 | // "> ad minim veniam, quis nostrud exercitation ullamco laboris\r\n" + |
|---|
| 110 | // "> nisi ut aliquip ex ea commodo consequat."; |
|---|
| 111 | // assertEquals(expectedText, ((TextPart)replyBody).getText()); |
|---|
| 112 | // |
|---|
| 113 | // // Check the reply header fields |
|---|
| 114 | // assertEquals(envelope.messageId, replyEnvelope.inReplyTo); |
|---|
| 115 | // assertEquals("Re: " + envelope.subject, replyEnvelope.subject); |
|---|
| 116 | // assertEquals(1, replyEnvelope.to.length); |
|---|
| 117 | // assertEquals(envelope.from[0], replyEnvelope.to[0]); |
|---|
| 118 | // assertTrue(replyEnvelope.cc == null || replyEnvelope.cc.length == 0); |
|---|
| 119 | // assertTrue(replyEnvelope.bcc == null || replyEnvelope.bcc.length == 0); |
|---|
| 120 | // |
|---|
| 121 | // // Set the Reply-To header and try again |
|---|
| 122 | // message.getEnvelope().replyTo = new String[] { "John Doe <jdoe@doemail.net>" }; |
|---|
| 123 | // replyMessage = message.toReplyMessage(); |
|---|
| 124 | // replyEnvelope = replyMessage.getEnvelope(); |
|---|
| 125 | // assertNotNull(replyEnvelope); |
|---|
| 126 | // replyBody = replyMessage.getBody(); |
|---|
| 127 | // assertNotNull(replyBody); |
|---|
| 128 | // |
|---|
| 129 | // // Check the reply header fields |
|---|
| 130 | // assertEquals(envelope.messageId, replyEnvelope.inReplyTo); |
|---|
| 131 | // assertEquals("Re: " + envelope.subject, replyEnvelope.subject); |
|---|
| 132 | // assertEquals(1, replyEnvelope.to.length); |
|---|
| 133 | // assertEquals(envelope.replyTo[0], replyEnvelope.to[0]); |
|---|
| 134 | // assertTrue(replyEnvelope.cc == null || replyEnvelope.cc.length == 0); |
|---|
| 135 | // assertTrue(replyEnvelope.bcc == null || replyEnvelope.bcc.length == 0); |
|---|
| 136 | // } |
|---|
| 137 | // |
|---|
| 138 | // public void testToReplyAllMessage() { |
|---|
| 139 | // Message replyMessage = message.toReplyAllMessage("jsmith@random.net"); |
|---|
| 140 | // MessageEnvelope replyEnvelope = replyMessage.getEnvelope(); |
|---|
| 141 | // assertNotNull(replyEnvelope); |
|---|
| 142 | // MessagePart replyBody = replyMessage.getBody(); |
|---|
| 143 | // assertNotNull(replyBody); |
|---|
| 144 | // |
|---|
| 145 | // // Perform a simple test to ensure that the reply converter executed |
|---|
| 146 | // assertTrue(replyBody instanceof TextPart); |
|---|
| 147 | // String expectedText = |
|---|
| 148 | // "On "+StringParser.createDateString(envelope.date)+", John Doe wrote:\r\n" + |
|---|
| 149 | // "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\r\n" + |
|---|
| 150 | // "> eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim\r\n" + |
|---|
| 151 | // "> ad minim veniam, quis nostrud exercitation ullamco laboris\r\n" + |
|---|
| 152 | // "> nisi ut aliquip ex ea commodo consequat."; |
|---|
| 153 | // assertEquals(expectedText, ((TextPart)replyBody).getText()); |
|---|
| 154 | // |
|---|
| 155 | // // Check the reply header fields |
|---|
| 156 | // assertEquals(envelope.messageId, replyEnvelope.inReplyTo); |
|---|
| 157 | // assertEquals("Re: " + envelope.subject, replyEnvelope.subject); |
|---|
| 158 | // |
|---|
| 159 | // assertEquals(2, replyEnvelope.to.length); |
|---|
| 160 | // assertEquals(envelope.from[0], replyEnvelope.to[0]); |
|---|
| 161 | // assertEquals(envelope.to[1], replyEnvelope.to[1]); |
|---|
| 162 | // |
|---|
| 163 | // assertEquals(1, replyEnvelope.cc.length); |
|---|
| 164 | // assertEquals(envelope.cc[0], replyEnvelope.cc[0]); |
|---|
| 165 | // |
|---|
| 166 | // assertTrue(replyEnvelope.bcc == null || replyEnvelope.bcc.length == 0); |
|---|
| 167 | // |
|---|
| 168 | // // Set the Reply-To header and try again |
|---|
| 169 | // message.getEnvelope().replyTo = new String[] { "John Doe <jdoe@doemail.net>" }; |
|---|
| 170 | // replyMessage = message.toReplyAllMessage("jsmith@random.net"); |
|---|
| 171 | // replyEnvelope = replyMessage.getEnvelope(); |
|---|
| 172 | // assertNotNull(replyEnvelope); |
|---|
| 173 | // replyBody = replyMessage.getBody(); |
|---|
| 174 | // assertNotNull(replyBody); |
|---|
| 175 | // |
|---|
| 176 | // // Check the reply header fields |
|---|
| 177 | // assertEquals(envelope.messageId, replyEnvelope.inReplyTo); |
|---|
| 178 | // assertEquals("Re: " + envelope.subject, replyEnvelope.subject); |
|---|
| 179 | // assertEquals(2, replyEnvelope.to.length); |
|---|
| 180 | // assertEquals(envelope.replyTo[0], replyEnvelope.to[0]); |
|---|
| 181 | // assertEquals(envelope.to[1], replyEnvelope.to[1]); |
|---|
| 182 | // |
|---|
| 183 | // assertTrue(replyEnvelope.bcc == null || replyEnvelope.bcc.length == 0); |
|---|
| 184 | // } |
|---|
| 185 | |
|---|
| 186 | // public void testToForwardMessage() { |
|---|
| 187 | // Message forwardMessage = message.toForwardMessage(); |
|---|
| 188 | // MessageEnvelope forwardEnvelope = forwardMessage.getEnvelope(); |
|---|
| 189 | // assertNotNull(forwardEnvelope); |
|---|
| 190 | // MessagePart forwardBody = forwardMessage.getBody(); |
|---|
| 191 | // assertNotNull(forwardBody); |
|---|
| 192 | // |
|---|
| 193 | // // Perform a simple test to ensure that the reply converter executed |
|---|
| 194 | // assertTrue(forwardBody instanceof TextPart); |
|---|
| 195 | // String expectedText = |
|---|
| 196 | // "----Original Message----\r\n"+ |
|---|
| 197 | // "Subject: The subject\r\n"+ |
|---|
| 198 | // "Date: "+StringParser.createDateString(envelope.date)+"\r\n"+ |
|---|
| 199 | // "From: John Doe <jdoe@generic.org>\r\n"+ |
|---|
| 200 | // "To: Jim Smith <jsmith@random.net>, Jane Doe <jdoe@generic.org>\r\n"+ |
|---|
| 201 | // "Cc: Jane Smith <jane.smith@random.net>\r\n"+ |
|---|
| 202 | // "\r\n" + |
|---|
| 203 | // "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\r\n" + |
|---|
| 204 | // "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim\r\n" + |
|---|
| 205 | // "ad minim veniam, quis nostrud exercitation ullamco laboris\r\n" + |
|---|
| 206 | // "nisi ut aliquip ex ea commodo consequat.\r\n"+ |
|---|
| 207 | // "------------------------"; |
|---|
| 208 | // assertEquals(expectedText, ((TextPart)forwardBody).getText()); |
|---|
| 209 | // |
|---|
| 210 | // // Check the forward headers |
|---|
| 211 | // assertEquals("Fwd: " + envelope.subject, forwardEnvelope.subject); |
|---|
| 212 | // assertNull(forwardEnvelope.sender); |
|---|
| 213 | // assertNull(forwardEnvelope.from); |
|---|
| 214 | // assertNull(forwardEnvelope.to); |
|---|
| 215 | // assertNull(forwardEnvelope.cc); |
|---|
| 216 | // assertNull(forwardEnvelope.inReplyTo); |
|---|
| 217 | // } |
|---|
| 218 | |
|---|
| 219 | public Test suite() { |
|---|
| 220 | TestSuite suite = new TestSuite("Message"); |
|---|
| 221 | |
|---|
| 222 | suite.addTest(new MessageTest("Message", new TestMethod() |
|---|
| 223 | { public void run(TestCase tc) {((MessageTest)tc).testMessage(); } })); |
|---|
| 224 | |
|---|
| 225 | // suite.addTest(new MessageTest("toReplyMessage", new TestMethod() |
|---|
| 226 | // { public void run(TestCase tc) {((MessageTest)tc).testToReplyMessage(); } })); |
|---|
| 227 | // |
|---|
| 228 | // suite.addTest(new MessageTest("toReplyAllMessage", new TestMethod() |
|---|
| 229 | // { public void run(TestCase tc) {((MessageTest)tc).testToReplyAllMessage(); } })); |
|---|
| 230 | // |
|---|
| 231 | // suite.addTest(new MessageTest("toForwardMessage", new TestMethod() |
|---|
| 232 | // { public void run(TestCase tc) {((MessageTest)tc).testToForwardMessage(); } })); |
|---|
| 233 | |
|---|
| 234 | return suite; |
|---|
| 235 | } |
|---|
| 236 | } |
|---|