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   * StringIdentityTest.java
19   *
20   */
21  
22  package javax.jdo.identity;
23  
24  import javax.jdo.JDONullIdentityException;
25  import javax.jdo.JDOUserException;
26  
27  import javax.jdo.util.BatchTestRunner;
28  
29  /***
30   *
31   */
32  public class StringIdentityTest extends SingleFieldIdentityTest {
33      
34      /*** Creates a new instance of StringIdentityTest */
35      public StringIdentityTest() {
36      }
37      
38      /***
39       * @param args the command line arguments
40       */
41      public static void main(String[] args) {
42          BatchTestRunner.run(StringIdentityTest.class);
43      }
44      
45      public void testConstructor() {
46          StringIdentity c1 = new StringIdentity(Object.class, "1");
47          StringIdentity c2 = new StringIdentity(Object.class, "1");
48          StringIdentity c3 = new StringIdentity(Object.class, "2");
49          assertEquals("Equal StringIdentity instances compare not equal.", c1, c2);
50          assertFalse ("Not equal StringIdentity instances compare equal", c1.equals(c3));
51      }
52      
53      public void testToStringConstructor() {
54          StringIdentity c1 = new StringIdentity(Object.class, "Now who's talking!");
55          StringIdentity c2 = new StringIdentity(Object.class, c1.toString());
56          assertEquals ("Equal StringIdentity instances compare not equal.", c1, c2);
57      }
58  
59      public void testSerialized() {
60          StringIdentity c1 = new StringIdentity(Object.class, "1");
61          StringIdentity c2 = new StringIdentity(Object.class, "1");
62          StringIdentity c3 = new StringIdentity(Object.class, "2");
63          Object[] scis = writeReadSerialized(new Object[] {c1, c2, c3});
64          Object sc1 = scis[0];
65          Object sc2 = scis[1];
66          Object sc3 = scis[2];
67          assertEquals ("Equal StringIdentity instances compare not equal.", c1, sc1);
68          assertEquals ("Equal StringIdentity instances compare not equal.", c2, sc2);
69          assertEquals ("Equal StringIdentity instances compare not equal.", sc1, c2);
70          assertEquals ("Equal StringIdentity instances compare not equal.", sc2, c1);
71          assertFalse ("Not equal StringIdentity instances compare equal.", c1.equals(sc3));
72          assertFalse ("Not equal StringIdentity instances compare equal.", sc1.equals(c3));
73          assertFalse ("Not equal StringIdentity instances compare equal.", sc1.equals(sc3));
74          assertFalse ("Not equal StringIdentity instances compare equal.", sc3.equals(sc1));
75      }
76  
77      public void testGetKeyAsObject() {
78          StringIdentity c1 = new StringIdentity(Object.class, "1");
79          assertEquals("keyAsObject doesn't match.", c1.getKeyAsObject(), "1");
80      }
81  
82      public void testBadConstructorNullParam() {
83          try {
84              StringIdentity c1 = new StringIdentity(Object.class, null);
85          } catch (JDONullIdentityException ex) {
86              return;
87          }
88          fail ("Failed to catch expected exception.");
89      }
90  
91  }