    1   /* Copyright 2004 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.vote;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.AccessDeniedException;
    21  import org.acegisecurity.Authentication;
    22  import org.acegisecurity.ConfigAttribute;
    23  import org.acegisecurity.ConfigAttributeDefinition;
    24  import org.acegisecurity.SecurityConfig;
    25
    26  import java.util.List;
    27  import java.util.Vector;
    28
    29
    30  /***
    31   * Tests {@link AbstractAccessDecisionManager}.
    32   *
    33   * @author Ben Alex
    34   * @version $Id: AbstractAccessDecisionManagerTests.java,v 1.3 2005/11/17 00:55:48 benalex Exp $
    35   */
    36  public class AbstractAccessDecisionManagerTests extends TestCase {
    37      //~ Constructors ===========================================================
    38
    39      public AbstractAccessDecisionManagerTests() {
    40          super();
    41      }
    42
    43      public AbstractAccessDecisionManagerTests(String arg0) {
    44          super(arg0);
    45      }
    46
    47      //~ Methods ================================================================
    48
    49      public final void setUp() throws Exception {
    50          super.setUp();
    51      }
    52
    53      public static void main(String[] args) {
    54          junit.textui.TestRunner.run(AbstractAccessDecisionManagerTests.class);
    55      }
    56
    57      public void testAllowIfAccessDecisionManagerDefaults()
    58          throws Exception {
    59          MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
    60          assertTrue(!mock.isAllowIfAllAbstainDecisions()); // default
    61          mock.setAllowIfAllAbstainDecisions(true);
    62          assertTrue(mock.isAllowIfAllAbstainDecisions()); // changed
    63      }
    64
    65      public void testDelegatesSupportsClassRequests() throws Exception {
    66          MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
    67          List list = new Vector();
    68          list.add(new DenyVoter());
    69          list.add(new MockStringOnlyVoter());
    70          mock.setDecisionVoters(list);
    71
    72          assertTrue(mock.supports(new String().getClass()));
    73          assertTrue(!mock.supports(new Integer(7).getClass()));
    74      }
    75
    76      public void testDelegatesSupportsRequests() throws Exception {
    77          MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
    78          List list = new Vector();
    79          DenyVoter voter = new DenyVoter();
    80          DenyAgainVoter denyVoter = new DenyAgainVoter();
    81          list.add(voter);
    82          list.add(denyVoter);
    83          mock.setDecisionVoters(list);
    84
    85          ConfigAttribute attr = new SecurityConfig("DENY_AGAIN_FOR_SURE");
    86          assertTrue(mock.supports(attr));
    87
    88          ConfigAttribute badAttr = new SecurityConfig("WE_DONT_SUPPORT_THIS");
    89          assertTrue(!mock.supports(badAttr));
    90      }
    91
    92      public void testProperlyStoresListOfVoters() throws Exception {
    93          MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
    94          List list = new Vector();
    95          DenyVoter voter = new DenyVoter();
    96          DenyAgainVoter denyVoter = new DenyAgainVoter();
    97          list.add(voter);
    98          list.add(denyVoter);
    99          mock.setDecisionVoters(list);
    100         assertEquals(list.size(), mock.getDecisionVoters().size());
    101     }
    102
    103     public void testRejectsEmptyList() throws Exception {
    104         MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
    105         List list = new Vector();
    106
    107         try {
    108             mock.setDecisionVoters(list);
    109             fail("Should have thrown IllegalArgumentException");
    110         } catch (IllegalArgumentException expected) {
    111             assertTrue(true);
    112         }
    113     }
    114
    115     public void testRejectsListContainingInvalidObjectTypes()
    116         throws Exception {
    117         MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
    118         List list = new Vector();
    119         DenyVoter voter = new DenyVoter();
    120         DenyAgainVoter denyVoter = new DenyAgainVoter();
    121         String notAVoter = "NOT_A_VOTER";
    122         list.add(voter);
    123         list.add(notAVoter);
    124         list.add(denyVoter);
    125
    126         try {
    127             mock.setDecisionVoters(list);
    128             fail("Should have thrown IllegalArgumentException");
    129         } catch (IllegalArgumentException expected) {
    130             assertTrue(true);
    131         }
    132     }
    133
    134     public void testRejectsNullVotersList() throws Exception {
    135         MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
    136
    137         try {
    138             mock.setDecisionVoters(null);
    139             fail("Should have thrown IllegalArgumentException");
    140         } catch (IllegalArgumentException expected) {
    141             assertTrue(true);
    142         }
    143     }
    144
    145     public void testRoleVoterAlwaysReturnsTrueToSupports() {
    146         RoleVoter rv = new RoleVoter();
    147         assertTrue(rv.supports(String.class));
    148     }
    149
    150     public void testWillNotStartIfDecisionVotersNotSet()
    151         throws Exception {
    152         MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
    153
    154         try {
    155             mock.afterPropertiesSet();
    156             fail("Should have thrown IllegalArgumentException");
    157         } catch (IllegalArgumentException expected) {
    158             assertTrue(true);
    159         }
    160     }
    161
    162     //~ Inner Classes ==========================================================
    163
    164     private class MockDecisionManagerImpl extends AbstractAccessDecisionManager {
    165         public void decide(Authentication authentication, Object object,
    166             ConfigAttributeDefinition config) throws AccessDeniedException {
    167             return;
    168         }
    169     }
    170
    171     private class MockStringOnlyVoter implements AccessDecisionVoter {
    172         public boolean supports(Class clazz) {
    173             if (String.class.isAssignableFrom(clazz)) {
    174                 return true;
    175             } else {
    176                 return false;
    177             }
    178         }
    179
    180         public boolean supports(ConfigAttribute attribute) {
    181             throw new UnsupportedOperationException(
    182                 "mock method not implemented");
    183         }
    184
    185         public int vote(Authentication authentication, Object object,
    186             ConfigAttributeDefinition config) {
    187             throw new UnsupportedOperationException(
    188                 "mock method not implemented");
    189         }
    190     }
    191 }