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
25 import org.acegisecurity.providers.TestingAuthenticationToken;
26
27 import javax.servlet.jsp.JspException;
28
29
30
31
32
33 public class AuthzImplAttributeTest extends TestCase {
34
35
36 private final Authz authz = new AuthzImpl();
37 private TestingAuthenticationToken currentUser;
38
39
40
41 protected void setUp() throws Exception {
42 super.setUp();
43
44 currentUser = new TestingAuthenticationToken("abc", "123",
45 new GrantedAuthority[] {
46 new GrantedAuthorityImpl("ROLE_SUPERVISOR"), new GrantedAuthorityImpl("ROLE_RESTRICTED"),
47 });
48
49 SecurityContextHolder.getContext().setAuthentication(currentUser);
50 }
51
52 protected void tearDown() throws Exception {
53 SecurityContextHolder.clearContext();
54 }
55
56 public void testAssertsIfAllGrantedSecond() {
57 boolean r1 = authz.allGranted("ROLE_SUPERVISOR,ROLE_SUPERTELLER");
58 boolean r2 = authz.anyGranted("ROLE_RESTRICTED");
59
60
61 assertFalse(r1 && r2);
62 }
63
64 public void testAssertsIfAnyGrantedLast() {
65 boolean r2 = authz.anyGranted("ROLE_BANKER");
66
67
68 assertFalse(r2);
69 }
70
71 public void testAssertsIfNotGrantedFirst() {
72 boolean r1 = authz.allGranted("ROLE_SUPERVISOR,ROLE_RESTRICTED");
73 boolean r2 = authz.noneGranted("ROLE_RESTRICTED");
74 boolean r3 = authz.anyGranted("ROLE_SUPERVISOR");
75
76
77 assertFalse(r1 && r2 && r3);
78 }
79
80 public void testAssertsIfNotGrantedIgnoresWhitespaceInAttribute() {
81
82 assertTrue(authz.anyGranted("\tROLE_SUPERVISOR \t, \r\n\t ROLE_TELLER "));
83 }
84
85 public void testIfAllGrantedIgnoresWhitespaceInAttribute() {
86
87 assertTrue(authz.allGranted("\nROLE_SUPERVISOR\t,ROLE_RESTRICTED\t\n\r "));
88 }
89
90 public void testIfNotGrantedIgnoresWhitespaceInAttribute()
91 throws JspException {
92
93 assertFalse(authz.allGranted(" \t ROLE_TELLER \r"));
94 }
95 }