1 17 18 package org.apache.james.imapserver; 19 20 import org.apache.james.imapserver.store.ImapMailbox; 21 import org.apache.james.imapserver.store.ImapStore; 22 import org.apache.james.imapserver.store.InMemoryStore; 23 import org.apache.james.imapserver.store.MailboxException; 24 25 import junit.framework.TestCase; 26 27 import java.util.Collection ; 28 29 39 public class ImapStoreTest extends TestCase 40 implements ImapConstants 41 { 42 private ImapStore imapStore; 43 44 public ImapStoreTest( String s ) 45 { 46 super( s ); 47 } 48 49 protected void setUp() throws Exception 50 { 51 super.setUp(); 52 imapStore = getStoreImplementation(); 53 } 54 55 protected ImapStore getStoreImplementation() 56 { 57 return new InMemoryStore(); 58 } 59 60 63 public void testCreate() throws Exception 64 { 65 ImapMailbox root = imapStore.getMailbox( ImapConstants.USER_NAMESPACE ); 67 68 ImapMailbox test = create( root, "test" ); 70 assertMailbox( "test", true ); 71 72 ImapMailbox noSelect = imapStore.createMailbox( root, "noSelect", false ); 74 assertMailbox( "noSelect", false ); 75 76 create( test, "child" ); 78 create( noSelect, "child" ); 79 80 assertMailbox( "test", true ); 81 assertMailbox( "test.child", true ); 82 assertMailbox( "noSelect", false ); 83 assertMailbox( "noSelect.child", true ); 84 85 try { 86 imapStore.createMailbox( root, "bad.name", true ); 87 fail( "Shouldn't create mailboxes with compound names." ); 88 } 89 catch ( MailboxException e ) {} 90 } 91 92 95 public void testDelete() throws Exception 96 { 97 create("test" ); 99 assertMailbox( "test", true ); 100 101 delete( "test" ); 102 assertNoMailbox( "test"); 103 104 ImapMailbox one = create( "one" ); 106 create( one, "two" ); 107 assertMailbox( "one", true ); 108 assertMailbox( "one.two", true ); 109 110 try { 111 delete( "one"); 112 fail( "Delete of mailbox with children should fail." ); 113 } 114 catch ( MailboxException e ) { 115 } 116 assertMailbox( "one", true ); 117 assertMailbox( "one.two", true ); 118 119 delete( "one.two"); 121 delete( "one"); 122 assertNoMailbox( "one" ); 123 assertNoMailbox( "one.two" ); 124 } 125 126 129 public void testListMailboxes() throws Exception 130 { 131 Collection coll; 132 coll = list("*"); 133 assertTrue( coll.isEmpty() ); 134 coll = list("%"); 135 assertTrue( coll.isEmpty() ); 136 137 ImapMailbox test = create( "test" ); 138 ImapMailbox testOne = create( test, "one" ); 139 ImapMailbox testTwo = create( test, "two" ); 140 ImapMailbox testTwoAaa = create( testTwo, "aaa" ); 141 ImapMailbox different = create( "different" ); 142 ImapMailbox differentOne = create( different, "one" ); 143 ImapMailbox differentTwo = create( different, "two" ); 144 145 coll = list("*"); 146 assertContents( coll, new ImapMailbox[]{test, testOne, testTwo, testTwoAaa, different, differentOne, differentTwo}); 147 148 coll = list("%"); 149 assertContents( coll, new ImapMailbox[]{test, different}); 150 151 coll = list("te*"); 152 assertContents( coll, new ImapMailbox[]{test, testOne, testTwo, testTwoAaa}); 153 154 coll = list("te%"); 155 assertContents( coll, new ImapMailbox[]{test}); 156 157 coll = list("test*"); 158 assertContents( coll, new ImapMailbox[]{test, testOne, testTwo, testTwoAaa}); 159 160 coll = list("test%"); 162 assertContents( coll, new ImapMailbox[]{test}); 163 164 coll = list("test.*"); 165 assertContents( coll, new ImapMailbox[]{testOne, testTwo, testTwoAaa}); 166 167 coll = list( "test.%" ); 168 assertContents( coll, new ImapMailbox[]{testOne, testTwo}); 169 170 } 171 172 176 private void assertContents( Collection coll, ImapMailbox[] imapMailboxes ) 177 throws Exception 178 { 179 assertEquals( coll.size(), imapMailboxes.length ); 180 for ( int i = 0; i < imapMailboxes.length; i++ ) 181 { 182 assertTrue( coll.contains( imapMailboxes[i] ) ); 183 } 184 } 185 186 187 191 private void assertMailbox( String name, boolean selectable ) 192 { 193 ImapMailbox mailbox = imapStore.getMailbox( prefixUserNamespace( name ) ); 194 assertNotNull( "Mailbox <" + name + "> expected to exist in store.", 195 mailbox ); 196 if ( selectable ) 197 { 198 assertTrue( "Mailbox <" + name + "> not selectable.", 199 mailbox.isSelectable() ); 200 } 201 else 202 { 203 assertTrue( "Mailbox <" + name + "> should not be selectable.", 204 ! mailbox.isSelectable() ); 205 } 206 } 207 208 211 private void assertNoMailbox( String name ) 212 { 213 ImapMailbox mailbox = imapStore.getMailbox( prefixUserNamespace( name )); 214 assertNull( "Mailbox <" + name + "> should not exist.", 215 mailbox ); 216 } 217 218 221 private ImapMailbox create( String name ) throws Exception 222 { 223 ImapMailbox root = imapStore.getMailbox( USER_NAMESPACE ); 224 return create( root, name ); 225 } 226 227 230 private ImapMailbox create( ImapMailbox parent, String name ) 231 throws MailboxException 232 { 233 return imapStore.createMailbox( parent, name, true ); 234 } 235 236 239 private void delete( String name ) throws MailboxException 240 { 241 ImapMailbox mailbox = imapStore.getMailbox( prefixUserNamespace( name ) ); 242 imapStore.deleteMailbox( mailbox ); 243 } 244 245 248 private Collection list( String pattern ) throws MailboxException 249 { 250 return imapStore.listMailboxes( prefixUserNamespace( pattern ) ); 251 } 252 253 256 private String prefixUserNamespace( String name ) 257 { 258 return USER_NAMESPACE + HIERARCHY_DELIMITER + name; 259 } 260 } 261 | Popular Tags |