KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > keyvalue > TestMultiKey


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.keyvalue;
17
18 import java.util.Arrays JavaDoc;
19
20 import junit.framework.Assert;
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 /**
26  * Unit tests for {@link org.apache.commons.collections.MultiKey}.
27  *
28  * @version $Revision: 1.4 $ $Date: 2004/03/13 12:43:43 $
29  *
30  * @author Stephen Colebourne
31  */

32 public class TestMultiKey extends TestCase {
33
34     Integer JavaDoc ONE = new Integer JavaDoc(1);
35     Integer JavaDoc TWO = new Integer JavaDoc(2);
36     Integer JavaDoc THREE = new Integer JavaDoc(3);
37     Integer JavaDoc FOUR = new Integer JavaDoc(4);
38     Integer JavaDoc FIVE = new Integer JavaDoc(5);
39     
40     public TestMultiKey(String JavaDoc 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 JavaDoc[] args) {
49         String JavaDoc[] testCaseName = { TestMultiKey.class.getName() };
50         junit.textui.TestRunner.main(testCaseName);
51     }
52
53     public void setUp() throws Exception JavaDoc {
54         super.setUp();
55     }
56
57     protected void tearDown() throws Exception JavaDoc {
58         super.tearDown();
59     }
60     
61     //-----------------------------------------------------------------------
62
public void testConstructors() throws Exception JavaDoc {
63         MultiKey mk = null;
64         mk = new MultiKey(ONE, TWO);
65         Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {ONE, TWO}, mk.getKeys()));
66
67         mk = new MultiKey(ONE, TWO, THREE);
68         Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {ONE, TWO, THREE}, mk.getKeys()));
69
70         mk = new MultiKey(ONE, TWO, THREE, FOUR);
71         Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {ONE, TWO, THREE, FOUR}, mk.getKeys()));
72
73         mk = new MultiKey(ONE, TWO, THREE, FOUR, FIVE);
74         Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {ONE, TWO, THREE, FOUR, FIVE}, mk.getKeys()));
75
76         mk = new MultiKey(new Object JavaDoc[] {THREE, FOUR, ONE, TWO}, false);
77         Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {THREE, FOUR, ONE, TWO}, mk.getKeys()));
78     }
79     
80     public void testConstructorsByArray() throws Exception JavaDoc {
81         MultiKey mk = null;
82         Object JavaDoc[] keys = new Object JavaDoc[] {THREE, FOUR, ONE, TWO};
83         mk = new MultiKey(keys);
84         Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {THREE, FOUR, ONE, TWO}, mk.getKeys()));
85         keys[3] = FIVE; // no effect
86
Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {THREE, FOUR, ONE, TWO}, mk.getKeys()));
87
88         keys = new Object JavaDoc[] {};
89         mk = new MultiKey(keys);
90         Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {}, mk.getKeys()));
91
92         keys = new Object JavaDoc[] {THREE, FOUR, ONE, TWO};
93         mk = new MultiKey(keys, true);
94         Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {THREE, FOUR, ONE, TWO}, mk.getKeys()));
95         keys[3] = FIVE; // no effect
96
Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {THREE, FOUR, ONE, TWO}, mk.getKeys()));
97
98         keys = new Object JavaDoc[] {THREE, FOUR, ONE, TWO};
99         mk = new MultiKey(keys, false);
100         Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {THREE, FOUR, ONE, TWO}, mk.getKeys()));
101         // change key - don't do this!
102
// the hashcode of the MultiKey is now broken
103
keys[3] = FIVE;
104         Assert.assertTrue(Arrays.equals(new Object JavaDoc[] {THREE, FOUR, ONE, FIVE}, mk.getKeys()));
105     }
106     
107     public void testConstructorsByArrayNull() throws Exception JavaDoc {
108         Object JavaDoc[] keys = null;
109         try {
110             new MultiKey(keys);
111             fail();
112         } catch (IllegalArgumentException JavaDoc ex) {}
113         try {
114             new MultiKey(keys, true);
115             fail();
116         } catch (IllegalArgumentException JavaDoc ex) {}
117         try {
118             new MultiKey(keys, false);
119             fail();
120         } catch (IllegalArgumentException JavaDoc 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 JavaDoc[] {}).size());
134         Assert.assertEquals(1, new MultiKey(new Object JavaDoc[] {ONE}).size());
135         Assert.assertEquals(2, new MultiKey(new Object JavaDoc[] {ONE, TWO}).size());
136         Assert.assertEquals(7, new MultiKey(new Object JavaDoc[] {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 JavaDoc ex) {}
147         try {
148             mk.getKey(2);
149             fail();
150         } catch (IndexOutOfBoundsException JavaDoc ex) {}
151     }
152     
153     public void testGetKeysSimpleConstructor() {
154         MultiKey mk = new MultiKey(ONE, TWO);
155         Object JavaDoc[] 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 JavaDoc[] keys = new Object JavaDoc[] {ONE, TWO};
163         MultiKey mk = new MultiKey(keys, true);
164         Object JavaDoc[] 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 JavaDoc[] keys = new Object JavaDoc[] {ONE, TWO};
174         MultiKey mk = new MultiKey(keys, false);
175         Object JavaDoc[] array = mk.getKeys();
176         Assert.assertTrue(array != keys); // still not equal
177
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