git clone of logicmail with some fixes/features added
1/*-
2 * Copyright (c) 2008, 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.Hashtable;
34
35import org.logicprobe.LogicMail.mail.FolderStatusRequest;
36import org.logicprobe.LogicMail.mail.FolderTreeItem;
37import org.logicprobe.LogicMail.mail.FolderTreeRequest;
38import org.logicprobe.LogicMail.mail.MessageToken;
39import org.logicprobe.LogicMail.mail.MockAbstractMailStore;
40import org.logicprobe.LogicMail.message.MimeMessagePart;
41
42import j2meunit.framework.Test;
43import j2meunit.framework.TestCase;
44import j2meunit.framework.TestMethod;
45import j2meunit.framework.TestSuite;
46
47public class AccountNodeTest extends TestCase {
48 private AccountNode instance = null;
49 private TestMailStoreServices mailStoreServices = null;
50 private TestMailStore mailStore = null;
51 private AccountNodeEvent accountNodeEvent = null;
52 private Hashtable mailboxStatusChangedEvents = new Hashtable();
53 private MailboxNodeListener mailboxNodeListener = new MailboxNodeListener() {
54 public void mailboxStatusChanged(MailboxNodeEvent e) {
55 mailboxStatusChangedEvents.put(e.getSource(), e);
56 }
57 };
58
59 /** Creates a new instance of AccountNodeTest */
60 public AccountNodeTest() {
61 }
62
63 public AccountNodeTest(String testName, TestMethod testMethod) {
64 super(testName, testMethod);
65 }
66
67 public void setUp() {
68 mailStore = new TestMailStore();
69 mailStore.rootFolder = new FolderTreeItem("", "", "");
70 mailStore.rootFolder.addChild(new FolderTreeItem("INBOX", "INBOX", "."));
71 mailStore.rootFolder.children()[0].addChild(new FolderTreeItem("1_One", "INBOX.1_One", "."));
72 mailStore.rootFolder.children()[0].addChild(new FolderTreeItem("2_Two", "INBOX.2_Two", "."));
73
74 mailStoreServices = new TestMailStoreServices(mailStore);
75
76 instance = new TestAccountNode(mailStoreServices);
77 instance.addAccountNodeListener(new AccountNodeListener() {
78 public void accountStatusChanged(AccountNodeEvent e) {
79 accountNodeEvent = e;
80 }
81 });
82 }
83
84 public void tearDown() {
85 instance = null;
86 mailStore = null;
87 accountNodeEvent = null;
88 }
89
90 public void testRefreshMailboxes() {
91
92 // Test simple building of the initial tree
93 instance.refreshMailboxes();
94 assertNotNull("accountNodeEvent", accountNodeEvent);
95 assertEquals(AccountNodeEvent.TYPE_MAILBOX_TREE, accountNodeEvent.getType());
96 MailboxNode rootMailbox = instance.getRootMailbox();
97 assertNotNull("rootMailbox", rootMailbox);
98
99 MailboxNode[] childMailboxes = rootMailbox.getMailboxes();
100 assertNotNull("childMailboxes", childMailboxes);
101 assertEquals(1, childMailboxes.length);
102 assertNotNull("childMailboxes[0]", childMailboxes[0]);
103 assertEquals("Inbox", childMailboxes[0].toString());
104 MailboxNode inboxNode = childMailboxes[0];
105
106 childMailboxes = childMailboxes[0].getMailboxes();
107 assertNotNull("childMailboxes2", childMailboxes);
108 assertEquals(2, childMailboxes.length);
109 assertNotNull("childMailboxes2[0]", childMailboxes[0]);
110 assertEquals("1_One", childMailboxes[0].toString());
111 MailboxNode oneNode = childMailboxes[0];
112 assertNotNull("childMailboxes2[1]", childMailboxes[1]);
113 assertEquals("2_Two", childMailboxes[1].toString());
114 MailboxNode twoNode = childMailboxes[1];
115
116 // Add another child node
117 mailStore.rootFolder.children()[0].addChild(new FolderTreeItem("3_Three", "INBOX.3_Three", "."));
118
119 // Now make sure another refresh will return the same
120 // nodes for the mailboxes that have not changed.
121 accountNodeEvent = null;
122 instance.refreshMailboxes();
123 assertNotNull("accountNodeEvent", accountNodeEvent);
124 assertEquals(AccountNodeEvent.TYPE_MAILBOX_TREE, accountNodeEvent.getType());
125 rootMailbox = instance.getRootMailbox();
126 assertNotNull("rootMailbox", rootMailbox);
127
128 childMailboxes = rootMailbox.getMailboxes();
129 assertNotNull("childMailboxes", childMailboxes);
130 assertEquals(1, childMailboxes.length);
131 assertNotNull("childMailboxes[0]", childMailboxes[0]);
132 assertEquals(inboxNode, childMailboxes[0]);
133
134 childMailboxes = childMailboxes[0].getMailboxes();
135 assertNotNull("childMailboxes2", childMailboxes);
136 assertEquals(3, childMailboxes.length);
137 assertNotNull("childMailboxes2[0]", childMailboxes[0]);
138 assertEquals(oneNode, childMailboxes[0]);
139 assertNotNull("childMailboxes2[1]", childMailboxes[1]);
140 assertEquals(twoNode, childMailboxes[1]);
141 assertNotNull("childMailboxes2[2]", childMailboxes[2]);
142 assertEquals("3_Three", childMailboxes[2].toString());
143 }
144
145 public void testRefreshMailboxStatus() {
146 // Make sure we have mailboxes to check
147 instance.refreshMailboxes();
148 assertNotNull("accountNodeEvent", accountNodeEvent);
149 assertEquals(AccountNodeEvent.TYPE_MAILBOX_TREE, accountNodeEvent.getType());
150 MailboxNode rootMailbox = instance.getRootMailbox();
151 assertNotNull("rootMailbox", rootMailbox);
152
153 // We'll assume the mailbox tree is valid, since we already
154 // have a test to verify that. So, let's register some listeners...
155 MailboxNode[] childMailboxes = rootMailbox.getMailboxes();
156 MailboxNode inboxNode = childMailboxes[0];
157 childMailboxes[0].addMailboxNodeListener(mailboxNodeListener);
158 childMailboxes = childMailboxes[0].getMailboxes();
159 childMailboxes[0].addMailboxNodeListener(mailboxNodeListener);
160 MailboxNode oneNode = childMailboxes[0];
161 childMailboxes[1].addMailboxNodeListener(mailboxNodeListener);
162 MailboxNode twoNode = childMailboxes[1];
163
164 // Now we'll create a mirror of the current FolderTreeItem
165 // structure with updated status counts
166 mailStore.statusUpdatedRootFolder = new FolderTreeItem("", "", "");
167 mailStore.statusUpdatedRootFolder.addChild(new FolderTreeItem("INBOX", "INBOX", "."));
168 mailStore.statusUpdatedRootFolder.children()[0].addChild(new FolderTreeItem("1_One", "INBOX.1_One", "."));
169 mailStore.statusUpdatedRootFolder.children()[0].addChild(new FolderTreeItem("2_Two", "INBOX.2_Two", "."));
170 mailStore.statusUpdatedRootFolder.children()[0].setUnseenCount(5);
171 mailStore.statusUpdatedRootFolder.children()[0].setMsgCount(10);
172 mailStore.statusUpdatedRootFolder.children()[0].children()[0].setUnseenCount(6);
173 mailStore.statusUpdatedRootFolder.children()[0].children()[0].setMsgCount(11);
174 mailStore.statusUpdatedRootFolder.children()[0].children()[1].setUnseenCount(7);
175 mailStore.statusUpdatedRootFolder.children()[0].children()[1].setMsgCount(12);
176
177 // Refresh status
178 instance.refreshMailboxStatus();
179
180 // Check for the necessary events and changes
181 assertTrue(mailboxStatusChangedEvents.containsKey(inboxNode));
182 assertEquals(5, inboxNode.getUnseenMessageCount());
183 //assertEquals(10, inboxNode.getMessageCount());
184
185 assertTrue(mailboxStatusChangedEvents.containsKey(oneNode));
186 assertEquals(6, oneNode.getUnseenMessageCount());
187 //assertEquals(11, oneNode.getMessageCount());
188
189 assertTrue(mailboxStatusChangedEvents.containsKey(twoNode));
190 assertEquals(7, twoNode.getUnseenMessageCount());
191 //assertEquals(12, twoNode.getMessageCount());
192
193 // Note: getMessageCount() assertions are commented out because
194 // they are now based on the actual MailboxNode contents. When
195 // this test was written, they were based on the IMAP folder
196 // status results.
197 }
198
199 public Test suite() {
200 TestSuite suite = new TestSuite("AccountNode");
201
202 suite.addTest(new AccountNodeTest("refreshMailboxes", new TestMethod()
203 { public void run(TestCase tc) {((AccountNodeTest)tc).testRefreshMailboxes(); } }));
204 suite.addTest(new AccountNodeTest("refreshMailboxStatus", new TestMethod()
205 { public void run(TestCase tc) {((AccountNodeTest)tc).testRefreshMailboxStatus(); } }));
206
207 return suite;
208 }
209
210 private class TestAccountNode extends AccountNode {
211 protected TestAccountNode(TestMailStoreServices mailStoreServices) {
212 super(mailStoreServices);
213 }
214 protected void save() { }
215 protected void load() { }
216 }
217
218 private class TestMailStoreServices extends MailStoreServices {
219 public TestMailStoreServices(TestMailStore mailStore) {
220 super(mailStore);
221 }
222
223 public void requestFolderRefresh(FolderTreeItem folderTreeItem) { }
224 public void requestMoreFolderMessages(FolderTreeItem folderTreeItem, MessageToken firstToken) { }
225 public boolean requestMessageRefresh(MessageToken messageToken, MimeMessagePart[] partsToSkip, int displayFormat) { return false; }
226 public boolean requestMessageRefreshCacheOnly(MessageToken messageToken, int displayFormat) { return false; }
227 public boolean requestEntireMessageRefresh(MessageToken messageToken) { return false; }
228 }
229
230 private class TestMailStore extends MockAbstractMailStore {
231 // This implementation only uses an auto-generated mock to reduce the
232 // amount of stub implementations that have to be constantly modified.
233 public FolderTreeItem rootFolder = null;
234 public FolderTreeItem statusUpdatedRootFolder = null;
235
236 public boolean hasFlags() {
237 return false;
238 }
239
240 public boolean hasFolders() {
241 return true;
242 }
243
244 public boolean hasMessageParts() {
245 return false;
246 }
247
248 public boolean hasAppend() {
249 return true;
250 }
251
252 public boolean hasCopy() {
253 return false;
254 }
255
256 public boolean hasUndelete() {
257 return false;
258 }
259
260 public boolean hasExpunge() {
261 return false;
262 }
263
264 public boolean isLocal() {
265 return false;
266 }
267
268 public FolderTreeRequest createFolderTreeRequest() {
269 fireFolderTreeUpdated(rootFolder);
270 return null;
271 }
272
273 public FolderStatusRequest createFolderStatusRequest(FolderTreeItem[] folders) {
274 for(int i=0; i<folders.length; i++) {
275 requestFolderStatusImpl(statusUpdatedRootFolder);
276 }
277 return null;
278 }
279
280 private void requestFolderStatusImpl(FolderTreeItem folder) {
281 fireFolderStatusChanged(folder);
282 if(folder.hasChildren()) {
283 FolderTreeItem[] children = folder.children();
284 for(int i=0; i<children.length; i++) {
285 requestFolderStatusImpl(children[i]);
286 }
287 }
288 }
289
290 public void shutdown(boolean wait) { }
291 }
292}