1 16 17 package org.apache.jetspeed.services.security; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 25 import org.apache.jetspeed.om.security.GroupRole; 26 import org.apache.jetspeed.om.security.JetspeedRoleFactory; 27 import org.apache.jetspeed.om.security.Role; 28 import org.apache.jetspeed.test.JetspeedTestCase; 29 import org.apache.turbine.services.TurbineServices; 30 import org.apache.turbine.util.StringUtils; 31 import org.apache.turbine.util.TurbineConfig; 32 33 39 40 public class TestRoleManagement extends JetspeedTestCase { 41 42 47 public TestRoleManagement( String name ) { 48 super( name ); 49 } 50 51 56 public static void main(String args[]) 57 { 58 junit.awtui.TestRunner.main( new String [] { TestRoleManagement.class.getName() } ); 59 } 60 61 public void setup() 62 { 63 } 65 66 72 public static Test suite() 73 { 74 return new TestSuite( TestRoleManagement.class ); 76 } 77 78 82 83 public void testGetRoles() throws Exception 84 { 85 RoleManagement service = getService(); 86 Role role = null; 87 HashMap map = new HashMap (); 88 89 try 90 { 91 Iterator roles = service.getRoles(); 92 while (roles.hasNext()) 93 { 94 role = (Role)roles.next(); 95 map.put(role.getName(), role); 96 } 97 assertTrue(map.get("user") != null); 98 assertTrue(map.get("admin") != null); 99 } 100 catch (Exception e) 101 { 102 fail(StringUtils.stackTrace(e)); 103 } 104 105 System.out.println("Completed getRoles Test OK "); 106 107 } 108 109 113 114 public void testGetRolesForUser() throws Exception 115 { 116 RoleManagement service = getService(); 117 Role role = null; 118 HashMap map = new HashMap (); 119 120 try 121 { 122 Iterator roles = service.getRoles("turbine"); 123 while (roles.hasNext()) 124 { 125 GroupRole gr = (GroupRole) roles.next(); 126 role = gr.getRole(); 127 map.put(role.getName(), role); 128 System.out.println("role = " + role.getName()); 129 } 130 assertTrue(map.get("user") != null); 131 assertTrue(map.get("admin") == null); 132 133 map.clear(); 134 roles = service.getRoles("admin"); 135 while (roles.hasNext()) 136 { 137 GroupRole gr = (GroupRole)roles.next(); 138 role = gr.getRole(); 139 map.put(role.getName(), role); 140 System.out.println("role = " + role.getName()); 141 } 142 assertTrue(map.get("user") != null); 143 assertTrue(map.get("admin") != null); 144 } 145 catch (Exception e) 146 { 147 fail(StringUtils.stackTrace(e)); 148 } 149 150 System.out.println("Completed getRoles Test OK "); 151 152 } 153 154 158 159 public void testAddRole() throws Exception 160 { 161 RoleManagement service = getService(); 162 Role role = null; 163 164 try 165 { 166 role = JetspeedRoleFactory.getInstance(); 167 role.setName("bogus"); 168 service.addRole(role); 169 System.out.println("new role id = " + role.getId()); 170 assertTrue(role.getId() != null); 171 } 172 catch(Exception e) 173 { 174 fail(StringUtils.stackTrace(e)); 175 } 176 try 177 { 178 role = JetspeedRoleFactory.getInstance(); 179 role.setName("bogus"); 180 service.addRole(role); 181 fail("Should've thrown a dup key exception on role"); 182 } 183 catch(Exception e) 184 { 185 assertTrue(e instanceof RoleException); 186 } 187 188 System.out.println("Completed addRole Test OK "); 189 190 } 191 192 196 197 public void testRemoveRole() throws Exception 198 { 199 RoleManagement service = getService(); 200 Role role = null; 201 202 try 203 { 204 service.removeRole("bogus"); 205 } 206 catch(Exception e) 207 { 208 fail(StringUtils.stackTrace(e)); 209 } 210 try 211 { 212 service.removeRole("catchmeifyoucan"); 213 fail("Should've thrown a not found exception on role"); 214 } 215 catch(Exception e) 216 { 217 assertTrue(e instanceof RoleException); 218 } 219 220 System.out.println("Completed addRole Test OK "); 221 222 } 223 224 228 229 public void testGetRole() throws Exception 230 { 231 RoleManagement service = getService(); 232 233 try 234 { 235 Role role = service.getRole("user"); 236 System.out.println("*** role nm = " + role.getName()); 237 System.out.println("*** role id = " + role.getId()); 238 assertTrue(role.getName().equals("user")); 239 } 240 catch (Exception e) 241 { 242 fail(StringUtils.stackTrace(e)); 243 } 244 245 System.out.println("Completed getRole Test OK "); 246 247 } 248 249 253 254 public void testSaveRole() throws Exception 255 { 256 RoleManagement service = getService(); 257 258 try 259 { 260 Role role = service.getRole("user"); 261 service.saveRole(role); 262 } 263 catch(Exception e) 264 { 265 fail(StringUtils.stackTrace(e)); 266 } 267 268 System.out.println("Completed saveRole Test OK "); 269 270 } 271 272 276 public void testGrantRole() throws Exception 277 { 278 RoleManagement service = getService(); 279 Role role = null; 280 281 try 282 { 283 service.grantRole("turbine", "admin"); 284 } 285 catch(Exception e) 286 { 287 fail(StringUtils.stackTrace(e)); 288 } 289 try 290 { 291 service.grantRole("baduser", "admin"); 292 fail("Should've thrown a bad user exception on grant"); 293 } 294 catch(Exception e) 295 { 296 assertTrue(e instanceof RoleException); 297 } 298 try 299 { 300 service.grantRole("turbine", "badrole"); 301 fail("Should've thrown a bad role exception on grant"); 302 } 303 catch(Exception e) 304 { 305 assertTrue(e instanceof RoleException); 306 } 307 308 System.out.println("Completed grantRole Test OK "); 309 310 } 311 312 316 public void testRevokeRole() throws Exception 317 { 318 RoleManagement service = getService(); 319 Role role = null; 320 321 try 322 { 323 service.revokeRole("turbine", "admin"); 324 } 325 catch(Exception e) 326 { 327 fail(StringUtils.stackTrace(e)); 328 } 329 try 330 { 331 service.revokeRole("baduser", "admin"); 332 fail("Should've thrown a bad user exception on revoke"); 333 } 334 catch(Exception e) 335 { 336 assertTrue(e instanceof RoleException); 337 } 338 339 System.out.println("Completed revokeRole Test OK "); 340 341 } 342 343 347 public void testHasRole() throws Exception 348 { 349 RoleManagement service = getService(); 350 Role role = null; 351 352 try 353 { 354 boolean has = service.hasRole("admin", "admin"); 355 assertTrue(true == has); 356 } 357 catch(Exception e) 358 { 359 fail(StringUtils.stackTrace(e)); 360 } 361 try 362 { 363 boolean has = service.hasRole("turbine", "admin"); 364 assertTrue(false == has); 365 } 366 catch(Exception e) 367 { 368 fail(StringUtils.stackTrace(e)); 369 } 370 371 System.out.println("Completed hasRole Test OK "); 372 373 } 374 375 379 private static TurbineConfig config = null; 380 381 385 static 386 { 387 try 388 { 389 config = new TurbineConfig( "webapp", "/WEB-INF/conf/TurbineResources.properties"); 390 config.init(); 391 } 392 catch (Exception e) 393 { 394 fail(StringUtils.stackTrace(e)); 395 } 396 } 397 398 private static RoleManagement getService() 399 { 400 return (RoleManagement)TurbineServices 401 .getInstance() 402 .getService(RoleManagement.SERVICE_NAME); 403 } 404 405 } 406 407 408 409 410 411 | Popular Tags |