SEC-113 Provide MethodInvocationUtils.

This commit is contained in:
Ben Alex 2005-11-25 04:17:25 +00:00
parent 863ba3f0a3
commit 731d7b2e89
14 changed files with 187 additions and 53 deletions

View File

@ -0,0 +1,134 @@
/* Copyright 2004, 2005 Acegi Technology Pty Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.acegisecurity.util;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.Assert;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* Static utility methods for creating <code>MethodInvocation</code>s usable
* within Acegi Security.
*
* <p>
* All methods of this class return a {@link
* org.acegisecurity.util.SimpleMethodInvocation}.
* </p>
*
* @author Ben Alex
* @version $Id$
*/
public class MethodInvocationUtils {
//~ Methods ================================================================
/**
* Generates a <code>MethodInvocation</code> for specified
* <code>methodName</code> on the passed object.
*
* @param object the object that will be used to find the relevant
* <code>Method</code>
* @param methodName the name of the method to find
*
* @return a <code>MethodInvocation</code>, or <code>null</code> if there
* was a problem
*/
public static MethodInvocation create(Object object, String methodName) {
return create(object, methodName, null);
}
/**
* Generates a <code>MethodInvocation</code> for specified
* <code>methodName</code> on the passed object, using the
* <code>args</code> to locate the method.
*
* @param object the object that will be used to find the relevant
* <code>Method</code>
* @param methodName the name of the method to find
* @param args arguments that are required as part of the method signature
*
* @return a <code>MethodInvocation</code>, or <code>null</code> if there
* was a problem
*/
public static MethodInvocation create(Object object, String methodName,
Object[] args) {
Assert.notNull(object, "Object required");
Class[] classArgs = null;
if (args != null) {
List list = new ArrayList();
for (int i = 0; i < args.length; i++) {
list.add(args[i].getClass());
}
classArgs = (Class[]) list.toArray();
}
return createFromClass(object.getClass(), methodName, classArgs);
}
/**
* Generates a <code>MethodInvocation</code> for specified
* <code>methodName</code> on the passed class.
*
* @param clazz the class of object that will be used to find the relevant
* <code>Method</code>
* @param methodName the name of the method to find
*
* @return a <code>MethodInvocation</code>, or <code>null</code> if there
* was a problem
*/
public static MethodInvocation createFromClass(Class clazz,
String methodName) {
return createFromClass(clazz, methodName);
}
/**
* Generates a <code>MethodInvocation</code> for specified
* <code>methodName</code> on the passed class, using the
* <code>args</code> to locate the method.
*
* @param clazz the class of object that will be used to find the relevant
* <code>Method</code>
* @param methodName the name of the method to find
* @param args arguments that are required as part of the method signature
*
* @return a <code>MethodInvocation</code>, or <code>null</code> if there
* was a problem
*/
public static MethodInvocation createFromClass(Class clazz,
String methodName, Class[] args) {
Assert.notNull(clazz, "Class required");
Assert.hasText(methodName, "MethodName required");
Method method;
try {
method = clazz.getMethod(methodName, args);
} catch (Exception e) {
return null;
}
return new SimpleMethodInvocation(method, args);
}
}

View File

