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