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.HashMap;
019: import java.util.Map;
020:
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: /**
026: * Test the DefaultKeyValue class.
027: *
028: * @since Commons Collections 3.0
029: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
030: *
031: * @author Neil O'Toole
032: */
033: public class TestDefaultKeyValue extends TestCase {
034:
035: private final String key = "name";
036: private final String value = "duke";
037:
038: /**
039: * JUnit constructor.
040: *
041: * @param testName the test name
042: */
043: public TestDefaultKeyValue(String testName) {
044: super (testName);
045:
046: }
047:
048: public static void main(String[] args) {
049: junit.textui.TestRunner.run(TestDefaultKeyValue.class);
050: }
051:
052: public static Test suite() {
053: return new TestSuite(TestDefaultKeyValue.class);
054: }
055:
056: //-----------------------------------------------------------------------
057: /**
058: * Make an instance of DefaultKeyValue with the default (null) key and value.
059: * Subclasses should override this method to return a DefaultKeyValue
060: * of the type being tested.
061: */
062: protected DefaultKeyValue makeDefaultKeyValue() {
063: return new DefaultKeyValue(null, null);
064: }
065:
066: /**
067: * Make an instance of DefaultKeyValue with the specified key and value.
068: * Subclasses should override this method to return a DefaultKeyValue
069: * of the type being tested.
070: */
071: protected DefaultKeyValue makeDefaultKeyValue(Object key,
072: Object value) {
073: return new DefaultKeyValue(key, value);
074: }
075:
076: //-----------------------------------------------------------------------
077: public void testAccessorsAndMutators() {
078: DefaultKeyValue kv = makeDefaultKeyValue();
079:
080: kv.setKey(key);
081: assertTrue(kv.getKey() == key);
082:
083: kv.setValue(value);
084: assertTrue(kv.getValue() == value);
085:
086: // check that null doesn't do anything funny
087: kv.setKey(null);
088: assertTrue(kv.getKey() == null);
089:
090: kv.setValue(null);
091: assertTrue(kv.getValue() == null);
092:
093: }
094:
095: public void testSelfReferenceHandling() {
096: // test that #setKey and #setValue do not permit
097: // the KVP to contain itself (and thus cause infinite recursion
098: // in #hashCode and #toString)
099:
100: DefaultKeyValue kv = makeDefaultKeyValue();
101:
102: try {
103: kv.setKey(kv);
104: fail("Should throw an IllegalArgumentException");
105: } catch (IllegalArgumentException iae) {
106: // expected to happen...
107:
108: // check that the KVP's state has not changed
109: assertTrue(kv.getKey() == null && kv.getValue() == null);
110: }
111:
112: try {
113: kv.setValue(kv);
114: fail("Should throw an IllegalArgumentException");
115: } catch (IllegalArgumentException iae) {
116: // expected to happen...
117:
118: // check that the KVP's state has not changed
119: assertTrue(kv.getKey() == null && kv.getValue() == null);
120: }
121: }
122:
123: /**
124: * Subclasses should override this method to test their own constructors.
125: */
126: public void testConstructors() {
127: // 1. test default constructor
128: DefaultKeyValue kv = new DefaultKeyValue();
129: assertTrue(kv.getKey() == null && kv.getValue() == null);
130:
131: // 2. test key-value constructor
132: kv = new DefaultKeyValue(key, value);
133: assertTrue(kv.getKey() == key && kv.getValue() == value);
134:
135: // 3. test copy constructor
136: DefaultKeyValue kv2 = new DefaultKeyValue(kv);
137: assertTrue(kv2.getKey() == key && kv2.getValue() == value);
138:
139: // test that the KVPs are independent
140: kv.setKey(null);
141: kv.setValue(null);
142:
143: assertTrue(kv2.getKey() == key && kv2.getValue() == value);
144:
145: // 4. test Map.Entry constructor
146: Map map = new HashMap();
147: map.put(key, value);
148: Map.Entry entry = (Map.Entry) map.entrySet().iterator().next();
149:
150: kv = new DefaultKeyValue(entry);
151: assertTrue(kv.getKey() == key && kv.getValue() == value);
152:
153: // test that the KVP is independent of the Map.Entry
154: entry.setValue(null);
155: assertTrue(kv.getValue() == value);
156:
157: }
158:
159: public void testEqualsAndHashCode() {
160: // 1. test with object data
161: DefaultKeyValue kv = makeDefaultKeyValue(key, value);
162: DefaultKeyValue kv2 = makeDefaultKeyValue(key, value);
163:
164: assertTrue(kv.equals(kv));
165: assertTrue(kv.equals(kv2));
166: assertTrue(kv.hashCode() == kv2.hashCode());
167:
168: // 2. test with nulls
169: kv = makeDefaultKeyValue(null, null);
170: kv2 = makeDefaultKeyValue(null, null);
171:
172: assertTrue(kv.equals(kv));
173: assertTrue(kv.equals(kv2));
174: assertTrue(kv.hashCode() == kv2.hashCode());
175: }
176:
177: public void testToString() {
178: DefaultKeyValue kv = makeDefaultKeyValue(key, value);
179: assertTrue(kv.toString().equals(
180: kv.getKey() + "=" + kv.getValue()));
181:
182: // test with nulls
183: kv = makeDefaultKeyValue(null, null);
184: assertTrue(kv.toString().equals(
185: kv.getKey() + "=" + kv.getValue()));
186: }
187:
188: public void testToMapEntry() {
189: DefaultKeyValue kv = makeDefaultKeyValue(key, value);
190:
191: Map map = new HashMap();
192: map.put(kv.getKey(), kv.getValue());
193: Map.Entry entry = (Map.Entry) map.entrySet().iterator().next();
194:
195: assertTrue(entry.equals(kv.toMapEntry()));
196: assertTrue(entry.hashCode() == kv.hashCode());
197: }
198:
199: }
|