    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.acl.basic;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.GrantedAuthority;
    21  import org.acegisecurity.GrantedAuthorityImpl;
    22  import org.acegisecurity.acl.AclEntry;
    23  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
    24  import org.acegisecurity.userdetails.User;
    25
    26
    27  /***
    28   * Tests {@link GrantedAuthorityEffectiveAclsResolver}.
    29   *
    30   * @author Ben Alex
    31   * @version $Id: GrantedAuthorityEffectiveAclsResolverTests.java,v 1.7 2005/11/29 13:10:13 benalex Exp $
    32   */
    33  public class GrantedAuthorityEffectiveAclsResolverTests extends TestCase {
    34      //~ Instance fields ========================================================
    35
    36      private SimpleAclEntry entry100RoleEverybody = new SimpleAclEntry("ROLE_EVERYBODY",
    37              new NamedEntityObjectIdentity("OBJECT", "100"), null, 14);
    38      private SimpleAclEntry entry100RoleOne = new SimpleAclEntry("ROLE_ONE",
    39              new NamedEntityObjectIdentity("OBJECT", "100"), null, 0);
    40      private SimpleAclEntry entry100RoleTwo = new SimpleAclEntry("ROLE_TWO",
    41              new NamedEntityObjectIdentity("OBJECT", "100"), null, 2);
    42      private UsernamePasswordAuthenticationToken scott = new UsernamePasswordAuthenticationToken("scott",
    43              "not used",
    44              new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_EVERYBODY"), new GrantedAuthorityImpl(
    45                      "ROLE_TWO")});
    46      private SimpleAclEntry entry100Scott = new SimpleAclEntry(scott
    47              .getPrincipal(), new NamedEntityObjectIdentity("OBJECT", "100"),
    48              null, 4);
    49      private UsernamePasswordAuthenticationToken dianne = new UsernamePasswordAuthenticationToken("dianne",
    50              "not used");
    51      private UsernamePasswordAuthenticationToken marissa = new UsernamePasswordAuthenticationToken("marissa",
    52              "not used",
    53              new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_EVERYBODY"), new GrantedAuthorityImpl("ROLE_ONE")});
    54      private SimpleAclEntry entry100Marissa = new SimpleAclEntry(marissa
    55              .getPrincipal(), new NamedEntityObjectIdentity("OBJECT", "100"),
    56              null, 2);
    57      private UsernamePasswordAuthenticationToken scottWithUserDetails = new UsernamePasswordAuthenticationToken(new User(
    58                  "scott", "NOT_USED", true, true, true, true,
    59                  new GrantedAuthority[] {new GrantedAuthorityImpl(
    60                          "ROLE_EVERYBODY")}), "not used",
    61              new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_EVERYBODY"), new GrantedAuthorityImpl("ROLE_TWO")});
    62
    63      // convenience group
    64      private SimpleAclEntry[] acls = {entry100Marissa, entry100Scott, entry100RoleEverybody, entry100RoleOne, entry100RoleTwo};
    65
    66      //~ Constructors ===========================================================
    67
    68      public GrantedAuthorityEffectiveAclsResolverTests() {
    69          super();
    70      }
    71
    72      public GrantedAuthorityEffectiveAclsResolverTests(String arg0) {
    73          super(arg0);
    74      }
    75
    76      //~ Methods ================================================================
    77
    78      public final void setUp() throws Exception {
    79          super.setUp();
    80      }
    81
    82      public static void main(String[] args) {
    83          junit.textui.TestRunner.run(GrantedAuthorityEffectiveAclsResolverTests.class);
    84      }
    85
    86      public void testResolveAclsForDianneWhoHasANullForAuthorities() {
    87          GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
    88          assertNull(resolver.resolveEffectiveAcls(acls, dianne));
    89      }
    90
    91      public void testResolveAclsForMarissa() {
    92          GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
    93          assertEquals(3, resolver.resolveEffectiveAcls(acls, marissa).length);
    94          assertEquals(entry100Marissa,
    95              resolver.resolveEffectiveAcls(acls, marissa)[0]);
    96          assertEquals(entry100RoleEverybody,
    97              resolver.resolveEffectiveAcls(acls, marissa)[1]);
    98          assertEquals(entry100RoleOne,
    99              resolver.resolveEffectiveAcls(acls, marissa)[2]);
    100     }
    101
    102     public void testResolveAclsForScottWithStringObjectAsPrincipal() {
    103         GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
    104         assertEquals(3, resolver.resolveEffectiveAcls(acls, scott).length);
    105         assertEquals(entry100Scott,
    106             resolver.resolveEffectiveAcls(acls, scott)[0]);
    107         assertEquals(entry100RoleEverybody,
    108             resolver.resolveEffectiveAcls(acls, scott)[1]);
    109         assertEquals(entry100RoleTwo,
    110             resolver.resolveEffectiveAcls(acls, scott)[2]);
    111     }
    112
    113     public void testResolveAclsForScottWithUserDetailsObjectAsPrincipal() {
    114         GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
    115         assertEquals(3,
    116             resolver.resolveEffectiveAcls(acls, scottWithUserDetails).length);
    117         assertEquals(entry100Scott,
    118             resolver.resolveEffectiveAcls(acls, scottWithUserDetails)[0]);
    119         assertEquals(entry100RoleEverybody,
    120             resolver.resolveEffectiveAcls(acls, scottWithUserDetails)[1]);
    121         assertEquals(entry100RoleTwo,
    122             resolver.resolveEffectiveAcls(acls, scottWithUserDetails)[2]);
    123     }
    124
    125     public void testResolveAclsReturnsNullIfNoAclsInFirstPlace() {
    126         GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
    127         assertNull(resolver.resolveEffectiveAcls(null, scott));
    128     }
    129
    130     public void testSkipsNonBasicAclEntryObjects() {
    131         GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
    132         AclEntry[] basicAcls = {entry100Marissa, entry100Scott, entry100RoleEverybody, entry100RoleOne, new MockAcl(), entry100RoleTwo};
    133         assertEquals(3, resolver.resolveEffectiveAcls(basicAcls, marissa).length);
    134     }
    135
    136     //~ Inner Classes ==========================================================
    137
    138     private class MockAcl implements AclEntry {
    139         // does nothing
    140     }
    141 }