1 16 package org.apache.commons.collections.map; 17 18 import java.util.Comparator ; 19 import java.util.Map ; 20 import java.util.SortedMap ; 21 import java.util.TreeMap ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 import org.apache.commons.collections.Factory; 27 import org.apache.commons.collections.FactoryUtils; 28 import org.apache.commons.collections.Transformer; 29 import org.apache.commons.collections.TransformerUtils; 30 31 40 public class TestLazySortedMap extends AbstractTestSortedMap { 41 42 protected static final Factory oneFactory = FactoryUtils.constantFactory("One"); 43 protected static final Factory nullFactory = FactoryUtils.nullFactory(); 44 45 public TestLazySortedMap(String testName) { 46 super(testName); 47 } 48 49 public static Test suite() { 50 return new TestSuite(TestLazySortedMap.class); 51 } 52 53 public static void main(String args[]) { 54 String [] testCaseName = { TestLazySortedMap.class.getName()}; 55 junit.textui.TestRunner.main(testCaseName); 56 } 57 58 protected SortedMap decorateMap(SortedMap map, Factory factory) { 60 return LazySortedMap.decorate(map, factory); 61 } 62 63 public Map makeEmptyMap() { 64 return decorateMap(new TreeMap (), nullFactory); 65 } 66 67 protected SortedMap makeTestSortedMap(Factory factory) { 68 return decorateMap(new TreeMap (), factory); 69 } 70 71 public boolean isSubMapViewsSerializable() { 72 return false; 74 } 75 76 public boolean isAllowNullKey() { 77 return false; 78 } 79 80 public void testMapGet() { 83 Map map = makeTestSortedMap(oneFactory); 84 assertEquals(0, map.size()); 85 String s1 = (String ) map.get("Five"); 86 assertEquals("One", s1); 87 assertEquals(1, map.size()); 88 String s2 = (String ) map.get(new String (new char[] {'F','i','v','e'})); 89 assertEquals("One", s2); 90 assertEquals(1, map.size()); 91 assertSame(s1, s2); 92 93 map = makeTestSortedMap(nullFactory); 94 Object o = map.get("Five"); 95 assertEquals(null,o); 96 assertEquals(1, map.size()); 97 98 } 99 100 public void testSortOrder() { 102 SortedMap map = makeTestSortedMap(oneFactory); 103 map.put("A", "a"); 104 map.get("B"); map.put("C", "c"); 106 assertEquals("First key should be A", map.firstKey(), "A"); 107 assertEquals("Last key should be C", map.lastKey(), "C"); 108 assertEquals("First key in tail map should be B", 109 map.tailMap("B").firstKey(), "B"); 110 assertEquals("Last key in head map should be B", 111 map.headMap("C").lastKey(), "B"); 112 assertEquals("Last key in submap should be B", 113 map.subMap("A","C").lastKey(), "B"); 114 115 Comparator c = map.comparator(); 116 assertTrue("natural order, so comparator should be null", 117 c == null); 118 } 119 120 public void testTransformerDecorate() { 121 Transformer transformer = TransformerUtils.asTransformer(oneFactory); 122 SortedMap map = LazySortedMap.decorate(new TreeMap (), transformer); 123 assertTrue(map instanceof LazySortedMap); 124 try { 125 map = LazySortedMap.decorate(new TreeMap (), (Transformer) null); 126 fail("Expecting IllegalArgumentException for null transformer"); 127 } catch (IllegalArgumentException e) { 128 } 130 try { 131 map = LazySortedMap.decorate(null, transformer); 132 fail("Expecting IllegalArgumentException for null map"); 133 } catch (IllegalArgumentException e) { 134 } 136 } 137 138 public String getCompatibilityVersion() { 139 return "3.1"; 140 } 141 142 } | Popular Tags |