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