1 /*
2 * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
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
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 4904067 5023830 7129185 8072015 8292955
27 * @summary Unit test for Collections.checkedMap
28 * @author Josh Bloch
29 * @run testng CheckedMapBash
30 * @key randomness
31 */
32
33 import org.testng.Assert;
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
36
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.Map;
44 import java.util.Random;
45 import java.util.Set;
46 import java.util.TreeMap;
47 import java.util.function.Supplier;
48
49 import static org.testng.Assert.fail;
50
51 public class CheckedMapBash {
52 static final Random rnd = new Random();
174 {"Collections.checkedMap(TreeMap(reverseOrder))",
175 (Supplier) () -> Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class)},
176 {"Collections.checkedMap(TreeMap.descendingMap())",
177 (Supplier) () -> Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class)},
178 {"Collections.checkedNavigableMap(TreeMap)",
179 (Supplier) () -> Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class)},
180 {"Collections.checkedNavigableMap(TreeMap(reverseOrder))",
181 (Supplier) () -> Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class)},
182 {"Collections.checkedNavigableMap(TreeMap.descendingMap())",
183 (Supplier) () -> Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class)},
184 };
185 return Arrays.asList(params);
186 }
187
188 @Test(groups = "type_check")
189 public static void testCheckedMapMerge() {
190 Map m = Collections.checkedMap(new HashMap<>(), Integer.class, Integer.class);
191 Assert.assertThrows(ClassCastException.class, () -> m.merge("key", "value", (v1, v2) -> null));
192 Assert.assertThrows(ClassCastException.class, () -> m.merge("key", 3, (v1, v2) -> v2));
193 }
194 }
|
1 /*
2 * Copyright (c) 2003, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
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
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 4904067 5023830 7129185 8072015 8292955
27 * @summary Unit test for Collections.checkedMap
28 * @author Josh Bloch
29 * @key randomness
30 * @library /test/lib
31 * @run testng CheckedMapBash
32 */
33
34 import jdk.test.lib.valueclass.VClass;
35 import org.testng.Assert;
36 import org.testng.annotations.DataProvider;
37 import org.testng.annotations.Test;
38
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.Iterator;
45 import java.util.Map;
46 import java.util.Random;
47 import java.util.Set;
48 import java.util.TreeMap;
49 import java.util.function.Supplier;
50
51 import static org.testng.Assert.fail;
52
53 public class CheckedMapBash {
54 static final Random rnd = new Random();
176 {"Collections.checkedMap(TreeMap(reverseOrder))",
177 (Supplier) () -> Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class)},
178 {"Collections.checkedMap(TreeMap.descendingMap())",
179 (Supplier) () -> Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class)},
180 {"Collections.checkedNavigableMap(TreeMap)",
181 (Supplier) () -> Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class)},
182 {"Collections.checkedNavigableMap(TreeMap(reverseOrder))",
183 (Supplier) () -> Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class)},
184 {"Collections.checkedNavigableMap(TreeMap.descendingMap())",
185 (Supplier) () -> Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class)},
186 };
187 return Arrays.asList(params);
188 }
189
190 @Test(groups = "type_check")
191 public static void testCheckedMapMerge() {
192 Map m = Collections.checkedMap(new HashMap<>(), Integer.class, Integer.class);
193 Assert.assertThrows(ClassCastException.class, () -> m.merge("key", "value", (v1, v2) -> null));
194 Assert.assertThrows(ClassCastException.class, () -> m.merge("key", 3, (v1, v2) -> v2));
195 }
196
197 @Test
198 public static void testValueCheckedMap() {
199 Map<VClass,VClass> m = Collections.checkedMap(new HashMap<>(), VClass.class, VClass.class);
200 m.put(new VClass(1, new int[] { 1 }), new VClass(10, new int[] { 10 }));
201 if (!m.containsKey(new VClass(1, new int[] { 1 })) || !m.containsValue(new VClass(10, new int[] { 10 })))
202 fail("value checkedMap lookup failed");
203 if (!new HashMap<>(m).equals(m))
204 fail("value checkedMap equals failed");
205 try {
206 ((Map) m).put("not a Tuple", new VClass(2, new int[] { 2 }));
207 fail("value checkedMap accepted wrong type");
208 } catch (ClassCastException expected) { }
209 }
210 }
|