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