1 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 ; 34 import java.util.List ; 35 36 39 public class UserManagementTest extends TestCase { 40 41 private MockUsersRepository m_mockUsersRepository; 42 private UserManagement m_userManagement; 43 44 protected void setUp() throws Exception { 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 } 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 [] usersArray = new String [] {"ccc", "aaa", "dddd", "bbbbb"}; 104 List users = Arrays.asList(usersArray); 105 106 for (int i = 0; i < users.size(); i++) { 107 String user = (String ) users.get(i); 108 assertTrue("user added", m_mockUsersRepository.addUser(user, "test")); 109 } 110 111 String [] 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 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 try { 125 m_userManagement.setAlias("testNonExist1", "testNonExist2", null); 126 fail("user unknown to server"); 127 } catch (UserManagementException e) { 128 } 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 } 141 142 try { 143 m_userManagement.setAlias("testNonExist1", "testAlias", null); 144 fail("user unknown to server"); 145 } catch (UserManagementException e) { 146 } 148 149 assertTrue("user added", m_userManagement.addUser("testAlias2", "test", null)); 150 151 assertTrue("alias for testAlias1 set to:testAlias2", m_userManagement.setAlias("testAlias1", "testAlias2", null)); 153 154 assertTrue("alias for testAlias2 set to:testAlias1", m_userManagement.setAlias("testAlias2", "testAlias1", null)); 156 157 assertEquals("Current alias for testAlias1 is: testAlias2", "testAlias2", m_userManagement.getAlias("testAlias1", null)); 159 160 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 assertTrue("alias for testAlias1 set to:testAlias3", m_userManagement.setAlias("testAlias1", "testAlias3", null)); 167 168 assertEquals("Current alias for testAlias1 is: testAlias3", "testAlias3", m_userManagement.getAlias("testAlias1", null)); 170 171 assertTrue("alias for testAlias1 unset", m_userManagement.unsetAlias("testAlias1", null)); 173 174 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 try { 185 m_userManagement.setForwardAddress("testNonExist1", "testForward1@locahost", null); 186 fail("user unknown to server"); 187 } catch (UserManagementException e) { 188 } 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 String forwardAddress = m_userManagement.getForwardAddress("testForwardUser", null); 199 assertEquals("forward for testForwardUser is: testForward1@locahost", "testForward1@locahost", forwardAddress); 200 201 assertTrue(m_userManagement.setForwardAddress("testForwardUser", "testForward2@locahost", null)); 203 204 forwardAddress = m_userManagement.getForwardAddress("testForwardUser", null); 206 assertEquals("forward for testForwardUser is: testForward2@locahost", "testForward2@locahost", forwardAddress); 207 208 assertTrue("Forward for testForwardUser unset", m_userManagement.unsetForwardAddress("testForwardUser", null)); 210 211 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 assertTrue("changed to empty password", m_userManagement.setPassword("testPwdUser", "", null)); 224 assertTrue("password changed to empty", m_mockUsersRepository.test("testPwdUser", "")); 225 226 assertTrue("changed password", m_userManagement.setPassword("testPwdUser", "pwd2", null)); 228 assertTrue("password not changed to pwd2", m_mockUsersRepository.test("testPwdUser", "pwd2")); 229 230 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 userRepositoryNames = m_userManagement.getUserRepositoryNames(); 239 assertTrue("default is there", userRepositoryNames.contains("LocalUsers")); 240 } 241 } 242 | Popular Tags |