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 junit.framework.Assert;
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  /***
25   * TestCase for ComboKey
26   *
27   * @author <a href="mailto:drfish@cox.net">J. Russell Smyth</a>
28   * @version $Id: ComboKeyTest.java 239636 2005-08-24 12:38:09Z henning $
29   */
30  public class ComboKeyTest extends TestCase
31  {
32      private ComboKey c1a = new ComboKey(
33          new SimpleKey[]{new StringKey("key1"), new StringKey("key2")});
34      private ComboKey c1b = new ComboKey(
35          new SimpleKey[]{new StringKey("key1"), new StringKey("key2")});
36      private ComboKey c2a = new ComboKey(
37          new SimpleKey[]{new StringKey("key3"), new StringKey("key4")});
38      // complex keys for test
39      private java.util.Date now = new java.util.Date();
40      private ComboKey c3a = new ComboKey(
41          new SimpleKey[]{new StringKey("key1"), null, new DateKey(now)});
42      private ComboKey c4a = new ComboKey(
43          new SimpleKey[]{new StringKey("key1"), null, new NumberKey(123456)});
44  
45      /***
46       * Simple constructor.
47       *
48       * @param name the name of the test to execute
49       */
50      public ComboKeyTest(String name)
51      {
52          super(name);
53      }
54  
55          /***
56           *
57           * @param args
58           */
59      public static void main(java.lang.String[] args)
60      {
61          junit.textui.TestRunner.run(suite());
62      }
63  
64          /***
65           *
66           * @return Test
67           */
68      public static Test suite()
69      {
70          TestSuite suite = new TestSuite(ComboKeyTest.class);
71  
72          return suite;
73      }
74  
75          /***
76           *
77           *
78           */
79      public void testReflexive()
80      {
81          Assert.assertTrue(c1a.equals(c1a));
82          // Complex key using null and date
83          // This currently has to use looseEquals as ComboKey.equals(Obj)
84          // does not accept null key values (WHY!)
85          Assert.assertTrue(c3a.looseEquals(c3a));
86      }
87  
88          /***
89           *
90           *
91           */
92      public void testSymmetric()
93      {
94          Assert.assertTrue(c1a.equals(c1b));
95          Assert.assertTrue(c1b.equals(c1a));
96      }
97  
98          /***
99           *
100          *
101          */
102     public void testNull()
103     {
104         Assert.assertTrue(!c1a.equals(null));
105     }
106 
107         /***
108          *
109          *
110          */
111     public void testNotEqual()
112     {
113         Assert.assertTrue(!c1a.equals(c2a));
114     }
115 
116         /***
117          *
118          *
119          */
120     public void testRoundTripWithStringKeys()
121     {
122         // two strings
123         ComboKey oldKey = new ComboKey(
124             new SimpleKey[]{new StringKey("key1"), new StringKey("key2")});
125         ComboKey newKey = null;
126         String stringValue = oldKey.toString();
127         try
128         {
129             newKey = new ComboKey(stringValue);
130         }
131         catch(Exception e)
132         {
133             fail("Exception " + e.getClass().getName()
134                      + " thrown on new ComboKey(" + stringValue + "):"
135                      + e.getMessage());
136         }
137         Assert.assertEquals(oldKey,newKey);
138     }
139 
140         /***
141          *
142          *
143          */
144     public void testRoundTripWithComplexKey()
145     {
146         // complex key
147         ComboKey oldKey = new ComboKey(
148             new SimpleKey[]{new StringKey("key1"), new NumberKey(12345),
149             new DateKey(new java.util.Date())});
150         ComboKey newKey = null;
151         String stringValue = oldKey.toString();
152         try
153         {
154             newKey = new ComboKey(stringValue);
155         }
156         catch (Exception e)
157         {
158             fail("Exception " + e.getClass().getName()
159                     + " thrown on new ComboKey("
160                     + stringValue + "):" + e.getMessage());
161         }
162         Assert.assertEquals(oldKey,newKey);
163     }
164 
165         /***
166          *
167          *
168          */
169     public void testRoundTripWithNullKey()
170     {
171         // with null key
172         ComboKey oldKey = new ComboKey(
173             new SimpleKey[]{new StringKey("key1"), null});
174         ComboKey newKey = null;
175         String stringValue = oldKey.toString();
176         try
177         {
178             newKey = new ComboKey(stringValue);
179         }
180         catch (Exception e)
181         {
182             fail("Exception " + e.getClass().getName()
183                     + " thrown on new ComboKey("
184                     + stringValue + "):" + e.getMessage());
185         }
186         // This currently has to use looseEquals as ComboKey.equals(Obj)
187         // does not accept null key values (WHY!)
188         Assert.assertTrue(oldKey.looseEquals(newKey));
189     }
190 
191 
192     /***
193      * Test of appendTo method, of class org.apache.torque.om.ComboKey.
194      */
195     public void testAppendTo()
196     {
197         StringBuffer sb = new StringBuffer();
198         c1a.appendTo(sb);
199         Assert.assertEquals("Skey1:Skey2:", sb.toString());
200     }
201 
202     /***
203      * Test of toString method, of class org.apache.torque.om.ComboKey.
204      */
205     public void testToString()
206     {
207         Assert.assertEquals("Skey1::N123456:", c4a.toString());
208     }
209 }