19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
|
24 import java.util.AbstractMap; |
|
25 import java.util.Collection; |
|
26 import java.util.Comparator; |
|
27 import java.util.HashMap; |
|
28 import java.util.Iterator; |
|
29 import java.util.List; |
|
30 import java.util.ListIterator; |
|
31 import java.util.Map; |
|
32 import java.util.Objects; |
|
33 import java.util.Set; |
|
34 import java.util.function.Supplier; |
24 import java.util.regex.Pattern; |
35 import java.util.regex.Pattern; |
25 |
36 |
|
37 import static java.util.Collections.emptyList; |
|
38 import static java.util.Collections.singleton; |
26 import static java.util.Objects.requireNonNull; |
39 import static java.util.Objects.requireNonNull; |
27 |
40 |
28 // |
41 // |
29 // A set of testing utility functions |
42 // A set of testing utility functions |
30 // |
43 // |
96 "Expected exception message to match the regex '%s', " + |
109 "Expected exception message to match the regex '%s', " + |
97 "actual message: %s", messageRegex, m), t); |
110 "actual message: %s", messageRegex, m), t); |
98 } |
111 } |
99 return t; |
112 return t; |
100 } |
113 } |
|
114 |
|
115 /* |
|
116 * Asserts that the given Collection is unmodifiable: any mutator method |
|
117 * throw an UnsupportedOperationException unconditionally. |
|
118 */ |
|
119 public static void assertUnmodifiableCollection(Collection<?> collection) { |
|
120 assertUnmodifiableCollection(collection, () -> null); |
|
121 } |
|
122 |
|
123 public static <E> void assertUnmodifiableCollection(Collection<E> collection, |
|
124 Supplier<? extends E> elementsFactory) { |
|
125 requireNonNull(collection, "collection"); |
|
126 requireNonNull(elementsFactory, "elementsFactory"); |
|
127 |
|
128 E e = elementsFactory.get(); |
|
129 |
|
130 assertUOE(() -> collection.add(e)); |
|
131 assertUOE(() -> collection.addAll(singleton(e))); |
|
132 Iterator<?> i = collection.iterator(); |
|
133 if (i.hasNext()) { |
|
134 i.next(); |
|
135 assertUOE(i::remove); |
|
136 } |
|
137 assertUOE(collection::clear); |
|
138 assertUOE(() -> collection.remove(e)); |
|
139 assertUOE(() -> collection.removeAll(singleton(e))); |
|
140 assertUOE(() -> collection.removeIf(x -> true)); |
|
141 assertUOE(() -> collection.retainAll(emptyList())); |
|
142 // No need to check toArray methods, since API guarantees arrays |
|
143 // returned by them are "safe" |
|
144 } |
|
145 |
|
146 public static void assertUnmodifiableSet(Set<?> set) { |
|
147 assertUnmodifiableCollection(set, () -> null); |
|
148 } |
|
149 |
|
150 public static <E> void assertUnmodifiableSet(Set<E> set, |
|
151 Supplier<? extends E> elementsFactory) { |
|
152 assertUnmodifiableCollection(set, elementsFactory); |
|
153 } |
|
154 |
|
155 public static void assertUnmodifiableList(List<?> list) { |
|
156 assertUnmodifiableList(list, () -> null); |
|
157 } |
|
158 |
|
159 public static <E> void assertUnmodifiableList(List<E> list, |
|
160 Supplier<? extends E> elementsFactory) { |
|
161 assertUnmodifiableList(list, elementsFactory, 3); // This list, its sublist and its sublist's sublist |
|
162 } |
|
163 |
|
164 private static <E> void assertUnmodifiableList(List<E> list, |
|
165 Supplier<? extends E> elementsFactory, |
|
166 int depth) { |
|
167 requireNonNull(list, "list"); |
|
168 requireNonNull(elementsFactory, "elementsFactory"); |
|
169 if (depth < 0) { |
|
170 throw new IllegalArgumentException("depth: " + depth); |
|
171 } |
|
172 if (depth == 0) { |
|
173 return; |
|
174 } |
|
175 |
|
176 E e = elementsFactory.get(); |
|
177 |
|
178 assertUnmodifiableCollection(list, elementsFactory); |
|
179 assertUOE(() -> list.add(0, e)); |
|
180 assertUOE(() -> list.addAll(0, singleton(e))); |
|
181 |
|
182 ListIterator<E> i = list.listIterator(); |
|
183 if (i.hasNext()) { |
|
184 i.next(); |
|
185 assertUOE(i::remove); |
|
186 assertUOE(() -> i.set(e)); |
|
187 assertUOE(() -> i.add(e)); |
|
188 } |
|
189 assertUOE(() -> list.remove((int) 0)); |
|
190 assertUOE(() -> list.replaceAll(x -> e)); |
|
191 assertUOE(() -> list.set(0, e)); |
|
192 |
|
193 // Any non-null general-purpose Comparator would do |
|
194 Comparator<Object> comparator = (a, b) -> Objects.hash(a, b); |
|
195 assertUOE(() -> list.sort(comparator)); |
|
196 |
|
197 assertUnmodifiableList(list.subList(0, list.size()), elementsFactory, depth - 1); |
|
198 } |
|
199 |
|
200 public static void assertUnmodifiableMap(Map<?, ?> map) { |
|
201 assertUnmodifiableMap(map, () -> new AbstractMap.SimpleImmutableEntry<>(null, null)); |
|
202 } |
|
203 |
|
204 public static <K, V> void assertUnmodifiableMap(Map<K, V> map, |
|
205 Supplier<? extends Map.Entry<? extends K, ? extends V>> entriesFactory) { |
|
206 requireNonNull(map, "map"); |
|
207 requireNonNull(entriesFactory, "entriesFactory"); |
|
208 assertUOE(map::clear); |
|
209 |
|
210 Map.Entry<? extends K, ? extends V> e1 = entriesFactory.get(); |
|
211 K k = e1.getKey(); |
|
212 V v = e1.getValue(); |
|
213 |
|
214 assertUOE(() -> map.compute(k, (k1, v1) -> v)); |
|
215 assertUOE(() -> map.computeIfAbsent(k, (k1) -> v)); |
|
216 assertUOE(() -> map.computeIfPresent(k, (k1, v1) -> v)); |
|
217 |
|
218 Set<Map.Entry<K, V>> entrySet = map.entrySet(); |
|
219 assertUnmodifiableSet(entrySet); |
|
220 for (Map.Entry<K, V> e : entrySet) { |
|
221 assertUOE(() -> e.setValue(null)); |
|
222 } |
|
223 |
|
224 assertUnmodifiableSet(map.keySet()); |
|
225 assertUOE(() -> map.merge(k, v, (k1, v1) -> v)); |
|
226 assertUOE(() -> map.put(k, v)); |
|
227 // Map.of(k, v) wouldn't do, as it doesn't permit nulls |
|
228 Map<K, V> m = new HashMap<>(); |
|
229 m.put(k, v); |
|
230 assertUOE(() -> map.putAll(m)); |
|
231 assertUOE(() -> map.putIfAbsent(k, v)); |
|
232 assertUOE(() -> map.remove(k)); |
|
233 assertUOE(() -> map.remove(k, v)); |
|
234 assertUOE(() -> map.replace(k, v)); |
|
235 assertUOE(() -> map.replace(k, v, v)); |
|
236 assertUOE(() -> map.replaceAll((k1, v1) -> v)); |
|
237 assertUnmodifiableCollection(map.values()); |
|
238 } |
|
239 |
|
240 public static void assertUOE(ThrowingProcedure code) { |
|
241 assertThrows(UnsupportedOperationException.class, code); |
|
242 } |
101 } |
243 } |