1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.map;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertSame;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24
25 import java.util.Comparator;
26 import java.util.Map;
27 import java.util.SortedMap;
28 import java.util.TreeMap;
29 import java.util.concurrent.ConcurrentSkipListMap;
30
31 import org.apache.commons.collections4.Factory;
32 import org.apache.commons.collections4.FactoryUtils;
33 import org.apache.commons.collections4.Transformer;
34 import org.apache.commons.collections4.TransformerUtils;
35 import org.junit.jupiter.api.Test;
36
37
38
39
40
41
42
43 @SuppressWarnings("boxing")
44 public class LazySortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
45
46 private static final class ReverseStringComparator implements Comparator<String> {
47
48 @Override
49 public int compare(final String arg0, final String arg1) {
50 return arg1.compareTo(arg0);
51 }
52
53 }
54
55 private static final Factory<Integer> oneFactory = FactoryUtils.constantFactory(1);
56
57 protected final Comparator<String> reverseStringComparator = new ReverseStringComparator();
58
59 @Override
60 public String getCompatibilityVersion() {
61 return "4";
62 }
63
64 @Override
65 public boolean isAllowNullKey() {
66 return false;
67 }
68
69 @Override
70 protected boolean isLazyMapTest() {
71 return true;
72 }
73
74 @Override
75 public SortedMap<K, V> makeObject() {
76 return LazySortedMap.lazySortedMap(new TreeMap<>(), FactoryUtils.<V>nullFactory());
77 }
78
79 @Override
80 @Test
81 public void testMapGet() {
82 Map<Integer, Number> map = LazySortedMap.lazySortedMap(new TreeMap<>(), oneFactory);
83 assertEquals(0, map.size());
84 final Number i1 = map.get(5);
85 assertEquals(1, i1);
86 assertEquals(1, map.size());
87
88 map = LazySortedMap.lazySortedMap(new TreeMap<>(), FactoryUtils.<Number>nullFactory());
89 final Number o = map.get(5);
90 assertNull(o);
91 assertEquals(1, map.size());
92
93 }
94
95 @Test
96 public void testReverseSortOrder() {
97 final SortedMap<String, Number> map = LazySortedMap.lazySortedMap(new ConcurrentSkipListMap<>(reverseStringComparator), oneFactory);
98 map.put("A", 5);
99 map.get("B");
100 map.put("C", 8);
101 assertEquals("A", map.lastKey(), "Last key should be A");
102 assertEquals("C", map.firstKey(), "First key should be C");
103 assertEquals("B", map.tailMap("B").firstKey(), "First key in tail map should be B");
104 assertEquals("B", map.headMap("A").lastKey(), "Last key in head map should be B");
105 assertEquals("B", map.subMap("C", "A").lastKey(), "Last key in submap should be B");
106
107 final Comparator<?> c = map.comparator();
108 assertSame(c, reverseStringComparator, "natural order, so comparator should be null");
109 }
110
111 @Test
112 public void testSortOrder() {
113 final SortedMap<String, Number> map = LazySortedMap.lazySortedMap(new TreeMap<>(), oneFactory);
114 map.put("A", 5);
115 map.get("B");
116 map.put("C", 8);
117 assertEquals("A", map.firstKey(), "First key should be A");
118 assertEquals("C", map.lastKey(), "Last key should be C");
119 assertEquals("B", map.tailMap("B").firstKey(), "First key in tail map should be B");
120 assertEquals("B", map.headMap("C").lastKey(), "Last key in head map should be B");
121 assertEquals("B", map.subMap("A", "C").lastKey(), "Last key in submap should be B");
122
123 final Comparator<?> c = map.comparator();
124 assertNull(c, "natural order, so comparator should be null");
125 }
126
127 @Test
128 public void testTransformerDecorate() {
129 final Transformer<Object, Integer> transformer = TransformerUtils.asTransformer(oneFactory);
130 final SortedMap<Integer, Number> map = LazySortedMap.lazySortedMap(new TreeMap<>(), transformer);
131 assertInstanceOf(LazySortedMap.class, map);
132 assertThrows(NullPointerException.class, () -> LazySortedMap.lazySortedMap(new TreeMap<>(), (Transformer<Integer, Number>) null),
133 "Expecting NullPointerException for null transformer");
134 assertThrows(NullPointerException.class, () -> LazySortedMap.lazySortedMap((SortedMap<Integer, Number>) null, transformer),
135 "Expecting NullPointerException for null map");
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149 }