    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  /*
    19   * IntIdentityTest.java
    20   *
    21   */
    22
    23  package javax.jdo.identity;
    24
    25  import javax.jdo.JDONullIdentityException;
    26
    27  import javax.jdo.util.BatchTestRunner;
    28
    29  /**
    30   *
    31   */
    32  public class IntIdentityTest extends SingleFieldIdentityTest {
    33
    34      /** Creates a new instance of IntIdentityTest */
    35      public IntIdentityTest() {
    36      }
    37
    38      /**
    39       * @param args the command line arguments
    40       */
    41      public static void main(String[] args) {
    42          BatchTestRunner.run(IntIdentityTest.class);
    43      }
    44
    45      public void testConstructor() {
    46          IntIdentity c1 = new IntIdentity(Object.class, (int)1);
    47          IntIdentity c2 = new IntIdentity(Object.class, (int)1);
    48          IntIdentity c3 = new IntIdentity(Object.class, (int)2);
    49          assertEquals("Equal IntIdentity instances compare not equal.", c1, c2);
    50          assertFalse ("Not equal IntIdentity instances compare equal", c1.equals(c3));
    51      }
    52
    53      public void testIntegerConstructor() {
    54          IntIdentity c1 = new IntIdentity(Object.class, (int)1);
    55          IntIdentity c2 = new IntIdentity(Object.class, new Integer((int)1));
    56          IntIdentity c3 = new IntIdentity(Object.class, new Integer((int)2));
    57          assertEquals ("Equal intIdentity instances compare not equal.", c1, c2);
    58          assertFalse ("Not equal IntIdentity instances compare equal", c1.equals(c3));
    59      }
    60
    61      public void testToStringConstructor() {
    62          IntIdentity c1 = new IntIdentity(Object.class, (int)1);
    63          IntIdentity c2 = new IntIdentity(Object.class, c1.toString());
    64          assertEquals ("Equal IntIdentity instances compare not equal.", c1, c2);
    65      }
    66
    67      public void testStringConstructor() {
    68          IntIdentity c1 = new IntIdentity(Object.class, (int)1);
    69          IntIdentity c2 = new IntIdentity(Object.class, "1");
    70          IntIdentity c3 = new IntIdentity(Object.class, "2");
    71          assertEquals ("Equal IntIdentity instances compare not equal.", c1, c2);
    72          assertFalse ("Not equal IntIdentity instances compare equal", c1.equals(c3));
    73      }
    74
    75      public void testIllegalStringConstructor() {
    76          try {
    77              IntIdentity c1 = new IntIdentity(Object.class, "b");
    78          } catch (IllegalArgumentException iae) {
    79              return; // good
    80          }
    81          fail ("No exception caught for illegal String.");
    82      }
    83
    84      public void testSerialized() {
    85          IntIdentity c1 = new IntIdentity(Object.class, (int)1);
    86          IntIdentity c2 = new IntIdentity(Object.class, "1");
    87          IntIdentity c3 = new IntIdentity(Object.class, "2");
    88          Object[] scis = writeReadSerialized(new Object[] {c1, c2, c3});
    89          Object sc1 = scis[0];
    90          Object sc2 = scis[1];
    91          Object sc3 = scis[2];
    92          assertEquals ("Equal IntIdentity instances compare not equal.", c1, sc1);
    93          assertEquals ("Equal IntIdentity instances compare not equal.", c2, sc2);
    94          assertEquals ("Equal IntIdentity instances compare not equal.", sc1, c2);
    95          assertEquals ("Equal IntIdentity instances compare not equal.", sc2, c1);
    96          assertFalse ("Not equal InrIdentity instances compare equal.", c1.equals(sc3));
    97          assertFalse ("Not equal IntIdentity instances compare equal.", sc1.equals(c3));
    98          assertFalse ("Not equal IntIdentity instances compare equal.", sc1.equals(sc3));
    99          assertFalse ("Not equal IntIdentity instances compare equal.", sc3.equals(sc1));
    100     }
    101     public void testGetKeyAsObjectPrimitive() {
    102         IntIdentity c1 = new IntIdentity(Object.class, 1);
    103         assertEquals("keyAsObject doesn't match.", c1.getKeyAsObject(), new Integer(1));
    104     }
    105
    106     public void testGetKeyAsObject() {
    107         IntIdentity c1 = new IntIdentity(Object.class, new Integer(1));
    108         assertEquals("keyAsObject doesn't match.", c1.getKeyAsObject(), new Integer(1));
    109     }
    110
    111     public void testBadConstructorNullIntegerParam() {
    112         try {
    113             IntIdentity c1 = new IntIdentity(Object.class, (Integer)null);
    114         } catch (JDONullIdentityException ex) {
    115             return;
    116         }
    117         fail ("Failed to catch expected exception.");
    118     }
    119
    120     public void testBadConstructorNullStringParam() {
    121         try {
    122             IntIdentity c1 = new IntIdentity(Object.class, (String)null);
    123         } catch (JDONullIdentityException ex) {
    124             return;
    125         }
    126         fail ("Failed to catch expected exception.");
    127     }
    128
    129     public void testCompareTo() {
    130     	IntIdentity c1 = new IntIdentity(Object.class, 1);
    131     	IntIdentity c2 = new IntIdentity(Object.class, 1);
    132     	IntIdentity c3 = new IntIdentity(Object.class, 2);
    133         IntIdentity c4 = new IntIdentity(Class.class, 1);
    134         assertEquals("Equal IntIdentity instances compare not equal.", 0, c1.compareTo(c2));
    135         assertTrue("Not equal IntIdentity instances have wrong compareTo result", c1.compareTo(c3) < 0);
    136         assertTrue("Not equal IntIdentity instances have wrong compareTo result", c3.compareTo(c1) > 0);
    137         assertTrue("Not equal IntIdentity instances have wrong compareTo result", c1.compareTo(c4) > 0);
    138     }
    139 }