1   package org.apache.torque.om;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Arrays;
20  
21  import junit.framework.Assert;
22  import junit.framework.TestCase;
23  
24  /***
25   * Currently just tests the equality of NumberKey.
26   *
27   * @author <a href="mailto:stephenh@chase3000.com">Stephen Haberman</a>
28   * @version $Id: NumberKeyTest.java 239636 2005-08-24 12:38:09Z henning $
29   */
30  public class NumberKeyTest extends TestCase
31  {
32  
33      /*** Test value. */
34      private NumberKey n1a = new NumberKey(1);
35      /*** Test value. */
36      private NumberKey n1b = new NumberKey(1);
37      /*** Test value. */
38      private NumberKey n1c = new NumberKey(1);
39      /*** Test value. */
40      private NumberKey n2a = new NumberKey(2);
41  
42      /***
43       * Simple constructor.
44       *
45       * @param name the name of the test to execute
46       */
47      public NumberKeyTest(String name)
48      {
49          super(name);
50      }
51  
52      /***
53       * Test a.equals(a)
54       */
55      public void testReflexive()
56      {
57          Assert.assertTrue(n1a.equals(n1a));
58      }
59  
60      /***
61       * Test a.equals(b) = b.equals(a)
62       */
63      public void testSymmetric()
64      {
65          Assert.assertTrue(n1a.equals(n1b));
66          Assert.assertTrue(n1b.equals(n1a));
67  
68          Assert.assertTrue(!"1".equals(n1a));
69          // As this used to give false, i.e. n1a was considered equal to "1"
70          // it can lead to difficult to find bugs if it is immediately
71          // changed to the opposite.  So this will throw an exception.
72          //Assert.assertTrue(!n1a.equals("1"));
73          try
74          {
75              Assert.assertTrue(!n1a.equals("1"));
76          }
77          catch (IllegalArgumentException e)
78          {
79              // expected
80          }
81          Assert.assertTrue(!n1a.equals(new Integer(1)));
82          Assert.assertTrue(!new Integer(1).equals(n1a));
83      }
84  
85      /***
86       * Test a.equals(b) = b.equals(c) = c.equals(a)
87       */
88      public void testTransitive()
89      {
90          Assert.assertTrue(n1a.equals(n1b));
91          Assert.assertTrue(n1b.equals(n1c));
92          Assert.assertTrue(n1c.equals(n1a));
93      }
94  
95      /***
96       * Test !a.equals(null)
97       */
98      public void testNull()
99      {
100         Assert.assertTrue(!n1a.equals(null));
101     }
102 
103     /***
104      * Test sorting.
105      */
106     public void testList()
107     {
108         Object[] array = new Object[] { n1a, n2a, n1b };
109         Arrays.sort(array);
110 
111         Assert.assertEquals(n1a, array[0]);
112         Assert.assertEquals(n1b, array[1]);
113         Assert.assertEquals(n2a, array[2]);
114     }
115 
116     /***
117      * Test long constructor
118      */
119     public void testLongConstructor()
120     {
121         NumberKey key = new NumberKey(9900000000000001L);
122         assertEquals("9900000000000001", key.toString());
123     }
124 }