1 16 package org.apache.commons.collections.map; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import junit.framework.Test; 22 import junit.framework.TestSuite; 23 24 import org.apache.commons.collections.Factory; 25 import org.apache.commons.collections.FactoryUtils; 26 import org.apache.commons.collections.Transformer; 27 import org.apache.commons.collections.functors.ConstantFactory; 28 29 38 public class TestDefaultedMap extends AbstractTestMap { 39 40 protected static final Factory nullFactory = FactoryUtils.nullFactory(); 41 42 public TestDefaultedMap(String testName) { 43 super(testName); 44 } 45 46 public static Test suite() { 47 return new TestSuite(TestDefaultedMap.class); 48 } 49 50 public static void main(String args[]) { 51 String [] testCaseName = { TestDefaultedMap.class.getName()}; 52 junit.textui.TestRunner.main(testCaseName); 53 } 54 55 public Map makeEmptyMap() { 57 return DefaultedMap.decorate(new HashMap (), nullFactory); 58 } 59 60 public void testMapGet() { 62 Map map = new DefaultedMap("NULL"); 63 64 assertEquals(0, map.size()); 65 assertEquals(false, map.containsKey("NotInMap")); 66 assertEquals("NULL", map.get("NotInMap")); 67 68 map.put("Key", "Value"); 69 assertEquals(1, map.size()); 70 assertEquals(true, map.containsKey("Key")); 71 assertEquals("Value", map.get("Key")); 72 assertEquals(false, map.containsKey("NotInMap")); 73 assertEquals("NULL", map.get("NotInMap")); 74 } 75 76 public void testMapGet2() { 77 HashMap base = new HashMap (); 78 Map map = DefaultedMap.decorate(base, "NULL"); 79 80 assertEquals(0, map.size()); 81 assertEquals(0, base.size()); 82 assertEquals(false, map.containsKey("NotInMap")); 83 assertEquals("NULL", map.get("NotInMap")); 84 85 map.put("Key", "Value"); 86 assertEquals(1, map.size()); 87 assertEquals(1, base.size()); 88 assertEquals(true, map.containsKey("Key")); 89 assertEquals("Value", map.get("Key")); 90 assertEquals(false, map.containsKey("NotInMap")); 91 assertEquals("NULL", map.get("NotInMap")); 92 } 93 94 public void testMapGet3() { 95 HashMap base = new HashMap (); 96 Map map = DefaultedMap.decorate(base, ConstantFactory.getInstance("NULL")); 97 98 assertEquals(0, map.size()); 99 assertEquals(0, base.size()); 100 assertEquals(false, map.containsKey("NotInMap")); 101 assertEquals("NULL", map.get("NotInMap")); 102 103 map.put("Key", "Value"); 104 assertEquals(1, map.size()); 105 assertEquals(1, base.size()); 106 assertEquals(true, map.containsKey("Key")); 107 assertEquals("Value", map.get("Key")); 108 assertEquals(false, map.containsKey("NotInMap")); 109 assertEquals("NULL", map.get("NotInMap")); 110 } 111 112 public void testMapGet4() { 113 HashMap base = new HashMap (); 114 Map map = DefaultedMap.decorate(base, new Transformer() { 115 public Object transform(Object input) { 116 if (input instanceof String ) { 117 return "NULL"; 118 } 119 return "NULL_OBJECT"; 120 } 121 }); 122 123 assertEquals(0, map.size()); 124 assertEquals(0, base.size()); 125 assertEquals(false, map.containsKey("NotInMap")); 126 assertEquals("NULL", map.get("NotInMap")); 127 assertEquals("NULL_OBJECT", map.get(new Integer (0))); 128 129 map.put("Key", "Value"); 130 assertEquals(1, map.size()); 131 assertEquals(1, base.size()); 132 assertEquals(true, map.containsKey("Key")); 133 assertEquals("Value", map.get("Key")); 134 assertEquals(false, map.containsKey("NotInMap")); 135 assertEquals("NULL", map.get("NotInMap")); 136 assertEquals("NULL_OBJECT", map.get(new Integer (0))); 137 } 138 139 public String getCompatibilityVersion() { 140 return "3.2"; 141 } 142 143 154 } 155 | Popular Tags |