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.assertSame;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.util.SortedMap;
24 import java.util.TreeMap;
25
26 import org.apache.commons.collections4.Unmodifiable;
27 import org.junit.jupiter.api.Test;
28
29
30
31
32
33
34
35
36 public class UnmodifiableSortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
37
38 @Override
39 public String getCompatibilityVersion() {
40 return "4";
41 }
42
43 @Override
44 public boolean isPutAddSupported() {
45 return false;
46 }
47
48 @Override
49 public boolean isPutChangeSupported() {
50 return false;
51 }
52
53 @Override
54 public boolean isRemoveSupported() {
55 return false;
56 }
57
58 @Override
59 public SortedMap<K, V> makeFullMap() {
60 final SortedMap<K, V> m = new TreeMap<>();
61 addSampleMappings(m);
62 return UnmodifiableSortedMap.unmodifiableSortedMap(m);
63 }
64
65 @Override
66 public SortedMap<K, V> makeObject() {
67 return UnmodifiableSortedMap.unmodifiableSortedMap(new TreeMap<>());
68 }
69
70 @Test
71 public void testDecorateFactory() {
72 final SortedMap<K, V> map = makeFullMap();
73 assertSame(map, UnmodifiableSortedMap.unmodifiableSortedMap(map));
74
75 assertThrows(NullPointerException.class, () -> UnmodifiableSortedMap.unmodifiableSortedMap(null));
76 }
77
78 @Test
79 public void testHeadMap() {
80 final SortedMap<K, V> map = makeFullMap();
81 final SortedMap<K, V> m = new TreeMap<>();
82
83 assertSame(m.isEmpty(), map.headMap((K) "again").isEmpty());
84 assertSame(18, map.size());
85
86 assertSame(17, map.headMap((K) "you").size());
87
88 assertSame(16, map.headMap((K) "we'll").size());
89 }
90
91 @Test
92 public void testSubMap() {
93 final SortedMap<K, V> map = makeFullMap();
94
95 assertSame(18, map.size());
96
97 assertSame(17, map.subMap((K) "again", (K) "you").size());
98
99 assertSame(16, map.subMap((K) "again", (K) "we'll").size());
100
101 assertSame(0, map.subMap((K) "again", (K) "again").size());
102
103 assertSame(map.headMap((K) "you").size(), map.subMap((K) "again", (K) "you").size());
104 }
105
106 @Test
107 public void testTailMap() {
108 final SortedMap<K, V> map = makeFullMap();
109
110 assertSame(18, map.size());
111
112 assertSame(1, map.tailMap((K) "you").size());
113
114 assertSame(2, map.tailMap((K) "we'll").size());
115
116 assertSame(18, map.tailMap((K) "again").size());
117 }
118
119 @Test
120 public void testUnmodifiable() {
121 assertTrue(makeObject() instanceof Unmodifiable);
122 assertTrue(makeFullMap() instanceof Unmodifiable);
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136 }