001: package org.apache.torque.om;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import junit.framework.Assert;
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: /**
028: * TestCase for ComboKey
029: *
030: * @author <a href="mailto:drfish@cox.net">J. Russell Smyth</a>
031: * @version $Id: ComboKeyTest.java 473821 2006-11-11 22:37:25Z tv $
032: */
033: public class ComboKeyTest extends TestCase {
034: private ComboKey c1a = new ComboKey(new SimpleKey[] {
035: new StringKey("key1"), new StringKey("key2") });
036: private ComboKey c1b = new ComboKey(new SimpleKey[] {
037: new StringKey("key1"), new StringKey("key2") });
038: private ComboKey c2a = new ComboKey(new SimpleKey[] {
039: new StringKey("key3"), new StringKey("key4") });
040: // complex keys for test
041: private java.util.Date now = new java.util.Date();
042: private ComboKey c3a = new ComboKey(new SimpleKey[] {
043: new StringKey("key1"), null, new DateKey(now) });
044: private ComboKey c4a = new ComboKey(new SimpleKey[] {
045: new StringKey("key1"), null, new NumberKey(123456) });
046:
047: /**
048: * Simple constructor.
049: *
050: * @param name the name of the test to execute
051: */
052: public ComboKeyTest(String name) {
053: super (name);
054: }
055:
056: /**
057: *
058: * @param args
059: */
060: public static void main(java.lang.String[] args) {
061: junit.textui.TestRunner.run(suite());
062: }
063:
064: /**
065: *
066: * @return Test
067: */
068: public static Test suite() {
069: TestSuite suite = new TestSuite(ComboKeyTest.class);
070:
071: return suite;
072: }
073:
074: /**
075: *
076: *
077: */
078: public void testReflexive() {
079: Assert.assertTrue(c1a.equals(c1a));
080: // Complex key using null and date
081: // This currently has to use looseEquals as ComboKey.equals(Obj)
082: // does not accept null key values (WHY!)
083: Assert.assertTrue(c3a.looseEquals(c3a));
084: }
085:
086: /**
087: *
088: *
089: */
090: public void testSymmetric() {
091: Assert.assertTrue(c1a.equals(c1b));
092: Assert.assertTrue(c1b.equals(c1a));
093: }
094:
095: /**
096: *
097: *
098: */
099: public void testNull() {
100: Assert.assertTrue(!c1a.equals(null));
101: }
102:
103: /**
104: *
105: *
106: */
107: public void testNotEqual() {
108: Assert.assertTrue(!c1a.equals(c2a));
109: }
110:
111: /**
112: *
113: *
114: */
115: public void testRoundTripWithStringKeys() {
116: // two strings
117: ComboKey oldKey = new ComboKey(new SimpleKey[] {
118: new StringKey("key1"), new StringKey("key2") });
119: ComboKey newKey = null;
120: String stringValue = oldKey.toString();
121: try {
122: newKey = new ComboKey(stringValue);
123: } catch (Exception e) {
124: fail("Exception " + e.getClass().getName()
125: + " thrown on new ComboKey(" + stringValue + "):"
126: + e.getMessage());
127: }
128: Assert.assertEquals(oldKey, newKey);
129: }
130:
131: /**
132: *
133: *
134: */
135: public void testRoundTripWithComplexKey() {
136: // complex key
137: ComboKey oldKey = new ComboKey(new SimpleKey[] {
138: new StringKey("key1"), new NumberKey(12345),
139: new DateKey(new java.util.Date()) });
140: ComboKey newKey = null;
141: String stringValue = oldKey.toString();
142: try {
143: newKey = new ComboKey(stringValue);
144: } catch (Exception e) {
145: fail("Exception " + e.getClass().getName()
146: + " thrown on new ComboKey(" + stringValue + "):"
147: + e.getMessage());
148: }
149: Assert.assertEquals(oldKey, newKey);
150: }
151:
152: /**
153: *
154: *
155: */
156: public void testRoundTripWithNullKey() {
157: // with null key
158: ComboKey oldKey = new ComboKey(new SimpleKey[] {
159: new StringKey("key1"), null });
160: ComboKey newKey = null;
161: String stringValue = oldKey.toString();
162: try {
163: newKey = new ComboKey(stringValue);
164: } catch (Exception e) {
165: fail("Exception " + e.getClass().getName()
166: + " thrown on new ComboKey(" + stringValue + "):"
167: + e.getMessage());
168: }
169: // This currently has to use looseEquals as ComboKey.equals(Obj)
170: // does not accept null key values (WHY!)
171: Assert.assertTrue(oldKey.looseEquals(newKey));
172: }
173:
174: /**
175: * Test of appendTo method, of class org.apache.torque.om.ComboKey.
176: */
177: public void testAppendTo() {
178: StringBuffer sb = new StringBuffer();
179: c1a.appendTo(sb);
180: Assert.assertEquals("Skey1:Skey2:", sb.toString());
181: }
182:
183: /**
184: * Test of toString method, of class org.apache.torque.om.ComboKey.
185: */
186: public void testToString() {
187: Assert.assertEquals("Skey1::N123456:", c4a.toString());
188: }
189: }
|