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.annotation;
017:
018: import java.lang.reflect.AccessibleObject;
019: import java.lang.reflect.Method;
020:
021: import junit.framework.TestCase;
022:
023: import org.acegisecurity.ConfigAttributeDefinition;
024: import org.acegisecurity.SecurityConfig;
025: import org.acegisecurity.annotation.test.Entity;
026: import org.acegisecurity.annotation.test.OrganisationService;
027: import org.acegisecurity.annotation.test.PersonService;
028: import org.acegisecurity.annotation.test.PersonServiceImpl;
029: import org.acegisecurity.annotation.test.Service;
030: import org.acegisecurity.annotation.test.ServiceImpl;
031: import org.acegisecurity.intercept.method.MethodDefinitionMap;
032: import org.acegisecurity.intercept.method.MethodDefinitionSourceEditor;
033: import org.aopalliance.intercept.MethodInvocation;
034:
035: /**
036: * Extra tests to demonstrate generics behaviour with <code>MethodDefinitionMap</code>.
037: *
038: * @author Ben Alex
039: * @version $Id: MethodDefinitionSourceEditorTigerTests.java 1521 2006-05-29 22:22:50Z benalex $
040: */
041: public class MethodDefinitionSourceEditorTigerTests extends TestCase {
042: //~ Constructors ===================================================================================================
043:
044: public MethodDefinitionSourceEditorTigerTests() {
045: super ();
046: }
047:
048: public MethodDefinitionSourceEditorTigerTests(String arg0) {
049: super (arg0);
050: }
051:
052: //~ Methods ========================================================================================================
053:
054: public static void main(String[] args) {
055: junit.textui.TestRunner
056: .run(MethodDefinitionSourceEditorTigerTests.class);
057: }
058:
059: public final void setUp() throws Exception {
060: super .setUp();
061: }
062:
063: public void testConcreteClassInvocationsAlsoReturnDefinitionsAgainstInterface()
064: throws Exception {
065: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
066: editor
067: .setAsText("org.acegisecurity.annotation.test.Service.makeLower*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.annotation.test.Service.makeUpper*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.annotation.test.ServiceImpl.makeUpper*=ROLE_FROM_IMPLEMENTATION");
068:
069: MethodDefinitionMap map = (MethodDefinitionMap) editor
070: .getValue();
071: assertEquals(3, map.getMethodMapSize());
072:
073: ConfigAttributeDefinition returnedMakeLower = map
074: .getAttributes(new MockMethodInvocation(Service.class,
075: "makeLowerCase", new Class[] { Entity.class }));
076: ConfigAttributeDefinition expectedMakeLower = new ConfigAttributeDefinition();
077: expectedMakeLower.addConfigAttribute(new SecurityConfig(
078: "ROLE_FROM_INTERFACE"));
079: assertEquals(expectedMakeLower, returnedMakeLower);
080:
081: ConfigAttributeDefinition returnedMakeUpper = map
082: .getAttributes(new MockMethodInvocation(
083: ServiceImpl.class, "makeUpperCase",
084: new Class[] { Entity.class }));
085: ConfigAttributeDefinition expectedMakeUpper = new ConfigAttributeDefinition();
086: expectedMakeUpper.addConfigAttribute(new SecurityConfig(
087: "ROLE_FROM_IMPLEMENTATION"));
088: expectedMakeUpper.addConfigAttribute(new SecurityConfig(
089: "ROLE_FROM_INTERFACE"));
090: assertEquals(expectedMakeUpper, returnedMakeUpper);
091: }
092:
093: public void testGenericsSuperclassDeclarationsAreIncludedWhenSubclassesOverride()
094: throws Exception {
095: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
096: editor
097: .setAsText("org.acegisecurity.annotation.test.Service.makeLower*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.annotation.test.Service.makeUpper*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.annotation.test.ServiceImpl.makeUpper*=ROLE_FROM_IMPLEMENTATION");
098:
099: MethodDefinitionMap map = (MethodDefinitionMap) editor
100: .getValue();
101: assertEquals(3, map.getMethodMapSize());
102:
103: ConfigAttributeDefinition returnedMakeLower = map
104: .getAttributes(new MockMethodInvocation(
105: PersonService.class, "makeLowerCase",
106: new Class[] { Entity.class }));
107: ConfigAttributeDefinition expectedMakeLower = new ConfigAttributeDefinition();
108: expectedMakeLower.addConfigAttribute(new SecurityConfig(
109: "ROLE_FROM_INTERFACE"));
110: assertEquals(expectedMakeLower, returnedMakeLower);
111:
112: ConfigAttributeDefinition returnedMakeLower2 = map
113: .getAttributes(new MockMethodInvocation(
114: OrganisationService.class, "makeLowerCase",
115: new Class[] { Entity.class }));
116: ConfigAttributeDefinition expectedMakeLower2 = new ConfigAttributeDefinition();
117: expectedMakeLower2.addConfigAttribute(new SecurityConfig(
118: "ROLE_FROM_INTERFACE"));
119: assertEquals(expectedMakeLower2, returnedMakeLower2);
120:
121: ConfigAttributeDefinition returnedMakeUpper = map
122: .getAttributes(new MockMethodInvocation(
123: PersonServiceImpl.class, "makeUpperCase",
124: new Class[] { Entity.class }));
125: ConfigAttributeDefinition expectedMakeUpper = new ConfigAttributeDefinition();
126: expectedMakeUpper.addConfigAttribute(new SecurityConfig(
127: "ROLE_FROM_IMPLEMENTATION"));
128: expectedMakeUpper.addConfigAttribute(new SecurityConfig(
129: "ROLE_FROM_INTERFACE"));
130: assertEquals(expectedMakeUpper, returnedMakeUpper);
131: }
132:
133: //~ Inner Classes ==================================================================================================
134:
135: private class MockMethodInvocation implements MethodInvocation {
136: Method method;
137:
138: private MockMethodInvocation() {
139: super ();
140: }
141:
142: public MockMethodInvocation(Class clazz, String methodName,
143: Class[] parameterTypes) throws NoSuchMethodException {
144: System.out.println(clazz + " " + methodName + " "
145: + parameterTypes[0]);
146: method = clazz.getMethod(methodName, parameterTypes);
147: }
148:
149: public Object[] getArguments() {
150: return null;
151: }
152:
153: public Method getMethod() {
154: return method;
155: }
156:
157: public AccessibleObject getStaticPart() {
158: return null;
159: }
160:
161: public Object getThis() {
162: return null;
163: }
164:
165: public Object proceed() throws Throwable {
166: return null;
167: }
168: }
169: }
|