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;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.MockAccessDecisionManager;
021: import org.acegisecurity.MockAfterInvocationManager;
022: import org.acegisecurity.MockAuthenticationManager;
023: import org.acegisecurity.MockRunAsManager;
024:
025: import org.acegisecurity.intercept.method.MockMethodDefinitionSource;
026:
027: import org.acegisecurity.util.SimpleMethodInvocation;
028:
029: /**
030: * Tests some {@link AbstractSecurityInterceptor} methods. Most of the testing for this class is found in the
031: * <code>MethodSecurityInterceptorTests</code> class.
032: *
033: * @author Ben Alex
034: * @version $Id: AbstractSecurityInterceptorTests.java 1496 2006-05-23 13:38:33Z benalex $
035: */
036: public class AbstractSecurityInterceptorTests extends TestCase {
037: //~ Constructors ===================================================================================================
038:
039: public AbstractSecurityInterceptorTests() {
040: super ();
041: }
042:
043: public AbstractSecurityInterceptorTests(String arg0) {
044: super (arg0);
045: }
046:
047: //~ Methods ========================================================================================================
048:
049: public static void main(String[] args) {
050: junit.textui.TestRunner
051: .run(AbstractSecurityInterceptorTests.class);
052: }
053:
054: public void testDetectsIfInvocationPassedIncompatibleSecureObject()
055: throws Exception {
056: MockSecurityInterceptorWhichOnlySupportsStrings si = new MockSecurityInterceptorWhichOnlySupportsStrings();
057: si.setRunAsManager(new MockRunAsManager());
058: si.setAuthenticationManager(new MockAuthenticationManager());
059: si.setAfterInvocationManager(new MockAfterInvocationManager());
060: si.setAccessDecisionManager(new MockAccessDecisionManager());
061: si.setObjectDefinitionSource(new MockMethodDefinitionSource(
062: false, true));
063:
064: try {
065: si.beforeInvocation(new SimpleMethodInvocation());
066: fail("Should have thrown IllegalArgumentException");
067: } catch (IllegalArgumentException expected) {
068: assertTrue(expected.getMessage().startsWith(
069: "Security invocation attempted for object"));
070: }
071: }
072:
073: public void testDetectsViolationOfGetSecureObjectClassMethod()
074: throws Exception {
075: MockSecurityInterceptorReturnsNull si = new MockSecurityInterceptorReturnsNull();
076: si.setRunAsManager(new MockRunAsManager());
077: si.setAuthenticationManager(new MockAuthenticationManager());
078: si.setAfterInvocationManager(new MockAfterInvocationManager());
079: si.setAccessDecisionManager(new MockAccessDecisionManager());
080: si.setObjectDefinitionSource(new MockMethodDefinitionSource(
081: false, true));
082:
083: try {
084: si.afterPropertiesSet();
085: fail("Should have thrown IllegalArgumentException");
086: } catch (IllegalArgumentException expected) {
087: assertEquals(
088: "Subclass must provide a non-null response to getSecureObjectClass()",
089: expected.getMessage());
090: }
091: }
092:
093: //~ Inner Classes ==================================================================================================
094:
095: private class MockSecurityInterceptorReturnsNull extends
096: AbstractSecurityInterceptor {
097: private ObjectDefinitionSource objectDefinitionSource;
098:
099: public Class getSecureObjectClass() {
100: return null;
101: }
102:
103: public ObjectDefinitionSource obtainObjectDefinitionSource() {
104: return objectDefinitionSource;
105: }
106:
107: public void setObjectDefinitionSource(
108: ObjectDefinitionSource objectDefinitionSource) {
109: this .objectDefinitionSource = objectDefinitionSource;
110: }
111: }
112:
113: private class MockSecurityInterceptorWhichOnlySupportsStrings
114: extends AbstractSecurityInterceptor {
115: private ObjectDefinitionSource objectDefinitionSource;
116:
117: public Class getSecureObjectClass() {
118: return String.class;
119: }
120:
121: public ObjectDefinitionSource obtainObjectDefinitionSource() {
122: return objectDefinitionSource;
123: }
124:
125: public void setObjectDefinitionSource(
126: ObjectDefinitionSource objectDefinitionSource) {
127: this.objectDefinitionSource = objectDefinitionSource;
128: }
129: }
130: }
|