1   package org.apache.torque.om;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.Assert;
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  
27  /***
28   * TestCase for ComboKey
29   *
30   * @author <a href="mailto:drfish@cox.net">J. Russell Smyth</a>
31   * @version $Id: ComboKeyTest.java 473821 2006-11-11 22:37:25Z tv $
32   */
33  public class ComboKeyTest extends TestCase
34  {
35      private ComboKey c1a = new ComboKey(
36          new SimpleKey[]{new StringKey("key1"), new StringKey("key2")});
37      private ComboKey c1b = new ComboKey(
38          new SimpleKey[]{new StringKey("key1"), new StringKey("key2")});
39      private ComboKey c2a = new ComboKey(
40          new SimpleKey[]{new StringKey("key3"), new StringKey("key4")});
41      // complex keys for test
42      private java.util.Date now = new java.util.Date();
43      private ComboKey c3a = new ComboKey(
44          new SimpleKey[]{new StringKey("key1"), null, new DateKey(now)});
45      private ComboKey c4a = new ComboKey(
46          new SimpleKey[]{new StringKey("key1"), null, new NumberKey(123456)});
47  
48      /***
49       * Simple constructor.
50       *
51       * @param name the name of the test to execute
52       */
53      public ComboKeyTest(String name)
54      {
55          super(name);
56      }
57  
58          /***
59           *
60           * @param args
61           */
62      public static void main(java.lang.String[] args)
63      {
64          junit.textui.TestRunner.run(suite());
65      }
66  
67          /***
68           *
69           * @return Test
70           */
71      public static Test suite()
72      {
73          TestSuite suite = new TestSuite(ComboKeyTest.class);
74  
75          return suite;
76      }
77  
78          /***
79           *
80           *
81           */
82      public void testReflexive()
83      {
84          Assert.assertTrue(c1a.equals(c1a));
85          // Complex key using null and date
86          // This currently has to use looseEquals as ComboKey.equals(Obj)
87          // does not accept null key values (WHY!)
88          Assert.assertTrue(c3a.looseEquals(c3a));
89      }
90  
91          /***
92           *
93           *
94           */
95      public void testSymmetric()
96      {
97          Assert.assertTrue(c1a.equals(c1b));
98          Assert.assertTrue(c1b.equals(c1a));
99      }
100 
101         /***
102          *
103          *
104          */
105     public void testNull()
106     {
107         Assert.assertTrue(!c1a.equals(null));
108     }
109 
110         /***
111          *
112          *
113          */
114     public void testNotEqual()
115     {
116         Assert.assertTrue(!c1a.equals(c2a));
117     }
118 
119         /***
120          *
121          *
122          */
123     public void testRoundTripWithStringKeys()
124     {
125         // two strings
126         ComboKey oldKey = new ComboKey(
127             new SimpleKey[]{new StringKey("key1"), new StringKey("key2")});
128         ComboKey newKey = null;
129         String stringValue = oldKey.toString();
130         try
131         {
132             newKey = new ComboKey(stringValue);
133         }
134         catch(Exception e)
135         {
136             fail("Exception " + e.getClass().getName()
137                      + " thrown on new ComboKey(" + stringValue + "):"
138                      + e.getMessage());
139         }
140         Assert.assertEquals(oldKey,newKey);
141     }
142 
143         /***
144          *
145          *
146          */
147     public void testRoundTripWithComplexKey()
148     {
149         // complex key
150         ComboKey oldKey = new ComboKey(
151             new SimpleKey[]{new StringKey("key1"), new NumberKey(12345),
152             new DateKey(new java.util.Date())});
153         ComboKey newKey = null;
154         String stringValue = oldKey.toString();
155         try
156         {
157             newKey = new ComboKey(stringValue);
158         }
159         catch (Exception e)
160         {
161             fail("Exception " + e.getClass().getName()
162                     + " thrown on new ComboKey("
163                     + stringValue + "):" + e.getMessage());
164         }
165         Assert.assertEquals(oldKey,newKey);
166     }
167 
168         /***
169          *
170          *
171          */
172     public void testRoundTripWithNullKey()
173     {
174         // with null key
175         ComboKey oldKey = new ComboKey(
176             new SimpleKey[]{new StringKey("key1"), null});
177         ComboKey newKey = null;
178         String stringValue = oldKey.toString();
179         try
180         {
181             newKey = new ComboKey(stringValue);
182         }
183         catch (Exception e)
184         {
185             fail("Exception " + e.getClass().getName()
186                     + " thrown on new ComboKey("
187                     + stringValue + "):" + e.getMessage());
188         }
189         // This currently has to use looseEquals as ComboKey.equals(Obj)
190         // does not accept null key values (WHY!)
191         Assert.assertTrue(oldKey.looseEquals(newKey));
192     }
193 
194 
195     /***
196      * Test of appendTo method, of class org.apache.torque.om.ComboKey.
197      */
198     public void testAppendTo()
199     {
200         StringBuffer sb = new StringBuffer();
201         c1a.appendTo(sb);
202         Assert.assertEquals("Skey1:Skey2:", sb.toString());
203     }
204 
205     /***
206      * Test of toString method, of class org.apache.torque.om.ComboKey.
207      */
208     public void testToString()
209     {
210         Assert.assertEquals("Skey1::N123456:", c4a.toString());
211     }
212 }