    1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.intercept.method;
    17
    18  import java.lang.reflect.AccessibleObject;
    19  import java.lang.reflect.Method;
    20
    21  import junit.framework.TestCase;
    22
    23  import org.acegisecurity.ConfigAttributeDefinition;
    24  import org.acegisecurity.Entity;
    25  import org.acegisecurity.OrganisationService;
    26  import org.acegisecurity.PersonService;
    27  import org.acegisecurity.PersonServiceImpl;
    28  import org.acegisecurity.SecurityConfig;
    29  import org.acegisecurity.Service;
    30  import org.acegisecurity.ServiceImpl;
    31  import org.aopalliance.intercept.MethodInvocation;
    32
    33
    34  /***
    35   * Extra tests to demonstrate generics behaviour with
    36   * <code>MethodDefinitionMap</code>.
    37   *
    38   * @author Ben Alex
    39   * @version $Id: MethodDefinitionSourceEditorTigerTests.java,v 1.2 2005/11/30 00:14:55 benalex Exp $
    40   */
    41  public class MethodDefinitionSourceEditorTigerTests extends TestCase {
    42      //~ Constructors ===========================================================
    43
    44      public MethodDefinitionSourceEditorTigerTests() {
    45          super();
    46      }
    47
    48      public MethodDefinitionSourceEditorTigerTests(String arg0) {
    49          super(arg0);
    50      }
    51
    52      //~ Methods ================================================================
    53
    54      public final void setUp() throws Exception {
    55          super.setUp();
    56      }
    57
    58      public static void main(String[] args) {
    59          junit.textui.TestRunner.run(MethodDefinitionSourceEditorTigerTests.class);
    60      }
    61
    62      public void testConcreteClassInvocationsAlsoReturnDefinitionsAgainstInterface()
    63          throws Exception {
    64          MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
    65          editor.setAsText(
    66              "org.acegisecurity.Service.makeLower*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.Service.makeUpper*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.ServiceImpl.makeUpper*=ROLE_FROM_IMPLEMENTATION");
    67
    68          MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
    69          assertEquals(3, map.getMethodMapSize());
    70
    71          ConfigAttributeDefinition returnedMakeLower = map.getAttributes(new MockMethodInvocation(
    72                      Service.class, "makeLowerCase", new Class[] {Entity.class}));
    73          ConfigAttributeDefinition expectedMakeLower = new ConfigAttributeDefinition();
    74          expectedMakeLower.addConfigAttribute(new SecurityConfig(
    75                  "ROLE_FROM_INTERFACE"));
    76          assertEquals(expectedMakeLower, returnedMakeLower);
    77
    78          ConfigAttributeDefinition returnedMakeUpper = map.getAttributes(new MockMethodInvocation(
    79                      ServiceImpl.class, "makeUpperCase",
    80                      new Class[] {Entity.class}));
    81          ConfigAttributeDefinition expectedMakeUpper = new ConfigAttributeDefinition();
    82          expectedMakeUpper.addConfigAttribute(new SecurityConfig(
    83                  "ROLE_FROM_IMPLEMENTATION"));
    84          expectedMakeUpper.addConfigAttribute(new SecurityConfig(
    85                  "ROLE_FROM_INTERFACE"));
    86          assertEquals(expectedMakeUpper, returnedMakeUpper);
    87      }
    88
    89      public void testGenericsSuperclassDeclarationsAreIncludedWhenSubclassesOverride()
    90          throws Exception {
    91          MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
    92          editor.setAsText(
    93              "org.acegisecurity.Service.makeLower*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.Service.makeUpper*=ROLE_FROM_INTERFACE\r\norg.acegisecurity.ServiceImpl.makeUpper*=ROLE_FROM_IMPLEMENTATION");
    94
    95          MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
    96          assertEquals(3, map.getMethodMapSize());
    97
    98          ConfigAttributeDefinition returnedMakeLower = map.getAttributes(new MockMethodInvocation(
    99                      PersonService.class, "makeLowerCase",
    100                     new Class[] {Entity.class}));
    101         ConfigAttributeDefinition expectedMakeLower = new ConfigAttributeDefinition();
    102         expectedMakeLower.addConfigAttribute(new SecurityConfig(
    103                 "ROLE_FROM_INTERFACE"));
    104         assertEquals(expectedMakeLower, returnedMakeLower);
    105
    106         ConfigAttributeDefinition returnedMakeLower2 = map.getAttributes(new MockMethodInvocation(
    107                     OrganisationService.class, "makeLowerCase",
    108                     new Class[] {Entity.class}));
    109         ConfigAttributeDefinition expectedMakeLower2 = new ConfigAttributeDefinition();
    110         expectedMakeLower2.addConfigAttribute(new SecurityConfig(
    111                 "ROLE_FROM_INTERFACE"));
    112         assertEquals(expectedMakeLower2, returnedMakeLower2);
    113
    114         ConfigAttributeDefinition returnedMakeUpper = map.getAttributes(new MockMethodInvocation(
    115                     PersonServiceImpl.class, "makeUpperCase",
    116                     new Class[] {Entity.class}));
    117         ConfigAttributeDefinition expectedMakeUpper = new ConfigAttributeDefinition();
    118         expectedMakeUpper.addConfigAttribute(new SecurityConfig(
    119                 "ROLE_FROM_IMPLEMENTATION"));
    120         expectedMakeUpper.addConfigAttribute(new SecurityConfig(
    121                 "ROLE_FROM_INTERFACE"));
    122         assertEquals(expectedMakeUpper, returnedMakeUpper);
    123     }
    124
    125     //~ Inner Classes ==========================================================
    126
    127     private class MockMethodInvocation implements MethodInvocation {
    128         Method method;
    129
    130         public MockMethodInvocation(Class clazz, String methodName,
    131             Class[] parameterTypes) throws NoSuchMethodException {
    132             System.out.println(clazz + " " + methodName + " "
    133                 + parameterTypes[0]);
    134             method = clazz.getMethod(methodName, parameterTypes);
    135         }
    136
    137         private MockMethodInvocation() {
    138             super();
    139         }
    140
    141         public Object[] getArguments() {
    142             return null;
    143         }
    144
    145         public Method getMethod() {
    146             return method;
    147         }
    148
    149         public AccessibleObject getStaticPart() {
    150             return null;
    151         }
    152
    153         public Object getThis() {
    154             return null;
    155         }
    156
    157         public Object proceed() throws Throwable {
    158             return null;
    159         }
    160     }
    161 }