1 package org.apache.turbine.services.security; 2 3 18 19 import java.util.Hashtable ; 20 import java.util.List ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 25 import org.apache.turbine.om.security.User; 26 import org.apache.turbine.test.BaseTurbineHsqlTest; 27 import org.apache.turbine.util.security.DataBackendException; 28 import org.apache.turbine.util.security.EntityExistsException; 29 import org.apache.turbine.util.security.UnknownEntityException; 30 31 public class TestSecurityUser 32 extends BaseTurbineHsqlTest 33 { 34 public TestSecurityUser(String name) 35 throws Exception 36 { 37 super(name, "conf/test/TurbineResources.properties"); 38 } 39 40 public static Test suite() 41 { 42 return new TestSuite(TestSecurityUser.class); 43 } 44 45 private void checkUserList() 46 throws Exception 47 { 48 SecurityService ss = TurbineSecurity.getService(); 49 assertEquals("User added to storage!", ss.getUserList(new org.apache.torque.util.Criteria()).size(), 2); 50 } 51 52 public void testInit() 53 { 54 SecurityService ss = TurbineSecurity.getService(); 55 assertTrue("Service failed to initialize", ss.getInit()); 56 } 57 58 public void testDatabase() 60 throws Exception 61 { 62 SecurityService ss = TurbineSecurity.getService(); 63 64 List users = ss.getUserList(new org.apache.torque.util.Criteria()); 65 66 assertEquals("User DB Wrong!", users.size(), 2); 67 } 68 69 public void testUsers() 70 throws Exception 71 { 72 SecurityService ss = TurbineSecurity.getService(); 73 UserManager um = ss.getUserManager(); 74 75 User u = um.retrieve("admin"); 76 assertNotNull("No Admin found!", u); 77 assertEquals("Admin Id wrong!", u.getId(), 1); 78 79 assertFalse(u.hasLoggedIn()); 81 u.setHasLoggedIn(Boolean.TRUE); 82 assertTrue(u.hasLoggedIn()); 83 u.setHasLoggedIn(Boolean.FALSE); 84 assertFalse(u.hasLoggedIn()); 85 86 assertEquals(u.getPermStorage().getClass(), Hashtable .class); 88 assertEquals(u.getTempStorage().getClass(), Hashtable .class); 89 90 Hashtable permStorage = u.getPermStorage(); 91 92 int access = u.getAccessCounter(); 93 u.incrementAccessCounter(); 94 95 um.store(u); 96 97 u = null; 98 99 User u2 = um.retrieve("admin"); 100 101 102 assertNotSame(permStorage, u2.getPermStorage()); 104 105 assertEquals(access + 1 , u2.getAccessCounter()); 107 108 checkUserList(); 109 } 110 111 public void testAddUser() 112 throws Exception 113 { 114 SecurityService ss = TurbineSecurity.getService(); 115 116 User newbie = TurbineSecurity.getUserInstance(); 117 newbie.setName("newbie"); 118 119 newbie.setFirstName("John"); 120 newbie.setLastName("Doe"); 121 122 ss.addUser(newbie, "newbie"); 123 124 List users = ss.getUserList(new org.apache.torque.util.Criteria()); 125 assertEquals("User was not added", users.size(), 3); 126 127 try 128 { 129 User admin = ss.getUser("admin"); 130 131 ss.addUser(admin, "admin"); 132 fail("Existing User could be added!"); 133 } 134 catch (Exception e) 135 { 136 assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), EntityExistsException.class); 137 } 138 139 try 140 { 141 User empty = TurbineSecurity.getUserInstance(); 142 143 ss.addUser(empty, "empty"); 144 fail("User with empty Username could be added!"); 145 } 146 catch (Exception e) 147 { 148 assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), DataBackendException.class); 149 } 150 151 assertEquals("User was not added", users.size(), 3); 152 } 153 154 public void testRemoveUser() 155 throws Exception 156 { 157 SecurityService ss = TurbineSecurity.getService(); 158 159 User newbie = ss.getUser("newbie"); 160 assertNotNull(newbie); 161 162 ss.removeUser(newbie); 163 164 try 165 { 166 User foo = TurbineSecurity.getUserInstance(); 167 foo.setName("foo"); 168 169 ss.removeUser(foo); 170 fail("Non Existing User could be deleted!"); 171 } 172 catch (Exception e) 173 { 174 assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), UnknownEntityException.class); 175 } 176 177 checkUserList(); 178 } 179 180 public void testRetrieve() 181 throws Exception 182 { 183 SecurityService ss = TurbineSecurity.getService(); 184 UserManager um = ss.getUserManager(); 185 186 User u = um.retrieve("admin"); 187 assertNotNull("No Admin found!", u); 188 assertEquals("Admin Id wrong!", u.getId(), 1); 189 190 User u2 = um.retrieveById(new Integer (1)); 191 assertNotNull("No Admin found!", u2); 192 assertEquals("Admin Name wrong!", u.getName(), "admin"); 193 194 assertEquals("Two different User objects retrieved!", u, u2); 195 } 196 } 197 198 | Popular Tags |