< prev index next >
test/jdk/java/util/Collection/MOAT.java
Print this page
rev 47953 : 8177290: add copy factory methods for unmodifiable List, Set, Map
8184690: add Collectors for collecting into unmodifiable List, Set, and Map
Reviewed-by: alanb, briangoetz, dholmes, jrose, rriggs, scolebourne
*** 1,7 ****
/*
! * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
--- 1,7 ----
/*
! * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*** 55,64 ****
--- 55,66 ----
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
import static java.util.Collections.*;
import java.lang.reflect.*;
+ import java.util.stream.Collectors;
+ import java.util.stream.Stream;
public class MOAT {
// Collections under test must not be initialized to contain this value,
// and maps under test must not contain this value as a key.
// It's used as a sentinel for absent-element testing.
*** 228,237 ****
--- 230,250 ----
testCollection(list);
testImmutableList(list);
testListMutatorsAlwaysThrow(list);
}
+ List<Integer> listCopy = List.copyOf(Arrays.asList(1, 2, 3));
+ testCollection(listCopy);
+ testImmutableList(listCopy);
+ testListMutatorsAlwaysThrow(listCopy);
+
+ List<Integer> listCollected = Stream.of(1, 2, 3).collect(Collectors.toUnmodifiableList());
+ equal(listCollected, List.of(1, 2, 3));
+ testCollection(listCollected);
+ testImmutableList(listCollected);
+ testListMutatorsAlwaysThrow(listCollected);
+
// Immutable Set
testEmptySet(Set.of());
testCollMutatorsAlwaysThrow(Set.of());
testEmptyCollMutatorsAlwaysThrow(Set.of());
for (Set<Integer> set : Arrays.asList(
*** 250,259 ****
--- 263,284 ----
testCollection(set);
testImmutableSet(set);
testCollMutatorsAlwaysThrow(set);
}
+ Set<Integer> setCopy = Set.copyOf(Arrays.asList(1, 2, 3));
+ testCollection(setCopy);
+ testImmutableSet(setCopy);
+ testCollMutatorsAlwaysThrow(setCopy);
+
+ Set<Integer> setCollected = Stream.of(1, 1, 2, 3, 2, 3)
+ .collect(Collectors.toUnmodifiableSet());
+ equal(setCollected, Set.of(1, 2, 3));
+ testCollection(setCollected);
+ testImmutableSet(setCollected);
+ testCollMutatorsAlwaysThrow(setCollected);
+
// Immutable Map
@SuppressWarnings("unchecked")
Map.Entry<Integer,Integer>[] ea = (Map.Entry<Integer,Integer>[])new Map.Entry<?,?>[20];
for (int i = 0; i < ea.length; i++) {
*** 278,287 ****
--- 303,341 ----
Map.ofEntries(ea))) {
testMap(map);
testImmutableMap(map);
testMapMutatorsAlwaysThrow(map);
}
+
+ Map<Integer,Integer> mapCopy = Map.copyOf(new HashMap<>(Map.of(1, 101, 2, 202, 3, 303)));
+ testMap(mapCopy);
+ testImmutableMap(mapCopy);
+ testMapMutatorsAlwaysThrow(mapCopy);
+
+ Map<Integer,Integer> mapCollected1 =
+ Stream.of(1, 2, 3)
+ .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i));
+ equal(mapCollected1, Map.of(1, 101, 2, 202, 3, 303));
+ testMap(mapCollected1);
+ testImmutableMap(mapCollected1);
+ testMapMutatorsAlwaysThrow(mapCollected1);
+
+ try {
+ Stream.of(1, 1, 2, 3, 2, 3)
+ .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i));
+ fail("duplicates should have thrown an exception");
+ } catch (IllegalStateException ise) {
+ pass();
+ }
+
+ Map<Integer,Integer> mapCollected2 =
+ Stream.of(1, 1, 2, 3, 2, 3)
+ .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i, Integer::sum));
+ equal(mapCollected2, Map.of(1, 202, 2, 404, 3, 606));
+ testMap(mapCollected2);
+ testImmutableMap(mapCollected2);
+ testMapMutatorsAlwaysThrow(mapCollected2);
}
private static void checkContainsSelf(Collection<Integer> c) {
check(c.containsAll(c));
check(c.containsAll(Arrays.asList(c.toArray())));
< prev index next >