    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   * LongIdentityTest.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 LongIdentityTest extends SingleFieldIdentityTest {
    33
    34      /** Creates a new instance of LongIdentityTest */
    35      public LongIdentityTest() {
    36      }
    37
    38      /**
    39       * @param args the command line arguments
    40       */
    41      public static void main(String[] args) {
    42          BatchTestRunner.run(LongIdentityTest.class);
    43      }
    44
    45      public void testConstructor() {
    46          LongIdentity c1 = new LongIdentity(Object.class, 1);
    47          LongIdentity c2 = new LongIdentity(Object.class, 1);
    48          LongIdentity c3 = new LongIdentity(Object.class, 2);
    49          LongIdentity c4 = new LongIdentity(Object.class, 0x100000001L);
    50          assertEquals("Equal LongIdentity instances compare not equal.", c1, c2);
    51          assertFalse ("Not equal LongIdentity instances compare equal", c1.equals(c3));
    52          assertFalse ("Not equal LongIdentity instances compare equal", c4.equals(c1));
    53      }
    54
    55      public void testLongConstructor() {
    56          LongIdentity c1 = new LongIdentity(Object.class, 1);
    57          LongIdentity c2 = new LongIdentity(Object.class, new Long(1));
    58          LongIdentity c3 = new LongIdentity(Object.class, new Long(2));
    59          assertEquals ("Equal LongIdentity instances compare not equal.", c1, c2);
    60          assertFalse ("Not equal LongIdentity instances compare equal", c1.equals(c3));
    61      }
    62
    63      public void testToStringConstructor() {
    64          LongIdentity c1 = new LongIdentity(Object.class, Long.MAX_VALUE);
    65          LongIdentity c2 = new LongIdentity(Object.class, c1.toString());
    66          assertEquals ("Equal LongIdentity instances compare not equal.", c1, c2);
    67      }
    68
    69      public void testStringConstructor() {
    70          LongIdentity c1 = new LongIdentity(Object.class, 1);
    71          LongIdentity c2 = new LongIdentity(Object.class, "1");
    72          LongIdentity c3 = new LongIdentity(Object.class, "2");
    73          assertEquals ("Equal LongIdentity instances compare not equal.", c1, c2);
    74          assertFalse ("Not equal LongIdentity instances compare equal", c1.equals(c3));
    75      }
    76
    77      public void testIllegalStringConstructor() {
    78          try {
    79              LongIdentity c1 = new LongIdentity(Object.class, "b");
    80          } catch (IllegalArgumentException iae) {
    81              return; // good
    82          }
    83          fail ("No exception caught for illegal String.");
    84      }
    85
    86      public void testSerialized() {
    87          LongIdentity c1 = new LongIdentity(Object.class, 1);
    88          LongIdentity c2 = new LongIdentity(Object.class, "1");
    89          LongIdentity c3 = new LongIdentity(Object.class, "2");
    90          Object[] scis = writeReadSerialized(new Object[] {c1, c2, c3});
    91          Object sc1 = scis[0];
    92          Object sc2 = scis[1];
    93          Object sc3 = scis[2];
    94          assertEquals ("Equal LongIdentity instances compare not equal.", c1, sc1);
    95          assertEquals ("Equal LongIdentity instances compare not equal.", c2, sc2);
    96          assertEquals ("Equal LongIdentity instances compare not equal.", sc1, c2);
    97          assertEquals ("Equal LongIdentity instances compare not equal.", sc2, c1);
    98          assertFalse ("Not equal LongIdentity instances compare equal.", c1.equals(sc3));
    99          assertFalse ("Not equal LongIdentity instances compare equal.", sc1.equals(c3));
    100         assertFalse ("Not equal LongIdentity instances compare equal.", sc1.equals(sc3));
    101         assertFalse ("Not equal LongIdentity instances compare equal.", sc3.equals(sc1));
    102     }
    103
    104     public void testGetKeyAsObjectPrimitive() {
    105         LongIdentity c1 = new LongIdentity(Object.class, 1L);
    106         assertEquals("keyAsObject doesn't match.", c1.getKeyAsObject(), new Long(1L));
    107     }
    108
    109     public void testGetKeyAsObject() {
    110         LongIdentity c1 = new LongIdentity(Object.class, new Long(1L));
    111         assertEquals("keyAsObject doesn't match.", c1.getKeyAsObject(), new Long(1L));
    112     }
    113
    114     public void testBadConstructorNullShortParam() {
    115         try {
    116             LongIdentity c1 = new LongIdentity(Object.class, (Long)null);
    117         } catch (JDONullIdentityException ex) {
    118             return;
    119         }
    120         fail ("Failed to catch expected exception.");
    121     }
    122
    123     public void testBadConstructorNullStringParam() {
    124         try {
    125             LongIdentity c1 = new LongIdentity(Object.class, (String)null);
    126         } catch (JDONullIdentityException ex) {
    127             return;
    128         }
    129         fail ("Failed to catch expected exception.");
    130     }
    131
    132     public void testCompareTo() {
    133     	LongIdentity c1 = new LongIdentity(Object.class, 1);
    134     	LongIdentity c2 = new LongIdentity(Object.class, 1);
    135     	LongIdentity c3 = new LongIdentity(Object.class, 2);
    136         LongIdentity c4 = new LongIdentity(Class.class, 1);
    137     	LongIdentity c5 = new LongIdentity(Object.class, 0x100000001L);
    138         assertEquals("Equal LongIdentity instances compare not equal.", 0, c1.compareTo(c2));
    139         assertTrue("Not equal LongIdentity instances have wrong compareTo result", c1.compareTo(c3) < 0);
    140         assertTrue("Not equal LongIdentity instances have wrong compareTo result", c3.compareTo(c1) > 0);
    141         assertTrue("Not equal LongIdentity instances have wrong compareTo result", c1.compareTo(c4) > 0);
    142         assertTrue("Not equal LongIdentity instances have wrong compareTo result", c5.compareTo(c1) > 0);
    143     }
    144 }