001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestKeyValue.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.datastructures;
009:
010: import junit.framework.TestCase;
011:
012: public class TestKeyValue extends TestCase {
013: public TestKeyValue(String name) {
014: super (name);
015: }
016:
017: public void testInstantiation() {
018: KeyValue keyvalue = new KeyValue("key", "value");
019: assertNotNull(keyvalue);
020: assertEquals(keyvalue.getKey(), "key");
021: assertEquals(keyvalue.getValue(), "value");
022: }
023:
024: public void testAccessors() {
025: KeyValue keyvalue = new KeyValue("key", "value");
026: assertEquals(keyvalue.getKey(), "key");
027: assertEquals(keyvalue.getValue(), "value");
028: keyvalue.setKey("key2");
029: keyvalue.setValue("value2");
030: assertEquals(keyvalue.getKey(), "key2");
031: assertEquals(keyvalue.getValue(), "value2");
032: }
033:
034: public void testEquals() {
035: KeyValue keyvalue1 = new KeyValue("key1", "value1");
036: KeyValue keyvalue2 = new KeyValue("key1", "value1");
037: KeyValue keyvalue3 = new KeyValue("key2", "value2");
038: KeyValue keyvalue4 = new KeyValue("key1", null);
039: KeyValue keyvalue5 = new KeyValue("key2", null);
040: KeyValue keyvalue6 = new KeyValue("key2", null);
041: KeyValue keyvalue7 = new KeyValue(null, "value1");
042: KeyValue keyvalue8 = new KeyValue(null, "value2");
043: KeyValue keyvalue9 = new KeyValue(null, "value2");
044: KeyValue keyvalue10 = new KeyValue(null, null);
045: KeyValue keyvalue11 = new KeyValue(null, null);
046:
047: assertFalse(keyvalue1.equals("key1"));
048: assertFalse(keyvalue1.equals("value1"));
049:
050: assertFalse(keyvalue1.equals(null));
051:
052: assertTrue(keyvalue1.equals(keyvalue1));
053: assertTrue(keyvalue1.equals(keyvalue2));
054: assertFalse(keyvalue1.equals(keyvalue3));
055: assertFalse(keyvalue1.equals(keyvalue4));
056: assertFalse(keyvalue1.equals(keyvalue5));
057: assertFalse(keyvalue1.equals(keyvalue6));
058: assertFalse(keyvalue1.equals(keyvalue7));
059: assertFalse(keyvalue1.equals(keyvalue8));
060: assertFalse(keyvalue1.equals(keyvalue9));
061: assertFalse(keyvalue1.equals(keyvalue10));
062: assertFalse(keyvalue1.equals(keyvalue11));
063:
064: assertTrue(keyvalue2.equals(keyvalue2));
065: assertFalse(keyvalue2.equals(keyvalue3));
066: assertFalse(keyvalue2.equals(keyvalue4));
067: assertFalse(keyvalue2.equals(keyvalue5));
068: assertFalse(keyvalue2.equals(keyvalue6));
069: assertFalse(keyvalue2.equals(keyvalue7));
070: assertFalse(keyvalue2.equals(keyvalue8));
071: assertFalse(keyvalue2.equals(keyvalue9));
072: assertFalse(keyvalue2.equals(keyvalue10));
073: assertFalse(keyvalue2.equals(keyvalue11));
074:
075: assertTrue(keyvalue3.equals(keyvalue3));
076: assertFalse(keyvalue3.equals(keyvalue4));
077: assertFalse(keyvalue3.equals(keyvalue5));
078: assertFalse(keyvalue3.equals(keyvalue6));
079: assertFalse(keyvalue3.equals(keyvalue7));
080: assertFalse(keyvalue3.equals(keyvalue8));
081: assertFalse(keyvalue3.equals(keyvalue9));
082: assertFalse(keyvalue3.equals(keyvalue10));
083: assertFalse(keyvalue3.equals(keyvalue11));
084:
085: assertTrue(keyvalue4.equals(keyvalue4));
086: assertFalse(keyvalue4.equals(keyvalue5));
087: assertFalse(keyvalue4.equals(keyvalue6));
088: assertFalse(keyvalue4.equals(keyvalue7));
089: assertFalse(keyvalue4.equals(keyvalue8));
090: assertFalse(keyvalue4.equals(keyvalue9));
091: assertFalse(keyvalue4.equals(keyvalue10));
092: assertFalse(keyvalue4.equals(keyvalue11));
093:
094: assertTrue(keyvalue5.equals(keyvalue5));
095: assertTrue(keyvalue5.equals(keyvalue6));
096: assertFalse(keyvalue5.equals(keyvalue7));
097: assertFalse(keyvalue5.equals(keyvalue8));
098: assertFalse(keyvalue5.equals(keyvalue9));
099: assertFalse(keyvalue5.equals(keyvalue10));
100: assertFalse(keyvalue5.equals(keyvalue11));
101:
102: assertTrue(keyvalue6.equals(keyvalue6));
103: assertFalse(keyvalue6.equals(keyvalue7));
104: assertFalse(keyvalue6.equals(keyvalue8));
105: assertFalse(keyvalue6.equals(keyvalue9));
106: assertFalse(keyvalue6.equals(keyvalue10));
107: assertFalse(keyvalue6.equals(keyvalue11));
108:
109: assertTrue(keyvalue7.equals(keyvalue7));
110: assertFalse(keyvalue7.equals(keyvalue8));
111: assertFalse(keyvalue7.equals(keyvalue9));
112: assertFalse(keyvalue7.equals(keyvalue10));
113: assertFalse(keyvalue7.equals(keyvalue11));
114:
115: assertTrue(keyvalue8.equals(keyvalue8));
116: assertTrue(keyvalue8.equals(keyvalue9));
117: assertFalse(keyvalue8.equals(keyvalue10));
118: assertFalse(keyvalue8.equals(keyvalue11));
119:
120: assertTrue(keyvalue9.equals(keyvalue9));
121: assertFalse(keyvalue9.equals(keyvalue10));
122: assertFalse(keyvalue9.equals(keyvalue11));
123:
124: assertTrue(keyvalue10.equals(keyvalue10));
125: assertTrue(keyvalue10.equals(keyvalue11));
126:
127: assertTrue(keyvalue11.equals(keyvalue11));
128: }
129:
130: public void testClone() {
131: KeyValue keyvalue1 = new KeyValue("key1", "value1");
132: KeyValue keyvalue2 = new KeyValue("key1", null);
133: KeyValue keyvalue3 = new KeyValue(null, "value1");
134: KeyValue keyvalue4 = new KeyValue(null, null);
135:
136: KeyValue keyvalue1_clone = keyvalue1.clone();
137: KeyValue keyvalue2_clone = keyvalue2.clone();
138: KeyValue keyvalue3_clone = keyvalue3.clone();
139: KeyValue keyvalue4_clone = keyvalue4.clone();
140:
141: assertTrue(keyvalue1.equals(keyvalue1_clone));
142: assertTrue(keyvalue2.equals(keyvalue2_clone));
143: assertTrue(keyvalue3.equals(keyvalue3_clone));
144: assertTrue(keyvalue4.equals(keyvalue4_clone));
145: }
146:
147: public void testHashCode() {
148: KeyValue keyvalue1 = new KeyValue("key", "value");
149: KeyValue keyvalue2 = new KeyValue("key", "value");
150: KeyValue keyvalue3 = new KeyValue("key2", "value2");
151: assertEquals(keyvalue1.hashCode(), keyvalue1.hashCode());
152: assertEquals(keyvalue2.hashCode(), keyvalue2.hashCode());
153: assertTrue(keyvalue1.hashCode() == keyvalue2.hashCode()
154: || keyvalue1.equals(keyvalue2));
155: assertTrue(keyvalue1.hashCode() != keyvalue3.hashCode()
156: || !keyvalue1.equals(keyvalue3));
157: assertTrue(keyvalue2.hashCode() != keyvalue3.hashCode()
158: || !keyvalue2.equals(keyvalue3));
159: }
160:
161: public void testToString() {
162: KeyValue keyvalue = new KeyValue("key", "value");
163: assertEquals("value", keyvalue.toString());
164: }
165: }
|