git clone of logicmail with some fixes/features added
1/*-
2 * Copyright (c) 2010, 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 javax.microedition.io.Connector;
34import javax.microedition.io.file.FileConnection;
35
36import net.rim.device.api.util.Arrays;
37
38import org.logicprobe.LogicMail.message.ApplicationContent;
39import org.logicprobe.LogicMail.message.ApplicationPart;
40import org.logicprobe.LogicMail.message.MimeMessageContent;
41import org.logicprobe.LogicMail.message.TextContent;
42import org.logicprobe.LogicMail.message.TextPart;
43
44import j2meunit.framework.Test;
45import j2meunit.framework.TestCase;
46import j2meunit.framework.TestMethod;
47import j2meunit.framework.TestSuite;
48
49/**
50 * Unit tests covering both MessageContentFileWriter and
51 * MessageContentFileReader.
52 */
53public class MessageContentFileTest extends TestCase {
54 private static String FILE_URL = "file:///SDCard/BlackBerry/MessageContentFileTest.dat";
55 private FileConnection fileConnection;
56
57 public MessageContentFileTest() {
58 }
59
60 public MessageContentFileTest(String testName, TestMethod testMethod) {
61 super(testName, testMethod);
62 }
63
64 public void setUp() throws Exception {
65 fileConnection = (FileConnection)Connector.open(FILE_URL);
66 if(fileConnection.exists()) {
67 fileConnection.delete();
68 }
69 }
70
71 public void tearDown() throws Exception {
72 if(fileConnection.exists()) {
73 fileConnection.delete();
74 }
75 }
76
77 public void testOpenNewFile() throws Throwable {
78 MessageContentFileWriter writer = new MessageContentFileWriter(fileConnection, "12340000");
79 writer.open();
80 assertTrue(writer.isOpen());
81 writer.close();
82
83 MessageContentFileReader reader = new MessageContentFileReader(fileConnection, "12340000");
84 reader.open();
85 assertTrue(reader.isOpen());
86 reader.close();
87 }
88
89 public void testOpenExistingFile() throws Throwable {
90 MessageContentFileWriter writer = new MessageContentFileWriter(fileConnection, "12340000");
91 writer.open();
92 assertTrue(writer.isOpen());
93 writer.close();
94
95 writer.open();
96 assertTrue(writer.isOpen());
97 writer.close();
98
99 MessageContentFileReader reader = new MessageContentFileReader(fileConnection, "12340000");
100 reader.open();
101 assertTrue(reader.isOpen());
102 reader.close();
103 }
104
105 public void testOpenNewFileWithHeaderValues() throws Throwable {
106 MessageContentFileWriter writer = new MessageContentFileWriter(fileConnection, "12340000");
107 writer.setCustomValues(new int[] { 10, 20, 30, 40 });
108 writer.open();
109 assertTrue(writer.isOpen());
110 writer.close();
111
112 MessageContentFileReader reader = new MessageContentFileReader(fileConnection, "12340000");
113 reader.open();
114 assertTrue(reader.isOpen());
115 int[] customValues = reader.getCustomValues();
116 assertNotNull(customValues);
117 assertEquals(4, customValues.length);
118 assertEquals(10, customValues[0]);
119 assertEquals(20, customValues[1]);
120 assertEquals(30, customValues[2]);
121 assertEquals(40, customValues[3]);
122 reader.close();
123 }
124
125 public void testAddContentToNewFile() throws Throwable {
126 TextPart part = new TextPart("plain", "", "", "", "", "", -1, "1");
127 TextContent content = new TextContent(part, "Hello World");
128
129 MessageContentFileWriter writer = new MessageContentFileWriter(fileConnection, "12340000");
130 writer.open();
131 assertTrue(writer.isOpen());
132 writer.appendContent(content);
133 writer.close();
134
135 MessageContentFileReader reader = new MessageContentFileReader(fileConnection, "12340000");
136 reader.open();
137 assertTrue(reader.isOpen());
138 assertTrue(reader.hasContent(part));
139 MimeMessageContent readContent = reader.getContent(part);
140 assertNotNull(readContent);
141 assertTrue(readContent instanceof TextContent);
142 TextContent readTextContent = (TextContent)readContent;
143 assertEquals(content.getText(), readTextContent.getText());
144 assertTrue(Arrays.equals(content.getRawData(), readTextContent.getRawData()));
145 reader.close();
146 }
147
148 public void testAddMultipleContentToNewFile() throws Throwable {
149 TextPart textPart = new TextPart("plain", "", "", "", "", "", -1, "1");
150 TextContent textContent = new TextContent(textPart, "Hello World");
151 ApplicationPart appPart = new ApplicationPart("octet-stream", "", "", "", "", -1, "2");
152 ApplicationContent appContent = new ApplicationContent(appPart, new byte[] { (byte)0xDE, (byte)0xAD, (byte)0xBE, (byte)0xEF } );
153
154 MessageContentFileWriter writer = new MessageContentFileWriter(fileConnection, "12340000");
155 writer.open();
156 assertTrue(writer.isOpen());
157 writer.appendContent(textContent);
158 writer.appendContent(appContent);
159 writer.close();
160
161 MessageContentFileReader reader = new MessageContentFileReader(fileConnection, "12340000");
162 reader.open();
163 assertTrue(reader.isOpen());
164
165 assertTrue(reader.hasContent(textPart));
166 MimeMessageContent readContent = reader.getContent(textPart);
167 assertNotNull(readContent);
168 assertTrue(readContent instanceof TextContent);
169 TextContent readTextContent = (TextContent)readContent;
170 assertEquals(textContent.getText(), readTextContent.getText());
171 assertTrue(Arrays.equals(textContent.getRawData(), readTextContent.getRawData()));
172
173 assertTrue(reader.hasContent(appPart));
174 readContent = reader.getContent(appPart);
175 assertNotNull(readContent);
176 assertTrue(readContent instanceof ApplicationContent);
177 assertTrue(Arrays.equals(appContent.getRawData(), readContent.getRawData()));
178
179 reader.close();
180 }
181
182 public void testAddContentToExistingFile() throws Throwable {
183 TextPart textPart = new TextPart("plain", "", "", "", "", "", -1, "1");
184 TextContent textContent = new TextContent(textPart, "Hello World");
185 ApplicationPart appPart = new ApplicationPart("octet-stream", "", "", "", "", -1, "2");
186 ApplicationContent appContent = new ApplicationContent(appPart, new byte[] { (byte)0xDE, (byte)0xAD, (byte)0xBE, (byte)0xEF } );
187
188 // Create the file with the first content section
189 MessageContentFileWriter writer = new MessageContentFileWriter(fileConnection, "12340000");
190 writer.open();
191 assertTrue(writer.isOpen());
192 writer.appendContent(textContent);
193 writer.close();
194
195 // Open the file again, and add new content
196 writer.open();
197 assertTrue(writer.isOpen());
198 writer.appendContent(appContent);
199 writer.close();
200
201 // Open the file for reading
202 MessageContentFileReader reader = new MessageContentFileReader(fileConnection, "12340000");
203 reader.open();
204 assertTrue(reader.isOpen());
205
206 // Check to make sure that the first content section exists
207 assertTrue(reader.hasContent(textPart));
208 MimeMessageContent readContent = reader.getContent(textPart);
209 assertNotNull(readContent);
210 assertTrue(readContent instanceof TextContent);
211 assertEquals(textContent.getText(), ((TextContent)readContent).getText());
212 assertTrue(Arrays.equals(textContent.getRawData(), readContent.getRawData()));
213
214 // Check to make sure that the second content section exists
215 assertTrue(reader.hasContent(appPart));
216 readContent = reader.getContent(appPart);
217 assertNotNull(readContent);
218 assertTrue(readContent instanceof ApplicationContent);
219 assertTrue(Arrays.equals(appContent.getRawData(), readContent.getRawData()));
220
221 reader.close();
222 }
223
224 public Test suite() {
225 TestSuite suite = new TestSuite("MessageContentFileTest");
226 suite.addTest(new MessageContentFileTest("openNewFile", new TestMethod()
227 { public void run(TestCase tc) throws Throwable {((MessageContentFileTest)tc).testOpenNewFile(); } }));
228 suite.addTest(new MessageContentFileTest("openExistingFile", new TestMethod()
229 { public void run(TestCase tc) throws Throwable {((MessageContentFileTest)tc).testOpenExistingFile(); } }));
230 suite.addTest(new MessageContentFileTest("openNewFileWithHeaderValues", new TestMethod()
231 { public void run(TestCase tc) throws Throwable {((MessageContentFileTest)tc).testOpenNewFileWithHeaderValues(); } }));
232 suite.addTest(new MessageContentFileTest("addContentToNewFile", new TestMethod()
233 { public void run(TestCase tc) throws Throwable {((MessageContentFileTest)tc).testAddContentToNewFile(); } }));
234 suite.addTest(new MessageContentFileTest("addMultipleContentToNewFile", new TestMethod()
235 { public void run(TestCase tc) throws Throwable {((MessageContentFileTest)tc).testAddMultipleContentToNewFile(); } }));
236 suite.addTest(new MessageContentFileTest("addContentToExistingFile", new TestMethod()
237 { public void run(TestCase tc) throws Throwable {((MessageContentFileTest)tc).testAddContentToExistingFile(); } }));
238
239 return suite;
240 }
241}