1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertSame;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.apache.commons.collections4.functors.NOPTransformer;
29 import org.apache.commons.collections4.map.HashedMap;
30 import org.apache.commons.collections4.splitmap.TransformedSplitMap;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34
35
36
37 @SuppressWarnings("boxing")
38 public class SplitMapUtilsTest {
39
40 private Map<String, Integer> backingMap;
41 private TransformedSplitMap<String, String, String, Integer> transformedMap;
42
43 private final Transformer<String, Integer> stringToInt = Integer::valueOf;
44
45 private void attemptGetOperation(final Runnable r) {
46 assertThrows(UnsupportedOperationException.class, () -> r.run(),
47 "Put exposed as writable Map must not allow Get operations");
48 }
49
50 private void attemptPutOperation(final Runnable r) {
51 assertThrows(UnsupportedOperationException.class, () -> r.run(),
52 "Get exposed as writable Map must not allow Put operations");
53 }
54
55 @BeforeEach
56 public void setUp() throws Exception {
57 backingMap = new HashMap<>();
58 transformedMap = TransformedSplitMap.transformingMap(backingMap, NOPTransformer.<String>nopTransformer(),
59 stringToInt);
60 for (int i = 0; i < 10; i++) {
61 transformedMap.put(String.valueOf(i), String.valueOf(i));
62 }
63 }
64
65 @Test
66 public void testAlreadyReadableMap() {
67 final HashedMap<String, Integer> hashedMap = new HashedMap<>();
68 assertSame(hashedMap, SplitMapUtils.readableMap(hashedMap));
69 }
70
71 @Test
72 public void testAlreadyWritableMap() {
73 final HashedMap<String, String> hashedMap = new HashedMap<>();
74 assertSame(hashedMap, SplitMapUtils.writableMap(hashedMap));
75 }
76
77 @Test
78 public void testReadableMap() {
79 final IterableMap<String, Integer> map = SplitMapUtils.readableMap(transformedMap);
80
81
82 for (int i = 0; i < 10; i++) {
83 assertFalse(map.containsValue(String.valueOf(i)));
84 assertEquals(i, map.get(String.valueOf(i)).intValue());
85 }
86
87
88 final MapIterator<String, Integer> it = map.mapIterator();
89 while (it.hasNext()) {
90 final String k = it.next();
91 assertEquals(k, it.getKey());
92 assertEquals(Integer.valueOf(k), it.getValue());
93 }
94
95
96 assertTrue(map instanceof Unmodifiable);
97
98
99 int sz = map.size();
100
101 attemptPutOperation(map::clear);
102
103 assertEquals(sz, map.size());
104
105 attemptPutOperation(() -> map.put("foo", 100));
106
107 final HashMap<String, Integer> m = new HashMap<>();
108 m.put("foo", 100);
109 m.put("bar", 200);
110 m.put("baz", 300);
111 attemptPutOperation(() -> map.putAll(m));
112
113
114 final IterableMap<String, Integer> other = SplitMapUtils.readableMap(transformedMap);
115 assertEquals(other, map);
116 assertEquals(other.hashCode(), map.hashCode());
117
118
119 for (int i = 0; i < 10; i++) {
120 assertEquals(i, map.remove(String.valueOf(i)).intValue());
121 assertEquals(--sz, map.size());
122 }
123 assertTrue(map.isEmpty());
124 assertSame(map, SplitMapUtils.readableMap(map));
125 }
126
127 @Test
128 @SuppressWarnings("unchecked")
129 public void testWritableMap() {
130 final Map<String, String> map = SplitMapUtils.writableMap(transformedMap);
131 attemptGetOperation(() -> map.get(null));
132 attemptGetOperation(map::entrySet);
133 attemptGetOperation(map::keySet);
134 attemptGetOperation(map::values);
135 attemptGetOperation(map::size);
136 attemptGetOperation(map::isEmpty);
137 attemptGetOperation(() -> map.containsKey(null));
138 attemptGetOperation(() -> map.containsValue(null));
139 attemptGetOperation(() -> map.remove(null));
140
141
142 final Map<String, String> other = SplitMapUtils.writableMap(transformedMap);
143 assertEquals(other, map);
144 assertEquals(other.hashCode(), map.hashCode());
145
146
147 int sz = backingMap.size();
148 assertFalse(backingMap.containsKey("foo"));
149 map.put("new", "66");
150 assertEquals(++sz, backingMap.size());
151
152
153 final Map<String, String> more = new HashMap<>();
154 more.put("foo", "77");
155 more.put("bar", "88");
156 more.put("baz", "99");
157 map.putAll(more);
158 assertEquals(sz + more.size(), backingMap.size());
159
160
161 map.clear();
162 assertTrue(backingMap.isEmpty());
163 assertSame(map, SplitMapUtils.writableMap((Put<String, String>) map));
164 }
165
166 }