1 16 package org.apache.commons.collections.keyvalue; 17 18 import java.util.Arrays ; 19 20 import junit.framework.Assert; 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 25 32 public class TestMultiKey extends TestCase { 33 34 Integer ONE = new Integer (1); 35 Integer TWO = new Integer (2); 36 Integer THREE = new Integer (3); 37 Integer FOUR = new Integer (4); 38 Integer FIVE = new Integer (5); 39 40 public TestMultiKey(String name) { 41 super(name); 42 } 43 44 public static Test suite() { 45 return new TestSuite(TestMultiKey.class); 46 } 47 48 public static void main(String [] args) { 49 String [] testCaseName = { TestMultiKey.class.getName() }; 50 junit.textui.TestRunner.main(testCaseName); 51 } 52 53 public void setUp() throws Exception { 54 super.setUp(); 55 } 56 57 protected void tearDown() throws Exception { 58 super.tearDown(); 59 } 60 61 public void testConstructors() throws Exception { 63 MultiKey mk = null; 64 mk = new MultiKey(ONE, TWO); 65 Assert.assertTrue(Arrays.equals(new Object [] {ONE, TWO}, mk.getKeys())); 66 67 mk = new MultiKey(ONE, TWO, THREE); 68 Assert.assertTrue(Arrays.equals(new Object [] {ONE, TWO, THREE}, mk.getKeys())); 69 70 mk = new MultiKey(ONE, TWO, THREE, FOUR); 71 Assert.assertTrue(Arrays.equals(new Object [] {ONE, TWO, THREE, FOUR}, mk.getKeys())); 72 73 mk = new MultiKey(ONE, TWO, THREE, FOUR, FIVE); 74 Assert.assertTrue(Arrays.equals(new Object [] {ONE, TWO, THREE, FOUR, FIVE}, mk.getKeys())); 75 76 mk = new MultiKey(new Object [] {THREE, FOUR, ONE, TWO}, false); 77 Assert.assertTrue(Arrays.equals(new Object [] {THREE, FOUR, ONE, TWO}, mk.getKeys())); 78 } 79 80 public void testConstructorsByArray() throws Exception { 81 MultiKey mk = null; 82 Object [] keys = new Object [] {THREE, FOUR, ONE, TWO}; 83 mk = new MultiKey(keys); 84 Assert.assertTrue(Arrays.equals(new Object [] {THREE, FOUR, ONE, TWO}, mk.getKeys())); 85 keys[3] = FIVE; Assert.assertTrue(Arrays.equals(new Object [] {THREE, FOUR, ONE, TWO}, mk.getKeys())); 87 88 keys = new Object [] {}; 89 mk = new MultiKey(keys); 90 Assert.assertTrue(Arrays.equals(new Object [] {}, mk.getKeys())); 91 92 keys = new Object [] {THREE, FOUR, ONE, TWO}; 93 mk = new MultiKey(keys, true); 94 Assert.assertTrue(Arrays.equals(new Object [] {THREE, FOUR, ONE, TWO}, mk.getKeys())); 95 keys[3] = FIVE; Assert.assertTrue(Arrays.equals(new Object [] {THREE, FOUR, ONE, TWO}, mk.getKeys())); 97 98 keys = new Object [] {THREE, FOUR, ONE, TWO}; 99 mk = new MultiKey(keys, false); 100 Assert.assertTrue(Arrays.equals(new Object [] {THREE, FOUR, ONE, TWO}, mk.getKeys())); 101 keys[3] = FIVE; 104 Assert.assertTrue(Arrays.equals(new Object [] {THREE, FOUR, ONE, FIVE}, mk.getKeys())); 105 } 106 107 public void testConstructorsByArrayNull() throws Exception { 108 Object [] keys = null; 109 try { 110 new MultiKey(keys); 111 fail(); 112 } catch (IllegalArgumentException ex) {} 113 try { 114 new MultiKey(keys, true); 115 fail(); 116 } catch (IllegalArgumentException ex) {} 117 try { 118 new MultiKey(keys, false); 119 fail(); 120 } catch (IllegalArgumentException ex) {} 121 } 122 123 public void testSize() { 124 Assert.assertEquals(2, new MultiKey(ONE, TWO).size()); 125 Assert.assertEquals(2, new MultiKey(null, null).size()); 126 Assert.assertEquals(3, new MultiKey(ONE, TWO, THREE).size()); 127 Assert.assertEquals(3, new MultiKey(null, null, null).size()); 128 Assert.assertEquals(4, new MultiKey(ONE, TWO, THREE, FOUR).size()); 129 Assert.assertEquals(4, new MultiKey(null, null, null, null).size()); 130 Assert.assertEquals(5, new MultiKey(ONE, TWO, THREE, FOUR, FIVE).size()); 131 Assert.assertEquals(5, new MultiKey(null, null, null, null, null).size()); 132 133 Assert.assertEquals(0, new MultiKey(new Object [] {}).size()); 134 Assert.assertEquals(1, new MultiKey(new Object [] {ONE}).size()); 135 Assert.assertEquals(2, new MultiKey(new Object [] {ONE, TWO}).size()); 136 Assert.assertEquals(7, new MultiKey(new Object [] {ONE, TWO, ONE, TWO, ONE, TWO, ONE}).size()); 137 } 138 139 public void testGetIndexed() { 140 MultiKey mk = new MultiKey(ONE, TWO); 141 Assert.assertSame(ONE, mk.getKey(0)); 142 Assert.assertSame(TWO, mk.getKey(1)); 143 try { 144 mk.getKey(-1); 145 fail(); 146 } catch (IndexOutOfBoundsException ex) {} 147 try { 148 mk.getKey(2); 149 fail(); 150 } catch (IndexOutOfBoundsException ex) {} 151 } 152 153 public void testGetKeysSimpleConstructor() { 154 MultiKey mk = new MultiKey(ONE, TWO); 155 Object [] array = mk.getKeys(); 156 Assert.assertSame(ONE, array[0]); 157 Assert.assertSame(TWO, array[1]); 158 Assert.assertEquals(2, array.length); 159 } 160 161 public void testGetKeysArrayConstructorCloned() { 162 Object [] keys = new Object [] {ONE, TWO}; 163 MultiKey mk = new MultiKey(keys, true); 164 Object [] array = mk.getKeys(); 165 Assert.assertTrue(array != keys); 166 Assert.assertTrue(Arrays.equals(array, keys)); 167 Assert.assertSame(ONE, array[0]); 168 Assert.assertSame(TWO, array[1]); 169 Assert.assertEquals(2, array.length); 170 } 171 172 public void testGetKeysArrayConstructorNonCloned() { 173 Object [] keys = new Object [] {ONE, TWO}; 174 MultiKey mk = new MultiKey(keys, false); 175 Object [] array = mk.getKeys(); 176 Assert.assertTrue(array != keys); Assert.assertTrue(Arrays.equals(array, keys)); 178 Assert.assertSame(ONE, array[0]); 179 Assert.assertSame(TWO, array[1]); 180 Assert.assertEquals(2, array.length); 181 } 182 183 public void testHashCode() { 184 MultiKey mk1 = new MultiKey(ONE, TWO); 185 MultiKey mk2 = new MultiKey(ONE, TWO); 186 MultiKey mk3 = new MultiKey(ONE, "TWO"); 187 188 Assert.assertTrue(mk1.hashCode() == mk1.hashCode()); 189 Assert.assertTrue(mk1.hashCode() == mk2.hashCode()); 190 Assert.assertTrue(mk1.hashCode() != mk3.hashCode()); 191 192 int total = (0 ^ ONE.hashCode()) ^ TWO.hashCode(); 193 Assert.assertEquals(total, mk1.hashCode()); 194 } 195 196 public void testEquals() { 197 MultiKey mk1 = new MultiKey(ONE, TWO); 198 MultiKey mk2 = new MultiKey(ONE, TWO); 199 MultiKey mk3 = new MultiKey(ONE, "TWO"); 200 201 Assert.assertEquals(mk1, mk1); 202 Assert.assertEquals(mk1, mk2); 203 Assert.assertTrue(mk1.equals(mk3) == false); 204 Assert.assertTrue(mk1.equals("") == false); 205 Assert.assertTrue(mk1.equals(null) == false); 206 } 207 208 } 209 | Popular Tags |