1 16 package org.apache.commons.collections; 17 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import junit.framework.Test; 25 26 import org.apache.commons.collections.map.AbstractTestMap; 27 28 41 public class TestSequencedHashMap extends AbstractTestMap { 42 45 protected SequencedHashMap labRat; 46 47 public TestSequencedHashMap(String name) { 48 super(name); 49 } 50 51 public static Test suite() { 52 return BulkTest.makeSuite(TestSequencedHashMap.class); 53 } 54 55 public String getCompatibilityVersion() { 58 return "2"; 59 } 60 61 public static void main(String [] args) { 62 String [] testCaseName = { TestSequencedHashMap.class.getName() }; 63 junit.textui.TestRunner.main(testCaseName); 64 } 65 66 public void setUp() throws Exception { 67 super.setUp(); 68 labRat = (SequencedHashMap) makeEmptyMap(); 71 } 72 73 public Map makeEmptyMap() { 74 return new SequencedHashMap(); 75 } 76 77 protected Object [] getKeys() { 78 return new Object [] { "foo", "baz", "eek" }; 79 } 80 81 protected Object [] getValues() { 82 return new Object [] { "bar", "frob", new Object () }; 83 } 84 85 public void testSequenceMap() throws Throwable { 86 Object [] keys = getKeys(); 87 int expectedSize = keys.length; 88 Object [] values = getValues(); 89 for (int i = 0; i < expectedSize; i++) { 90 labRat.put(keys[i], values[i]); 91 } 92 93 assertEquals("size() does not match expected size", 95 expectedSize, labRat.size()); 96 97 SequencedHashMap clone = (SequencedHashMap) labRat.clone(); 99 assertEquals("Size of clone does not match original", 100 labRat.size(), clone.size()); 101 Iterator origEntries = labRat.entrySet().iterator(); 102 Iterator copiedEntries = clone.entrySet().iterator(); 103 while (origEntries.hasNext()) { 104 Map.Entry origEntry = (Map.Entry )origEntries.next(); 105 Map.Entry copiedEntry = (Map.Entry )copiedEntries.next(); 106 assertEquals("Cloned key does not match original", 107 origEntry.getKey(), copiedEntry.getKey()); 108 assertEquals("Cloned value does not match original", 109 origEntry.getValue(), copiedEntry.getValue()); 110 assertEquals("Cloned entry does not match original", 111 origEntry, copiedEntry); 112 } 113 assertTrue("iterator() returned different number of elements than keys()", 114 !copiedEntries.hasNext()); 115 116 List seq = labRat.sequence(); 118 assertEquals("sequence() returns more keys than in the Map", 119 expectedSize, seq.size()); 120 121 for (int i = 0; i < seq.size(); i++) { 122 assertEquals("Key " + i + " is not the same as the key in the Map", 123 keys[i], seq.get(i)); 124 } 125 } 126 127 public void testYoungest() { 128 labRat.put(new Integer (1),"foo"); 129 labRat.put(new Integer (2),"bar"); 130 assertTrue("first key is correct",labRat.get(0).equals(new Integer (1))); 131 labRat.put(new Integer (1),"boo"); 132 assertTrue("second key is reassigned to first",labRat.get(0).equals(new Integer (2))); 133 } 134 135 public void testYoungestReplaceNullWithValue() { 136 labRat.put(new Integer (1),null); 137 labRat.put(new Integer (2),"foo"); 138 assertTrue("first key is correct",labRat.get(0).equals(new Integer (1))); 139 labRat.put(new Integer (1),"bar"); 140 assertTrue("second key is reassigned to first",labRat.get(0).equals(new Integer (2))); 141 } 142 143 public void testYoungestReplaceValueWithNull() { 144 labRat.put(new Integer (1),"bar"); 145 labRat.put(new Integer (2),"foo"); 146 assertTrue("first key is correct",labRat.get(0).equals(new Integer (1))); 147 labRat.put(new Integer (1),null); 148 assertTrue("second key is reassigned to first",labRat.get(0).equals(new Integer (2))); 149 } 150 151 public void testFullMapSerialization() 153 throws IOException , ClassNotFoundException { 154 SequencedHashMap map = (SequencedHashMap) makeFullMap(); 155 156 if (!(map instanceof Serializable )) return; 157 158 byte[] objekt = writeExternalFormToBytes((Serializable ) map); 159 SequencedHashMap map2 = (SequencedHashMap) readExternalFormFromBytes(objekt); 160 161 assertEquals("Both maps are same size",map.size(), getSampleKeys().length); 162 assertEquals("Both maps are same size",map2.size(),getSampleKeys().length); 163 164 assertEquals("Both maps have the same first key", 165 map.getFirstKey(),getSampleKeys()[0]); 166 assertEquals("Both maps have the same first key", 167 map2.getFirstKey(),getSampleKeys()[0]); 168 assertEquals("Both maps have the same last key", 169 map.getLastKey(),getSampleKeys()[getSampleKeys().length - 1]); 170 assertEquals("Both maps have the same last key", 171 map2.getLastKey(),getSampleKeys()[getSampleKeys().length - 1]); 172 } 173 174 public void testIndexOf() throws Exception { 175 Object [] keys = getKeys(); 176 int expectedSize = keys.length; 177 Object [] values = getValues(); 178 for (int i = 0; i < expectedSize; i++) { 179 labRat.put(keys[i], values[i]); 180 } 181 for (int i = 0; i < keys.length; i++) { 184 assertEquals("indexOf with existing key failed", i, labRat.indexOf(keys[i])); 185 } 186 assertEquals("test with non-existing key failed", -1, labRat.indexOf("NonExistingKey")); 188 } 189 190 public void tearDown() throws Exception { 191 labRat = null; 192 } 193 } 194 | Popular Tags |