KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 /**
26  * Test the DefaultKeyValue class.
27  *
28  * @since Commons Collections 3.0
29  * @version $Revision: 1.3 $ $Date: 2004/02/18 01:20:40 $
30  *
31  * @author Neil O'Toole
32  */

33 public class TestDefaultKeyValue extends TestCase {
34     
35     private final String JavaDoc key = "name";
36     private final String JavaDoc value = "duke";
37
38     /**
39      * JUnit constructor.
40      *
41      * @param testName the test name
42      */

43     public TestDefaultKeyValue(String JavaDoc testName) {
44         super(testName);
45
46     }
47
48     public static void main(String JavaDoc[] args) {
49         junit.textui.TestRunner.run(TestDefaultKeyValue.class);
50     }
51
52     public static Test suite() {
53         return new TestSuite(TestDefaultKeyValue.class);
54     }
55
56     //-----------------------------------------------------------------------
57
/**
58      * Make an instance of DefaultKeyValue with the default (null) key and value.
59      * Subclasses should override this method to return a DefaultKeyValue
60      * of the type being tested.
61      */

62     protected DefaultKeyValue makeDefaultKeyValue() {
63         return new DefaultKeyValue(null, null);
64     }
65
66     /**
67      * Make an instance of DefaultKeyValue with the specified key and value.
68      * Subclasses should override this method to return a DefaultKeyValue
69      * of the type being tested.
70      */

71     protected DefaultKeyValue makeDefaultKeyValue(Object JavaDoc key, Object JavaDoc value) {
72         return new DefaultKeyValue(key, value);
73     }
74
75     //-----------------------------------------------------------------------
76
public void testAccessorsAndMutators() {
77         DefaultKeyValue kv = makeDefaultKeyValue();
78
79         kv.setKey(key);
80         assertTrue(kv.getKey() == key);
81
82         kv.setValue(value);
83         assertTrue(kv.getValue() == value);
84
85         // check that null doesn't do anything funny
86
kv.setKey(null);
87         assertTrue(kv.getKey() == null);
88
89         kv.setValue(null);
90         assertTrue(kv.getValue() == null);
91
92     }
93
94     public void testSelfReferenceHandling() {
95         // test that #setKey and #setValue do not permit
96
// the KVP to contain itself (and thus cause infinite recursion
97
// in #hashCode and #toString)
98

99         DefaultKeyValue kv = makeDefaultKeyValue();
100
101         try {
102             kv.setKey(kv);
103             fail("Should throw an IllegalArgumentException");
104         } catch (IllegalArgumentException JavaDoc iae) {
105             // expected to happen...
106

107             // check that the KVP's state has not changed
108
assertTrue(kv.getKey() == null && kv.getValue() == null);
109         }
110
111         try {
112             kv.setValue(kv);
113             fail("Should throw an IllegalArgumentException");
114         } catch (IllegalArgumentException JavaDoc iae) {
115             // expected to happen...
116

117             // check that the KVP's state has not changed
118
assertTrue(kv.getKey() == null && kv.getValue() == null);
119         }
120     }
121
122     /**
123      * Subclasses should override this method to test their own constructors.
124      */

125     public void testConstructors() {
126         // 1. test default constructor
127
DefaultKeyValue kv = new DefaultKeyValue();
128         assertTrue(kv.getKey() == null && kv.getValue() == null);
129
130         // 2. test key-value constructor
131
kv = new DefaultKeyValue(key, value);
132         assertTrue(kv.getKey() == key && kv.getValue() == value);
133
134         // 3. test copy constructor
135
DefaultKeyValue kv2 = new DefaultKeyValue(kv);
136         assertTrue(kv2.getKey() == key && kv2.getValue() == value);
137
138         // test that the KVPs are independent
139
kv.setKey(null);
140         kv.setValue(null);
141
142         assertTrue(kv2.getKey() == key && kv2.getValue() == value);
143
144         // 4. test Map.Entry constructor
145
Map JavaDoc map = new HashMap JavaDoc();
146         map.put(key, value);
147         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) map.entrySet().iterator().next();
148
149         kv = new DefaultKeyValue(entry);
150         assertTrue(kv.getKey() == key && kv.getValue() == value);
151
152         // test that the KVP is independent of the Map.Entry
153
entry.setValue(null);
154         assertTrue(kv.getValue() == value);
155
156     }
157
158     public void testEqualsAndHashCode() {
159         // 1. test with object data
160
DefaultKeyValue kv = makeDefaultKeyValue(key, value);
161         DefaultKeyValue kv2 = makeDefaultKeyValue(key, value);
162
163         assertTrue(kv.equals(kv));
164         assertTrue(kv.equals(kv2));
165         assertTrue(kv.hashCode() == kv2.hashCode());
166
167         // 2. test with nulls
168
kv = makeDefaultKeyValue(null, null);
169         kv2 = makeDefaultKeyValue(null, null);
170
171         assertTrue(kv.equals(kv));
172         assertTrue(kv.equals(kv2));
173         assertTrue(kv.hashCode() == kv2.hashCode());
174     }
175
176     public void testToString() {
177         DefaultKeyValue kv = makeDefaultKeyValue(key, value);
178         assertTrue(kv.toString().equals(kv.getKey() + "=" + kv.getValue()));
179
180         // test with nulls
181
kv = makeDefaultKeyValue(null, null);
182         assertTrue(kv.toString().equals(kv.getKey() + "=" + kv.getValue()));
183     }
184
185     public void testToMapEntry() {
186         DefaultKeyValue kv = makeDefaultKeyValue(key, value);
187
188         Map JavaDoc map = new HashMap JavaDoc();
189         map.put(kv.getKey(), kv.getValue());
190         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) map.entrySet().iterator().next();
191
192         assertTrue(entry.equals(kv.toMapEntry()));
193         assertTrue(entry.hashCode() == kv.hashCode());
194     }
195
196 }
197
Popular Tags