    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.acl.basic;
    17
    18  import junit.framework.TestCase;
    19
    20
    21  /***
    22   * Tests {@link SimpleAclEntry}.
    23   *
    24   * @author Ben Alex
    25   * @version $Id: SimpleAclEntryTests.java,v 1.3 2005/11/17 00:56:09 benalex Exp $
    26   */
    27  public class SimpleAclEntryTests extends TestCase {
    28      //~ Constructors ===========================================================
    29
    30      public SimpleAclEntryTests() {
    31          super();
    32      }
    33
    34      public SimpleAclEntryTests(String arg0) {
    35          super(arg0);
    36      }
    37
    38      //~ Methods ================================================================
    39
    40      public final void setUp() throws Exception {
    41          super.setUp();
    42      }
    43
    44      public static void main(String[] args) {
    45          junit.textui.TestRunner.run(SimpleAclEntryTests.class);
    46      }
    47
    48      public void testCorrectOperation() {
    49          String recipient = "marissa";
    50          AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity("domain",
    51                  "12");
    52          SimpleAclEntry acl = new SimpleAclEntry(recipient, objectIdentity,
    53                  null, 0);
    54
    55          assertFalse(acl.isPermitted(SimpleAclEntry.ADMINISTRATION));
    56          acl.addPermission(SimpleAclEntry.ADMINISTRATION);
    57          assertTrue(acl.isPermitted(SimpleAclEntry.ADMINISTRATION));
    58          assertFalse(acl.isPermitted(SimpleAclEntry.CREATE));
    59          assertFalse(acl.isPermitted(SimpleAclEntry.DELETE));
    60          assertFalse(acl.isPermitted(SimpleAclEntry.READ));
    61          assertFalse(acl.isPermitted(SimpleAclEntry.WRITE));
    62          assertEquals("A----", acl.printPermissionsBlock());
    63          acl.deletePermission(SimpleAclEntry.ADMINISTRATION);
    64          assertFalse(acl.isPermitted(SimpleAclEntry.ADMINISTRATION));
    65          assertEquals("-----", acl.printPermissionsBlock());
    66
    67          acl.addPermissions(new int[] {SimpleAclEntry.READ, SimpleAclEntry.WRITE});
    68          acl.addPermission(SimpleAclEntry.CREATE);
    69          assertFalse(acl.isPermitted(SimpleAclEntry.ADMINISTRATION));
    70          assertTrue(acl.isPermitted(SimpleAclEntry.CREATE));
    71          assertFalse(acl.isPermitted(SimpleAclEntry.DELETE));
    72          assertTrue(acl.isPermitted(SimpleAclEntry.READ));
    73          assertTrue(acl.isPermitted(SimpleAclEntry.WRITE));
    74          assertEquals("-RWC-", acl.printPermissionsBlock());
    75
    76          acl.deletePermission(SimpleAclEntry.CREATE);
    77          acl.deletePermissions(new int[] {SimpleAclEntry.READ, SimpleAclEntry.WRITE});
    78          assertEquals("-----", acl.printPermissionsBlock());
    79
    80          acl.togglePermission(SimpleAclEntry.CREATE);
    81          assertTrue(acl.isPermitted(SimpleAclEntry.CREATE));
    82          assertFalse(acl.isPermitted(SimpleAclEntry.ADMINISTRATION));
    83          acl.togglePermission(SimpleAclEntry.CREATE);
    84          assertFalse(acl.isPermitted(SimpleAclEntry.CREATE));
    85
    86          acl.togglePermission(SimpleAclEntry.DELETE);
    87          assertTrue(acl.isPermitted(SimpleAclEntry.DELETE));
    88          assertEquals("----D", acl.printPermissionsBlock());
    89      }
    90
    91      public void testDetectsNullOnMainConstructor() {
    92          String recipient = "marissa";
    93          AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity("domain",
    94                  "12");
    95
    96          try {
    97              new SimpleAclEntry(recipient, null, null, 2);
    98              fail("Should have thrown IllegalArgumentException");
    99          } catch (IllegalArgumentException expected) {
    100             assertTrue(true);
    101         }
    102
    103         try {
    104             new SimpleAclEntry(null, objectIdentity, null, 2);
    105             fail("Should have thrown IllegalArgumentException");
    106         } catch (IllegalArgumentException expected) {
    107             assertTrue(true);
    108         }
    109     }
    110
    111     public void testGettersSetters() {
    112         SimpleAclEntry acl = new SimpleAclEntry();
    113
    114         AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity("domain",
    115                 "693");
    116         acl.setAclObjectIdentity(objectIdentity);
    117         assertEquals(objectIdentity, acl.getAclObjectIdentity());
    118
    119         AclObjectIdentity parentObjectIdentity = new NamedEntityObjectIdentity("domain",
    120                 "13");
    121         acl.setAclObjectParentIdentity(parentObjectIdentity);
    122         assertEquals(parentObjectIdentity, acl.getAclObjectParentIdentity());
    123
    124         acl.setMask(2);
    125         assertEquals(2, acl.getMask());
    126
    127         acl.setRecipient("scott");
    128         assertEquals("scott", acl.getRecipient());
    129     }
    130
    131     public void testRejectsInvalidMasksInAddMethod() {
    132         String recipient = "marissa";
    133         AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity("domain",
    134                 "12");
    135         SimpleAclEntry acl = new SimpleAclEntry(recipient, objectIdentity,
    136                 null, 4);
    137
    138         try {
    139             acl.addPermission(Integer.MAX_VALUE);
    140             fail("Should have thrown IllegalArgumentException");
    141         } catch (IllegalArgumentException expected) {
    142             assertTrue(true);
    143         }
    144     }
    145
    146     public void testRejectsInvalidMasksInDeleteMethod() {
    147         String recipient = "marissa";
    148         AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity("domain",
    149                 "12");
    150         SimpleAclEntry acl = new SimpleAclEntry(recipient, objectIdentity,
    151                 null, 0);
    152         acl.addPermissions(new int[] {SimpleAclEntry.READ, SimpleAclEntry.WRITE, SimpleAclEntry.CREATE});
    153
    154         try {
    155             acl.deletePermission(SimpleAclEntry.READ); // can't write if we can't read
    156             fail("Should have thrown IllegalArgumentException");
    157         } catch (IllegalArgumentException expected) {
    158             assertTrue(true);
    159         }
    160     }
    161
    162     public void testRejectsInvalidMasksInTogglePermissionMethod() {
    163         String recipient = "marissa";
    164         AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity("domain",
    165                 "12");
    166         SimpleAclEntry acl = new SimpleAclEntry(recipient, objectIdentity,
    167                 null, 0);
    168         acl.addPermissions(new int[] {SimpleAclEntry.READ, SimpleAclEntry.WRITE, SimpleAclEntry.CREATE});
    169
    170         try {
    171             acl.togglePermission(SimpleAclEntry.READ); // can't write if we can't read
    172             fail("Should have thrown IllegalArgumentException");
    173         } catch (IllegalArgumentException expected) {
    174             assertTrue(true);
    175         }
    176     }
    177
    178     public void testToString() {
    179         String recipient = "marissa";
    180         AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity("domain",
    181                 "12");
    182         SimpleAclEntry acl = new SimpleAclEntry(recipient, objectIdentity,
    183                 null, 0);
    184         acl.addPermissions(new int[] {SimpleAclEntry.READ, SimpleAclEntry.WRITE, SimpleAclEntry.CREATE});
    185         assertTrue(acl.toString().endsWith("marissa=-RWC- ............................111. (14)]"));
    186     }
    187 }