SEC-999: First commit of expression-based authorization implementation

This commit is contained in:
Luke Taylor
2008-10-24 00:38:36 +00:00
parent 0dd82cb91a
commit 4aa32f7d06
45 changed files with 1213 additions and 569 deletions

View File

@@ -15,9 +15,13 @@
package org.springframework.security.annotation;
import java.util.List;
import javax.annotation.security.RolesAllowed;
import javax.annotation.security.PermitAll;
import org.springframework.security.expression.annotation.PreAuthorize;
/**
* @version $Id$
*/
@@ -28,6 +32,7 @@ public interface BusinessService {
@Secured({"ROLE_ADMIN"})
@RolesAllowed({"ROLE_ADMIN"})
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void someAdminMethod();
@Secured({"ROLE_USER", "ROLE_ADMIN"})
@@ -45,4 +50,11 @@ public interface BusinessService {
public int someOther(String s);
public int someOther(int input);
public List methodReturningAList(List someList);
public Object[] methodReturningAnArray(Object[] someArray);
public List methodReturningAList(String userName, String extraParam);
}

View File

@@ -1,5 +1,8 @@
package org.springframework.security.annotation;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Joe Scalise
@@ -33,4 +36,17 @@ public class BusinessServiceImpl<E extends Entity> implements BusinessService {
public int someOther(int input) {
return input;
}
public List methodReturningAList(List someList) {
return someList;
}
public List methodReturningAList(String userName, String arg2) {
return new ArrayList();
}
public Object[] methodReturningAnArray(Object[] someArray) {
return null;
}
}

View File

@@ -1,5 +1,8 @@
package org.springframework.security.annotation;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.security.RolesAllowed;
import javax.annotation.security.PermitAll;
@@ -34,4 +37,17 @@ public class Jsr250BusinessServiceImpl implements BusinessService {
public int someOther(int input) {
return input;
}
public List methodReturningAList(List someList) {
return someList;
}
public List methodReturningAList(String userName, String arg2) {
return new ArrayList();
}
public Object[] methodReturningAnArray(Object[] someArray) {
return null;
}
}

View File

@@ -2,6 +2,8 @@ package org.springframework.security.annotation;
import static org.junit.Assert.assertEquals;
import java.util.List;
import javax.annotation.security.DenyAll;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
@@ -9,6 +11,7 @@ import javax.annotation.security.RolesAllowed;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.security.ConfigAttribute;
import org.springframework.security.ConfigAttributeDefinition;
/**
@@ -17,56 +20,56 @@ import org.springframework.security.ConfigAttributeDefinition;
* @version $Id$
*/
public class Jsr250MethodDefinitionSourceTests {
Jsr250MethodDefinitionSource mds = new Jsr250MethodDefinitionSource();
Jsr250MethodDefinitionSource mds = new Jsr250MethodDefinitionSource();
A a = new A();
UserAllowedClass userAllowed = new UserAllowedClass();
DenyAllClass denyAll = new DenyAllClass();
@Test
public void methodWithRolesAllowedHasCorrectAttribute() throws Exception {
ConfigAttributeDefinition accessAttributes = mds.findAttributes(a.getClass().getMethod("adminMethod"), null);
assertEquals(1, accessAttributes.getConfigAttributes().size());
assertEquals("ADMIN", accessAttributes.getConfigAttributes().iterator().next().toString());
List<ConfigAttribute> accessAttributes = mds.findAttributes(a.getClass().getMethod("adminMethod"), null);
assertEquals(1, accessAttributes.size());
assertEquals("ADMIN", accessAttributes.get(0).toString());
}
@Test
public void permitAllMethodHasPermitAllAttribute() throws Exception {
ConfigAttributeDefinition accessAttributes = mds.findAttributes(a.getClass().getMethod("permitAllMethod"), null);
assertEquals(1, accessAttributes.getConfigAttributes().size());
assertEquals("javax.annotation.security.PermitAll", accessAttributes.getConfigAttributes().iterator().next().toString());
List<ConfigAttribute> accessAttributes = mds.findAttributes(a.getClass().getMethod("permitAllMethod"), null);
assertEquals(1, accessAttributes.size());
assertEquals("javax.annotation.security.PermitAll", accessAttributes.get(0).toString());
}
@Test
public void noRoleMethodHasDenyAllAttributeWithDenyAllClass() throws Exception {
ConfigAttributeDefinition accessAttributes = mds.findAttributes(denyAll.getClass());
assertEquals(1, accessAttributes.getConfigAttributes().size());
assertEquals("javax.annotation.security.DenyAll", accessAttributes.getConfigAttributes().iterator().next().toString());
List<ConfigAttribute> accessAttributes = mds.findAttributes(denyAll.getClass());
assertEquals(1, accessAttributes.size());
assertEquals("javax.annotation.security.DenyAll", accessAttributes.get(0).toString());
}
@Test
public void adminMethodHasAdminAttributeWithDenyAllClass() throws Exception {
ConfigAttributeDefinition accessAttributes = mds.findAttributes(denyAll.getClass().getMethod("adminMethod"), null);
assertEquals(1, accessAttributes.getConfigAttributes().size());
assertEquals("ADMIN", accessAttributes.getConfigAttributes().iterator().next().toString());
List<ConfigAttribute> accessAttributes = mds.findAttributes(denyAll.getClass().getMethod("adminMethod"), null);
assertEquals(1, accessAttributes.size());
assertEquals("ADMIN", accessAttributes.get(0).toString());
}
@Test
public void noRoleMethodHasNoAttributes() throws Exception {
ConfigAttributeDefinition accessAttributes = mds.findAttributes(a.getClass().getMethod("noRoleMethod"), null);
List<ConfigAttribute> accessAttributes = mds.findAttributes(a.getClass().getMethod("noRoleMethod"), null);
Assert.assertNull(accessAttributes);
}
@Test
public void classRoleIsAppliedToNoRoleMethod() throws Exception {
ConfigAttributeDefinition accessAttributes = mds.findAttributes(userAllowed.getClass().getMethod("noRoleMethod"), null);
List<ConfigAttribute> accessAttributes = mds.findAttributes(userAllowed.getClass().getMethod("noRoleMethod"), null);
Assert.assertNull(accessAttributes);
}
@Test
public void methodRoleOverridesClassRole() throws Exception {
ConfigAttributeDefinition accessAttributes = mds.findAttributes(userAllowed.getClass().getMethod("adminMethod"), null);
assertEquals(1, accessAttributes.getConfigAttributes().size());
assertEquals("ADMIN", accessAttributes.getConfigAttributes().iterator().next().toString());
List<ConfigAttribute> accessAttributes = mds.findAttributes(userAllowed.getClass().getMethod("adminMethod"), null);
assertEquals(1, accessAttributes.size());
assertEquals("ADMIN", accessAttributes.get(0).toString());
}
//~ Inner Classes ======================================================================================================
@@ -87,7 +90,7 @@ public class Jsr250MethodDefinitionSourceTests {
public void noRoleMethod() {}
@RolesAllowed("ADMIN")
public void adminMethod() {}
public void adminMethod() {}
}
@DenyAll
@@ -96,7 +99,7 @@ public class Jsr250MethodDefinitionSourceTests {
public void noRoleMethod() {}
@RolesAllowed("ADMIN")
public void adminMethod() {}
public void adminMethod() {}
}

View File

@@ -15,11 +15,13 @@
package org.springframework.security.annotation;
import java.lang.reflect.Method;
import java.util.List;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.ConfigAttribute;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.SecurityConfig;
import org.springframework.util.StringUtils;
@@ -50,22 +52,19 @@ public class SecuredMethodDefinitionSourceTests extends TestCase {
fail("Should be a superMethod called 'someUserMethod3' on class!");
}
ConfigAttributeDefinition attrs = this.mds.findAttributes(method, DepartmentServiceImpl.class);
List<ConfigAttribute> attrs = mds.findAttributes(method, DepartmentServiceImpl.class);
assertNotNull(attrs);
if (logger.isDebugEnabled()) {
logger.debug("attrs: " + StringUtils.collectionToCommaDelimitedString(attrs.getConfigAttributes()));
logger.debug("attrs: " + StringUtils.collectionToCommaDelimitedString(attrs));
}
// expect 1 attribute
assertTrue("Did not find 1 attribute", attrs.getConfigAttributes().size() == 1);
assertTrue("Did not find 1 attribute", attrs.size() == 1);
// should have 1 SecurityConfig
for (Object obj : attrs.getConfigAttributes()) {
assertTrue(obj instanceof SecurityConfig);
SecurityConfig sc = (SecurityConfig) obj;
for (ConfigAttribute sc : attrs) {
assertEquals("Found an incorrect role", "ROLE_ADMIN", sc.getAttribute());
}
@@ -77,37 +76,35 @@ public class SecuredMethodDefinitionSourceTests extends TestCase {
fail("Should be a superMethod called 'someUserMethod3' on class!");
}
ConfigAttributeDefinition superAttrs = this.mds.findAttributes(superMethod, DepartmentServiceImpl.class);
List<ConfigAttribute> superAttrs = this.mds.findAttributes(superMethod, DepartmentServiceImpl.class);
assertNotNull(superAttrs);
if (logger.isDebugEnabled()) {
logger.debug("superAttrs: " + StringUtils.collectionToCommaDelimitedString(superAttrs.getConfigAttributes()));
logger.debug("superAttrs: " + StringUtils.collectionToCommaDelimitedString(superAttrs));
}
// This part of the test relates to SEC-274
// expect 1 attribute
assertTrue("Did not find 1 attribute", superAttrs.getConfigAttributes().size() == 1);
assertEquals("Did not find 1 attribute", 1, superAttrs.size());
// should have 1 SecurityConfig
for (Object obj : superAttrs.getConfigAttributes()) {
assertTrue(obj instanceof SecurityConfig);
SecurityConfig sc = (SecurityConfig) obj;
for (ConfigAttribute sc : superAttrs) {
assertEquals("Found an incorrect role", "ROLE_ADMIN", sc.getAttribute());
}
}
public void testGetAttributesClass() {
ConfigAttributeDefinition attrs = this.mds.findAttributes(BusinessService.class);
List<ConfigAttribute> attrs = this.mds.findAttributes(BusinessService.class);
assertNotNull(attrs);
// expect 1 annotation
assertTrue(attrs.getConfigAttributes().size() == 1);
assertEquals(1, attrs.size());
// should have 1 SecurityConfig
SecurityConfig sc = (SecurityConfig) attrs.getConfigAttributes().iterator().next();
SecurityConfig sc = ((SecurityConfig) attrs.get(0));
assertTrue(sc.getAttribute().equals("ROLE_USER"));
assertEquals("ROLE_USER", sc.getAttribute());
}
public void testGetAttributesMethod() {
@@ -119,21 +116,19 @@ public class SecuredMethodDefinitionSourceTests extends TestCase {
fail("Should be a method called 'someUserAndAdminMethod' on class!");
}
ConfigAttributeDefinition attrs = this.mds.findAttributes(method, BusinessService.class);
List<ConfigAttribute> attrs = this.mds.findAttributes(method, BusinessService.class);
assertNotNull(attrs);
// expect 2 attributes
assertTrue(attrs.getConfigAttributes().size() == 2);
assertEquals(2, attrs.size());
boolean user = false;
boolean admin = false;
// should have 2 SecurityConfigs
for (Object obj : attrs.getConfigAttributes()) {
assertTrue(obj instanceof SecurityConfig);
SecurityConfig sc = (SecurityConfig) obj;
for (ConfigAttribute sc : attrs) {
assertTrue(sc instanceof SecurityConfig);
if (sc.getAttribute().equals("ROLE_USER")) {
user = true;
@@ -145,5 +140,5 @@ public class SecuredMethodDefinitionSourceTests extends TestCase {
// expect to have ROLE_USER and ROLE_ADMIN
assertTrue(user && admin);
}
}

View File

@@ -33,8 +33,8 @@ public class CustomAfterInvocationProviderBeanDefinitionDecoratorTests {
MethodSecurityInterceptor msi = (MethodSecurityInterceptor) appContext.getBean(BeanIds.METHOD_SECURITY_INTERCEPTOR);
AfterInvocationProviderManager apm = (AfterInvocationProviderManager) msi.getAfterInvocationManager();
assertNotNull(apm);
assertEquals(1, apm.getProviders().size());
assertTrue(apm.getProviders().get(0) instanceof MockAfterInvocationProvider);
assertEquals(2, apm.getProviders().size());
assertTrue(apm.getProviders().get(1) instanceof MockAfterInvocationProvider);
}
private void setContext(String context) {

View File

@@ -3,6 +3,9 @@ package org.springframework.security.config;
import static org.junit.Assert.*;
import static org.springframework.security.config.ConfigTestUtils.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
@@ -180,6 +183,50 @@ public class GlobalMethodSecurityBeanDefinitionParserTests {
target.someUserMethod1();
}
@Test(expected=AccessDeniedException.class)
public void accessIsDeniedForHasRoleExpression() {
setContext(
"<global-method-security spel-annotations='enabled'/>" +
"<b:bean id='target' class='org.springframework.security.annotation.ExpressionProtectedBusinessServiceImpl'/>" +
AUTH_PROVIDER_XML);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("bob","bobspassword"));
target = (BusinessService) appContext.getBean("target");
target.someAdminMethod();
}
@Test
public void preAndPostFilterAnnotationsWorkWithLists() {
setContext(
"<global-method-security spel-annotations='enabled'/>" +
"<b:bean id='target' class='org.springframework.security.annotation.ExpressionProtectedBusinessServiceImpl'/>" +
AUTH_PROVIDER_XML);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("bob","bobspassword"));
target = (BusinessService) appContext.getBean("target");
List arg = new ArrayList();
arg.add("joe");
arg.add("bob");
arg.add("sam");
List result = target.methodReturningAList(arg);
// Expression is (filterObject == name or filterObject == 'sam'), so "joe" should be gone after pre-filter
// PostFilter should remove sam from the return object
assertEquals(1, result.size());
assertEquals("bob", result.get(0));
}
@Test
public void preAndPostFilterAnnotationsWorkWithArrays() {
setContext(
"<global-method-security spel-annotations='enabled'/>" +
"<b:bean id='target' class='org.springframework.security.annotation.ExpressionProtectedBusinessServiceImpl'/>" +
AUTH_PROVIDER_XML);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("bob","bobspassword"));
target = (BusinessService) appContext.getBean("target");
Object[] arg = new String[] {"joe", "bob", "sam"};
Object[] result = target.methodReturningAnArray(arg);
assertEquals(1, result.length);
assertEquals("bob", result[0]);
}
private void setContext(String context) {
appContext = new InMemoryXmlApplicationContext(context);
}

View File

@@ -0,0 +1,71 @@
package org.springframework.security.expression.support;
import static org.junit.Assert.assertEquals;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.annotation.ExpressionProtectedBusinessServiceImpl;
import org.springframework.security.expression.support.AbstractExpressionBasedMethodConfigAttribute;
import org.springframework.security.expression.support.MethodExpressionVoter;
import org.springframework.security.providers.TestingAuthenticationToken;
import org.springframework.security.util.SimpleMethodInvocation;
import org.springframework.security.vote.AccessDecisionVoter;
public class MethodExpressionVoterTests {
private TestingAuthenticationToken joe = new TestingAuthenticationToken("joe", "joespass", "blah");
private MethodInvocation miStringArgs;
private MethodInvocation miListArg;
private List listArg;
@Before
public void setUp() throws Exception {
Method m = ExpressionProtectedBusinessServiceImpl.class.getMethod("methodReturningAList",
String.class, String.class);
miStringArgs = new SimpleMethodInvocation(new Object(), m, new String[] {"joe", "arg2Value"});
m = ExpressionProtectedBusinessServiceImpl.class.getMethod("methodReturningAList", List.class);
listArg = new ArrayList(Arrays.asList("joe", "bob"));
miListArg = new SimpleMethodInvocation(new Object(), m, new Object[] {listArg});
}
@Test
public void hasRoleExpressionAllowsUserWithRole() throws Exception {
MethodExpressionVoter am = new MethodExpressionVoter();
ConfigAttributeDefinition cad = new ConfigAttributeDefinition(new PreInvocationExpressionBasedMethodConfigAttribute(null, null, "hasRole('blah')"));
assertEquals(AccessDecisionVoter.ACCESS_GRANTED, am.vote(joe, miStringArgs, cad));
}
@Test
public void hasRoleExpressionDeniesUserWithoutRole() throws Exception {
MethodExpressionVoter am = new MethodExpressionVoter();
ConfigAttributeDefinition cad = new ConfigAttributeDefinition(new PreInvocationExpressionBasedMethodConfigAttribute(null, null, "hasRole('joedoesnt')"));
assertEquals(AccessDecisionVoter.ACCESS_DENIED, am.vote(joe, miStringArgs, cad));
}
@Test
public void matchingArgAgainstAuthenticationNameIsSuccessful() throws Exception {
MethodExpressionVoter am = new MethodExpressionVoter();
ConfigAttributeDefinition cad = new ConfigAttributeDefinition(new PreInvocationExpressionBasedMethodConfigAttribute(null, null, "(#userName == name) and (name == 'joe')"));
assertEquals(AccessDecisionVoter.ACCESS_GRANTED, am.vote(joe, miStringArgs, cad));
}
@Test
public void accessIsGrantedIfNoPreAuthorizeAttributeIsUsed() throws Exception {
MethodExpressionVoter am = new MethodExpressionVoter();
ConfigAttributeDefinition cad = new ConfigAttributeDefinition(new PreInvocationExpressionBasedMethodConfigAttribute("(name == 'jim')", "someList", null));
assertEquals(AccessDecisionVoter.ACCESS_GRANTED, am.vote(joe, miListArg, cad));
// All objects should have been removed, because the expression is always false
assertEquals(0, listArg.size());
}
}

View File

@@ -1,94 +0,0 @@
/* Copyright 2004, 2005, 2006 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.springframework.security.intercept.method;
import junit.framework.TestCase;
import org.springframework.security.util.SimpleMethodInvocation;
import org.aopalliance.intercept.MethodInvocation;
/**
* Tests {@link AbstractMethodDefinitionSource} and associated {@link ConfigAttributeDefinition}.
*
* @author Ben Alex
* @version $Id$
*/
public class AbstractMethodDefinitionSourceTests extends TestCase {
//~ Constructors ===================================================================================================
public AbstractMethodDefinitionSourceTests() {
super();
}
public AbstractMethodDefinitionSourceTests(String arg0) {
super(arg0);
}
//~ Methods ========================================================================================================
public static void main(String[] args) {
junit.textui.TestRunner.run(AbstractMethodDefinitionSourceTests.class);
}
public final void setUp() throws Exception {
super.setUp();
}
public void testDoesNotSupportAnotherObject() {
MockMethodDefinitionSource mds = new MockMethodDefinitionSource(false, true);
assertFalse(mds.supports(String.class));
}
public void testGetAttributesForANonMethodInvocation() {
MockMethodDefinitionSource mds = new MockMethodDefinitionSource(false, true);
try {
mds.getAttributes(new String());
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
public void testGetAttributesForANullObject() {
MockMethodDefinitionSource mds = new MockMethodDefinitionSource(false, true);
try {
mds.getAttributes(null);
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
public void testGetAttributesForMethodInvocation() {
MockMethodDefinitionSource mds = new MockMethodDefinitionSource(false, true);
try {
mds.getAttributes(new SimpleMethodInvocation());
fail("Should have thrown UnsupportedOperationException");
} catch (UnsupportedOperationException expected) {
assertTrue(true);
}
}
public void testSupportsMethodInvocation() {
MockMethodDefinitionSource mds = new MockMethodDefinitionSource(false, true);
assertTrue(mds.supports(MethodInvocation.class));
}
}

View File

@@ -3,10 +3,13 @@ package org.springframework.security.intercept.method;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.ConfigAttribute;
import org.springframework.security.SecurityConfig;
/**
* Tests for {@link MapBasedMethodDefinitionSource}.
@@ -15,8 +18,8 @@ import org.springframework.security.ConfigAttributeDefinition;
* @since 2.0.4
*/
public class MapBasedMethodDefinitionSourceTests {
private final ConfigAttributeDefinition ROLE_A = new ConfigAttributeDefinition("ROLE_A");
private final ConfigAttributeDefinition ROLE_B = new ConfigAttributeDefinition("ROLE_B");
private final List<? extends ConfigAttribute> ROLE_A = Arrays.asList(new SecurityConfig("ROLE_A"));
private final List<? extends ConfigAttribute> ROLE_B = Arrays.asList(new SecurityConfig("ROLE_B"));
private MapBasedMethodDefinitionSource mds;
private Method someMethodString;
private Method someMethodInteger;
@@ -32,7 +35,7 @@ public class MapBasedMethodDefinitionSourceTests {
public void wildcardedMatchIsOverwrittenByMoreSpecificMatch() {
mds.addSecureMethod(MockService.class, "some*", ROLE_A);
mds.addSecureMethod(MockService.class, "someMethod*", ROLE_B);
assertEquals(ROLE_B, mds.getAttributes(someMethodInteger, MockService.class));
assertEquals(ROLE_B, mds.getAttributes(someMethodInteger, MockService.class).getConfigAttributes());
}
@Test
@@ -40,8 +43,8 @@ public class MapBasedMethodDefinitionSourceTests {
mds.addSecureMethod(MockService.class, someMethodInteger, ROLE_A);
mds.addSecureMethod(MockService.class, someMethodString, ROLE_B);
assertEquals(ROLE_A, mds.getAttributes(someMethodInteger, MockService.class));
assertEquals(ROLE_B, mds.getAttributes(someMethodString, MockService.class));
assertEquals(ROLE_A, mds.getAttributes(someMethodInteger, MockService.class).getConfigAttributes());
assertEquals(ROLE_B, mds.getAttributes(someMethodString, MockService.class).getConfigAttributes());
}
private class MockService {

View File

@@ -1,57 +0,0 @@
/* Copyright 2004, 2005, 2006 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.springframework.security.intercept.method;
import java.lang.reflect.Method;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.ITargetObject;
/**
* Tests {@link MethodDefinitionAttributes}.
*
* @author Cameron Braid
* @author Ben Alex
* @version $Id$
*/
public class MethodDefinitionAttributesTests {
private MethodDefinitionAttributes build() {
MethodDefinitionAttributes mda = new MethodDefinitionAttributes();
mda.setAttributes(new MockAttributes());
return mda;
}
@Test
public void testMethodsReturned() throws Exception {
Class clazz = ITargetObject.class;
Method method = clazz.getMethod("countLength", new Class[] {String.class});
ConfigAttributeDefinition result = build().findAttributes(method, ITargetObject.class);
Assert.assertEquals(1, result.getConfigAttributes().size());
}
@Test
public void testClassesReturned() throws Exception {
Class clazz = ITargetObject.class;
ConfigAttributeDefinition result = build().findAttributes(ITargetObject.class);
Assert.assertEquals(1, result.getConfigAttributes().size());
}
}

View File

@@ -15,6 +15,8 @@
package org.springframework.security.intercept.method;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.JoinPoint;
import org.springframework.security.ConfigAttributeDefinition;
import java.lang.reflect.Method;
@@ -29,7 +31,7 @@ import java.util.Collection;
* @author Ben Alex
* @version $Id$
*/
public class MockMethodDefinitionSource extends AbstractMethodDefinitionSource {
public class MockMethodDefinitionSource implements MethodDefinitionSource {
//~ Instance fields ================================================================================================
private List list;
@@ -65,14 +67,19 @@ public class MockMethodDefinitionSource extends AbstractMethodDefinitionSource {
return list;
} else {
return null;
}
}
}
protected ConfigAttributeDefinition lookupAttributes(Method method) {
public ConfigAttributeDefinition getAttributes(Object object) throws IllegalArgumentException {
throw new UnsupportedOperationException("mock method not implemented");
}
public ConfigAttributeDefinition getAttributes(Method method, Class targetClass) {
public ConfigAttributeDefinition getAttributes(Method method, Class targetClass) {
throw new UnsupportedOperationException("mock method not implemented");
}
}
public boolean supports(Class clazz) {
return (MethodInvocation.class.isAssignableFrom(clazz) || JoinPoint.class.isAssignableFrom(clazz));
}
}