1 19 20 package org.apache.james.mailboxmanager; 21 22 import java.util.Date ; 23 24 import javax.mail.Flags ; 25 import javax.mail.MessagingException ; 26 import javax.mail.internet.MimeMessage ; 27 28 import junit.framework.TestCase; 29 30 import org.apache.james.mailboxmanager.impl.GeneralMessageSetImpl; 31 import org.apache.james.mailboxmanager.impl.MailboxListenerCollector; 32 import org.apache.james.mailboxmanager.mailbox.ImapMailboxSession; 33 import org.apache.james.mailboxmanager.manager.MailboxManager; 34 35 public abstract class AbstractImapMailboxSelfTestCase extends TestCase { 36 37 protected MailboxManager mailboxManager; 38 39 protected ImapMailboxSession mailbox; 40 41 protected MailboxListenerCollector collector; 42 43 public static final String INBOX="#users.tuser.INBOX"; 44 45 public void setUp() throws Exception { 46 super.setUp(); 47 mailboxManager.createMailbox(INBOX); 48 mailbox=mailboxManager.getImapMailboxSession(INBOX); 49 collector=new MailboxListenerCollector(); 50 mailbox.addListener(collector, 0); 51 assertNotNull(mailbox); 52 } 53 54 public void testGetFirstUnseen() throws MailboxManagerException, MessagingException { 55 assertNotNull(mailbox); 56 for (int i = 0; i < 5; i++) { 57 MimeMessage mm=TestUtil.createMessage(); 58 mm.setFlags(new Flags (Flags.Flag.SEEN), true); 59 MessageResult mr=mailbox.appendMessage(mm, new Date (), MessageResult.UID); 60 assertEquals(i+1, mr.getUid()); 61 } 62 for (int i = 0; i < 3; i++) { 63 MessageResult mr=mailbox.appendMessage(TestUtil.createMessage(), new Date (), MessageResult.UID); 64 assertEquals(i+6, mr.getUid()); 65 } 66 for (int i = 0; i < 3; i++) { 67 MimeMessage mm=TestUtil.createMessage(); 68 mm.setFlags(new Flags (Flags.Flag.SEEN), true); 69 MessageResult mr=mailbox.appendMessage(mm, new Date (), MessageResult.UID); 70 assertEquals(i+9, mr.getUid()); 71 } 72 MessageResult mr; 73 mr=mailbox.getFirstUnseen(MessageResult.UID | MessageResult.MSN); 74 assertNotNull(mr); 75 assertEquals(6, mr.getUid()); 76 assertEquals(6, mr.getMsn()); 77 mailbox.setFlags(new Flags (Flags.Flag.DELETED), true, false, GeneralMessageSetImpl.uidRange(1,3), null); 78 mailbox.expunge(GeneralMessageSetImpl.all(), 0); 79 mailbox.getExpungedEvents(true); 80 mr=mailbox.getFirstUnseen(MessageResult.UID | MessageResult.MSN); 81 assertNotNull(mr); 82 assertEquals(6, mr.getUid()); 83 assertEquals(3, mr.getMsn()); 84 } 85 86 public void testGetExpungedEvents() throws MessagingException , MailboxManagerException { 87 for (int i = 0; i < 5; i++) { 88 MessageResult mr=mailbox.appendMessage(TestUtil.createMessage(), new Date (), MessageResult.UID | MessageResult.MSN); 89 assertEquals(i+1, mr.getUid()); 90 assertEquals(i+1, mr.getMsn()); 91 } 92 mailbox.setFlags(new Flags (Flags.Flag.DELETED), true, false, GeneralMessageSetImpl.uidRange(2, 4), null); 93 final MessageResult[] expungeResult1=mailbox.expunge(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN); 94 checkMessageResults(new long[] {2,3,4},new int[] {2,3,4},expungeResult1); 95 96 final MessageResult[] getResult1 = mailbox.getMessages(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN); 97 checkMessageResults(new long[] {1,5},new int[] {1,5},getResult1); 98 99 100 final MessageResult[] expungeEventResult1 = mailbox.getExpungedEvents(false); 101 checkMessageResults(new long[] {2,3,4},new int[] {2,3,4},expungeEventResult1); 102 final MessageResult[] expungeEventResult2 = mailbox.getExpungedEvents(true); 103 checkMessageResults(new long[] {2,3,4},new int[] {2,2,2},expungeEventResult2); 104 105 106 final MessageResult[] getResult2 = mailbox.getMessages(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN); 107 checkMessageResults(new long[] {1,5},new int[] {1,2},getResult2); 108 109 for (int i = 0; i < 5; i++) { 110 MessageResult mr=mailbox.appendMessage(TestUtil.createMessage(), new Date (), MessageResult.UID | MessageResult.MSN); 111 assertEquals(6+i, mr.getUid()); 112 assertEquals(3+i, mr.getMsn()); 113 } 114 115 final MessageResult[] getResult3 = mailbox.getMessages(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN); 116 checkMessageResults(new long[] {1,5,6,7,8,9,10},new int[] {1,2,3,4,5,6,7},getResult3); 117 118 mailbox.setFlags(new Flags (Flags.Flag.DELETED), true, false, GeneralMessageSetImpl.msnRange(2,4), null); 119 mailbox.setFlags(new Flags (Flags.Flag.DELETED), true, false, GeneralMessageSetImpl.oneMsn(6), null); 120 121 final MessageResult[] expungeResult2=mailbox.expunge(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN); 122 checkMessageResults(new long[] {5,6,7,9},new int[] {2,3,4,6},expungeResult2); 123 124 final MessageResult[] getResult4 = mailbox.getMessages(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN); 125 checkMessageResults(new long[] {1,8,10},new int[] {1,5,7},getResult4); 126 127 final MessageResult[] expungeEventResult3 = mailbox.getExpungedEvents(false); 128 checkMessageResults(new long[] {5,6,7,9},new int[] {2,3,4,6},expungeEventResult3); 129 final MessageResult[] expungeEventResult4 = mailbox.getExpungedEvents(true); 130 checkMessageResults(new long[] {5,6,7,9},new int[] {2,2,2,3},expungeEventResult4); 131 132 final MessageResult[] getResult5 = mailbox.getMessages(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN); 133 checkMessageResults(new long[] {1,8,10},new int[] {1,2,3},getResult5); 134 } 135 136 public void testGetUidNext() throws MessagingException { 137 assertEquals(1, mailbox.getUidNext()); 138 MessageResult mr=mailbox.appendMessage(TestUtil.createMessage(), new Date (), MessageResult.UID | MessageResult.MSN); 139 assertEquals(1,mr.getUid()); 140 assertEquals(2, mailbox.getUidNext()); 141 } 142 143 public void testAddedEvents() throws MailboxManagerException, MessagingException { 144 assertEquals(0,mailbox.getFlagEvents(false).length); 145 MessageResult mr=mailbox.appendMessage(TestUtil.createMessage(), new Date (), MessageResult.UID | MessageResult.MSN); 146 assertEquals(0,mailbox.getFlagEvents(false).length); 147 assertEquals(0,mailbox.getFlagEvents(true).length); 148 } 149 150 protected void assertEventCount(MailboxListenerCollector collector,long[] added, long[] flags, long[] expunged) { 151 assertEquals("added size", added.length, collector.getAddedList(true).size()); 152 assertEquals("flags size", added.length, collector.getFlaggedList(true).size()); 153 assertEquals("deleted size", added.length, collector.getExpungedList(true).size()); 154 155 } 156 157 protected void checkMessageResults(long[] uids,int[] msns,MessageResult[] messageResults) { 158 assertEquals(uids.length, msns.length); 159 assertEquals(uids.length, messageResults.length); 160 for (int i = 0; i < messageResults.length; i++) { 161 assertEquals("Uid at pos "+i,uids[i], messageResults[i].getUid()); 162 assertEquals("Msn at pos "+i,msns[i], messageResults[i].getMsn()); 163 } 164 } 165 166 } 167 | Popular Tags |