1 16 package org.apache.commons.collections.map; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 25 import org.apache.commons.collections.TransformerUtils; 26 import org.apache.commons.collections.collection.TestTransformedCollection; 27 28 37 public class TestTransformedMap extends AbstractTestMap { 38 39 public TestTransformedMap(String testName) { 40 super(testName); 41 } 42 43 public static Test suite() { 44 return new TestSuite(TestTransformedMap.class); 45 } 46 47 public static void main(String args[]) { 48 String [] testCaseName = { TestTransformedMap.class.getName()}; 49 junit.textui.TestRunner.main(testCaseName); 50 } 51 52 public Map makeEmptyMap() { 54 return TransformedMap.decorate(new HashMap (), TransformerUtils.nopTransformer(), TransformerUtils.nopTransformer()); 55 } 56 57 public void testTransformedMap() { 59 Object [] els = new Object [] {"1", "3", "5", "7", "2", "4", "6"}; 60 61 Map map = TransformedMap.decorate(new HashMap (), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER, null); 62 assertEquals(0, map.size()); 63 for (int i = 0; i < els.length; i++) { 64 map.put(els[i], els[i]); 65 assertEquals(i + 1, map.size()); 66 assertEquals(true, map.containsKey(new Integer ((String ) els[i]))); 67 assertEquals(false, map.containsKey(els[i])); 68 assertEquals(true, map.containsValue(els[i])); 69 assertEquals(els[i], map.get(new Integer ((String ) els[i]))); 70 } 71 72 assertEquals(null, map.remove(els[0])); 73 assertEquals(els[0], map.remove(new Integer ((String ) els[0]))); 74 75 map = TransformedMap.decorate(new HashMap (), null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER); 76 assertEquals(0, map.size()); 77 for (int i = 0; i < els.length; i++) { 78 map.put(els[i], els[i]); 79 assertEquals(i + 1, map.size()); 80 assertEquals(true, map.containsValue(new Integer ((String ) els[i]))); 81 assertEquals(false, map.containsValue(els[i])); 82 assertEquals(true, map.containsKey(els[i])); 83 assertEquals(new Integer ((String ) els[i]), map.get(els[i])); 84 } 85 86 assertEquals(new Integer ((String ) els[0]), map.remove(els[0])); 87 88 Set entrySet = map.entrySet(); 89 Map.Entry [] array = (Map.Entry []) entrySet.toArray(new Map.Entry [0]); 90 array[0].setValue("66"); 91 assertEquals(new Integer (66), array[0].getValue()); 92 assertEquals(new Integer (66), map.get(array[0].getKey())); 93 94 Map.Entry entry = (Map.Entry ) entrySet.iterator().next(); 95 entry.setValue("88"); 96 assertEquals(new Integer (88), entry.getValue()); 97 assertEquals(new Integer (88), map.get(entry.getKey())); 98 } 99 100 public String getCompatibilityVersion() { 101 return "3.1"; 102 } 103 104 } 115 | Popular Tags |