001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.keyvalue;
017:
018: import java.util.Arrays;
019:
020: import junit.framework.Assert;
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: /**
026: * Unit tests for {@link org.apache.commons.collections.MultiKey}.
027: *
028: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
029: *
030: * @author Stephen Colebourne
031: */
032: public class TestMultiKey extends TestCase {
033:
034: Integer ONE = new Integer(1);
035: Integer TWO = new Integer(2);
036: Integer THREE = new Integer(3);
037: Integer FOUR = new Integer(4);
038: Integer FIVE = new Integer(5);
039:
040: public TestMultiKey(String name) {
041: super (name);
042: }
043:
044: public static Test suite() {
045: return new TestSuite(TestMultiKey.class);
046: }
047:
048: public static void main(String[] args) {
049: String[] testCaseName = { TestMultiKey.class.getName() };
050: junit.textui.TestRunner.main(testCaseName);
051: }
052:
053: public void setUp() throws Exception {
054: super .setUp();
055: }
056:
057: protected void tearDown() throws Exception {
058: super .tearDown();
059: }
060:
061: //-----------------------------------------------------------------------
062: public void testConstructors() throws Exception {
063: MultiKey mk = null;
064: mk = new MultiKey(ONE, TWO);
065: Assert.assertTrue(Arrays.equals(new Object[] { ONE, TWO }, mk
066: .getKeys()));
067:
068: mk = new MultiKey(ONE, TWO, THREE);
069: Assert.assertTrue(Arrays.equals(
070: new Object[] { ONE, TWO, THREE }, mk.getKeys()));
071:
072: mk = new MultiKey(ONE, TWO, THREE, FOUR);
073: Assert.assertTrue(Arrays.equals(new Object[] { ONE, TWO, THREE,
074: FOUR }, mk.getKeys()));
075:
076: mk = new MultiKey(ONE, TWO, THREE, FOUR, FIVE);
077: Assert.assertTrue(Arrays.equals(new Object[] { ONE, TWO, THREE,
078: FOUR, FIVE }, mk.getKeys()));
079:
080: mk = new MultiKey(new Object[] { THREE, FOUR, ONE, TWO }, false);
081: Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR,
082: ONE, TWO }, mk.getKeys()));
083: }
084:
085: public void testConstructorsByArray() throws Exception {
086: MultiKey mk = null;
087: Object[] keys = new Object[] { THREE, FOUR, ONE, TWO };
088: mk = new MultiKey(keys);
089: Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR,
090: ONE, TWO }, mk.getKeys()));
091: keys[3] = FIVE; // no effect
092: Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR,
093: ONE, TWO }, mk.getKeys()));
094:
095: keys = new Object[] {};
096: mk = new MultiKey(keys);
097: Assert.assertTrue(Arrays.equals(new Object[] {}, mk.getKeys()));
098:
099: keys = new Object[] { THREE, FOUR, ONE, TWO };
100: mk = new MultiKey(keys, true);
101: Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR,
102: ONE, TWO }, mk.getKeys()));
103: keys[3] = FIVE; // no effect
104: Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR,
105: ONE, TWO }, mk.getKeys()));
106:
107: keys = new Object[] { THREE, FOUR, ONE, TWO };
108: mk = new MultiKey(keys, false);
109: Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR,
110: ONE, TWO }, mk.getKeys()));
111: // change key - don't do this!
112: // the hashcode of the MultiKey is now broken
113: keys[3] = FIVE;
114: Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR,
115: ONE, FIVE }, mk.getKeys()));
116: }
117:
118: public void testConstructorsByArrayNull() throws Exception {
119: Object[] keys = null;
120: try {
121: new MultiKey(keys);
122: fail();
123: } catch (IllegalArgumentException ex) {
124: }
125: try {
126: new MultiKey(keys, true);
127: fail();
128: } catch (IllegalArgumentException ex) {
129: }
130: try {
131: new MultiKey(keys, false);
132: fail();
133: } catch (IllegalArgumentException ex) {
134: }
135: }
136:
137: public void testSize() {
138: Assert.assertEquals(2, new MultiKey(ONE, TWO).size());
139: Assert.assertEquals(2, new MultiKey(null, null).size());
140: Assert.assertEquals(3, new MultiKey(ONE, TWO, THREE).size());
141: Assert.assertEquals(3, new MultiKey(null, null, null).size());
142: Assert.assertEquals(4, new MultiKey(ONE, TWO, THREE, FOUR)
143: .size());
144: Assert.assertEquals(4, new MultiKey(null, null, null, null)
145: .size());
146: Assert.assertEquals(5,
147: new MultiKey(ONE, TWO, THREE, FOUR, FIVE).size());
148: Assert.assertEquals(5, new MultiKey(null, null, null, null,
149: null).size());
150:
151: Assert.assertEquals(0, new MultiKey(new Object[] {}).size());
152: Assert.assertEquals(1, new MultiKey(new Object[] { ONE })
153: .size());
154: Assert.assertEquals(2, new MultiKey(new Object[] { ONE, TWO })
155: .size());
156: Assert.assertEquals(7, new MultiKey(new Object[] { ONE, TWO,
157: ONE, TWO, ONE, TWO, ONE }).size());
158: }
159:
160: public void testGetIndexed() {
161: MultiKey mk = new MultiKey(ONE, TWO);
162: Assert.assertSame(ONE, mk.getKey(0));
163: Assert.assertSame(TWO, mk.getKey(1));
164: try {
165: mk.getKey(-1);
166: fail();
167: } catch (IndexOutOfBoundsException ex) {
168: }
169: try {
170: mk.getKey(2);
171: fail();
172: } catch (IndexOutOfBoundsException ex) {
173: }
174: }
175:
176: public void testGetKeysSimpleConstructor() {
177: MultiKey mk = new MultiKey(ONE, TWO);
178: Object[] array = mk.getKeys();
179: Assert.assertSame(ONE, array[0]);
180: Assert.assertSame(TWO, array[1]);
181: Assert.assertEquals(2, array.length);
182: }
183:
184: public void testGetKeysArrayConstructorCloned() {
185: Object[] keys = new Object[] { ONE, TWO };
186: MultiKey mk = new MultiKey(keys, true);
187: Object[] array = mk.getKeys();
188: Assert.assertTrue(array != keys);
189: Assert.assertTrue(Arrays.equals(array, keys));
190: Assert.assertSame(ONE, array[0]);
191: Assert.assertSame(TWO, array[1]);
192: Assert.assertEquals(2, array.length);
193: }
194:
195: public void testGetKeysArrayConstructorNonCloned() {
196: Object[] keys = new Object[] { ONE, TWO };
197: MultiKey mk = new MultiKey(keys, false);
198: Object[] array = mk.getKeys();
199: Assert.assertTrue(array != keys); // still not equal
200: Assert.assertTrue(Arrays.equals(array, keys));
201: Assert.assertSame(ONE, array[0]);
202: Assert.assertSame(TWO, array[1]);
203: Assert.assertEquals(2, array.length);
204: }
205:
206: public void testHashCode() {
207: MultiKey mk1 = new MultiKey(ONE, TWO);
208: MultiKey mk2 = new MultiKey(ONE, TWO);
209: MultiKey mk3 = new MultiKey(ONE, "TWO");
210:
211: Assert.assertTrue(mk1.hashCode() == mk1.hashCode());
212: Assert.assertTrue(mk1.hashCode() == mk2.hashCode());
213: Assert.assertTrue(mk1.hashCode() != mk3.hashCode());
214:
215: int total = (0 ^ ONE.hashCode()) ^ TWO.hashCode();
216: Assert.assertEquals(total, mk1.hashCode());
217: }
218:
219: public void testEquals() {
220: MultiKey mk1 = new MultiKey(ONE, TWO);
221: MultiKey mk2 = new MultiKey(ONE, TWO);
222: MultiKey mk3 = new MultiKey(ONE, "TWO");
223:
224: Assert.assertEquals(mk1, mk1);
225: Assert.assertEquals(mk1, mk2);
226: Assert.assertTrue(mk1.equals(mk3) == false);
227: Assert.assertTrue(mk1.equals("") == false);
228: Assert.assertTrue(mk1.equals(null) == false);
229: }
230:
231: }
|