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.afterinvocation;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.AccessDeniedException;
021: import org.acegisecurity.Authentication;
022: import org.acegisecurity.ConfigAttribute;
023: import org.acegisecurity.ConfigAttributeDefinition;
024: import org.acegisecurity.SecurityConfig;
025:
026: import org.acegisecurity.intercept.web.FilterInvocation;
027:
028: import org.acegisecurity.util.SimpleMethodInvocation;
029:
030: import org.aopalliance.intercept.MethodInvocation;
031:
032: import java.util.List;
033: import java.util.Vector;
034:
035: /**
036: * Tests {@link AfterInvocationProviderManager}.
037: *
038: * @author Ben Alex
039: * @version $Id: AfterInvocationProviderManagerTests.java 1496 2006-05-23 13:38:33Z benalex $
040: */
041: public class AfterInvocationProviderManagerTests extends TestCase {
042: //~ Constructors ===================================================================================================
043:
044: public AfterInvocationProviderManagerTests() {
045: super ();
046: }
047:
048: public AfterInvocationProviderManagerTests(String arg0) {
049: super (arg0);
050: }
051:
052: //~ Methods ========================================================================================================
053:
054: public static void main(String[] args) {
055: junit.textui.TestRunner
056: .run(AfterInvocationProviderManagerTests.class);
057: }
058:
059: public final void setUp() throws Exception {
060: super .setUp();
061: }
062:
063: public void testCorrectOperation() throws Exception {
064: AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
065: List list = new Vector();
066: list.add(new MockAfterInvocationProvider("swap1",
067: MethodInvocation.class, new SecurityConfig(
068: "GIVE_ME_SWAP1")));
069: list.add(new MockAfterInvocationProvider("swap2",
070: MethodInvocation.class, new SecurityConfig(
071: "GIVE_ME_SWAP2")));
072: list.add(new MockAfterInvocationProvider("swap3",
073: MethodInvocation.class, new SecurityConfig(
074: "GIVE_ME_SWAP3")));
075: manager.setProviders(list);
076: assertEquals(list, manager.getProviders());
077: manager.afterPropertiesSet();
078:
079: ConfigAttributeDefinition attr1 = new ConfigAttributeDefinition();
080: attr1.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP1"));
081:
082: ConfigAttributeDefinition attr2 = new ConfigAttributeDefinition();
083: attr2.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP2"));
084:
085: ConfigAttributeDefinition attr3 = new ConfigAttributeDefinition();
086: attr3.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP3"));
087:
088: ConfigAttributeDefinition attr2and3 = new ConfigAttributeDefinition();
089: attr2and3
090: .addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP2"));
091: attr2and3
092: .addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP3"));
093:
094: ConfigAttributeDefinition attr4 = new ConfigAttributeDefinition();
095: attr4
096: .addConfigAttribute(new SecurityConfig(
097: "NEVER_CAUSES_SWAP"));
098:
099: assertEquals("swap1", manager.decide(null,
100: new SimpleMethodInvocation(), attr1,
101: "content-before-swapping"));
102:
103: assertEquals("swap2", manager.decide(null,
104: new SimpleMethodInvocation(), attr2,
105: "content-before-swapping"));
106:
107: assertEquals("swap3", manager.decide(null,
108: new SimpleMethodInvocation(), attr3,
109: "content-before-swapping"));
110:
111: assertEquals("content-before-swapping", manager.decide(null,
112: new SimpleMethodInvocation(), attr4,
113: "content-before-swapping"));
114:
115: assertEquals("swap3", manager.decide(null,
116: new SimpleMethodInvocation(), attr2and3,
117: "content-before-swapping"));
118: }
119:
120: public void testRejectsEmptyProvidersList() {
121: AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
122: List list = new Vector();
123:
124: try {
125: manager.setProviders(list);
126: fail("Should have thrown IllegalArgumentException");
127: } catch (IllegalArgumentException expected) {
128: assertTrue(true);
129: }
130: }
131:
132: public void testRejectsNonAfterInvocationProviders() {
133: AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
134: List list = new Vector();
135: list.add(new MockAfterInvocationProvider("swap1",
136: MethodInvocation.class, new SecurityConfig(
137: "GIVE_ME_SWAP1")));
138: list.add(new Integer(45));
139: list.add(new MockAfterInvocationProvider("swap3",
140: MethodInvocation.class, new SecurityConfig(
141: "GIVE_ME_SWAP3")));
142:
143: try {
144: manager.setProviders(list);
145: fail("Should have thrown IllegalArgumentException");
146: } catch (IllegalArgumentException expected) {
147: assertTrue(true);
148: }
149: }
150:
151: public void testRejectsNullProvidersList() throws Exception {
152: AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
153:
154: try {
155: manager.afterPropertiesSet();
156: fail("Should have thrown IllegalArgumentException");
157: } catch (IllegalArgumentException expected) {
158: assertTrue(true);
159: }
160: }
161:
162: public void testSupportsConfigAttributeIteration() throws Exception {
163: AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
164: List list = new Vector();
165: list.add(new MockAfterInvocationProvider("swap1",
166: MethodInvocation.class, new SecurityConfig(
167: "GIVE_ME_SWAP1")));
168: list.add(new MockAfterInvocationProvider("swap2",
169: MethodInvocation.class, new SecurityConfig(
170: "GIVE_ME_SWAP2")));
171: list.add(new MockAfterInvocationProvider("swap3",
172: MethodInvocation.class, new SecurityConfig(
173: "GIVE_ME_SWAP3")));
174: manager.setProviders(list);
175: manager.afterPropertiesSet();
176:
177: assertFalse(manager.supports(new SecurityConfig(
178: "UNKNOWN_ATTRIB")));
179: assertTrue(manager
180: .supports(new SecurityConfig("GIVE_ME_SWAP2")));
181: }
182:
183: public void testSupportsSecureObjectIteration() throws Exception {
184: AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
185: List list = new Vector();
186: list.add(new MockAfterInvocationProvider("swap1",
187: MethodInvocation.class, new SecurityConfig(
188: "GIVE_ME_SWAP1")));
189: list.add(new MockAfterInvocationProvider("swap2",
190: MethodInvocation.class, new SecurityConfig(
191: "GIVE_ME_SWAP2")));
192: list.add(new MockAfterInvocationProvider("swap3",
193: MethodInvocation.class, new SecurityConfig(
194: "GIVE_ME_SWAP3")));
195: manager.setProviders(list);
196: manager.afterPropertiesSet();
197:
198: assertFalse(manager.supports(FilterInvocation.class));
199: assertTrue(manager.supports(MethodInvocation.class));
200: }
201:
202: //~ Inner Classes ==================================================================================================
203:
204: /**
205: * Always returns the constructor-defined <code>forceReturnObject</code>, provided the same configuration
206: * attribute was provided. Also stores the secure object it supports.
207: */
208: private class MockAfterInvocationProvider implements
209: AfterInvocationProvider {
210: private Class secureObject;
211: private ConfigAttribute configAttribute;
212: private Object forceReturnObject;
213:
214: public MockAfterInvocationProvider(Object forceReturnObject,
215: Class secureObject, ConfigAttribute configAttribute) {
216: this .forceReturnObject = forceReturnObject;
217: this .secureObject = secureObject;
218: this .configAttribute = configAttribute;
219: }
220:
221: private MockAfterInvocationProvider() {
222: }
223:
224: public Object decide(Authentication authentication,
225: Object object, ConfigAttributeDefinition config,
226: Object returnedObject) throws AccessDeniedException {
227: if (config.contains(configAttribute)) {
228: return forceReturnObject;
229: }
230:
231: return returnedObject;
232: }
233:
234: public boolean supports(Class clazz) {
235: return secureObject.isAssignableFrom(clazz);
236: }
237:
238: public boolean supports(ConfigAttribute attribute) {
239: return attribute.equals(configAttribute);
240: }
241: }
242: }
|