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