KickJava   Java API By Example, From Geeks To Geeks.

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


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.InMemoryStore;
22 import org.apache.james.imapserver.store.MailboxException;
23 import org.apache.james.services.User;
24 import org.apache.james.userrepository.DefaultUser;
25
26 import junit.framework.TestCase;
27
28 /**
29  * A test for implementations of the {@link ImapHost} interface.
30  *
31  * TODO Tests to write:
32  * - Creating and accessing mailboxes with qualified names
33  * - Create existing mailbox
34  * - Delete Inbox
35  * - Rename
36  * - Rename Inbox
37  * - ListMailboxes
38  * - Copying messages - need to make sure that the copied message
39  * is independent of the original
40  *
41  *
42  * @version $Revision: 1.4.2.3 $
43  */

44 public class ImapHostTest extends TestCase
45         implements ImapConstants
46 {
47     private ImapHost imapHost;
48     private User user;
49
50     public ImapHostTest( String JavaDoc s )
51     {
52         super( s );
53     }
54
55     protected void setUp() throws Exception JavaDoc
56     {
57         super.setUp();
58         user = new DefaultUser( "user", null );
59
60         imapHost = getHostImplementation();
61         imapHost.createPrivateMailAccount( user );
62     }
63
64     protected ImapHost getHostImplementation()
65     {
66         return new JamesImapHost( new InMemoryStore() );
67     }
68
69     /**
70      * Tests creation of mailboxes in user's personal space. No namespaces are used.
71      */

72     public void testCreatePersonal() throws Exception JavaDoc
73     {
74         // Create a single mailbox.
75
create( "test" );
76         assertMailbox( "test", true );
77
78         // Create a child of an existing mailbox.
79
create("test.another" );
80         assertMailbox( "test", true );
81         assertMailbox( "test.another", true );
82
83         // A multi-level create, which creates intervening mailboxes,
84
// with the \NoSelect attribute set.
85
create( "this.is.another.mailbox");
86         assertMailbox( "this", false );
87         assertMailbox( "this.is", false );
88         assertMailbox( "this.is.another", false );
89         assertMailbox( "this.is.another.mailbox", true );
90
91         // Create a child of an existing, no-select mailbox.
92
create( "this.is.yet.another.mailbox");
93         assertMailbox( "this", false );
94         assertMailbox( "this.is", false );
95         assertMailbox( "this.is.yet", false );
96         assertMailbox( "this.is.yet.another", false );
97         assertMailbox( "this.is.yet.another.mailbox", true );
98     }
99
100     /**
101      * Tests deletion of mailboxes in user's personal space. No namespaces are used.
102      * @throws Exception
103      */

104     public void testDelete() throws Exception JavaDoc
105     {
106         // Simple create/delete
107
create( "test" );
108         assertMailbox( "test", true );
109         delete( "test" );
110         assertNoMailbox( "test");
111
112         // Create a chain and delete the parent.
113
// Child should remain, and parent be switched to NoSelect.
114
create( "one" );
115         create( "one.two" );
116         assertMailbox( "one", true );
117         assertMailbox( "one.two", true );
118         delete( "one");
119         assertMailbox( "one", false);
120         assertMailbox( "one.two", true );
121
122         // Can't delete mailbox with NoSelect attribute and children.
123
try
124         {
125             delete( "one" );
126             fail( "Should not be able to delete a non-selectabl mailbox which has children." );
127         }
128         catch( MailboxException e )
129         {
130             // TODO check for correct exception.
131
}
132
133         // Delete the child, then the non-selectable parent
134
delete( "one.two");
135         delete( "one");
136         assertNoMailbox( "one.two" );
137         assertNoMailbox( "one" );
138     }
139
140     /**
141      * Checks that a mailbox with the supplied name exists, and that its
142      * NoSelect flag matches that expected.
143      */

144     private void assertMailbox( String JavaDoc name, boolean selectable ) throws MailboxException
145     {
146         ImapMailbox mailbox = imapHost.getMailbox( user, name );
147         assertNotNull( "Mailbox <" + name + "> expected to exist in store.",
148                        mailbox );
149         if ( selectable )
150         {
151             assertTrue( "Mailbox <" + name + "> not selectable.",
152                         mailbox.isSelectable() );
153         }
154         else
155         {
156             assertTrue( "Mailbox <" + name + "> should not be selectable.",
157                         ! mailbox.isSelectable() );
158         }
159     }
160
161     /**
162      * Asserts that a mailbox with the supplied name doesn't exist.
163      */

164     private void assertNoMailbox( String JavaDoc name ) throws Exception JavaDoc
165     {
166         ImapMailbox mailbox = imapHost.getMailbox( user, name );
167         assertNull( "Mailbox <" + name + "> should not exist.",
168                     mailbox );
169     }
170
171     /**
172      * Calls {@link ImapHost#createMailbox} with the specified name and the test user.
173      */

174     private ImapMailbox create( String JavaDoc name ) throws Exception JavaDoc
175     {
176         return imapHost.createMailbox( user, name );
177     }
178
179     /**
180      * Calls {@link ImapHost#deleteMailbox} with the specified name and the test user.
181      */

182     private void delete( String JavaDoc name ) throws Exception JavaDoc
183     {
184         imapHost.deleteMailbox( user, name );
185     }
186 }
187
Popular Tags