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.ConfigAttributeDefinition;
021: import org.acegisecurity.MockJoinPoint;
022: import org.acegisecurity.SecurityConfig;
023: import org.acegisecurity.TargetObject;
024:
025: import org.aopalliance.intercept.MethodInvocation;
026:
027: import java.lang.reflect.AccessibleObject;
028: import java.lang.reflect.Method;
029:
030: import java.util.Iterator;
031:
032: /**
033: * Tests {@link MethodDefinitionSourceEditor} and its asociated {@link MethodDefinitionMap}.
034: *
035: * @author Ben Alex
036: * @version $Id: MethodDefinitionSourceEditorTests.java 1496 2006-05-23 13:38:33Z benalex $
037: */
038: public class MethodDefinitionSourceEditorTests extends TestCase {
039: //~ Constructors ===================================================================================================
040:
041: public MethodDefinitionSourceEditorTests() {
042: super ();
043: }
044:
045: public MethodDefinitionSourceEditorTests(String arg0) {
046: super (arg0);
047: }
048:
049: //~ Methods ========================================================================================================
050:
051: public static void main(String[] args) {
052: junit.textui.TestRunner
053: .run(MethodDefinitionSourceEditorTests.class);
054: }
055:
056: public final void setUp() throws Exception {
057: super .setUp();
058: }
059:
060: public void testAspectJJointPointLookup() throws Exception {
061: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
062: editor
063: .setAsText("org.acegisecurity.TargetObject.countLength=ROLE_ONE,ROLE_TWO,RUN_AS_ENTRY");
064:
065: MethodDefinitionMap map = (MethodDefinitionMap) editor
066: .getValue();
067:
068: Class clazz = TargetObject.class;
069: Method method = clazz.getMethod("countLength",
070: new Class[] { String.class });
071: MockJoinPoint joinPoint = new MockJoinPoint(new TargetObject(),
072: method);
073:
074: ConfigAttributeDefinition returnedCountLength = map
075: .getAttributes(joinPoint);
076:
077: ConfigAttributeDefinition expectedCountLength = new ConfigAttributeDefinition();
078: expectedCountLength.addConfigAttribute(new SecurityConfig(
079: "ROLE_ONE"));
080: expectedCountLength.addConfigAttribute(new SecurityConfig(
081: "ROLE_TWO"));
082: expectedCountLength.addConfigAttribute(new SecurityConfig(
083: "RUN_AS_ENTRY"));
084: assertEquals(expectedCountLength, returnedCountLength);
085: }
086:
087: public void testClassNameNotFoundResultsInException() {
088: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
089:
090: try {
091: editor
092: .setAsText("org.acegisecurity.DOES_NOT_EXIST_NAME=FOO,BAR");
093: fail("Should have given IllegalArgumentException");
094: } catch (IllegalArgumentException expected) {
095: assertTrue(true);
096: }
097: }
098:
099: public void testClassNameNotInProperFormatResultsInException() {
100: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
101:
102: try {
103: editor.setAsText("DOES_NOT_EXIST_NAME=FOO,BAR");
104: fail("Should have given IllegalArgumentException");
105: } catch (IllegalArgumentException expected) {
106: assertTrue(true);
107: }
108: }
109:
110: public void testClassNameValidButMethodNameInvalidResultsInException() {
111: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
112:
113: try {
114: editor
115: .setAsText("org.acegisecurity.TargetObject.INVALID_METHOD=FOO,BAR");
116: fail("Should have given IllegalArgumentException");
117: } catch (IllegalArgumentException expected) {
118: assertTrue(true);
119: }
120: }
121:
122: public void testConcreteClassInvocationsAlsoReturnDefinitionsAgainstInterface()
123: throws Exception {
124: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
125: editor
126: .setAsText("org.acegisecurity.ITargetObject.makeLower*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.ITargetObject.makeUpper*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.TargetObject.makeUpper*=ROLE_FROM_IMPLEMENTATION");
127:
128: MethodDefinitionMap map = (MethodDefinitionMap) editor
129: .getValue();
130: assertEquals(3, map.getMethodMapSize());
131:
132: ConfigAttributeDefinition returnedMakeLower = map
133: .getAttributes(new MockMethodInvocation(
134: TargetObject.class, "makeLowerCase",
135: new Class[] { String.class }));
136: ConfigAttributeDefinition expectedMakeLower = new ConfigAttributeDefinition();
137: expectedMakeLower.addConfigAttribute(new SecurityConfig(
138: "ROLE_FROM_INTERFACE"));
139: assertEquals(expectedMakeLower, returnedMakeLower);
140:
141: ConfigAttributeDefinition returnedMakeUpper = map
142: .getAttributes(new MockMethodInvocation(
143: TargetObject.class, "makeUpperCase",
144: new Class[] { String.class }));
145: ConfigAttributeDefinition expectedMakeUpper = new ConfigAttributeDefinition();
146: expectedMakeUpper.addConfigAttribute(new SecurityConfig(
147: "ROLE_FROM_IMPLEMENTATION"));
148: expectedMakeUpper.addConfigAttribute(new SecurityConfig(
149: "ROLE_FROM_INTERFACE"));
150: assertEquals(expectedMakeUpper, returnedMakeUpper);
151: }
152:
153: public void testEmptyStringReturnsEmptyMap() {
154: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
155: editor.setAsText("");
156:
157: MethodDefinitionMap map = (MethodDefinitionMap) editor
158: .getValue();
159: assertEquals(0, map.getMethodMapSize());
160: }
161:
162: public void testIterator() {
163: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
164: editor
165: .setAsText("org.acegisecurity.TargetObject.countLength=ROLE_ONE,ROLE_TWO,RUN_AS_ENTRY\r\norg.acegisecurity.TargetObject.make*=ROLE_NINE,ROLE_SUPERVISOR");
166:
167: MethodDefinitionMap map = (MethodDefinitionMap) editor
168: .getValue();
169: Iterator iter = map.getConfigAttributeDefinitions();
170: int counter = 0;
171:
172: while (iter.hasNext()) {
173: iter.next();
174: counter++;
175: }
176:
177: assertEquals(3, counter);
178: }
179:
180: public void testMultiMethodParsing() {
181: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
182: editor
183: .setAsText("org.acegisecurity.TargetObject.countLength=ROLE_ONE,ROLE_TWO,RUN_AS_ENTRY\r\norg.acegisecurity.TargetObject.make*=ROLE_NINE,ROLE_SUPERVISOR");
184:
185: MethodDefinitionMap map = (MethodDefinitionMap) editor
186: .getValue();
187: assertEquals(3, map.getMethodMapSize());
188: }
189:
190: public void testMultiMethodParsingWhereLaterMethodsOverrideEarlierMethods()
191: throws Exception {
192: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
193: editor
194: .setAsText("org.acegisecurity.TargetObject.*=ROLE_GENERAL\r\norg.acegisecurity.TargetObject.makeLower*=ROLE_LOWER\r\norg.acegisecurity.TargetObject.make*=ROLE_MAKE\r\norg.acegisecurity.TargetObject.makeUpper*=ROLE_UPPER");
195:
196: MethodDefinitionMap map = (MethodDefinitionMap) editor
197: .getValue();
198: assertEquals(5, map.getMethodMapSize());
199:
200: ConfigAttributeDefinition returnedMakeLower = map
201: .getAttributes(new MockMethodInvocation(
202: TargetObject.class, "makeLowerCase",
203: new Class[] { String.class }));
204: ConfigAttributeDefinition expectedMakeLower = new ConfigAttributeDefinition();
205: expectedMakeLower.addConfigAttribute(new SecurityConfig(
206: "ROLE_LOWER"));
207: assertEquals(expectedMakeLower, returnedMakeLower);
208:
209: ConfigAttributeDefinition returnedMakeUpper = map
210: .getAttributes(new MockMethodInvocation(
211: TargetObject.class, "makeUpperCase",
212: new Class[] { String.class }));
213: ConfigAttributeDefinition expectedMakeUpper = new ConfigAttributeDefinition();
214: expectedMakeUpper.addConfigAttribute(new SecurityConfig(
215: "ROLE_UPPER"));
216: assertEquals(expectedMakeUpper, returnedMakeUpper);
217:
218: ConfigAttributeDefinition returnedCountLength = map
219: .getAttributes(new MockMethodInvocation(
220: TargetObject.class, "countLength",
221: new Class[] { String.class }));
222: ConfigAttributeDefinition expectedCountLength = new ConfigAttributeDefinition();
223: expectedCountLength.addConfigAttribute(new SecurityConfig(
224: "ROLE_GENERAL"));
225: assertEquals(expectedCountLength, returnedCountLength);
226: }
227:
228: public void testNullIsReturnedByMethodDefinitionSourceWhenMethodInvocationNotDefined()
229: throws Exception {
230: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
231: editor
232: .setAsText("org.acegisecurity.TargetObject.countLength=ROLE_ONE,ROLE_TWO,RUN_AS_ENTRY");
233:
234: MethodDefinitionMap map = (MethodDefinitionMap) editor
235: .getValue();
236:
237: ConfigAttributeDefinition configAttributeDefinition = map
238: .getAttributes(new MockMethodInvocation(
239: TargetObject.class, "makeLowerCase",
240: new Class[] { String.class }));
241: assertNull(configAttributeDefinition);
242: }
243:
244: public void testNullReturnsEmptyMap() {
245: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
246: editor.setAsText(null);
247:
248: MethodDefinitionMap map = (MethodDefinitionMap) editor
249: .getValue();
250: assertEquals(0, map.getMethodMapSize());
251: }
252:
253: public void testSingleMethodParsing() throws Exception {
254: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
255: editor
256: .setAsText("org.acegisecurity.TargetObject.countLength=ROLE_ONE,ROLE_TWO,RUN_AS_ENTRY");
257:
258: MethodDefinitionMap map = (MethodDefinitionMap) editor
259: .getValue();
260:
261: ConfigAttributeDefinition returnedCountLength = map
262: .getAttributes(new MockMethodInvocation(
263: TargetObject.class, "countLength",
264: new Class[] { String.class }));
265: ConfigAttributeDefinition expectedCountLength = new ConfigAttributeDefinition();
266: expectedCountLength.addConfigAttribute(new SecurityConfig(
267: "ROLE_ONE"));
268: expectedCountLength.addConfigAttribute(new SecurityConfig(
269: "ROLE_TWO"));
270: expectedCountLength.addConfigAttribute(new SecurityConfig(
271: "RUN_AS_ENTRY"));
272: assertEquals(expectedCountLength, returnedCountLength);
273: }
274:
275: //~ Inner Classes ==================================================================================================
276:
277: private class MockMethodInvocation implements MethodInvocation {
278: Method method;
279:
280: private MockMethodInvocation() {
281: super ();
282: }
283:
284: public MockMethodInvocation(Class clazz, String methodName,
285: Class[] parameterTypes) throws NoSuchMethodException {
286: method = clazz.getMethod(methodName, parameterTypes);
287: }
288:
289: public Object[] getArguments() {
290: return null;
291: }
292:
293: public Method getMethod() {
294: return method;
295: }
296:
297: public AccessibleObject getStaticPart() {
298: return null;
299: }
300:
301: public Object getThis() {
302: return null;
303: }
304:
305: public Object proceed() throws Throwable {
306: return null;
307: }
308: }
309: }
|