1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.services.security;
18
19 import java.util.Iterator;
20 import java.util.HashMap;
21
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 import org.apache.turbine.services.TurbineServices;
27 import org.apache.turbine.util.TurbineConfig;
28 import org.apache.turbine.util.StringUtils;
29
30
31 import org.apache.jetspeed.test.JetspeedTestCase;
32 import org.apache.jetspeed.om.security.Permission;
33 import org.apache.jetspeed.om.security.JetspeedPermissionFactory;
34
35 import org.apache.jetspeed.services.security.JetspeedSecurityCache;
36
37 /***
38 * Unit test for PermissionManagement interface
39 *
40 * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
41 * @version $Id: TestPermissionManagement.java,v 1.1 2004/04/07 22:02:43 jford Exp $
42 */
43
44 public class TestPermissionManagement extends JetspeedTestCase {
45
46 /***
47 * Defines the testcase name for JUnit.
48 *
49 * @param name the testcase's name.
50 */
51 public TestPermissionManagement( String name ) {
52 super( name );
53 }
54
55 /***
56 * Start the tests.
57 *
58 * @param args the arguments. Not used
59 */
60 public static void main(String args[])
61 {
62 junit.awtui.TestRunner.main( new String[] { TestPermissionManagement.class.getName() } );
63 }
64
65 public void setup()
66 {
67
68 }
69
70 /***
71 * Creates the test suite.
72 *
73 * @return a test suite (<code>TestSuite</code>) that includes all methods
74 * starting with "test"
75 */
76 public static Test suite()
77 {
78
79 return new TestSuite( TestPermissionManagement.class );
80 }
81
82 /***
83 * Tests getPermissions method
84 * @throws Exception
85 */
86
87 public void testGetPermissions() throws Exception
88 {
89 PermissionManagement service = getService();
90 Permission permission = null;
91 HashMap map = new HashMap();
92 JetspeedSecurityCache.loadRolePermissions();
93
94 try
95 {
96 Iterator permissions = service.getPermissions();
97 while (permissions.hasNext())
98 {
99 permission = (Permission)permissions.next();
100 map.put(permission.getName(), permission);
101
102
103 }
104 assertTrue(map.get("view") != null);
105 assertTrue(map.get("customize") != null);
106 }
107 catch (Exception e)
108 {
109 fail(StringUtils.stackTrace(e));
110 }
111
112 System.out.println("Completed getPermissions Test OK ");
113
114 }
115
116 /***
117 * Tests getPermissions method
118 * @throws Exception
119 */
120
121 public void testGetPermissionsForUser() throws Exception
122 {
123 PermissionManagement service = getService();
124 Permission permission = null;
125 HashMap map = new HashMap();
126 JetspeedSecurityCache.loadRolePermissions();
127
128 try
129 {
130 Iterator permissions = service.getPermissions("user");
131 while (permissions.hasNext())
132 {
133 permission = (Permission)permissions.next();
134 map.put(permission.getName(), permission);
135 }
136 assertTrue(map.get("view") != null);
137 assertTrue(map.get("customize") != null);
138 assertTrue(map.get("close") == null);
139
140 map.clear();
141 permissions = service.getPermissions("admin");
142 while (permissions.hasNext())
143 {
144 permission = (Permission)permissions.next();
145 map.put(permission.getName(), permission);
146 }
147 assertTrue(map.get("view") != null);
148 assertTrue(map.get("customize") != null);
149 assertTrue(map.get("close") != null);
150
151 }
152 catch (Exception e)
153 {
154 fail(StringUtils.stackTrace(e));
155 }
156
157 System.out.println("Completed getPermissions Test OK ");
158
159 }
160
161 /***
162 * Tests addPermission method
163 * @throws Exception
164 */
165
166 public void testAddPermission() throws Exception
167 {
168 PermissionManagement service = getService();
169 Permission permission = null;
170 JetspeedSecurityCache.loadRolePermissions();
171
172 try
173 {
174 permission = JetspeedPermissionFactory.getInstance();
175 permission.setName("bogus");
176 service.addPermission(permission);
177 System.out.println("new permission id = " + permission.getId());
178 assertTrue(permission.getId() != null);
179 }
180 catch(Exception e)
181 {
182 fail(StringUtils.stackTrace(e));
183 }
184 try
185 {
186 permission = JetspeedPermissionFactory.getInstance();
187 permission.setName("bogus");
188 service.addPermission(permission);
189 fail("Should've thrown a dup key exception on permission");
190 }
191 catch(Exception e)
192 {
193 assertTrue(e instanceof PermissionException);
194 }
195
196 System.out.println("Completed addPermission Test OK ");
197
198 }
199
200 /***
201 * Tests getRemovePermission method
202 * @throws Exception
203 */
204
205 public void testRemovePermission() throws Exception
206 {
207 PermissionManagement service = getService();
208 Permission permission = null;
209 JetspeedSecurityCache.loadRolePermissions();
210
211 try
212 {
213 service.removePermission("bogus");
214 }
215 catch(Exception e)
216 {
217 fail(StringUtils.stackTrace(e));
218 }
219 try
220 {
221 service.removePermission("catchmeifyoucan");
222 fail("Should've thrown a not found exception on permission");
223 }
224 catch(Exception e)
225 {
226 assertTrue(e instanceof PermissionException);
227 }
228
229 System.out.println("Completed addPermission Test OK ");
230
231 }
232
233 /***
234 * Tests getPermission method
235 * @throws Exception
236 */
237
238 public void testGetPermission() throws Exception
239 {
240 PermissionManagement service = getService();
241 JetspeedSecurityCache.loadRolePermissions();
242
243 try
244 {
245 Permission permission = service.getPermission("view");
246 System.out.println("*** permission id = " + permission.getId());
247 assertTrue(permission.getName().equals("view"));
248 }
249 catch (Exception e)
250 {
251 fail(StringUtils.stackTrace(e));
252 }
253
254 System.out.println("Completed getPermission Test OK ");
255
256 }
257
258 /***
259 * Tests savePermission method
260 * @throws Exception
261 */
262
263 public void testSavePermission() throws Exception
264 {
265 PermissionManagement service = getService();
266 JetspeedSecurityCache.loadRolePermissions();
267
268 try
269 {
270 Permission permission = service.getPermission("customize");
271 service.savePermission(permission);
272 }
273 catch(Exception e)
274 {
275 fail(StringUtils.stackTrace(e));
276 }
277
278 System.out.println("Completed savePermission Test OK ");
279
280 }
281
282 /***
283 * Tests grantPermission method
284 * @throws Exception
285 */
286 public void testGrantPermission() throws Exception
287 {
288 PermissionManagement service = getService();
289 Permission permission = null;
290 JetspeedSecurityCache.loadRolePermissions();
291
292 try
293 {
294 service.grantPermission("user", "close");
295 }
296 catch(Exception e)
297 {
298 fail(StringUtils.stackTrace(e));
299 }
300 try
301 {
302 service.grantPermission("badrole", "close");
303 fail("Should've thrown a bad role exception on grant");
304 }
305 catch(Exception e)
306 {
307 assertTrue(e instanceof PermissionException);
308 }
309 try
310 {
311 service.grantPermission("user", "badpermission");
312 fail("Should've thrown a bad permission exception on grant");
313 }
314 catch(Exception e)
315 {
316 assertTrue(e instanceof PermissionException);
317 }
318
319 System.out.println("Completed grantPermission Test OK ");
320
321 }
322
323 /***
324 * Tests revokePermission method
325 * @throws Exception
326 */
327 public void testRevokePermission() throws Exception
328 {
329 PermissionManagement service = getService();
330 Permission permission = null;
331 JetspeedSecurityCache.loadRolePermissions();
332
333 try
334 {
335 service.revokePermission("user", "close");
336 }
337 catch(Exception e)
338 {
339 fail(StringUtils.stackTrace(e));
340 }
341 try
342 {
343 service.revokePermission("badrole", "close");
344 fail("Should've thrown a bad user exception on revoke");
345 }
346 catch(Exception e)
347 {
348 assertTrue(e instanceof PermissionException);
349 }
350
351 System.out.println("Completed revokePermission Test OK ");
352
353 }
354
355 /***
356 * Tests hasPermission method
357 * @throws Exception
358 */
359 public void testHasPermission() throws Exception
360 {
361 PermissionManagement service = getService();
362 Permission permission = null;
363 JetspeedSecurityCache.loadRolePermissions();
364
365 try
366 {
367 boolean has = service.hasPermission("user", "view");
368 assertTrue(true == has);
369 }
370 catch(Exception e)
371 {
372 fail(StringUtils.stackTrace(e));
373 }
374 try
375 {
376 boolean has = service.hasPermission("user", "close");
377 assertTrue(false == has);
378 }
379 catch(Exception e)
380 {
381 fail(StringUtils.stackTrace(e));
382 }
383
384 System.out.println("Completed hasPermission Test OK ");
385
386 }
387
388
389
390
391
392 private static TurbineConfig config = null;
393
394 /***
395 Sets up TurbineConfig using the system property:
396 <pre>turbine.properties</pre>
397 */
398 static
399 {
400 try
401 {
402 config = new TurbineConfig( "webapp", "/WEB-INF/conf/TurbineResources.properties");
403 config.init();
404 }
405 catch (Exception e)
406 {
407 fail(StringUtils.stackTrace(e));
408 }
409 }
410
411 private static PermissionManagement getService()
412 {
413 return (PermissionManagement)TurbineServices
414 .getInstance()
415 .getService(PermissionManagement.SERVICE_NAME);
416 }
417
418 }
419
420
421
422
423
424