From 3fe112f769cf3be634cfdf383e647241969785f7 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Mon, 1 Dec 2008 14:28:24 +0000 Subject: [PATCH] Added tests for AbstractAclVoter. --- .../security/vote/AbstractAclVoterTests.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 core/src/test/java/org/springframework/security/vote/AbstractAclVoterTests.java diff --git a/core/src/test/java/org/springframework/security/vote/AbstractAclVoterTests.java b/core/src/test/java/org/springframework/security/vote/AbstractAclVoterTests.java new file mode 100644 index 0000000000..bc9ed4f338 --- /dev/null +++ b/core/src/test/java/org/springframework/security/vote/AbstractAclVoterTests.java @@ -0,0 +1,74 @@ +package org.springframework.security.vote; + +import static org.junit.Assert.*; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +import org.aopalliance.intercept.MethodInvocation; +import org.aspectj.lang.JoinPoint; +import org.junit.Test; +import org.springframework.security.Authentication; +import org.springframework.security.ConfigAttribute; +import org.springframework.security.MockJoinPoint; +import org.springframework.security.TargetObject; +import org.springframework.security.util.MethodInvocationUtils; + +/** + * + * @author Luke Taylor + * @version $Id$ + */ +public class AbstractAclVoterTests { + private AbstractAclVoter voter = new AbstractAclVoter() { + public boolean supports(ConfigAttribute attribute) { + return false; + } + public int vote(Authentication authentication, Object object, List attributes) { + return 0; + } + }; + + @Test + public void supportsMethodInvocationsAndJoinPoints() throws Exception { + assertTrue(voter.supports(MethodInvocation.class)); + assertTrue(voter.supports(JoinPoint.class)); + assertFalse(voter.supports(String.class)); + } + + @Test + public void expectedDomainObjectArgumentIsReturnedFromMethodInvocation() throws Exception { + voter.setProcessDomainObjectClass(String.class); + MethodInvocation mi = MethodInvocationUtils.create(new TestClass(), "methodTakingAString", "The Argument"); + assertEquals("The Argument", voter.getDomainObjectInstance(mi)); + } + + @Test + public void expectedDomainObjectArgumentIsReturnedFromJoinPoint() throws Exception { + voter.setProcessDomainObjectClass(String.class); + Method method = TestClass.class.getMethod("methodTakingAString", new Class[] {String.class}); + MockJoinPoint joinPoint = new MockJoinPoint(new TestClass(), method, "The Argument"); + assertEquals("The Argument", voter.getDomainObjectInstance(joinPoint)); + } + + @Test + public void correctArgumentIsSelectedFromMultipleArgs() throws Exception { + voter.setProcessDomainObjectClass(String.class); + MethodInvocation mi = MethodInvocationUtils.create(new TestClass(), + "methodTakingAListAndAString", new ArrayList(), "The Argument"); + assertEquals("The Argument", voter.getDomainObjectInstance(mi)); + } + + private static class TestClass { + public void methodTakingAString(String arg) { + } + + public void methodTaking2Strings(String arg1, String arg2) { + } + + public void methodTakingAListAndAString(ArrayList arg1, String arg2) { + } + } + +}