KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > ImapStoreTest


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

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 JavaDoc;
28
29 /**
30  * A test for implementations of the {@link org.apache.james.imapserver.store.ImapStore} interface.
31  *
32  * TODO tests to write
33  * - Rename
34  * - List (various combinations)
35  * -
36  *
37  * @version $Revision: 1.3.2.3 $
38  */

39 public class ImapStoreTest extends TestCase
40         implements ImapConstants
41 {
42     private ImapStore imapStore;
43
44     public ImapStoreTest( String JavaDoc s )
45     {
46         super( s );
47     }
48
49     protected void setUp() throws Exception JavaDoc
50     {
51         super.setUp();
52         imapStore = getStoreImplementation();
53     }
54
55     protected ImapStore getStoreImplementation()
56     {
57         return new InMemoryStore();
58     }
59
60     /**
61      * Tests creation of mailboxes in the Store.
62      */

63     public void testCreate() throws Exception JavaDoc
64     {
65         // Get the user namespace.
66
ImapMailbox root = imapStore.getMailbox( ImapConstants.USER_NAMESPACE );
67
68         // Create a single mailbox.
69
ImapMailbox test = create( root, "test" );
70         assertMailbox( "test", true );
71
72         // Create a single mailbox as noselect.
73
ImapMailbox noSelect = imapStore.createMailbox( root, "noSelect", false );
74         assertMailbox( "noSelect", false );
75
76         // Create children for these mailboxes.
77
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     /**
93      * Tests deletion of mailboxes from the store.
94      */

95     public void testDelete() throws Exception JavaDoc
96     {
97         // Simple create/delete
98
create("test" );
99         assertMailbox( "test", true );
100
101         delete( "test" );
102         assertNoMailbox( "test");
103
104         // Create a chain and attempt to delete the parent.
105
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 the child, then the parent
120
delete( "one.two");
121         delete( "one");
122         assertNoMailbox( "one" );
123         assertNoMailbox( "one.two" );
124     }
125
126     /**
127      * Tests the {@link ImapStore#listMailboxes} method.
128      */

129     public void testListMailboxes() throws Exception JavaDoc
130     {
131         Collection JavaDoc 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         // TODO - should this return children?
161
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     /**
173      * Asserts that the supplied collection contains exactly the mailboxes in the
174      * array provided.
175      */

176     private void assertContents( Collection JavaDoc coll, ImapMailbox[] imapMailboxes )
177             throws Exception JavaDoc
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     /**
188      * Checks that a mailbox with the supplied name exists, and that its
189      * NoSelect flag matches that expected.
190      */

191     private void assertMailbox( String JavaDoc 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     /**
209      * Asserts that a mailbox with the supplied name doesn't exist.
210      */

211     private void assertNoMailbox( String JavaDoc name )
212     {
213         ImapMailbox mailbox = imapStore.getMailbox( prefixUserNamespace( name ));
214         assertNull( "Mailbox <" + name + "> should not exist.",
215                     mailbox );
216     }
217
218     /**
219      * Creates a mailbox with the specified name in the root user namespace.
220      */

221     private ImapMailbox create( String JavaDoc name ) throws Exception JavaDoc
222     {
223         ImapMailbox root = imapStore.getMailbox( USER_NAMESPACE );
224         return create( root, name );
225     }
226
227     /**
228      * Creates a mailbox under the parent provided with the specified name.
229      */

230     private ImapMailbox create( ImapMailbox parent, String JavaDoc name )
231             throws MailboxException
232     {
233         return imapStore.createMailbox( parent, name, true );
234     }
235
236     /**
237      * Deletes a mailbox from the store.
238      */

239     private void delete( String JavaDoc name ) throws MailboxException
240     {
241         ImapMailbox mailbox = imapStore.getMailbox( prefixUserNamespace( name ) );
242         imapStore.deleteMailbox( mailbox );
243     }
244
245     /**
246      * Executes {@link ImapStore#listMailboxes} with the supplied pattern.
247      */

248     private Collection JavaDoc list( String JavaDoc pattern ) throws MailboxException
249     {
250         return imapStore.listMailboxes( prefixUserNamespace( pattern ) );
251     }
252
253     /**
254      * Prefixes a mailbox name with the "#mail" namespace.
255      */

256     private String JavaDoc prefixUserNamespace( String JavaDoc name )
257     {
258         return USER_NAMESPACE + HIERARCHY_DELIMITER + name;
259     }
260 }
261
Popular Tags