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.ConfigAttribute;
021: import org.acegisecurity.ConfigAttributeDefinition;
022: import org.acegisecurity.GrantedAuthority;
023: import org.acegisecurity.GrantedAuthorityImpl;
024: import org.acegisecurity.ITargetObject;
025: import org.acegisecurity.OtherTargetObject;
026: import org.acegisecurity.SecurityConfig;
027: import org.acegisecurity.TargetObject;
028:
029: import org.acegisecurity.acl.basic.SomeDomain;
030:
031: import org.acegisecurity.context.SecurityContextHolder;
032:
033: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
034:
035: import org.acegisecurity.util.SimpleMethodInvocation;
036:
037: import org.springframework.context.ApplicationContext;
038: import org.springframework.context.support.ClassPathXmlApplicationContext;
039:
040: import java.lang.reflect.Method;
041:
042: import java.util.HashSet;
043: import java.util.Iterator;
044: import java.util.Set;
045:
046: /**
047: * Tests {@link MethodDefinitionAttributes}.
048: *
049: * @author Cameron Braid
050: * @author Ben Alex
051: * @version $Id: MethodDefinitionAttributesTests.java 1496 2006-05-23 13:38:33Z benalex $
052: */
053: public class MethodDefinitionAttributesTests extends TestCase {
054: //~ Instance fields ================================================================================================
055:
056: ClassPathXmlApplicationContext applicationContext;
057:
058: //~ Constructors ===================================================================================================
059:
060: public MethodDefinitionAttributesTests(String a) {
061: super (a);
062: }
063:
064: //~ Methods ========================================================================================================
065:
066: private ConfigAttributeDefinition getConfigAttributeDefinition(
067: Class clazz, String methodName, Class[] args)
068: throws Exception {
069: final Method method = clazz.getMethod(methodName, args);
070: MethodDefinitionAttributes source = new MethodDefinitionAttributes();
071: source.setAttributes(new MockAttributes());
072:
073: ConfigAttributeDefinition config = source
074: .getAttributes(new SimpleMethodInvocation() {
075: public Method getMethod() {
076: return method;
077: }
078: });
079:
080: return config;
081: }
082:
083: public static void main(String[] args) {
084: junit.textui.TestRunner
085: .run(MethodDefinitionAttributesTests.class);
086: }
087:
088: private ITargetObject makeInterceptedTarget() {
089: ApplicationContext context = new ClassPathXmlApplicationContext(
090: "org/acegisecurity/intercept/method/applicationContext.xml");
091:
092: return (ITargetObject) context.getBean("target");
093: }
094:
095: public final void setUp() throws Exception {
096: super .setUp();
097: }
098:
099: public void testAttributesForInterfaceTargetObject()
100: throws Exception {
101: ConfigAttributeDefinition def1 = getConfigAttributeDefinition(
102: ITargetObject.class, "countLength",
103: new Class[] { String.class });
104: Set set1 = toSet(def1);
105: assertTrue(set1.contains(new SecurityConfig("MOCK_INTERFACE")));
106: assertTrue(set1.contains(new SecurityConfig(
107: "MOCK_INTERFACE_METHOD_COUNT_LENGTH")));
108:
109: ConfigAttributeDefinition def2 = getConfigAttributeDefinition(
110: ITargetObject.class, "makeLowerCase",
111: new Class[] { String.class });
112: Set set2 = toSet(def2);
113: assertTrue(set2.contains(new SecurityConfig("MOCK_INTERFACE")));
114: assertTrue(set2.contains(new SecurityConfig(
115: "MOCK_INTERFACE_METHOD_MAKE_LOWER_CASE")));
116:
117: ConfigAttributeDefinition def3 = getConfigAttributeDefinition(
118: ITargetObject.class, "makeUpperCase",
119: new Class[] { String.class });
120: Set set3 = toSet(def3);
121: assertTrue(set3.contains(new SecurityConfig("MOCK_INTERFACE")));
122: assertTrue(set3.contains(new SecurityConfig(
123: "MOCK_INTERFACE_METHOD_MAKE_UPPER_CASE")));
124: }
125:
126: public void testAttributesForOtherTargetObject() throws Exception {
127: ConfigAttributeDefinition def1 = getConfigAttributeDefinition(
128: OtherTargetObject.class, "countLength",
129: new Class[] { String.class });
130: Set set1 = toSet(def1);
131: assertTrue(set1.contains(new SecurityConfig("MOCK_INTERFACE")));
132: assertTrue(set1.contains(new SecurityConfig(
133: "MOCK_INTERFACE_METHOD_COUNT_LENGTH")));
134:
135: // Confirm MOCK_CLASS_METHOD_COUNT_LENGTH not added, as it's a String (not a ConfigAttribute)
136: // Confirm also MOCK_CLASS not added, as we return null for class
137: assertEquals(2, set1.size());
138:
139: ConfigAttributeDefinition def2 = getConfigAttributeDefinition(
140: OtherTargetObject.class, "makeLowerCase",
141: new Class[] { String.class });
142: Set set2 = toSet(def2);
143: assertTrue(set2.contains(new SecurityConfig("MOCK_INTERFACE")));
144: assertTrue(set2.contains(new SecurityConfig(
145: "MOCK_INTERFACE_METHOD_MAKE_LOWER_CASE")));
146: assertTrue(set2.contains(new SecurityConfig(
147: "MOCK_CLASS_METHOD_MAKE_LOWER_CASE")));
148:
149: // Confirm MOCK_CLASS not added, as we return null for class
150: assertEquals(3, set2.size());
151:
152: ConfigAttributeDefinition def3 = getConfigAttributeDefinition(
153: OtherTargetObject.class, "makeUpperCase",
154: new Class[] { String.class });
155: Set set3 = toSet(def3);
156: assertTrue(set3.contains(new SecurityConfig("MOCK_INTERFACE")));
157: assertTrue(set3.contains(new SecurityConfig(
158: "MOCK_INTERFACE_METHOD_MAKE_UPPER_CASE")));
159: assertTrue(set3.contains(new SecurityConfig("RUN_AS"))); // defined against interface
160:
161: assertEquals(3, set3.size());
162: }
163:
164: public void testAttributesForTargetObject() throws Exception {
165: ConfigAttributeDefinition def1 = getConfigAttributeDefinition(
166: TargetObject.class, "countLength",
167: new Class[] { String.class });
168: Set set1 = toSet(def1);
169: assertTrue(set1.contains(new SecurityConfig("MOCK_INTERFACE")));
170: assertTrue(set1.contains(new SecurityConfig(
171: "MOCK_INTERFACE_METHOD_COUNT_LENGTH")));
172:
173: assertTrue(set1.contains(new SecurityConfig("MOCK_CLASS")));
174:
175: // Confirm the MOCK_CLASS_METHOD_COUNT_LENGTH was not added, as it's not a ConfigAttribute
176: assertEquals(3, set1.size());
177:
178: ConfigAttributeDefinition def2 = getConfigAttributeDefinition(
179: TargetObject.class, "makeLowerCase",
180: new Class[] { String.class });
181: Set set2 = toSet(def2);
182: assertTrue(set2.contains(new SecurityConfig("MOCK_INTERFACE")));
183: assertTrue(set2.contains(new SecurityConfig(
184: "MOCK_INTERFACE_METHOD_MAKE_LOWER_CASE")));
185: assertTrue(set2.contains(new SecurityConfig("MOCK_CLASS")));
186: assertTrue(set2.contains(new SecurityConfig(
187: "MOCK_CLASS_METHOD_MAKE_LOWER_CASE")));
188: assertEquals(4, set2.size());
189:
190: ConfigAttributeDefinition def3 = getConfigAttributeDefinition(
191: TargetObject.class, "makeUpperCase",
192: new Class[] { String.class });
193: Set set3 = toSet(def3);
194: assertTrue(set3.contains(new SecurityConfig("MOCK_INTERFACE")));
195: assertTrue(set3.contains(new SecurityConfig(
196: "MOCK_INTERFACE_METHOD_MAKE_UPPER_CASE")));
197: assertTrue(set3.contains(new SecurityConfig("MOCK_CLASS")));
198: assertTrue(set3.contains(new SecurityConfig(
199: "MOCK_CLASS_METHOD_MAKE_UPPER_CASE")));
200: assertTrue(set3.contains(new SecurityConfig("RUN_AS")));
201: assertEquals(5, set3.size());
202: }
203:
204: public void testMethodCallWithRunAsReplacement() throws Exception {
205: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
206: "Test", "Password",
207: new GrantedAuthority[] { new GrantedAuthorityImpl(
208: "MOCK_INTERFACE_METHOD_MAKE_UPPER_CASE") });
209: SecurityContextHolder.getContext().setAuthentication(token);
210:
211: ITargetObject target = makeInterceptedTarget();
212: String result = target.makeUpperCase("hello");
213: assertEquals(
214: "HELLO org.acegisecurity.MockRunAsAuthenticationToken true",
215: result);
216:
217: SecurityContextHolder.getContext().setAuthentication(null);
218: }
219:
220: public void testMethodCallWithoutRunAsReplacement()
221: throws Exception {
222: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
223: "Test", "Password",
224: new GrantedAuthority[] { new GrantedAuthorityImpl(
225: "MOCK_INTERFACE_METHOD_MAKE_LOWER_CASE") });
226: SecurityContextHolder.getContext().setAuthentication(token);
227:
228: ITargetObject target = makeInterceptedTarget();
229: String result = target.makeLowerCase("HELLO");
230:
231: assertEquals(
232: "hello org.acegisecurity.providers.UsernamePasswordAuthenticationToken true",
233: result);
234:
235: SecurityContextHolder.getContext().setAuthentication(null);
236: }
237:
238: public void testNullReturnedIfZeroAttributesDefinedForMethodInvocation()
239: throws Exception {
240: // SomeDomain is not defined in the MockAttributes()
241: // (which getConfigAttributeDefinition refers to)
242: ConfigAttributeDefinition def = getConfigAttributeDefinition(
243: SomeDomain.class, "getId", null);
244: assertNull(def);
245: }
246:
247: /**
248: * convert a <code>ConfigAttributeDefinition</code> into a set of <code>ConfigAttribute</code>(s)
249: *
250: * @param def the <code>ConfigAttributeDefinition</code> to cover
251: *
252: * @return a Set of <code>ConfigAttributes</code>
253: */
254: private Set toSet(ConfigAttributeDefinition def) {
255: Set set = new HashSet();
256: Iterator i = def.getConfigAttributes();
257:
258: while (i.hasNext()) {
259: ConfigAttribute a = (ConfigAttribute) i.next();
260: set.add(a);
261: }
262:
263: return set;
264: }
265: }
|