1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.taglibs.velocity;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.GrantedAuthority;
21 import org.acegisecurity.GrantedAuthorityImpl;
22
23 import org.acegisecurity.context.SecurityContextHolder;
24 import org.acegisecurity.context.SecurityContextImpl;
25
26 import org.acegisecurity.providers.TestingAuthenticationToken;
27
28 import javax.servlet.jsp.JspException;
29
30
31 /***
32 * DOCUMENT ME!
33 */
34 public class AuthzImplAttributeTest extends TestCase {
35
36
37 private final Authz authz = new AuthzImpl();
38 private TestingAuthenticationToken currentUser;
39
40
41
42 protected void setUp() throws Exception {
43 super.setUp();
44
45 currentUser = new TestingAuthenticationToken("abc", "123",
46 new GrantedAuthority[] {new GrantedAuthorityImpl(
47 "ROLE_SUPERVISOR"), new GrantedAuthorityImpl(
48 "ROLE_RESTRICTED"),});
49
50 SecurityContextHolder.getContext().setAuthentication(currentUser);
51 }
52
53 protected void tearDown() throws Exception {
54 SecurityContextHolder.setContext(new SecurityContextImpl());
55 }
56
57 public void testAssertsIfAllGrantedSecond() {
58 boolean r1 = authz.allGranted("ROLE_SUPERVISOR,ROLE_SUPERTELLER");
59 boolean r2 = authz.anyGranted("ROLE_RESTRICTED");
60
61
62 assertFalse(r1 && r2);
63 }
64
65 public void testAssertsIfAnyGrantedLast() {
66 boolean r2 = authz.anyGranted("ROLE_BANKER");
67
68
69 assertFalse(r2);
70 }
71
72 public void testAssertsIfNotGrantedFirst() {
73 boolean r1 = authz.allGranted("ROLE_SUPERVISOR,ROLE_RESTRICTED");
74 boolean r2 = authz.noneGranted("ROLE_RESTRICTED");
75 boolean r3 = authz.anyGranted("ROLE_SUPERVISOR");
76
77
78 assertFalse(r1 && r2 && r3);
79 }
80
81 public void testAssertsIfNotGrantedIgnoresWhitespaceInAttribute() {
82
83 assertTrue(authz.anyGranted(
84 "\tROLE_SUPERVISOR \t, \r\n\t ROLE_TELLER "));
85 }
86
87 public void testIfAllGrantedIgnoresWhitespaceInAttribute() {
88
89 assertTrue(authz.allGranted(
90 "\nROLE_SUPERVISOR\t,ROLE_RESTRICTED\t\n\r "));
91 }
92
93 public void testIfNotGrantedIgnoresWhitespaceInAttribute()
94 throws JspException {
95
96 assertFalse(authz.allGranted(" \t ROLE_TELLER \r"));
97 }
98 }