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