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   * CharIdentityTest.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   * @author clr
32   */
33  public class CharIdentityTest extends SingleFieldIdentityTest {
34      
35      /** Creates a new instance of CharIdentityTest */
36      public CharIdentityTest() {
37      }
38      
39      /**
40       * @param args the command line arguments
41       */
42      public static void main(String[] args) {
43          BatchTestRunner.run(CharIdentityTest.class);
44      }
45      
46      public void testConstructor() {
47          CharIdentity c1 = new CharIdentity(Object.class, 'a');
48          CharIdentity c2 = new CharIdentity(Object.class, 'a');
49          CharIdentity c3 = new CharIdentity(Object.class, 'b');
50          assertEquals("Equal CharIdentity instances compare not equal.", c1, c2);
51          assertFalse ("Not equal CharIdentity instances compare equal", c1.equals(c3));
52      }
53      
54      public void testCharacterConstructor() {
55          CharIdentity c1 = new CharIdentity(Object.class, 'a');
56          CharIdentity c2 = new CharIdentity(Object.class, new Character('a'));
57          CharIdentity c3 = new CharIdentity(Object.class, new Character('b'));
58          assertEquals("Equal CharIdentity instances compare not equal.", c1, c2);
59          assertFalse ("Not equal CharIdentity instances compare equal", c1.equals(c3));
60      }
61      
62      public void testToStringConstructor() {
63          CharIdentity c1 = new CharIdentity(Object.class, 'a');
64          CharIdentity c2 = new CharIdentity(Object.class, c1.toString());
65          assertEquals ("Equal CharIdentity instances compare not equal.", c1, c2);
66      }
67      
68      public void testStringConstructor() {
69          CharIdentity c1 = new CharIdentity(Object.class, 'a');
70          CharIdentity c2 = new CharIdentity(Object.class, "a");
71          CharIdentity c3 = new CharIdentity(Object.class, "b");
72          assertEquals ("Equal CharIdentity instances compare not equal.", c1, c2);
73          assertFalse ("Not equal CharIdentity instances compare equal", c1.equals(c3));
74      }
75      
76      public void testStringConstructorTooLong() {
77          try {
78              CharIdentity c1 = new CharIdentity(Object.class, "ab");
79          } catch (IllegalArgumentException iae) {
80              return; // good
81          }
82          fail ("No exception caught for String too long.");
83      }
84      
85      public void testStringConstructorTooShort() {
86          try {
87              CharIdentity c1 = new CharIdentity(Object.class, "");
88          } catch (IllegalArgumentException iae) {
89              return; // good
90          }
91          fail ("No exception caught for String too short.");
92      }
93      
94      public void testSerialized() {
95          CharIdentity c1 = new CharIdentity(Object.class, 'a');
96          CharIdentity c2 = new CharIdentity(Object.class, "a");
97          CharIdentity c3 = new CharIdentity(Object.class, "b");
98          Object[] scis = writeReadSerialized(new Object[] {c1, c2, c3});
99          Object sc1 = scis[0];
100         Object sc2 = scis[1];
101         Object sc3 = scis[2];
102         assertEquals ("Equal CharIdentity instances compare not equal.", c1, sc1);
103         assertEquals ("Equal CharIdentity instances compare not equal.", c2, sc2);
104         assertEquals ("Equal CharIdentity instances compare not equal.", sc1, c2);
105         assertEquals ("Equal CharIdentity instances compare not equal.", sc2, c1);
106         assertFalse ("Not equal CharIdentity instances compare equal.", c1.equals(sc3));
107         assertFalse ("Not equal CharIdentity instances compare equal.", sc1.equals(c3));
108         assertFalse ("Not equal CharIdentity instances compare equal.", sc1.equals(sc3));
109         assertFalse ("Not equal CharIdentity instances compare equal.", sc3.equals(sc1));
110     }
111     public void testGetKeyAsObjectPrimitive() {
112         CharIdentity c1 = new CharIdentity(Object.class, '1');
113         assertEquals("keyAsObject doesn't match.", c1.getKeyAsObject(), new Character('1'));
114     }
115 
116     public void testGetKeyAsObject() {
117         CharIdentity c1 = new CharIdentity(Object.class, new Character('1'));
118         assertEquals("keyAsObject doesn't match.", c1.getKeyAsObject(), new Character('1'));
119     }
120 
121     public void testBadConstructorNullCharacterParam() {
122         try {
123             CharIdentity c1 = new CharIdentity(Object.class, (Character)null);
124         } catch (JDONullIdentityException ex) {
125             return;
126         }
127         fail ("Failed to catch expected exception.");
128     }
129 
130     public void testBadConstructorNullStringParam() {
131         try {
132             CharIdentity c1 = new CharIdentity(Object.class, (String)null);
133         } catch (JDONullIdentityException ex) {
134             return;
135         }
136         fail ("Failed to catch expected exception.");
137     }
138 
139     public void testCompareTo() {
140     	CharIdentity c1 = new CharIdentity(Object.class, '1');
141     	CharIdentity c2 = new CharIdentity(Object.class, '1');
142     	CharIdentity c3 = new CharIdentity(Object.class, '2');
143         CharIdentity c4 = new CharIdentity(Class.class, '1');
144         assertEquals("Equal CharIdentity instances compare not equal.", 0, c1.compareTo(c2));
145         assertTrue("Not equal CharIdentity instances have wrong compareTo result", c1.compareTo(c3) < 0);
146         assertTrue("Not equal CharIdentity instances have wrong compareTo result", c3.compareTo(c1) > 0); 
147         assertTrue("Not equal CharIdentity instances have wrong compareTo result", c1.compareTo(c4) > 0);
148     }
149 }