001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.intercept.method;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022: import org.acegisecurity.ITargetObject;
023:
024: import org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor;
025:
026: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
027:
028: import org.acegisecurity.util.MethodInvocationUtils;
029:
030: import org.aopalliance.intercept.MethodInvocation;
031:
032: import org.springframework.context.ApplicationContext;
033: import org.springframework.context.support.ClassPathXmlApplicationContext;
034:
035: /**
036: * Tests {@link org.acegisecurity.intercept.method.MethodInvocationPrivilegeEvaluator}.
037: *
038: * @author Ben Alex
039: * @version $Id: MethodInvocationPrivilegeEvaluatorTests.java 1675 2006-09-15 03:38:36Z benalex $
040: */
041: public class MethodInvocationPrivilegeEvaluatorTests extends TestCase {
042: //~ Constructors ===================================================================================================
043:
044: public MethodInvocationPrivilegeEvaluatorTests() {
045: super ();
046: }
047:
048: public MethodInvocationPrivilegeEvaluatorTests(String arg0) {
049: super (arg0);
050: }
051:
052: //~ Methods ========================================================================================================
053:
054: private Object lookupTargetObject() {
055: ApplicationContext context = new ClassPathXmlApplicationContext(
056: "org/acegisecurity/intercept/method/aopalliance/applicationContext.xml");
057:
058: return context.getBean("target");
059: }
060:
061: public static void main(String[] args) {
062: junit.textui.TestRunner
063: .run(MethodInvocationPrivilegeEvaluatorTests.class);
064: }
065:
066: private MethodSecurityInterceptor makeSecurityInterceptor() {
067: ApplicationContext context = new ClassPathXmlApplicationContext(
068: "org/acegisecurity/intercept/method/aopalliance/applicationContext.xml");
069:
070: return (MethodSecurityInterceptor) context
071: .getBean("securityInterceptor");
072: }
073:
074: public void testAllowsAccessUsingCreate() throws Exception {
075: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
076: "Test", "Password",
077: new GrantedAuthority[] { new GrantedAuthorityImpl(
078: "MOCK_LOWER") });
079: Object object = lookupTargetObject();
080: MethodInvocation mi = MethodInvocationUtils.create(object,
081: "makeLowerCase", new Object[] { "foobar" });
082: MethodSecurityInterceptor interceptor = makeSecurityInterceptor();
083:
084: MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator();
085: mipe.setSecurityInterceptor(interceptor);
086: mipe.afterPropertiesSet();
087:
088: assertTrue(mipe.isAllowed(mi, token));
089: }
090:
091: public void testAllowsAccessUsingCreateFromClass() throws Exception {
092: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
093: "Test", "Password",
094: new GrantedAuthority[] { new GrantedAuthorityImpl(
095: "MOCK_LOWER") });
096: MethodInvocation mi = MethodInvocationUtils.createFromClass(
097: ITargetObject.class, "makeLowerCase",
098: new Class[] { String.class },
099: new Object[] { "Hello world" });
100: MethodSecurityInterceptor interceptor = makeSecurityInterceptor();
101:
102: MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator();
103: mipe.setSecurityInterceptor(interceptor);
104: mipe.afterPropertiesSet();
105:
106: assertTrue(mipe.isAllowed(mi, token));
107: }
108:
109: public void testDeclinesAccessUsingCreate() throws Exception {
110: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
111: "Test", "Password",
112: new GrantedAuthority[] { new GrantedAuthorityImpl(
113: "ROLE_NOT_HELD") });
114: Object object = lookupTargetObject();
115: MethodInvocation mi = MethodInvocationUtils.create(object,
116: "makeLowerCase", new Object[] { "foobar" });
117: MethodSecurityInterceptor interceptor = makeSecurityInterceptor();
118:
119: MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator();
120: mipe.setSecurityInterceptor(interceptor);
121: mipe.afterPropertiesSet();
122:
123: assertFalse(mipe.isAllowed(mi, token));
124: }
125:
126: public void testDeclinesAccessUsingCreateFromClass()
127: throws Exception {
128: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
129: "Test", "Password",
130: new GrantedAuthority[] { new GrantedAuthorityImpl(
131: "ROLE_NOT_HELD") });
132: MethodInvocation mi = MethodInvocationUtils.createFromClass(
133: ITargetObject.class, "makeLowerCase",
134: new Class[] { String.class },
135: new Object[] { "helloWorld" });
136: MethodSecurityInterceptor interceptor = makeSecurityInterceptor();
137:
138: MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator();
139: mipe.setSecurityInterceptor(interceptor);
140: mipe.afterPropertiesSet();
141:
142: assertFalse(mipe.isAllowed(mi, token));
143: }
144: }
|