KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > management > UserManagementTest


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

19
20
21
22 package org.apache.james.management;
23
24 import junit.framework.TestCase;
25 import org.apache.avalon.framework.container.ContainerUtil;
26 import org.apache.james.services.UsersRepository;
27 import org.apache.james.services.UsersStore;
28 import org.apache.james.test.mock.avalon.MockLogger;
29 import org.apache.james.test.mock.avalon.MockServiceManager;
30 import org.apache.james.test.mock.james.MockUsersStore;
31 import org.apache.james.userrepository.MockUsersRepository;
32
33 import java.util.Arrays JavaDoc;
34 import java.util.List JavaDoc;
35
36 /**
37  * Tests the UserManagement
38  */

39 public class UserManagementTest extends TestCase {
40
41     private MockUsersRepository m_mockUsersRepository;
42     private UserManagement m_userManagement;
43
44     protected void setUp() throws Exception JavaDoc {
45         m_userManagement = new UserManagement();
46         ContainerUtil.enableLogging(m_userManagement, new MockLogger());
47         ContainerUtil.service(m_userManagement, setUpServiceManager());
48     }
49
50     private MockServiceManager setUpServiceManager() {
51         MockServiceManager serviceManager = new MockServiceManager();
52         m_mockUsersRepository = new MockUsersRepository();
53         serviceManager.put(UsersRepository.ROLE, m_mockUsersRepository);
54         serviceManager.put(UsersStore.ROLE, new MockUsersStore(m_mockUsersRepository));
55         return serviceManager;
56     }
57
58     public void testUserCount() throws UserManagementException {
59         assertEquals("no user yet", 0, m_userManagement.countUsers(null));
60         m_mockUsersRepository.addUser("testCount1", "testCount");
61         assertEquals("1 user", 1, m_userManagement.countUsers(null));
62         m_mockUsersRepository.addUser("testCount2", "testCount");
63         assertEquals("2 users", 2, m_userManagement.countUsers(null));
64         m_mockUsersRepository.removeUser("testCount1");
65         assertEquals("1 user", 1, m_userManagement.countUsers(null));
66     }
67
68     public void testDefaultRepositoryIsLocalUsers() throws UserManagementException {
69         m_userManagement.addUser("testCount1", "testCount", null);
70         m_userManagement.addUser("testCount2", "testCount", "LocalUsers");
71
72         assertEquals("2 users", 2, m_userManagement.countUsers(null));
73         assertEquals("2 users", 2, m_userManagement.countUsers("LocalUsers"));
74     }
75
76     public void testNonExistingRepository() throws UserManagementException {
77         try {
78             m_userManagement.addUser("testCount1", "testCount", "NonExisting");
79             fail("retrieved non-existing repository");
80         } catch (UserManagementException e) {
81             // success
82
}
83     }
84
85     public void testAddUserAndVerify() throws UserManagementException {
86         assertTrue("user added", m_mockUsersRepository.addUser("testCount1", "testCount"));
87         assertFalse("user not there", m_userManagement.verifyExists("testNotAdded", null));
88         assertTrue("user is there", m_userManagement.verifyExists("testCount1", null));
89         m_mockUsersRepository.removeUser("testCount1");
90         assertFalse("user not there", m_userManagement.verifyExists("testCount1", null));
91     }
92
93     public void testDelUser() throws UserManagementException {
94         assertTrue("user added", m_mockUsersRepository.addUser("testDel", "test"));
95         assertFalse("user not there", m_userManagement.verifyExists("testNotDeletable", null));
96         assertTrue("user is there", m_userManagement.verifyExists("testDel", null));
97         m_mockUsersRepository.removeUser("testDel");
98         assertFalse("user no longer there", m_userManagement.verifyExists("testDel", null));
99     }
100
101     public void testListUsers() throws UserManagementException {
102
103         String JavaDoc[] usersArray = new String JavaDoc[] {"ccc", "aaa", "dddd", "bbbbb"};
104         List JavaDoc users = Arrays.asList(usersArray);
105
106         for (int i = 0; i < users.size(); i++) {
107             String JavaDoc user = (String JavaDoc) users.get(i);
108             assertTrue("user added", m_mockUsersRepository.addUser(user, "test"));
109         }
110
111         String JavaDoc[] userNames = m_userManagement.listAllUsers(null);
112         assertEquals("user count", users.size(), userNames.length);
113
114         for (int i = 0; i < userNames.length; i++) {
115             String JavaDoc user = userNames[i];
116             if (!users.contains(user)) fail("user not listed");
117         }
118     }
119
120     public void testAlias() throws UserManagementException {
121         m_mockUsersRepository.setForceUseJamesUser();
122
123         // do some tests when parameter users don't exist
124
try {
125             m_userManagement.setAlias("testNonExist1", "testNonExist2", null);
126             fail("user unknown to server");
127         } catch (UserManagementException e) {
128             // success
129
}
130
131         assertTrue("user added", m_userManagement.addUser("testAlias1", "test", null));
132
133         assertNull("no alias set", m_userManagement.getAlias("testAlias1", null));
134
135         try {
136             m_userManagement.setAlias("testAlias1", "testNonExist2", null);
137             fail("alias unknown to server");
138         } catch (UserManagementException e) {
139             // success
140
}
141
142         try {
143             m_userManagement.setAlias("testNonExist1", "testAlias", null);
144             fail("user unknown to server");
145         } catch (UserManagementException e) {
146             // success
147
}
148
149         assertTrue("user added", m_userManagement.addUser("testAlias2", "test", null));
150
151         // regular alias
152
assertTrue("alias for testAlias1 set to:testAlias2", m_userManagement.setAlias("testAlias1", "testAlias2", null));
153
154         //TODO: is this correct? even primitive circular aliasing allowed!
155
assertTrue("alias for testAlias2 set to:testAlias1", m_userManagement.setAlias("testAlias2", "testAlias1", null));
156
157         // did first one persist?
158
assertEquals("Current alias for testAlias1 is: testAlias2", "testAlias2", m_userManagement.getAlias("testAlias1", null));
159
160         //TODO: is this correct? setting self as alias!
161
assertTrue("alias for testAlias1 set to:testAlias1", m_userManagement.setAlias("testAlias1", "testAlias1", null));
162
163         assertTrue("user added", m_userManagement.addUser("testAlias3", "test", null));
164
165         // re-set, simply overwrites
166
assertTrue("alias for testAlias1 set to:testAlias3", m_userManagement.setAlias("testAlias1", "testAlias3", null));
167
168         // check overwrite
169
assertEquals("Current alias for testAlias1 is: testAlias3", "testAlias3", m_userManagement.getAlias("testAlias1", null));
170
171         // retreat
172
assertTrue("alias for testAlias1 unset", m_userManagement.unsetAlias("testAlias1", null));
173
174         // check removed alias
175
//sendCommand("showalias testAlias1");
176
assertNull("User testAlias1 does not currently have an alias", m_userManagement.getAlias("testAlias1", null));
177
178     }
179
180     public void testForward() throws UserManagementException {
181         m_mockUsersRepository.setForceUseJamesUser();
182
183         // do some tests when parameter users don't exist
184
try {
185             m_userManagement.setForwardAddress("testNonExist1", "testForward1@locahost", null);
186             fail("user unknown to server");
187         } catch (UserManagementException e) {
188             // success
189
}
190
191         assertTrue("user added", m_userManagement.addUser("testForwardUser", "test", null));
192
193         assertNull("no forward set", m_userManagement.getForwardAddress("testForwardUser", null));
194
195         assertTrue(m_userManagement.setForwardAddress("testForwardUser", "testForward1@locahost", null));
196
197         // did it persist?
198
String JavaDoc forwardAddress = m_userManagement.getForwardAddress("testForwardUser", null);
199         assertEquals("forward for testForwardUser is: testForward1@locahost", "testForward1@locahost", forwardAddress);
200
201         // re-set, simply overwrites
202
assertTrue(m_userManagement.setForwardAddress("testForwardUser", "testForward2@locahost", null));
203
204         // check overwrite
205
forwardAddress = m_userManagement.getForwardAddress("testForwardUser", null);
206         assertEquals("forward for testForwardUser is: testForward2@locahost", "testForward2@locahost", forwardAddress);
207
208         // retreat
209
assertTrue("Forward for testForwardUser unset", m_userManagement.unsetForwardAddress("testForwardUser", null));
210
211         // check removed forward
212
assertNull("no more forward set", m_userManagement.getForwardAddress("testForwardUser", null));
213
214     }
215
216     public void testSetPassword() throws UserManagementException {
217
218         assertTrue("user added", m_userManagement.addUser("testPwdUser", "pwd1", null));
219
220         assertTrue("initial password", m_mockUsersRepository.test("testPwdUser", "pwd1"));
221
222         // set empty pwd
223
assertTrue("changed to empty password", m_userManagement.setPassword("testPwdUser", "", null));
224         assertTrue("password changed to empty", m_mockUsersRepository.test("testPwdUser", ""));
225
226         // change pwd
227
assertTrue("changed password", m_userManagement.setPassword("testPwdUser", "pwd2", null));
228         assertTrue("password not changed to pwd2", m_mockUsersRepository.test("testPwdUser", "pwd2"));
229
230         // assure case sensitivity
231
assertTrue("changed password", m_userManagement.setPassword("testPwdUser", "pWD2", null));
232         assertFalse("password no longer pwd2", m_mockUsersRepository.test("testPwdUser", "pwd2"));
233         assertTrue("password changed to pWD2", m_mockUsersRepository.test("testPwdUser", "pWD2"));
234
235     }
236     
237     public void testListRepositories() throws UserManagementException {
238         List JavaDoc userRepositoryNames = m_userManagement.getUserRepositoryNames();
239         assertTrue("default is there", userRepositoryNames.contains("LocalUsers"));
240     }
241 }
242
Popular Tags