1 16 17 package org.apache.jetspeed.services.security; 18 19 import junit.framework.Test; 21 import junit.framework.TestSuite; 22 23 import org.apache.turbine.services.TurbineServices; 24 import org.apache.turbine.util.TurbineConfig; 25 import org.apache.turbine.util.StringUtils; 26 27 import org.apache.jetspeed.test.JetspeedTestCase; 29 import org.apache.jetspeed.services.JetspeedSecurity; 30 import org.apache.jetspeed.om.security.Role; 31 import org.apache.jetspeed.om.security.Permission; 32 import org.apache.jetspeed.om.security.JetspeedUser; 33 34 40 41 public class TestSecurityCache extends JetspeedTestCase { 42 43 48 public TestSecurityCache( String name ) { 49 super( name ); 50 } 51 52 57 public static void main(String args[]) 58 { 59 junit.awtui.TestRunner.main( new String [] { TestSecurityCache.class.getName() } ); 60 } 61 62 public void setup() 63 { 64 } 66 67 73 public static Test suite() 74 { 75 return new TestSuite( TestSecurityCache.class ); 77 } 78 79 83 84 public void testLoadCache() throws Exception 85 { 86 SecurityCacheService service = getService(); 87 88 try 89 { 90 JetspeedUser user = JetspeedSecurity.getUser("turbine"); 91 service.load(user.getUserName()); 92 Role role = service.getRole(user.getUserName(), "user"); 93 assertTrue(role.getName().equals("user")); 94 assertTrue(service.hasRole(user.getUserName(), "user")); 95 assertTrue(service.hasPermission("user", "view")); 96 assertTrue(service.hasPermission("user", "customize")); 97 assertTrue(service.hasPermission("user", "maximize")); 98 assertTrue(!service.hasPermission("user", "failure")); 99 } 100 catch (Exception e) 101 { 102 fail(StringUtils.stackTrace(e)); 103 } 104 105 System.out.println("Completed loadCache Test OK "); 106 107 } 108 109 110 public void testAddRemoveFromCache() throws Exception 111 { 112 SecurityCacheService service = getService(); 113 114 try 115 { 116 JetspeedUser user = JetspeedSecurity.getUser("anon"); 117 service.load(user.getUserName()); 118 119 Role role1 = service.getRole(user.getUserName(), "guest"); 120 assertTrue(role1.getName().equals("guest")); 121 assertTrue(service.hasPermission("guest", "view")); 122 123 Role role2 = JetspeedSecurity.getRole("user"); 125 service.addRole(user.getUserName(), role2); 126 assertTrue(service.hasRole(user.getUserName(), "user")); 127 assertTrue(service.getRole(user.getUserName(),"user").getName().equals("user")); 128 129 service.removeRole(user.getUserName(), "user"); 131 assertTrue(!service.hasRole(user.getUserName(), "user")); 132 Role role3 = service.getRole(user.getUserName(),"user"); 133 assertTrue(null == role3); 134 135 Permission perm1 = JetspeedSecurity.getPermission("info"); 137 assertTrue(null != perm1); 138 service.addPermission("guest", perm1); 139 Permission permission = service.getPermission("guest", "info"); 140 assertTrue(permission.getName().equals("info")); 141 assertTrue(service.hasPermission("guest", "info")); 142 143 service.removePermission("guest", "info"); 145 assertTrue(!service.hasPermission( "guest", "info")); 146 Permission perm2 = service.getPermission( "guest", "info"); 147 assertTrue(null == perm2); 148 149 } 150 catch (Exception e) 151 { 152 fail(StringUtils.stackTrace(e)); 153 } 154 155 System.out.println("Completed addRemoveFromCache Test OK "); 156 157 } 158 159 public void testRemoveAll() throws Exception 160 { 161 SecurityCacheService service = getService(); 162 try 163 { 164 Role role = JetspeedSecurity.getRole("admin"); 165 JetspeedUser anon = JetspeedSecurity.getUser("anon"); 166 service.load(anon.getUserName()); 167 JetspeedUser turbine = JetspeedSecurity.getUser("turbine"); 168 service.load(turbine.getUserName()); 169 170 service.addRole(anon.getUserName(), role); 171 service.addRole(turbine.getUserName(), role); 172 173 assertTrue(service.hasRole(anon.getUserName(), role.getName())); 174 assertTrue(service.hasRole(turbine.getUserName(), role.getName())); 175 176 service.removeAllRoles("admin"); 177 178 assertTrue(!service.hasRole(anon.getUserName(), role.getName())); 179 assertTrue(!service.hasRole(turbine.getUserName(), role.getName())); 180 181 } 182 catch (Exception e) 183 { 184 fail(StringUtils.stackTrace(e)); 185 } 186 187 System.out.println("Completed removeAll Test OK "); 188 189 } 190 191 195 private static TurbineConfig config = null; 196 197 201 static 202 { 203 try 204 { 205 config = new TurbineConfig( "webapp", "/WEB-INF/conf/TurbineResources.properties"); 206 config.init(); 207 } 208 catch (Exception e) 209 { 210 fail(StringUtils.stackTrace(e)); 211 } 212 } 213 214 private static SecurityCacheService getService() 215 { 216 return (SecurityCacheService)TurbineServices 217 .getInstance() 218 .getService(SecurityCacheService.SERVICE_NAME); 219 } 220 221 } 222 223 224 225 226 227 228 | Popular Tags |