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