1 65 package com.jcorporate.expresso.core.security.tests; 66 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.security.User; 69 import com.jcorporate.expresso.ext.dbobj.regobj.Address; 70 import com.jcorporate.expresso.services.test.ExpressoTestCase; 71 import com.jcorporate.expresso.services.test.TestSystemInitializer; 72 import junit.framework.Test; 73 import junit.framework.TestSuite; 74 75 82 public class UserTest 83 extends ExpressoTestCase { 84 90 public UserTest(String testName) 91 throws Exception { 92 super(testName); 93 } 94 95 96 102 public static void main(String [] args) 103 throws Exception { 104 105 junit.textui.TestRunner.run(suite()); 107 } 108 109 110 117 public static Test suite() 118 throws Exception { 119 return new TestSuite(UserTest.class); 120 } 121 122 123 126 127 132 public void testUser() 133 throws Exception { 134 135 138 System.out.println("UserTest :Creating test user"); 139 140 User newUser = new User(); 141 newUser.setLoginName("TEST"); 142 newUser.setDataContext(TestSystemInitializer.getTestContext()); 143 144 if (newUser.find()) { 145 newUser.delete(); 146 } 147 148 newUser.setLoginName("TEST"); 149 newUser.setDisplayName("Initial"); 150 newUser.setEmail("root@javacorp.com"); 151 newUser.setPassword("TEST"); 152 newUser.add(); 153 assertTrue("Password Test #1", passwdTest("TEST", "1")); 154 155 158 User testUser = new User(); 159 testUser.setDataContext(TestSystemInitializer.getTestContext()); 160 testUser.setLoginName("TEST"); 161 testUser.find(); 162 testUser.setDisplayName("Test User"); 163 testUser.update(); 164 assertTrue("Password Test #2", passwdTest("TEST", "2")); 165 166 169 testUser = new User(); 170 testUser.setDataContext(TestSystemInitializer.getTestContext()); 171 testUser.setLoginName("TEST"); 172 testUser.find(); 173 testUser.setPassword("ANOTHER"); 174 testUser.update(); 175 176 179 182 assertTrue("Password Test #3", passwdTest("ANOTHER", "3")); 183 assertTrue("Password Test #4", passwdTest("ANOTHER", "4")); 184 testUser.setPassword(""); 185 testUser.update(); 186 assertTrue("Password Test #5", passwdTest("", "5")); 187 testUser.setPassword("SECOND"); 188 testUser.update(); 189 testUser = new User(); 190 testUser.setDataContext(TestSystemInitializer.getTestContext()); 191 testUser.setLoginName("TEST"); 192 testUser.find(); 193 assertTrue("PasswordTest #6", passwdTest("SECOND", "6")); 194 testUser = new User(); 195 testUser.setDataContext(TestSystemInitializer.getTestContext()); 196 testUser.setLoginName("TEST"); 197 testUser.find(); 198 assertTrue("PasswordTest #7", passwdTest("SECOND", "7")); 199 testUser.delete(); 200 } 201 202 203 208 private boolean passwdTest(String newPass, String place) 209 throws DBException { 210 final String myName = "UserTest.passwdTest(String,String)"; 211 212 215 User testUser = new User(); 216 testUser.setDataContext(TestSystemInitializer.getTestContext()); 217 testUser.setLoginName("TEST"); 218 testUser.find(); 219 220 if (!testUser.passwordEquals(newPass)) { 221 System.out.println(myName + ":Password not stored correctly (" + 222 place + ")"); 223 224 return false; 225 } 226 227 System.out.println(myName + ":Password OK (" + place + ")"); 228 229 return true; 230 } 231 232 233 236 237 243 public void testUserListener() throws Exception { 244 System.out.println("UserTest: Testing the listener"); 245 246 User newUser = new User(); 247 newUser.setLoginName("TEST"); 248 newUser.setDataContext(TestSystemInitializer.getTestContext()); 249 250 if (newUser.find()) { 251 System.out.println("deleting user"); 252 newUser.delete(); 253 } 254 255 newUser.setLoginName("TEST"); 257 newUser.setDisplayName("Initial"); 258 newUser.setEmail("root@javacorp.com"); 259 newUser.setPassword("TEST"); 260 newUser.add(); 261 262 264 Address addr = new Address(); 265 addr.setDataContext(TestSystemInitializer.getTestContext()); 266 addr.setField("ExpUid", newUser.getUid()); 267 addr.setField("Street1", "test str"); 268 addr.setField("City", "testcity"); 269 addr.setField("State", "teststate"); 270 addr.setField("Zip", "ziptest"); 271 addr.setField("Country", "countrytest"); 272 addr.add(); 273 274 Address addr2 = new Address(); 275 addr2.setDataContext(TestSystemInitializer.getTestContext()); 276 addr2.setField("ExpUid", newUser.getUid()); 277 if (!addr2.find()) { 278 fail("address object not found"); 279 } 280 281 newUser.delete(); 283 284 if (addr2.find()) { 286 fail("address object was found after user was deleted"); 287 } 288 289 290 } 291 292 293 298 public void tearDown() throws Exception { 299 User newUser = new User(); 300 newUser.setLoginName("TEST"); 301 newUser.setDataContext(TestSystemInitializer.getTestContext()); 302 303 if (newUser.find()) { 304 System.out.println("deleting user in the tear down"); 305 newUser.delete(); 306 } 307 } 308 309 310 } 311 | Popular Tags |