001: package org.columba.mail.folder.imap;
002:
003: import java.io.IOException;
004: import java.util.Arrays;
005: import java.util.Collections;
006: import java.util.List;
007:
008: import junit.framework.TestCase;
009:
010: import org.columba.core.command.CommandCancelledException;
011: import org.columba.mail.imap.TestHeaderList;
012: import org.columba.mail.imap.TestServer;
013: import org.columba.mail.message.ColumbaHeader;
014: import org.columba.mail.message.IPersistantHeaderList;
015: import org.columba.ristretto.imap.IMAPException;
016: import org.columba.ristretto.imap.MailboxStatus;
017:
018: public class IMAPSyncTest extends TestCase {
019:
020: public void testSimpleNewMails() throws IOException,
021: CommandCancelledException, IMAPException, Exception {
022: IPersistantHeaderList testList = new TestHeaderList();
023: MailboxStatus status = new MailboxStatus();
024: status.setMessages(100);
025: status.setUidNext(101);
026: TestServer testServer = new TestServer(status);
027:
028: IMAPFolder folder = new IMAPFolder(testList, testServer);
029:
030: // Add some messages
031: for (int i = 1; i <= 100; i++) {
032: ColumbaHeader header = new ColumbaHeader();
033: header.set("columba.uid", i);
034:
035: testList.add(header, i);
036: testServer.addHeader(header, i);
037: }
038:
039: // Check for the new messages
040: for (int i = 1; i <= 10; i++) {
041: ColumbaHeader header = new ColumbaHeader();
042: header.set("columba.uid", i + 100);
043: testServer.addHeader(header, i + 100);
044: }
045: status.setMessages(110);
046: status.setUidNext(111);
047:
048: assertEquals(100, testList.count());
049: folder.synchronizeHeaderlist();
050:
051: // Check for success
052: assertEquals(110, testList.count());
053: List keys = Arrays.asList(testList.getUids());
054: Collections.sort(keys);
055: int testUid = 1;
056: for (Object uid : keys) {
057: assertEquals(testUid++, uid);
058: }
059:
060: assertEquals(111, testUid);
061: }
062:
063: public void testSimpleRemovedMails() throws IOException,
064: CommandCancelledException, IMAPException, Exception {
065: IPersistantHeaderList testList = new TestHeaderList();
066: MailboxStatus status = new MailboxStatus();
067: status.setMessages(100);
068: status.setUidNext(101);
069: TestServer testServer = new TestServer(status);
070:
071: IMAPFolder folder = new IMAPFolder(testList, testServer);
072:
073: // Add some messages
074: for (int i = 1; i <= 100; i++) {
075: ColumbaHeader header = new ColumbaHeader();
076: header.set("columba.uid", i);
077:
078: testList.add(header, i);
079: testServer.addHeader(header, i);
080: }
081:
082: // Check for the new messages
083: for (int i = 1; i <= 10; i++) {
084: testServer.removeHeader(100 - 3 * i);
085: }
086: status.setMessages(90);
087:
088: assertEquals(100, testList.count());
089: folder.synchronizeHeaderlist();
090:
091: // Check for success
092: assertEquals(90, testList.count());
093: List keys = Arrays.asList(testList.getUids());
094: for (Object uid : keys) {
095: if (testServer.getHeaderList().exists(uid) == false) {
096: System.out
097: .println("UID is removed but not from headerlist: "
098: + uid);
099: assertTrue(false);
100: }
101: }
102: }
103:
104: public void testNewAndRemovedMails() throws IOException,
105: CommandCancelledException, IMAPException, Exception {
106: IPersistantHeaderList testList = new TestHeaderList();
107: MailboxStatus status = new MailboxStatus();
108: status.setMessages(100);
109: status.setUidNext(101);
110: TestServer testServer = new TestServer(status);
111:
112: IMAPFolder folder = new IMAPFolder(testList, testServer);
113:
114: // Add some messages
115: for (int i = 1; i <= 100; i++) {
116: ColumbaHeader header = new ColumbaHeader();
117: header.set("columba.uid", i);
118:
119: testList.add(header, i);
120: testServer.addHeader(header, i);
121:
122: }
123:
124: // Add some new messages
125: // and remove the same number of old messages
126: for (int i = 1; i <= 10; i++) {
127: ColumbaHeader header = new ColumbaHeader();
128: header.set("columba.uid", i + 100);
129: testServer.addHeader(header, i + 100);
130: testServer.removeHeader(100 - 3 * i);
131:
132: }
133: status.setMessages(100);
134: status.setUidNext(111);
135:
136: assertEquals(100, testList.count());
137: folder.synchronizeHeaderlist();
138:
139: // Check for success
140: assertEquals(100, testList.count());
141: List keys = Arrays.asList(testList.getUids());
142: for (Object uid : keys) {
143: if (testServer.getHeaderList().exists(uid) == false) {
144: System.out
145: .println("UID is removed but not from headerlist: "
146: + uid);
147: assertTrue(false);
148: }
149: }
150: }
151:
152: }
|