From 863ba3f0a36dcd9be8c87fbd0667b8fb635529ab Mon Sep 17 00:00:00 2001 From: Ben Alex Date: Fri, 25 Nov 2005 03:26:10 +0000 Subject: [PATCH] SEC-99: Extra tests to explore generics behaviour. --- .../test/java/org/acegisecurity/Entity.java | 52 ++++++ .../java/org/acegisecurity/Organisation.java | 44 +++++ .../acegisecurity/OrganisationService.java | 5 + .../OrganisationServiceImpl.java | 9 + .../test/java/org/acegisecurity/Person.java | 44 +++++ .../java/org/acegisecurity/PersonService.java | 5 + .../org/acegisecurity/PersonServiceImpl.java | 9 + .../test/java/org/acegisecurity/Service.java | 37 ++++ .../java/org/acegisecurity/ServiceImpl.java | 23 +++ ...ethodDefinitionSourceEditorTigerTests.java | 162 ++++++++++++++++++ 10 files changed, 390 insertions(+) create mode 100644 core-tiger/src/test/java/org/acegisecurity/Entity.java create mode 100644 core-tiger/src/test/java/org/acegisecurity/Organisation.java create mode 100644 core-tiger/src/test/java/org/acegisecurity/OrganisationService.java create mode 100644 core-tiger/src/test/java/org/acegisecurity/OrganisationServiceImpl.java create mode 100644 core-tiger/src/test/java/org/acegisecurity/Person.java create mode 100644 core-tiger/src/test/java/org/acegisecurity/PersonService.java create mode 100644 core-tiger/src/test/java/org/acegisecurity/PersonServiceImpl.java create mode 100644 core-tiger/src/test/java/org/acegisecurity/Service.java create mode 100644 core-tiger/src/test/java/org/acegisecurity/ServiceImpl.java create mode 100644 core-tiger/src/test/java/org/acegisecurity/intercept/method/MethodDefinitionSourceEditorTigerTests.java diff --git a/core-tiger/src/test/java/org/acegisecurity/Entity.java b/core-tiger/src/test/java/org/acegisecurity/Entity.java new file mode 100644 index 0000000000..89b8b039a9 --- /dev/null +++ b/core-tiger/src/test/java/org/acegisecurity/Entity.java @@ -0,0 +1,52 @@ +/* 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; + +import org.springframework.util.Assert; + + +/** + * An entity used in our generics testing. + * + * @author Ben Alex + * @version $Id$ + */ +public class Entity { + //~ Instance fields ======================================================== + + String info; + + //~ Constructors =========================================================== + + public Entity(String info) { + Assert.hasText(info, "Some information must be given!"); + this.info = info; + } + + //~ Methods ================================================================ + + public String getInfo() { + return info; + } + + void makeLowercase() { + this.info = info.toLowerCase(); + } + + void makeUppercase() { + this.info = info.toUpperCase(); + } +} diff --git a/core-tiger/src/test/java/org/acegisecurity/Organisation.java b/core-tiger/src/test/java/org/acegisecurity/Organisation.java new file mode 100644 index 0000000000..03dbf7bd42 --- /dev/null +++ b/core-tiger/src/test/java/org/acegisecurity/Organisation.java @@ -0,0 +1,44 @@ +/* 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; + +/** + * An extended version of Entity. + * + * @author Ben Alex + * @version $Id$ + */ +public class Organisation extends Entity { + //~ Instance fields ======================================================== + + private boolean active = true; + + //~ Constructors =========================================================== + + public Organisation(String name) { + super(name); + } + + //~ Methods ================================================================ + + public boolean isActive() { + return this.active; + } + + void deactive() { + this.active = true; + } +} diff --git a/core-tiger/src/test/java/org/acegisecurity/OrganisationService.java b/core-tiger/src/test/java/org/acegisecurity/OrganisationService.java new file mode 100644 index 0000000000..0c40630eb7 --- /dev/null +++ b/core-tiger/src/test/java/org/acegisecurity/OrganisationService.java @@ -0,0 +1,5 @@ +package org.acegisecurity; + +public interface OrganisationService extends Service { + public void deactive(Organisation org); +} diff --git a/core-tiger/src/test/java/org/acegisecurity/OrganisationServiceImpl.java b/core-tiger/src/test/java/org/acegisecurity/OrganisationServiceImpl.java new file mode 100644 index 0000000000..d4b6f84889 --- /dev/null +++ b/core-tiger/src/test/java/org/acegisecurity/OrganisationServiceImpl.java @@ -0,0 +1,9 @@ +package org.acegisecurity; + +public class OrganisationServiceImpl extends ServiceImpl implements OrganisationService { + + public void deactive(Organisation org) { + org.deactive(); + } + +} diff --git a/core-tiger/src/test/java/org/acegisecurity/Person.java b/core-tiger/src/test/java/org/acegisecurity/Person.java new file mode 100644 index 0000000000..5fad5927d7 --- /dev/null +++ b/core-tiger/src/test/java/org/acegisecurity/Person.java @@ -0,0 +1,44 @@ +/* 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; + +/** + * An extended version of Entity. + * + * @author Ben Alex + * @version $Id$ + */ +public class Person extends Entity { + //~ Instance fields ======================================================== + + private boolean active = true; + + //~ Constructors =========================================================== + + public Person(String name) { + super(name); + } + + //~ Methods ================================================================ + + public boolean isActive() { + return this.active; + } + + void deactive() { + this.active = true; + } +} diff --git a/core-tiger/src/test/java/org/acegisecurity/PersonService.java b/core-tiger/src/test/java/org/acegisecurity/PersonService.java new file mode 100644 index 0000000000..c184602bd4 --- /dev/null +++ b/core-tiger/src/test/java/org/acegisecurity/PersonService.java @@ -0,0 +1,5 @@ +package org.acegisecurity; + +public interface PersonService extends Service { + public void deactive(Person person); +} diff --git a/core-tiger/src/test/java/org/acegisecurity/PersonServiceImpl.java b/core-tiger/src/test/java/org/acegisecurity/PersonServiceImpl.java new file mode 100644 index 0000000000..bf4f11eb9b --- /dev/null +++ b/core-tiger/src/test/java/org/acegisecurity/PersonServiceImpl.java @@ -0,0 +1,9 @@ +package org.acegisecurity; + +public class PersonServiceImpl extends ServiceImpl implements PersonService { + + public void deactive(Person person) { + person.deactive(); + } + +} diff --git a/core-tiger/src/test/java/org/acegisecurity/Service.java b/core-tiger/src/test/java/org/acegisecurity/Service.java new file mode 100644 index 0000000000..f98c604058 --- /dev/null +++ b/core-tiger/src/test/java/org/acegisecurity/Service.java @@ -0,0 +1,37 @@ +/* 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; + +import java.util.Collection; + + +/** + * An interface that uses Java 5 generics. + * + * @author Ben Alex + * @version $Id$ + */ +public interface Service { + //~ Methods ================================================================ + + public int countElements(Collection ids); + + public void makeLowerCase(E input); + + public void makeUpperCase(E input); + + public void publicMakeLowerCase(E input); +} diff --git a/core-tiger/src/test/java/org/acegisecurity/ServiceImpl.java b/core-tiger/src/test/java/org/acegisecurity/ServiceImpl.java new file mode 100644 index 0000000000..03b0be57a9 --- /dev/null +++ b/core-tiger/src/test/java/org/acegisecurity/ServiceImpl.java @@ -0,0 +1,23 @@ +package org.acegisecurity; + +import java.util.Collection; + +public class ServiceImpl implements Service { + + public int countElements(Collection ids) { + return 0; + } + + public void makeLowerCase(E input) { + input.makeLowercase(); + } + + public void makeUpperCase(E input) { + input.makeUppercase(); + } + + public void publicMakeLowerCase(E input) { + input.makeUppercase(); + } + +} diff --git a/core-tiger/src/test/java/org/acegisecurity/intercept/method/MethodDefinitionSourceEditorTigerTests.java b/core-tiger/src/test/java/org/acegisecurity/intercept/method/MethodDefinitionSourceEditorTigerTests.java new file mode 100644 index 0000000000..05dda2da95 --- /dev/null +++ b/core-tiger/src/test/java/org/acegisecurity/intercept/method/MethodDefinitionSourceEditorTigerTests.java @@ -0,0 +1,162 @@ +/* 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.intercept.method; + +import junit.framework.TestCase; + +import org.acegisecurity.ConfigAttributeDefinition; +import org.acegisecurity.Entity; +import org.acegisecurity.OrganisationService; +import org.acegisecurity.PersonService; +import org.acegisecurity.PersonServiceImpl; +import org.acegisecurity.SecurityConfig; +import org.acegisecurity.Service; +import org.acegisecurity.ServiceImpl; + +import org.aopalliance.intercept.MethodInvocation; + +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Method; + + +/** + * Extra tests to demonstrate generics behaviour with + * MethodDefinitionMap. + * + * @author Ben Alex + * @version $Id$ + */ +public class MethodDefinitionSourceEditorTigerTests extends TestCase { + //~ Constructors =========================================================== + + public MethodDefinitionSourceEditorTigerTests() { + super(); + } + + public MethodDefinitionSourceEditorTigerTests(String arg0) { + super(arg0); + } + + //~ Methods ================================================================ + + public final void setUp() throws Exception { + super.setUp(); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(MethodDefinitionSourceEditorTests.class); + } + + public void testConcreteClassInvocationsAlsoReturnDefinitionsAgainstInterface() + throws Exception { + MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor(); + editor.setAsText( + "org.acegisecurity.Service.makeLower*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.Service.makeUpper*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.ServiceImpl.makeUpper*=ROLE_FROM_IMPLEMENTATION"); + + MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue(); + assertEquals(3, map.getMethodMapSize()); + + ConfigAttributeDefinition returnedMakeLower = map.getAttributes(new MockMethodInvocation( + Service.class, "makeLowerCase", new Class[] {Entity.class})); + ConfigAttributeDefinition expectedMakeLower = new ConfigAttributeDefinition(); + expectedMakeLower.addConfigAttribute(new SecurityConfig( + "ROLE_FROM_INTERFACE")); + assertEquals(expectedMakeLower, returnedMakeLower); + + ConfigAttributeDefinition returnedMakeUpper = map.getAttributes(new MockMethodInvocation( + ServiceImpl.class, "makeUpperCase", + new Class[] {Entity.class})); + ConfigAttributeDefinition expectedMakeUpper = new ConfigAttributeDefinition(); + expectedMakeUpper.addConfigAttribute(new SecurityConfig( + "ROLE_FROM_IMPLEMENTATION")); + expectedMakeUpper.addConfigAttribute(new SecurityConfig( + "ROLE_FROM_INTERFACE")); + assertEquals(expectedMakeUpper, returnedMakeUpper); + } + + public void testGenericsSuperclassDeclarationsAreIncludedWhenSubclassesOverride() + throws Exception { + MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor(); + editor.setAsText( + "org.acegisecurity.Service.makeLower*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.Service.makeUpper*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.ServiceImpl.makeUpper*=ROLE_FROM_IMPLEMENTATION"); + + MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue(); + assertEquals(3, map.getMethodMapSize()); + + ConfigAttributeDefinition returnedMakeLower = map.getAttributes(new MockMethodInvocation( + PersonService.class, "makeLowerCase", + new Class[] {Entity.class})); + ConfigAttributeDefinition expectedMakeLower = new ConfigAttributeDefinition(); + expectedMakeLower.addConfigAttribute(new SecurityConfig( + "ROLE_FROM_INTERFACE")); + assertEquals(expectedMakeLower, returnedMakeLower); + + ConfigAttributeDefinition returnedMakeLower2 = map.getAttributes(new MockMethodInvocation( + OrganisationService.class, "makeLowerCase", + new Class[] {Entity.class})); + ConfigAttributeDefinition expectedMakeLower2 = new ConfigAttributeDefinition(); + expectedMakeLower2.addConfigAttribute(new SecurityConfig( + "ROLE_FROM_INTERFACE")); + assertEquals(expectedMakeLower2, returnedMakeLower2); + + ConfigAttributeDefinition returnedMakeUpper = map.getAttributes(new MockMethodInvocation( + PersonServiceImpl.class, "makeUpperCase", + new Class[] {Entity.class})); + ConfigAttributeDefinition expectedMakeUpper = new ConfigAttributeDefinition(); + expectedMakeUpper.addConfigAttribute(new SecurityConfig( + "ROLE_FROM_IMPLEMENTATION")); + expectedMakeUpper.addConfigAttribute(new SecurityConfig( + "ROLE_FROM_INTERFACE")); + assertEquals(expectedMakeUpper, returnedMakeUpper); + } + + //~ Inner Classes ========================================================== + + private class MockMethodInvocation implements MethodInvocation { + Method method; + + public MockMethodInvocation(Class clazz, String methodName, + Class[] parameterTypes) throws NoSuchMethodException { + System.out.println(clazz + " " + methodName + " " + + parameterTypes[0]); + method = clazz.getMethod(methodName, parameterTypes); + } + + private MockMethodInvocation() { + super(); + } + + public Object[] getArguments() { + return null; + } + + public Method getMethod() { + return method; + } + + public AccessibleObject getStaticPart() { + return null; + } + + public Object getThis() { + return null; + } + + public Object proceed() throws Throwable { + return null; + } + } +}