1 package org.apache.torque.om; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001-2003 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>. 55 */ 56 57 import junit.framework.Assert; 58 import junit.framework.Test; 59 import junit.framework.TestCase; 60 import junit.framework.TestSuite; 61 62 /*** 63 * TestCase for ComboKey 64 * 65 * @author <a href="mailto:drfish@cox.net">J. Russell Smyth</a> 66 * @version $Revision: 1.4 $ 67 */ 68 public class ComboKeyTest extends TestCase 69 { 70 private ComboKey c1a = new ComboKey( 71 new SimpleKey[]{new StringKey("key1"), new StringKey("key2")}); 72 private ComboKey c1b = new ComboKey( 73 new SimpleKey[]{new StringKey("key1"), new StringKey("key2")}); 74 private ComboKey c2a = new ComboKey( 75 new SimpleKey[]{new StringKey("key3"), new StringKey("key4")}); 76 // complex keys for test 77 private java.util.Date now = new java.util.Date(); 78 private ComboKey c3a = new ComboKey( 79 new SimpleKey[]{new StringKey("key1"), null, new DateKey(now)}); 80 private ComboKey c4a = new ComboKey( 81 new SimpleKey[]{new StringKey("key1"), null, new NumberKey(123456)}); 82 83 /*** 84 * Simple constructor. 85 * 86 * @param name the name of the test to execute 87 */ 88 public ComboKeyTest(String name) 89 { 90 super(name); 91 } 92 93 /*** 94 * 95 * @param args 96 */ 97 public static void main(java.lang.String[] args) 98 { 99 junit.textui.TestRunner.run(suite()); 100 } 101 102 /*** 103 * 104 * @return Test 105 */ 106 public static Test suite() 107 { 108 TestSuite suite = new TestSuite(ComboKeyTest.class); 109 110 return suite; 111 } 112 113 /*** 114 * 115 * 116 */ 117 public void testReflexive() 118 { 119 Assert.assertTrue(c1a.equals(c1a)); 120 // Complex key using null and date 121 // This currently has to use looseEquals as ComboKey.equals(Obj) 122 // does not accept null key values (WHY!) 123 Assert.assertTrue(c3a.looseEquals(c3a)); 124 } 125 126 /*** 127 * 128 * 129 */ 130 public void testSymmetric() 131 { 132 Assert.assertTrue(c1a.equals(c1b)); 133 Assert.assertTrue(c1b.equals(c1a)); 134 } 135 136 /*** 137 * 138 * 139 */ 140 public void testNull() 141 { 142 Assert.assertTrue(!c1a.equals(null)); 143 } 144 145 /*** 146 * 147 * 148 */ 149 public void testNotEqual() 150 { 151 Assert.assertTrue(!c1a.equals(c2a)); 152 } 153 154 /*** 155 * 156 * 157 */ 158 public void testRoundTripWithStringKeys() 159 { 160 // two strings 161 ComboKey oldKey = new ComboKey( 162 new SimpleKey[]{new StringKey("key1"), new StringKey("key2")}); 163 ComboKey newKey = null; 164 String stringValue = oldKey.toString(); 165 try 166 { 167 newKey = new ComboKey(stringValue); 168 } 169 catch(Exception e) 170 { 171 fail("Exception " + e.getClass().getName() 172 + " thrown on new ComboKey(" + stringValue + "):" 173 + e.getMessage()); 174 } 175 Assert.assertEquals(oldKey,newKey); 176 } 177 178 /*** 179 * 180 * 181 */ 182 public void testRoundTripWithComplexKey() 183 { 184 // complex key 185 ComboKey oldKey = new ComboKey( 186 new SimpleKey[]{new StringKey("key1"), new NumberKey(12345), 187 new DateKey(new java.util.Date())}); 188 ComboKey newKey = null; 189 String stringValue = oldKey.toString(); 190 try 191 { 192 newKey = new ComboKey(stringValue); 193 } 194 catch (Exception e) 195 { 196 fail("Exception " + e.getClass().getName() 197 + " thrown on new ComboKey(" 198 + stringValue + "):" + e.getMessage()); 199 } 200 Assert.assertEquals(oldKey,newKey); 201 } 202 203 /*** 204 * 205 * 206 */ 207 public void testRoundTripWithNullKey() 208 { 209 // with null key 210 ComboKey oldKey = new ComboKey( 211 new SimpleKey[]{new StringKey("key1"), null}); 212 ComboKey newKey = null; 213 String stringValue = oldKey.toString(); 214 try 215 { 216 newKey = new ComboKey(stringValue); 217 } 218 catch (Exception e) 219 { 220 fail("Exception " + e.getClass().getName() 221 + " thrown on new ComboKey(" 222 + stringValue + "):" + e.getMessage()); 223 } 224 // This currently has to use looseEquals as ComboKey.equals(Obj) 225 // does not accept null key values (WHY!) 226 Assert.assertTrue(oldKey.looseEquals(newKey)); 227 } 228 229 230 /*** 231 * Test of appendTo method, of class org.apache.torque.om.ComboKey. 232 */ 233 public void testAppendTo() 234 { 235 StringBuffer sb = new StringBuffer(); 236 c1a.appendTo(sb); 237 Assert.assertEquals("Skey1:Skey2:", sb.toString()); 238 } 239 240 /*** 241 * Test of toString method, of class org.apache.torque.om.ComboKey. 242 */ 243 public void testToString() 244 { 245 Assert.assertEquals("Skey1::N123456:", c4a.toString()); 246 } 247 }

This page was automatically generated by Maven