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