1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.taglibs.authz;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.GrantedAuthority;
21 import org.acegisecurity.GrantedAuthorityImpl;
22 import org.acegisecurity.context.SecurityContextHolder;
23 import org.acegisecurity.context.SecurityContextImpl;
24 import org.acegisecurity.providers.TestingAuthenticationToken;
25
26 import org.springframework.mock.web.MockPageContext;
27
28 import javax.servlet.jsp.JspException;
29 import javax.servlet.jsp.tagext.Tag;
30
31
32 /***
33 * Test case to implement commons-el expression language expansion.
34 */
35 public class AuthorizeTagExpressionLanguageTests extends TestCase {
36
37
38 private final AuthorizeTag authorizeTag = new AuthorizeTag();
39 private MockPageContext pageContext;
40 private TestingAuthenticationToken currentUser;
41
42
43
44 public void testAllGrantedUsesExpressionLanguageWhenExpressionIsEL()
45 throws JspException {
46 pageContext.setAttribute("authority", "ROLE_TELLER");
47 authorizeTag.setIfAllGranted("${authority}");
48
49 assertEquals("allows body - authority var contains ROLE_TELLER",
50 Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
51 }
52
53 public void testAnyGrantedUsesExpressionLanguageWhenExpressionIsEL()
54 throws JspException {
55 pageContext.setAttribute("authority", "ROLE_TELLER");
56 authorizeTag.setIfAnyGranted("${authority}");
57
58 assertEquals("allows body - authority var contains ROLE_TELLER",
59 Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
60 }
61
62 public void testNotGrantedUsesExpressionLanguageWhenExpressionIsEL()
63 throws JspException {
64 pageContext.setAttribute("authority", "ROLE_TELLER");
65 authorizeTag.setIfNotGranted("${authority}");
66
67 assertEquals("allows body - authority var contains ROLE_TELLER",
68 Tag.SKIP_BODY, authorizeTag.doStartTag());
69 }
70
71 protected void setUp() throws Exception {
72 super.setUp();
73
74 pageContext = new MockPageContext();
75 authorizeTag.setPageContext(pageContext);
76
77 currentUser = new TestingAuthenticationToken("abc", "123",
78 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_TELLER"),});
79
80 SecurityContextHolder.getContext().setAuthentication(currentUser);
81 }
82
83 protected void tearDown() throws Exception {
84 SecurityContextHolder.setContext(new SecurityContextImpl());
85 }
86 }