Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/SyncSubMutexes.java
38812 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 804820926* @summary Check that Collections.synchronizedNavigableSet().tailSet() is using27* the same lock object as it's source.28* @run testng SyncSubMutexes29*/30import java.lang.reflect.Field;31import java.util.*;32import java.util.Set;33import java.util.Arrays;3435import org.testng.annotations.Test;36import org.testng.annotations.DataProvider;37import static org.testng.Assert.assertSame;3839public class SyncSubMutexes {4041@Test(dataProvider = "Collections")42public void testCollections(Collection<String> instance) {43// nothing to test, no subset methods44}4546@Test(dataProvider = "Lists")47public void testLists(List<String> instance) {48assertSame(getSyncCollectionMutex(instance.subList(0, 1)), getSyncCollectionMutex(instance));49}5051@Test(dataProvider = "Sets")52public void testSets(Set<String> instance) {53// nothing to test, no subset methods5455}5657@Test(dataProvider = "SortedSets")58public void testSortedSets(SortedSet<String> instance) {59assertSame(getSyncCollectionMutex(instance.headSet("Echo")), getSyncCollectionMutex(instance));60assertSame(getSyncCollectionMutex(instance.tailSet("Charlie")), getSyncCollectionMutex(instance));61assertSame(getSyncCollectionMutex(instance.subSet("Charlie", "Echo")), getSyncCollectionMutex(instance));6263}6465@Test(dataProvider = "NavigableSets")66public void testNavigableSets(NavigableSet<String> instance) {67assertSame(getSyncCollectionMutex(instance.descendingSet()), getSyncCollectionMutex(instance));68assertSame(getSyncCollectionMutex(instance.headSet("Echo")), getSyncCollectionMutex(instance));69assertSame(getSyncCollectionMutex(instance.headSet("Echo", true)), getSyncCollectionMutex(instance));70assertSame(getSyncCollectionMutex(instance.tailSet("Charlie")), getSyncCollectionMutex(instance));71assertSame(getSyncCollectionMutex(instance.tailSet("Charlie", true)), getSyncCollectionMutex(instance));72assertSame(getSyncCollectionMutex(instance.subSet("Charlie", "Echo")), getSyncCollectionMutex(instance));73assertSame(getSyncCollectionMutex(instance.subSet("Charlie", true, "Echo", true)), getSyncCollectionMutex(instance));74}7576@Test(dataProvider = "Maps")77public void testMaps(Map<String, String> instance) {78assertSame(getSyncCollectionMutex(instance.entrySet()), getSyncMapMutex(instance));79assertSame(getSyncCollectionMutex(instance.keySet()), getSyncMapMutex(instance));80assertSame(getSyncCollectionMutex(instance.values()), getSyncMapMutex(instance));81}8283@Test(dataProvider = "SortedMaps")84public void testSortedMaps(SortedMap<String, String> instance) {85assertSame(getSyncCollectionMutex(instance.entrySet()), getSyncMapMutex(instance));86assertSame(getSyncCollectionMutex(instance.keySet()), getSyncMapMutex(instance));87assertSame(getSyncCollectionMutex(instance.values()), getSyncMapMutex(instance));88assertSame(getSyncMapMutex(instance.headMap("Echo")), getSyncMapMutex(instance));89assertSame(getSyncMapMutex(instance.tailMap("Charlie")), getSyncMapMutex(instance));90assertSame(getSyncMapMutex(instance.subMap("Charlie", "Echo")), getSyncMapMutex(instance));91}9293@Test(dataProvider = "NavigableMaps")94public void testNavigableMaps(NavigableMap<String, String> instance) {95assertSame(getSyncMapMutex(instance.descendingMap()), getSyncMapMutex(instance));96assertSame(getSyncCollectionMutex(instance.entrySet()), getSyncMapMutex(instance));97assertSame(getSyncCollectionMutex(instance.keySet()), getSyncMapMutex(instance));98assertSame(getSyncCollectionMutex(instance.descendingKeySet()), getSyncMapMutex(instance));99assertSame(getSyncCollectionMutex(instance.values()), getSyncMapMutex(instance));100assertSame(getSyncMapMutex(instance.headMap("Echo")), getSyncMapMutex(instance));101assertSame(getSyncMapMutex(instance.headMap("Echo", true)), getSyncMapMutex(instance));102assertSame(getSyncMapMutex(instance.tailMap("Charlie")), getSyncMapMutex(instance));103assertSame(getSyncMapMutex(instance.tailMap("Charlie", true)), getSyncMapMutex(instance));104assertSame(getSyncMapMutex(instance.subMap("Charlie", true, "Echo", true)), getSyncMapMutex(instance));105assertSame(getSyncMapMutex(instance.subMap("Charlie", true, "Echo", true)), getSyncMapMutex(instance));106}107108@DataProvider(name = "Collections", parallel = true)109public static Iterator<Object[]> collectionProvider() {110return makeCollections().iterator();111}112113@DataProvider(name = "Lists", parallel = true)114public static Iterator<Object[]> listProvider() {115return makeLists().iterator();116}117118@DataProvider(name = "Sets", parallel = true)119public static Iterator<Object[]> setProvider() {120return makeSets().iterator();121}122123@DataProvider(name = "SortedSets", parallel = true)124public static Iterator<Object[]> sortedsetProvider() {125return makeSortedSets().iterator();126}127128@DataProvider(name = "NavigableSets", parallel = true)129public static Iterator<Object[]> navigablesetProvider() {130return makeNavigableSets().iterator();131}132133@DataProvider(name = "Maps", parallel = true)134public static Iterator<Object[]> mapProvider() {135return makeMaps().iterator();136}137138@DataProvider(name = "SortedMaps", parallel = true)139public static Iterator<Object[]> sortedmapProvider() {140return makeSortedMaps().iterator();141}142143@DataProvider(name = "NavigableMaps", parallel = true)144public static Iterator<Object[]> navigablemapProvider() {145return makeNavigableMaps().iterator();146}147148private static final Collection<String> BASE_COLLECTION = Collections.unmodifiableCollection(149Arrays.asList("Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf")150);151private static final Map<String, String> BASE_MAP;152153static {154Map<String, String> map = new HashMap<>();155for(String each : BASE_COLLECTION) {156map.put(each, "*" + each + "*");157}158BASE_MAP = Collections.unmodifiableMap(map);159}160161public static Collection<Object[]> makeCollections() {162Collection<Object[]> instances = new ArrayList<>();163instances.add(new Object[] {Collections.synchronizedCollection(new ArrayList<>(BASE_COLLECTION))});164instances.addAll(makeLists());165166return instances;167}168169public static Collection<Object[]> makeLists() {170Collection<Object[]> instances = new ArrayList<>();171instances.add(new Object[] {Collections.synchronizedList(new ArrayList<>(BASE_COLLECTION))});172instances.add(new Object[] {Collections.synchronizedList(new ArrayList<>(BASE_COLLECTION)).subList(1, 2)});173174return instances;175}176177public static Collection<Object[]> makeSets() {178Collection<Object[]> instances = new ArrayList<>();179180instances.add(new Object[] {Collections.synchronizedSet(new TreeSet<>(BASE_COLLECTION))});181instances.addAll(makeSortedSets());182return instances;183}184185public static Collection<Object[]> makeSortedSets() {186Collection<Object[]> instances = new ArrayList<>();187instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION))});188instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION)).headSet("Foxtrot")});189instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION)).tailSet("Bravo")});190instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION)).subSet("Bravo", "Foxtrot")});191instances.addAll(makeNavigableSets());192193return instances;194}195196public static Collection<Object[]> makeNavigableSets() {197Collection<Object[]> instances = new ArrayList<>();198199instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION))});200instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).descendingSet().descendingSet()});201instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).headSet("Foxtrot")});202instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).headSet("Foxtrot", true)});203instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).tailSet("Bravo")});204instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).tailSet("Bravo", true)});205instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).subSet("Bravo", "Foxtrot")});206instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).subSet("Bravo", true, "Foxtrot", true)});207208return instances;209}210211public static Collection<Object[]> makeMaps() {212Collection<Object[]> instances = new ArrayList<>();213214instances.add(new Object[] {Collections.synchronizedMap(new HashMap<>(BASE_MAP))});215instances.addAll(makeSortedMaps());216217return instances;218}219220public static Collection<Object[]> makeSortedMaps() {221Collection<Object[]> instances = new ArrayList<>();222223instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP))});224instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP)).headMap("Foxtrot")});225instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP)).tailMap("Bravo")});226instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP)).subMap("Bravo", "Foxtrot")});227instances.addAll(makeNavigableMaps());228229return instances;230}231232public static Collection<Object[]> makeNavigableMaps() {233Collection<Object[]> instances = new ArrayList<>();234235instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP))});236instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP).descendingMap().descendingMap())});237instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).headMap("Foxtrot")});238instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).headMap("Foxtrot", true)});239instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).tailMap("Bravo")});240instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).tailMap("Bravo", true)});241instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).subMap("Bravo", "Foxtrot")});242instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).subMap("Bravo", true, "Foxtrot", true)});243244return instances;245}246247private static Object getSyncCollectionMutex(Collection<?> from) {248try {249Class<?> synchronizedCollectionClazz = Class.forName("java.util.Collections$SynchronizedCollection");250Field f = synchronizedCollectionClazz.getDeclaredField("mutex");251f.setAccessible(true);252return f.get(from);253} catch ( ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {254throw new RuntimeException("Unable to get mutex field.", e);255}256}257258private static Object getSyncMapMutex(Map<?,?> from) {259try {260Class<?> synchronizedMapClazz = Class.forName("java.util.Collections$SynchronizedMap");261Field f = synchronizedMapClazz.getDeclaredField("mutex");262f.setAccessible(true);263return f.get(from);264} catch ( ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {265throw new RuntimeException("Unable to get mutex field.", e);266}267}268269}270271272