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.aspectj;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.AccessDeniedException;
021: import org.acegisecurity.GrantedAuthority;
022: import org.acegisecurity.GrantedAuthorityImpl;
023: import org.acegisecurity.MockAccessDecisionManager;
024: import org.acegisecurity.MockApplicationContext;
025: import org.acegisecurity.MockAuthenticationManager;
026: import org.acegisecurity.MockJoinPoint;
027: import org.acegisecurity.MockRunAsManager;
028: import org.acegisecurity.TargetObject;
029:
030: import org.acegisecurity.context.SecurityContextHolder;
031:
032: import org.acegisecurity.intercept.method.MethodDefinitionMap;
033: import org.acegisecurity.intercept.method.MethodDefinitionSourceEditor;
034:
035: import org.acegisecurity.providers.TestingAuthenticationToken;
036:
037: import java.lang.reflect.Method;
038:
039: /**
040: * Tests {@link AspectJSecurityInterceptor}.
041: *
042: * @author Ben Alex
043: * @version $Id: AspectJSecurityInterceptorTests.java 1496 2006-05-23 13:38:33Z benalex $
044: */
045: public class AspectJSecurityInterceptorTests extends TestCase {
046: //~ Constructors ===================================================================================================
047:
048: public AspectJSecurityInterceptorTests() {
049: super ();
050: }
051:
052: public AspectJSecurityInterceptorTests(String arg0) {
053: super (arg0);
054: }
055:
056: //~ Methods ========================================================================================================
057:
058: public static void main(String[] args) {
059: junit.textui.TestRunner
060: .run(AspectJSecurityInterceptorTests.class);
061: }
062:
063: public final void setUp() throws Exception {
064: super .setUp();
065: }
066:
067: public void testCallbackIsInvokedWhenPermissionGranted()
068: throws Exception {
069: AspectJSecurityInterceptor si = new AspectJSecurityInterceptor();
070: si.setApplicationEventPublisher(MockApplicationContext
071: .getContext());
072: si.setAccessDecisionManager(new MockAccessDecisionManager());
073: si.setAuthenticationManager(new MockAuthenticationManager());
074: si.setRunAsManager(new MockRunAsManager());
075:
076: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
077: editor
078: .setAsText("org.acegisecurity.TargetObject.countLength=MOCK_ONE,MOCK_TWO");
079:
080: MethodDefinitionMap map = (MethodDefinitionMap) editor
081: .getValue();
082: si.setObjectDefinitionSource(map);
083: assertEquals(map, si.getObjectDefinitionSource());
084:
085: si.afterPropertiesSet();
086:
087: Class clazz = TargetObject.class;
088: Method method = clazz.getMethod("countLength",
089: new Class[] { String.class });
090: MockJoinPoint joinPoint = new MockJoinPoint(new TargetObject(),
091: method);
092:
093: MockAspectJCallback aspectJCallback = new MockAspectJCallback();
094:
095: SecurityContextHolder
096: .getContext()
097: .setAuthentication(
098: new TestingAuthenticationToken(
099: "marissa",
100: "koala",
101: new GrantedAuthority[] { new GrantedAuthorityImpl(
102: "MOCK_ONE") }));
103:
104: Object result = si.invoke(joinPoint, aspectJCallback);
105:
106: assertEquals("object proceeded", result);
107:
108: SecurityContextHolder.getContext().setAuthentication(null);
109: }
110:
111: public void testCallbackIsNotInvokedWhenPermissionDenied()
112: throws Exception {
113: AspectJSecurityInterceptor si = new AspectJSecurityInterceptor();
114: si.setApplicationEventPublisher(MockApplicationContext
115: .getContext());
116: si.setAccessDecisionManager(new MockAccessDecisionManager());
117: si.setAuthenticationManager(new MockAuthenticationManager());
118: si.setRunAsManager(new MockRunAsManager());
119:
120: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
121: editor
122: .setAsText("org.acegisecurity.TargetObject.countLength=MOCK_ONE,MOCK_TWO");
123:
124: MethodDefinitionMap map = (MethodDefinitionMap) editor
125: .getValue();
126: si.setObjectDefinitionSource(map);
127:
128: si.afterPropertiesSet();
129:
130: Class clazz = TargetObject.class;
131: Method method = clazz.getMethod("countLength",
132: new Class[] { String.class });
133: MockJoinPoint joinPoint = new MockJoinPoint(new TargetObject(),
134: method);
135:
136: MockAspectJCallback aspectJCallback = new MockAspectJCallback();
137: aspectJCallback.setThrowExceptionIfInvoked(true);
138:
139: SecurityContextHolder.getContext().setAuthentication(
140: new TestingAuthenticationToken("marissa", "koala",
141: new GrantedAuthority[] {}));
142:
143: try {
144: si.invoke(joinPoint, aspectJCallback);
145: fail("Should have thrown AccessDeniedException");
146: } catch (AccessDeniedException expected) {
147: assertTrue(true);
148: }
149:
150: SecurityContextHolder.getContext().setAuthentication(null);
151: }
152:
153: //~ Inner Classes ==================================================================================================
154:
155: private class MockAspectJCallback implements AspectJCallback {
156: private boolean throwExceptionIfInvoked = false;
157:
158: private MockAspectJCallback() {
159: }
160:
161: public Object proceedWithObject() {
162: if (throwExceptionIfInvoked) {
163: throw new IllegalStateException(
164: "AspectJCallback proceeded");
165: }
166:
167: return "object proceeded";
168: }
169:
170: public void setThrowExceptionIfInvoked(
171: boolean throwExceptionIfInvoked) {
172: this.throwExceptionIfInvoked = throwExceptionIfInvoked;
173: }
174: }
175: }
|