Path: blob/master/test/jdk/java/util/Collections/SyncSubMutexes.java
41149 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* @modules java.base/java.util:open29* @run testng SyncSubMutexes30*/31import java.lang.reflect.Field;32import java.util.*;33import java.util.Set;34import java.util.Arrays;3536import org.testng.annotations.Test;37import org.testng.annotations.DataProvider;38import static org.testng.Assert.assertSame;3940public class SyncSubMutexes {4142@Test(dataProvider = "Collections")43public void testCollections(Collection<String> instance) {44// nothing to test, no subset methods45}4647@Test(dataProvider = "Lists")48public void testLists(List<String> instance) {49assertSame(getSyncCollectionMutex(instance.subList(0, 1)), getSyncCollectionMutex(instance));50}5152@Test(dataProvider = "Sets")53public void testSets(Set<String> instance) {54// nothing to test, no subset methods5556}5758@Test(dataProvider = "SortedSets")59public void testSortedSets(SortedSet<String> instance) {60assertSame(getSyncCollectionMutex(instance.headSet("Echo")), getSyncCollectionMutex(instance));61assertSame(getSyncCollectionMutex(instance.tailSet("Charlie")), getSyncCollectionMutex(instance));62assertSame(getSyncCollectionMutex(instance.subSet("Charlie", "Echo")), getSyncCollectionMutex(instance));6364}6566@Test(dataProvider = "NavigableSets")67public void testNavigableSets(NavigableSet<String> instance) {68assertSame(getSyncCollectionMutex(instance.descendingSet()), getSyncCollectionMutex(instance));69assertSame(getSyncCollectionMutex(instance.headSet("Echo")), getSyncCollectionMutex(instance));70assertSame(getSyncCollectionMutex(instance.headSet("Echo", true)), getSyncCollectionMutex(instance));71assertSame(getSyncCollectionMutex(instance.tailSet("Charlie")), getSyncCollectionMutex(instance));72assertSame(getSyncCollectionMutex(instance.tailSet("Charlie", true)), getSyncCollectionMutex(instance));73assertSame(getSyncCollectionMutex(instance.subSet("Charlie", "Echo")), getSyncCollectionMutex(instance));74assertSame(getSyncCollectionMutex(instance.subSet("Charlie", true, "Echo", true)), getSyncCollectionMutex(instance));75}7677@Test(dataProvider = "Maps")78public void testMaps(Map<String, String> instance) {79assertSame(getSyncCollectionMutex(instance.entrySet()), getSyncMapMutex(instance));80assertSame(getSyncCollectionMutex(instance.keySet()), getSyncMapMutex(instance));81assertSame(getSyncCollectionMutex(instance.values()), getSyncMapMutex(instance));82}8384@Test(dataProvider = "SortedMaps")85public void testSortedMaps(SortedMap<String, String> instance) {86assertSame(getSyncCollectionMutex(instance.entrySet()), getSyncMapMutex(instance));87assertSame(getSyncCollectionMutex(instance.keySet()), getSyncMapMutex(instance));88assertSame(getSyncCollectionMutex(instance.values()), getSyncMapMutex(instance));89assertSame(getSyncMapMutex(instance.headMap("Echo")), getSyncMapMutex(instance));90assertSame(getSyncMapMutex(instance.tailMap("Charlie")), getSyncMapMutex(instance));91assertSame(getSyncMapMutex(instance.subMap("Charlie", "Echo")), getSyncMapMutex(instance));92}9394@Test(dataProvider = "NavigableMaps")95public void testNavigableMaps(NavigableMap<String, String> instance) {96assertSame(getSyncMapMutex(instance.descendingMap()), getSyncMapMutex(instance));97assertSame(getSyncCollectionMutex(instance.entrySet()), getSyncMapMutex(instance));98assertSame(getSyncCollectionMutex(instance.keySet()), getSyncMapMutex(instance));99assertSame(getSyncCollectionMutex(instance.descendingKeySet()), getSyncMapMutex(instance));100assertSame(getSyncCollectionMutex(instance.values()), getSyncMapMutex(instance));101assertSame(getSyncMapMutex(instance.headMap("Echo")), getSyncMapMutex(instance));102assertSame(getSyncMapMutex(instance.headMap("Echo", true)), getSyncMapMutex(instance));103assertSame(getSyncMapMutex(instance.tailMap("Charlie")), getSyncMapMutex(instance));104assertSame(getSyncMapMutex(instance.tailMap("Charlie", true)), getSyncMapMutex(instance));105assertSame(getSyncMapMutex(instance.subMap("Charlie", true, "Echo", true)), getSyncMapMutex(instance));106assertSame(getSyncMapMutex(instance.subMap("Charlie", true, "Echo", true)), getSyncMapMutex(instance));107}108109@DataProvider(name = "Collections", parallel = true)110public static Iterator<Object[]> collectionProvider() {111return makeCollections().iterator();112}113114@DataProvider(name = "Lists", parallel = true)115public static Iterator<Object[]> listProvider() {116return makeLists().iterator();117}118119@DataProvider(name = "Sets", parallel = true)120public static Iterator<Object[]> setProvider() {121return makeSets().iterator();122}123124@DataProvider(name = "SortedSets", parallel = true)125public static Iterator<Object[]> sortedsetProvider() {126return makeSortedSets().iterator();127}128129@DataProvider(name = "NavigableSets", parallel = true)130public static Iterator<Object[]> navigablesetProvider() {131return makeNavigableSets().iterator();132}133134@DataProvider(name = "Maps", parallel = true)135public static Iterator<Object[]> mapProvider() {136return makeMaps().iterator();137}138139@DataProvider(name = "SortedMaps", parallel = true)140public static Iterator<Object[]> sortedmapProvider() {141return makeSortedMaps().iterator();142}143144@DataProvider(name = "NavigableMaps", parallel = true)145public static Iterator<Object[]> navigablemapProvider() {146return makeNavigableMaps().iterator();147}148149private static final Collection<String> BASE_COLLECTION = Collections.unmodifiableCollection(150Arrays.asList("Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf")151);152private static final Map<String, String> BASE_MAP;153154static {155Map<String, String> map = new HashMap<>();156for(String each : BASE_COLLECTION) {157map.put(each, "*" + each + "*");158}159BASE_MAP = Collections.unmodifiableMap(map);160}161162public static Collection<Object[]> makeCollections() {163Collection<Object[]> instances = new ArrayList<>();164instances.add(new Object[] {Collections.synchronizedCollection(new ArrayList<>(BASE_COLLECTION))});165instances.addAll(makeLists());166167return instances;168}169170public static Collection<Object[]> makeLists() {171Collection<Object[]> instances = new ArrayList<>();172instances.add(new Object[] {Collections.synchronizedList(new ArrayList<>(BASE_COLLECTION))});173instances.add(new Object[] {Collections.synchronizedList(new ArrayList<>(BASE_COLLECTION)).subList(1, 2)});174175return instances;176}177178public static Collection<Object[]> makeSets() {179Collection<Object[]> instances = new ArrayList<>();180181instances.add(new Object[] {Collections.synchronizedSet(new TreeSet<>(BASE_COLLECTION))});182instances.addAll(makeSortedSets());183return instances;184}185186public static Collection<Object[]> makeSortedSets() {187Collection<Object[]> instances = new ArrayList<>();188instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION))});189instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION)).headSet("Foxtrot")});190instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION)).tailSet("Bravo")});191instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION)).subSet("Bravo", "Foxtrot")});192instances.addAll(makeNavigableSets());193194return instances;195}196197public static Collection<Object[]> makeNavigableSets() {198Collection<Object[]> instances = new ArrayList<>();199200instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION))});201instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).descendingSet().descendingSet()});202instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).headSet("Foxtrot")});203instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).headSet("Foxtrot", true)});204instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).tailSet("Bravo")});205instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).tailSet("Bravo", true)});206instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).subSet("Bravo", "Foxtrot")});207instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).subSet("Bravo", true, "Foxtrot", true)});208209return instances;210}211212public static Collection<Object[]> makeMaps() {213Collection<Object[]> instances = new ArrayList<>();214215instances.add(new Object[] {Collections.synchronizedMap(new HashMap<>(BASE_MAP))});216instances.addAll(makeSortedMaps());217218return instances;219}220221public static Collection<Object[]> makeSortedMaps() {222Collection<Object[]> instances = new ArrayList<>();223224instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP))});225instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP)).headMap("Foxtrot")});226instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP)).tailMap("Bravo")});227instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP)).subMap("Bravo", "Foxtrot")});228instances.addAll(makeNavigableMaps());229230return instances;231}232233public static Collection<Object[]> makeNavigableMaps() {234Collection<Object[]> instances = new ArrayList<>();235236instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP))});237instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP).descendingMap().descendingMap())});238instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).headMap("Foxtrot")});239instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).headMap("Foxtrot", true)});240instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).tailMap("Bravo")});241instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).tailMap("Bravo", true)});242instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).subMap("Bravo", "Foxtrot")});243instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).subMap("Bravo", true, "Foxtrot", true)});244245return instances;246}247248private static Object getSyncCollectionMutex(Collection<?> from) {249try {250Class<?> synchronizedCollectionClazz = Class.forName("java.util.Collections$SynchronizedCollection");251Field f = synchronizedCollectionClazz.getDeclaredField("mutex");252f.setAccessible(true);253return f.get(from);254} catch ( ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {255throw new RuntimeException("Unable to get mutex field.", e);256}257}258259private static Object getSyncMapMutex(Map<?,?> from) {260try {261Class<?> synchronizedMapClazz = Class.forName("java.util.Collections$SynchronizedMap");262Field f = synchronizedMapClazz.getDeclaredField("mutex");263f.setAccessible(true);264return f.get(from);265} catch ( ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {266throw new RuntimeException("Unable to get mutex field.", e);267}268}269270}271272273