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          assertEquals("Equal LongIdentity instances compare not equal.", c1, c2);
50          assertFalse ("Not equal LongIdentity instances compare equal", c1.equals(c3));
51      }
52  
53      public void testLongConstructor() {
54          LongIdentity c1 = new LongIdentity(Object.class, 1);
55          LongIdentity c2 = new LongIdentity(Object.class, new Long(1));
56          LongIdentity c3 = new LongIdentity(Object.class, new Long(2));
57          assertEquals ("Equal LongIdentity instances compare not equal.", c1, c2);
58          assertFalse ("Not equal LongIdentity instances compare equal", c1.equals(c3));
59      }
60  
61      public void testToStringConstructor() {
62          LongIdentity c1 = new LongIdentity(Object.class, Long.MAX_VALUE);
63          LongIdentity c2 = new LongIdentity(Object.class, c1.toString());
64          assertEquals ("Equal LongIdentity instances compare not equal.", c1, c2);
65      }
66  
67      public void testStringConstructor() {
68          LongIdentity c1 = new LongIdentity(Object.class, 1);
69          LongIdentity c2 = new LongIdentity(Object.class, "1");
70          LongIdentity c3 = new LongIdentity(Object.class, "2");
71          assertEquals ("Equal LongIdentity instances compare not equal.", c1, c2);
72          assertFalse ("Not equal LongIdentity instances compare equal", c1.equals(c3));
73      }
74      
75      public void testIllegalStringConstructor() {
76          try {
77              LongIdentity c1 = new LongIdentity(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          LongIdentity c1 = new LongIdentity(Object.class, 1);
86          LongIdentity c2 = new LongIdentity(Object.class, "1");
87          LongIdentity c3 = new LongIdentity(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 LongIdentity instances compare not equal.", c1, sc1);
93          assertEquals ("Equal LongIdentity instances compare not equal.", c2, sc2);
94          assertEquals ("Equal LongIdentity instances compare not equal.", sc1, c2);
95          assertEquals ("Equal LongIdentity instances compare not equal.", sc2, c1);
96          assertFalse ("Not equal LongIdentity instances compare equal.", c1.equals(sc3));
97          assertFalse ("Not equal LongIdentity instances compare equal.", sc1.equals(c3));
98          assertFalse ("Not equal LongIdentity instances compare equal.", sc1.equals(sc3));
99          assertFalse ("Not equal LongIdentity instances compare equal.", sc3.equals(sc1));
100     }
101     
102     public void testGetKeyAsObjectPrimitive() {
103         LongIdentity c1 = new LongIdentity(Object.class, 1L);
104         assertEquals("keyAsObject doesn't match.", c1.getKeyAsObject(), new Long(1L));
105     }
106 
107     public void testGetKeyAsObject() {
108         LongIdentity c1 = new LongIdentity(Object.class, new Long(1L));
109         assertEquals("keyAsObject doesn't match.", c1.getKeyAsObject(), new Long(1L));
110     }
111 
112     public void testBadConstructorNullShortParam() {
113         try {
114             LongIdentity c1 = new LongIdentity(Object.class, (Long)null);
115         } catch (JDONullIdentityException ex) {
116             return;
117         }
118         fail ("Failed to catch expected exception.");
119     }
120 
121     public void testBadConstructorNullStringParam() {
122         try {
123             LongIdentity c1 = new LongIdentity(Object.class, (String)null);
124         } catch (JDONullIdentityException ex) {
125             return;
126         }
127         fail ("Failed to catch expected exception.");
128     }
129 
130 }