@ -1,4 +1,4 @@
/* Copyright 2004 Acegi Technology Pty Limited
/* Copyright 2004, 2005 Acegi Technology Pty Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* limitations under the License.
*/
package org.acegisecurity;
package org.acegisecurity.util;
import org.aopalliance.intercept.MethodInvocation;
@ -22,12 +22,12 @@ import java.lang.reflect.Method;
/**
* Represents the AOP Alliance <code>MethodInvocation</code> secure object.
* Represents the AOP Alliance <code>MethodInvocation</code>.
*
* @author Ben Alex
* @version $Id$
*/
public class MockMethodInvocation implements MethodInvocation {
public class SimpleMethodInvocation implements MethodInvocation {
//~ Instance fields ========================================================
private Method method;
@ -35,12 +35,12 @@ public class MockMethodInvocation implements MethodInvocation {
//~ Constructors ===========================================================
public MockMethodInvocation(Method method, Object[] arguments) {
public SimpleMethodInvocation(Method method, Object[] arguments) {
this.method = method;
this.arguments = arguments;
}
public MockMethodInvocation() {}
public SimpleMethodInvocation() {}
//~ Methods ================================================================

View File

@ -21,9 +21,9 @@ import org.acegisecurity.AccessDeniedException;
import org.acegisecurity.Authentication;
import org.acegisecurity.ConfigAttribute;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.SecurityConfig;
import org.acegisecurity.intercept.web.FilterInvocation;
import org.acegisecurity.util.SimpleMethodInvocation;
import org.aopalliance.intercept.MethodInvocation;
@ -88,23 +88,23 @@ public class AfterInvocationProviderManagerTests extends TestCase {
attr4.addConfigAttribute(new SecurityConfig("NEVER_CAUSES_SWAP"));
assertEquals("swap1",
manager.decide(null, new MockMethodInvocation(), attr1,
manager.decide(null, new SimpleMethodInvocation(), attr1,
"content-before-swapping"));
assertEquals("swap2",
manager.decide(null, new MockMethodInvocation(), attr2,
manager.decide(null, new SimpleMethodInvocation(), attr2,
"content-before-swapping"));
assertEquals("swap3",
manager.decide(null, new MockMethodInvocation(), attr3,
manager.decide(null, new SimpleMethodInvocation(), attr3,
"content-before-swapping"));
assertEquals("content-before-swapping",
manager.decide(null, new MockMethodInvocation(), attr4,
manager.decide(null, new SimpleMethodInvocation(), attr4,
"content-before-swapping"));
assertEquals("swap3",
manager.decide(null, new MockMethodInvocation(), attr2and3,
manager.decide(null, new SimpleMethodInvocation(), attr2and3,
"content-before-swapping"));
}

View File

@ -20,13 +20,13 @@ import junit.framework.TestCase;
import org.acegisecurity.AuthorizationServiceException;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.MockAclManager;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.SecurityConfig;
import org.acegisecurity.acl.AclEntry;
import org.acegisecurity.acl.AclManager;
import org.acegisecurity.acl.basic.MockAclObjectIdentity;
import org.acegisecurity.acl.basic.SimpleAclEntry;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.util.SimpleMethodInvocation;
import java.util.List;
import java.util.Vector;
@ -88,7 +88,7 @@ public class BasicAclEntryAfterInvocationCollectionFilteringProviderTests
// Filter
List filteredList = (List) provider.decide(auth,
new MockMethodInvocation(), attr, list);
new SimpleMethodInvocation(), attr, list);
assertEquals(0, filteredList.size());
}
@ -124,7 +124,7 @@ public class BasicAclEntryAfterInvocationCollectionFilteringProviderTests
// Filter
List filteredList = (List) provider.decide(auth,
new MockMethodInvocation(), attr, list);
new SimpleMethodInvocation(), attr, list);
assertEquals(0, filteredList.size());
}
@ -161,7 +161,7 @@ public class BasicAclEntryAfterInvocationCollectionFilteringProviderTests
// Filter
List filteredList = (List) provider.decide(auth,
new MockMethodInvocation(), attr, list);
new SimpleMethodInvocation(), attr, list);
assertEquals(1, filteredList.size());
assertEquals("belmont", filteredList.get(0));
@ -199,7 +199,7 @@ public class BasicAclEntryAfterInvocationCollectionFilteringProviderTests
// Filter
String[] filteredList = (String[]) provider.decide(auth,
new MockMethodInvocation(), attr, list);
new SimpleMethodInvocation(), attr, list);
assertEquals(1, filteredList.length);
assertEquals("belmont", filteredList[0]);
@ -228,7 +228,7 @@ public class BasicAclEntryAfterInvocationCollectionFilteringProviderTests
// Filter
try {
provider.decide(auth, new MockMethodInvocation(), attr,
provider.decide(auth, new SimpleMethodInvocation(), attr,
new String("RETURN_OBJECT_NOT_COLLECTION"));
fail("Should have thrown AuthorizationServiceException");
} catch (AuthorizationServiceException expected) {
@ -259,7 +259,7 @@ public class BasicAclEntryAfterInvocationCollectionFilteringProviderTests
// Filter
List filteredList = (List) provider.decide(auth,
new MockMethodInvocation(), attr, null);
new SimpleMethodInvocation(), attr, null);
assertNull(filteredList);
}
@ -296,14 +296,14 @@ public class BasicAclEntryAfterInvocationCollectionFilteringProviderTests
// As no matching config attrib, ensure provider doesn't change list
assertEquals(4,
((List) provider.decide(auth, new MockMethodInvocation(), attr, list))
((List) provider.decide(auth, new SimpleMethodInvocation(), attr, list))
.size());
// Filter, this time with the conf attrib provider setup to answer
attr.addConfigAttribute(new SecurityConfig("AFTER_ACL_COLLECTION_ADMIN"));
List filteredList = (List) provider.decide(auth,
new MockMethodInvocation(), attr, list);
new SimpleMethodInvocation(), attr, list);
assertEquals(1, filteredList.size());
assertEquals("sydney", filteredList.get(0));
@ -341,7 +341,7 @@ public class BasicAclEntryAfterInvocationCollectionFilteringProviderTests
// Filter
List filteredList = (List) provider.decide(auth,
new MockMethodInvocation(), attr, list);
new SimpleMethodInvocation(), attr, list);
assertEquals(1, filteredList.size());
assertEquals("sydney", filteredList.get(0));

View File

@ -20,13 +20,13 @@ import junit.framework.TestCase;
import org.acegisecurity.AccessDeniedException;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.MockAclManager;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.SecurityConfig;
import org.acegisecurity.acl.AclEntry;
import org.acegisecurity.acl.AclManager;
import org.acegisecurity.acl.basic.MockAclObjectIdentity;
import org.acegisecurity.acl.basic.SimpleAclEntry;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.util.SimpleMethodInvocation;
/**
@ -75,7 +75,7 @@ public class BasicAclEntryAfterInvocationProviderTests extends TestCase {
attr.addConfigAttribute(new SecurityConfig("AFTER_ACL_READ"));
try {
provider.decide(auth, new MockMethodInvocation(), attr, "belmont");
provider.decide(auth, new SimpleMethodInvocation(), attr, "belmont");
fail("Should have thrown AccessDeniedException");
} catch (AccessDeniedException expected) {
assertTrue(true);
@ -104,7 +104,7 @@ public class BasicAclEntryAfterInvocationProviderTests extends TestCase {
attr.addConfigAttribute(new SecurityConfig("AFTER_ACL_READ"));
try {
provider.decide(auth, new MockMethodInvocation(), attr, "belmont");
provider.decide(auth, new SimpleMethodInvocation(), attr, "belmont");
fail("Should have thrown AccessDeniedException");
} catch (AccessDeniedException expected) {
assertTrue(true);
@ -135,7 +135,7 @@ public class BasicAclEntryAfterInvocationProviderTests extends TestCase {
// Filter
assertEquals("belmont",
provider.decide(auth, new MockMethodInvocation(), attr, "belmont"));
provider.decide(auth, new SimpleMethodInvocation(), attr, "belmont"));
}
public void testGrantsAccessIfReturnedObjectIsNull()
@ -160,7 +160,7 @@ public class BasicAclEntryAfterInvocationProviderTests extends TestCase {
attr.addConfigAttribute(new SecurityConfig("AFTER_ACL_READ"));
// Filter
assertNull(provider.decide(auth, new MockMethodInvocation(), attr, null));
assertNull(provider.decide(auth, new SimpleMethodInvocation(), attr, null));
}
public void testRespectsModificationsToProcessConfigAttribute()
@ -185,12 +185,12 @@ public class BasicAclEntryAfterInvocationProviderTests extends TestCase {
// As no matching config attrib, ensure provider returns original obj
assertEquals("sydney",
provider.decide(auth, new MockMethodInvocation(), attr, "sydney"));
provider.decide(auth, new SimpleMethodInvocation(), attr, "sydney"));
// Filter, this time with the conf attrib provider setup to answer
attr.addConfigAttribute(new SecurityConfig("AFTER_ACL_ADMIN"));
assertEquals("sydney",
provider.decide(auth, new MockMethodInvocation(), attr, "sydney"));
provider.decide(auth, new SimpleMethodInvocation(), attr, "sydney"));
}
public void testRespectsModificationsToRequirePermissions()
@ -217,7 +217,7 @@ public class BasicAclEntryAfterInvocationProviderTests extends TestCase {
// Filter
assertEquals("sydney",
provider.decide(auth, new MockMethodInvocation(), attr, "sydney"));
provider.decide(auth, new SimpleMethodInvocation(), attr, "sydney"));
}
public void testStartupDetectsMissingAclManager() throws Exception {

View File

@ -18,13 +18,13 @@ package org.acegisecurity.context.rmi;
import junit.framework.TestCase;
import org.acegisecurity.Authentication;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.TargetObject;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.context.SecurityContextImpl;
import org.acegisecurity.context.rmi.ContextPropagatingRemoteInvocation;
import org.acegisecurity.context.rmi.ContextPropagatingRemoteInvocationFactory;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.util.SimpleMethodInvocation;
import org.aopalliance.intercept.MethodInvocation;
@ -112,7 +112,7 @@ public class ContextPropagatingRemoteInvocationTests extends TestCase {
Class clazz = TargetObject.class;
Method method = clazz.getMethod("makeLowerCase",
new Class[] {String.class});
MethodInvocation mi = new MockMethodInvocation(method,
MethodInvocation mi = new SimpleMethodInvocation(method,
new Object[] {"SOME_STRING"});
ContextPropagatingRemoteInvocationFactory factory = new ContextPropagatingRemoteInvocationFactory();

View File

@ -19,7 +19,7 @@ import junit.framework.TestCase;
import org.acegisecurity.AuthenticationCredentialsNotFoundException;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.util.SimpleMethodInvocation;
/**
@ -56,7 +56,7 @@ public class AuthenticationCredentialsNotFoundEventTests extends TestCase {
}
try {
new AuthenticationCredentialsNotFoundEvent(new MockMethodInvocation(),
new AuthenticationCredentialsNotFoundEvent(new SimpleMethodInvocation(),
null, new AuthenticationCredentialsNotFoundException("test"));
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
@ -64,7 +64,7 @@ public class AuthenticationCredentialsNotFoundEventTests extends TestCase {
}
try {
new AuthenticationCredentialsNotFoundEvent(new MockMethodInvocation(),
new AuthenticationCredentialsNotFoundEvent(new SimpleMethodInvocation(),
new ConfigAttributeDefinition(), null);
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {

View File

@ -19,9 +19,9 @@ import junit.framework.TestCase;
import org.acegisecurity.AccessDeniedException;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.event.authorization.AuthorizationFailureEvent;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.util.SimpleMethodInvocation;
/**
@ -59,7 +59,7 @@ public class AuthorizationFailureEventTests extends TestCase {
}
try {
new AuthorizationFailureEvent(new MockMethodInvocation(), null,
new AuthorizationFailureEvent(new SimpleMethodInvocation(), null,
new UsernamePasswordAuthenticationToken("foo", "bar"),
new AccessDeniedException("error"));
fail("Should have thrown IllegalArgumentException");
@ -68,7 +68,7 @@ public class AuthorizationFailureEventTests extends TestCase {
}
try {
new AuthorizationFailureEvent(new MockMethodInvocation(),
new AuthorizationFailureEvent(new SimpleMethodInvocation(),
new ConfigAttributeDefinition(), null,
new AccessDeniedException("error"));
fail("Should have thrown IllegalArgumentException");
@ -77,7 +77,7 @@ public class AuthorizationFailureEventTests extends TestCase {
}
try {
new AuthorizationFailureEvent(new MockMethodInvocation(),
new AuthorizationFailureEvent(new SimpleMethodInvocation(),
new ConfigAttributeDefinition(),
new UsernamePasswordAuthenticationToken("foo", "bar"), null);
fail("Should have thrown IllegalArgumentException");

View File

@ -18,8 +18,8 @@ package org.acegisecurity.event.authorization;
import junit.framework.TestCase;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.util.SimpleMethodInvocation;
/**
@ -55,7 +55,7 @@ public class AuthorizedEventTests extends TestCase {
}
try {
new AuthorizedEvent(new MockMethodInvocation(), null,
new AuthorizedEvent(new SimpleMethodInvocation(), null,
new UsernamePasswordAuthenticationToken("foo", "bar"));
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
@ -63,7 +63,7 @@ public class AuthorizedEventTests extends TestCase {
}
try {
new AuthorizedEvent(new MockMethodInvocation(),
new AuthorizedEvent(new SimpleMethodInvocation(),
new ConfigAttributeDefinition(), null);
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {

View File

@ -20,9 +20,9 @@ import junit.framework.TestCase;
import org.acegisecurity.MockAccessDecisionManager;
import org.acegisecurity.MockAfterInvocationManager;
import org.acegisecurity.MockAuthenticationManager;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.MockRunAsManager;
import org.acegisecurity.intercept.method.MockMethodDefinitionSource;
import org.acegisecurity.util.SimpleMethodInvocation;
/**
@ -60,7 +60,7 @@ public class AbstractSecurityInterceptorTests extends TestCase {
si.setObjectDefinitionSource(new MockMethodDefinitionSource(false, true));
try {
si.beforeInvocation(new MockMethodInvocation());
si.beforeInvocation(new SimpleMethodInvocation());
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(expected.getMessage().startsWith("Security invocation attempted for object"));

View File

@ -18,9 +18,9 @@ package org.acegisecurity.intercept;
import junit.framework.TestCase;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.SecurityConfig;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.util.SimpleMethodInvocation;
import org.aopalliance.intercept.MethodInvocation;
@ -61,7 +61,7 @@ public class InterceptorStatusTokenTests extends TestCase {
ConfigAttributeDefinition attr = new ConfigAttributeDefinition();
attr.addConfigAttribute(new SecurityConfig("FOO"));
MethodInvocation mi = new MockMethodInvocation();
MethodInvocation mi = new SimpleMethodInvocation();
InterceptorStatusToken token = new InterceptorStatusToken(new UsernamePasswordAuthenticationToken(
"marissa", "koala"), true, attr, mi);

View File

@ -17,7 +17,7 @@ package org.acegisecurity.intercept.method;
import junit.framework.TestCase;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.util.SimpleMethodInvocation;
import org.aopalliance.intercept.MethodInvocation;
@ -85,7 +85,7 @@ public class AbstractMethodDefinitionSourceTests extends TestCase {
true);
try {
mds.getAttributes(new MockMethodInvocation());
mds.getAttributes(new SimpleMethodInvocation());
fail("Should have thrown UnsupportedOperationException");
} catch (UnsupportedOperationException expected) {
assertTrue(true);

View File

@ -22,13 +22,13 @@ import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.ITargetObject;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.OtherTargetObject;
import org.acegisecurity.SecurityConfig;
import org.acegisecurity.TargetObject;
import org.acegisecurity.acl.basic.SomeDomain;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.util.SimpleMethodInvocation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@ -209,7 +209,7 @@ public class MethodDefinitionAttributesTests extends TestCase {
MethodDefinitionAttributes source = new MethodDefinitionAttributes();
source.setAttributes(new MockAttributes());
ConfigAttributeDefinition config = source.getAttributes(new MockMethodInvocation() {
ConfigAttributeDefinition config = source.getAttributes(new SimpleMethodInvocation() {
public Method getMethod() {
return method;
}

View File

@ -20,13 +20,13 @@ import junit.framework.TestCase;
import org.acegisecurity.AuthorizationServiceException;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.MockAclManager;
import org.acegisecurity.MockMethodInvocation;
import org.acegisecurity.SecurityConfig;
import org.acegisecurity.acl.AclEntry;
import org.acegisecurity.acl.AclManager;
import org.acegisecurity.acl.basic.MockAclObjectIdentity;
import org.acegisecurity.acl.basic.SimpleAclEntry;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.util.SimpleMethodInvocation;
import org.aopalliance.intercept.MethodInvocation;
@ -444,7 +444,7 @@ public class BasicAclEntryVoterTests extends TestCase {
Class clazz = String.class;
Method method = clazz.getMethod("toString", new Class[] {});
MethodInvocation mi = new MockMethodInvocation(method,
MethodInvocation mi = new SimpleMethodInvocation(method,
new Object[] {domainObject});
try {
@ -462,7 +462,7 @@ public class BasicAclEntryVoterTests extends TestCase {
Method method = clazz.getMethod("someServiceMethod",
new Class[] {SomeDomainObject.class});
return new MockMethodInvocation(method, new Object[] {domainObject});
return new SimpleMethodInvocation(method, new Object[] {domainObject});
}
//~ Inner Classes ==========================================================