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