    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 NamedEntityObjectIdentity}.
    23   *
    24   * @author Ben Alex
    25   * @version $Id: NamedEntityObjectIdentityTests.java,v 1.2 2005/11/17 00:56:09 benalex Exp $
    26   */
    27  public class NamedEntityObjectIdentityTests extends TestCase {
    28      //~ Constructors ===========================================================
    29
    30      public NamedEntityObjectIdentityTests() {
    31          super();
    32      }
    33
    34      public NamedEntityObjectIdentityTests(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(NamedEntityObjectIdentityTests.class);
    46      }
    47
    48      public void testConstructionViaReflection() throws Exception {
    49          SomeDomain domainObject = new SomeDomain();
    50          domainObject.setId(34);
    51
    52          NamedEntityObjectIdentity name = new NamedEntityObjectIdentity(domainObject);
    53          assertEquals("34", name.getId());
    54          assertEquals(domainObject.getClass().getName(), name.getClassname());
    55          name.toString();
    56      }
    57
    58      public void testConstructionViaReflectionFailsIfNoGetIdMethod()
    59          throws Exception {
    60          try {
    61              new NamedEntityObjectIdentity(new Integer(45));
    62              fail("Should have thrown IllegalArgumentException");
    63          } catch (IllegalArgumentException expected) {
    64              assertTrue(true);
    65          }
    66      }
    67
    68      public void testConstructionViaReflectionFailsIfNullPassed()
    69          throws Exception {
    70          try {
    71              new NamedEntityObjectIdentity(null);
    72              fail("Should have thrown IllegalArgumentException");
    73          } catch (IllegalArgumentException expected) {
    74              assertTrue(true);
    75          }
    76      }
    77
    78      public void testDefaultConstructorRejected() {
    79          try {
    80              new NamedEntityObjectIdentity();
    81              fail("Should have thrown IllegalArgumentException");
    82          } catch (IllegalArgumentException expected) {
    83              assertTrue(true);
    84          }
    85      }
    86
    87      public void testEquality() {
    88          NamedEntityObjectIdentity original = new NamedEntityObjectIdentity("foo",
    89                  "12");
    90          assertFalse(original.equals(null));
    91          assertFalse(original.equals(new Integer(354)));
    92          assertFalse(original.equals(
    93                  new NamedEntityObjectIdentity("foo", "23232")));
    94          assertTrue(original.equals(new NamedEntityObjectIdentity("foo", "12")));
    95          assertTrue(original.equals(original));
    96      }
    97
    98      public void testNormalConstructionRejectedIfInvalidArguments()
    99          throws Exception {
    100         try {
    101             new NamedEntityObjectIdentity(null, "12");
    102             fail("Should have thrown IllegalArgumentException");
    103         } catch (IllegalArgumentException expected) {
    104             assertTrue(true);
    105         }
    106
    107         try {
    108             new NamedEntityObjectIdentity("classname", null);
    109             fail("Should have thrown IllegalArgumentException");
    110         } catch (IllegalArgumentException expected) {
    111             assertTrue(true);
    112         }
    113
    114         try {
    115             new NamedEntityObjectIdentity("", "12");
    116             fail("Should have thrown IllegalArgumentException");
    117         } catch (IllegalArgumentException expected) {
    118             assertTrue(true);
    119         }
    120
    121         try {
    122             new NamedEntityObjectIdentity("classname", "");
    123             fail("Should have thrown IllegalArgumentException");
    124         } catch (IllegalArgumentException expected) {
    125             assertTrue(true);
    126         }
    127     }
    128
    129     public void testNormalOperation() {
    130         NamedEntityObjectIdentity name = new NamedEntityObjectIdentity("domain",
    131                 "id");
    132         assertEquals("domain", name.getClassname());
    133         assertEquals("id", name.getId());
    134     }
    135 }