1 16 package org.apache.commons.collections.keyvalue; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import junit.framework.TestCase; 22 23 35 public abstract class AbstractTestMapEntry extends TestCase { 36 37 protected final String key = "name"; 38 protected final String value = "duke"; 39 40 45 public AbstractTestMapEntry(String testName) { 46 super(testName); 47 } 48 49 55 public Map.Entry makeMapEntry() { 56 return makeMapEntry(null, null); 57 } 58 59 64 public abstract Map.Entry makeMapEntry(Object key, Object value); 65 66 69 public Map.Entry makeKnownMapEntry() { 70 return makeKnownMapEntry(null, null); 71 } 72 73 76 public Map.Entry makeKnownMapEntry(Object key, Object value) { 77 Map map = new HashMap (1); 78 map.put(key, value); 79 Map.Entry entry = (Map.Entry ) map.entrySet().iterator().next(); 80 return entry; 81 } 82 83 public void testAccessorsAndMutators() { 85 Map.Entry entry = makeMapEntry(key, value); 86 87 assertTrue(entry.getKey() == key); 88 89 entry.setValue(value); 90 assertTrue(entry.getValue() == value); 91 92 entry = makeMapEntry(null, null); 94 assertTrue(entry.getKey() == null); 95 96 entry.setValue(null); 97 assertTrue(entry.getValue() == null); 98 } 99 100 106 107 public void testSelfReferenceHandling() { 108 112 Map.Entry entry = makeMapEntry(); 113 114 try { 115 entry.setValue(entry); 116 fail("Should throw an IllegalArgumentException"); 117 } catch (IllegalArgumentException iae) { 118 120 assertTrue(entry.getKey() == null && entry.getValue() == null); 122 } 123 } 124 125 129 public abstract void testConstructors(); 130 131 public void testEqualsAndHashCode() { 132 Map.Entry e1 = makeMapEntry(key, value); 134 Map.Entry e2 = makeKnownMapEntry(key, value); 135 136 assertTrue(e1.equals(e1)); 137 assertTrue(e2.equals(e1)); 138 assertTrue(e1.equals(e2)); 139 assertTrue(e1.hashCode() == e2.hashCode()); 140 141 e1 = makeMapEntry(); 143 e2 = makeKnownMapEntry(); 144 145 assertTrue(e1.equals(e1)); 146 assertTrue(e2.equals(e1)); 147 assertTrue(e1.equals(e2)); 148 assertTrue(e1.hashCode() == e2.hashCode()); 149 } 150 151 public void testToString() { 152 Map.Entry entry = makeMapEntry(key, value); 153 assertTrue(entry.toString().equals(entry.getKey() + "=" + entry.getValue())); 154 155 entry = makeMapEntry(); 157 assertTrue(entry.toString().equals(entry.getKey() + "=" + entry.getValue())); 158 } 159 160 } 161 | Popular Tags |