1 16 package org.apache.commons.collections.keyvalue; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 25 33 public class TestDefaultKeyValue extends TestCase { 34 35 private final String key = "name"; 36 private final String value = "duke"; 37 38 43 public TestDefaultKeyValue(String testName) { 44 super(testName); 45 46 } 47 48 public static void main(String [] args) { 49 junit.textui.TestRunner.run(TestDefaultKeyValue.class); 50 } 51 52 public static Test suite() { 53 return new TestSuite(TestDefaultKeyValue.class); 54 } 55 56 62 protected DefaultKeyValue makeDefaultKeyValue() { 63 return new DefaultKeyValue(null, null); 64 } 65 66 71 protected DefaultKeyValue makeDefaultKeyValue(Object key, Object value) { 72 return new DefaultKeyValue(key, value); 73 } 74 75 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 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 99 DefaultKeyValue kv = makeDefaultKeyValue(); 100 101 try { 102 kv.setKey(kv); 103 fail("Should throw an IllegalArgumentException"); 104 } catch (IllegalArgumentException iae) { 105 107 assertTrue(kv.getKey() == null && kv.getValue() == null); 109 } 110 111 try { 112 kv.setValue(kv); 113 fail("Should throw an IllegalArgumentException"); 114 } catch (IllegalArgumentException iae) { 115 117 assertTrue(kv.getKey() == null && kv.getValue() == null); 119 } 120 } 121 122 125 public void testConstructors() { 126 DefaultKeyValue kv = new DefaultKeyValue(); 128 assertTrue(kv.getKey() == null && kv.getValue() == null); 129 130 kv = new DefaultKeyValue(key, value); 132 assertTrue(kv.getKey() == key && kv.getValue() == value); 133 134 DefaultKeyValue kv2 = new DefaultKeyValue(kv); 136 assertTrue(kv2.getKey() == key && kv2.getValue() == value); 137 138 kv.setKey(null); 140 kv.setValue(null); 141 142 assertTrue(kv2.getKey() == key && kv2.getValue() == value); 143 144 Map map = new HashMap (); 146 map.put(key, value); 147 Map.Entry entry = (Map.Entry ) map.entrySet().iterator().next(); 148 149 kv = new DefaultKeyValue(entry); 150 assertTrue(kv.getKey() == key && kv.getValue() == value); 151 152 entry.setValue(null); 154 assertTrue(kv.getValue() == value); 155 156 } 157 158 public void testEqualsAndHashCode() { 159 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 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 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 map = new HashMap (); 189 map.put(kv.getKey(), kv.getValue()); 190 Map.Entry entry = (Map.Entry ) map.entrySet().iterator().next(); 191 192 assertTrue(entry.equals(kv.toMapEntry())); 193 assertTrue(entry.hashCode() == kv.hashCode()); 194 } 195 196 } 197 | Popular Tags |