Path: blob/master/test/jdk/java/util/Spliterator/SpliteratorFailFastTest.java
41149 views
/*1* Copyright (c) 2013, 2017, 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*/2223import java.util.ArrayList;24import java.util.Arrays;25import java.util.ConcurrentModificationException;26import java.util.HashMap;27import java.util.HashSet;28import java.util.LinkedHashMap;29import java.util.LinkedHashSet;30import java.util.LinkedList;31import java.util.List;32import java.util.PriorityQueue;33import java.util.Spliterator;34import java.util.Stack;35import java.util.TreeMap;36import java.util.TreeSet;37import java.util.Vector;38import java.util.WeakHashMap;39import java.util.function.Supplier;4041import org.testng.Assert.ThrowingRunnable;42import org.testng.annotations.DataProvider;43import org.testng.annotations.Test;4445import static org.testng.Assert.assertThrows;4647/**48* @test49* @bug 814874850* @summary Spliterator fail-fast tests51* @run testng SpliteratorFailFastTest52*/5354@Test55public class SpliteratorFailFastTest extends SpliteratorLateBindingFailFastHelper {5657static Object[][] spliteratorDataProvider;5859@DataProvider(name = "Source")60public static Object[][] spliteratorDataProvider() {61if (spliteratorDataProvider != null) {62return spliteratorDataProvider;63}6465List<Object[]> data = new ArrayList<>();66SpliteratorDataBuilder<Integer> db =67new SpliteratorDataBuilder<>(data, 5, Arrays.asList(1, 2, 3, 4));6869// Collections7071db.addList(ArrayList::new);7273db.addList(LinkedList::new);7475db.addList(Vector::new);7677db.addList(AbstractRandomAccessListImpl::new);7879db.addCollection(HashSet::new);8081db.addCollection(LinkedHashSet::new);8283db.addCollection(TreeSet::new);8485db.addCollection(c -> {86Stack<Integer> s = new Stack<>();87s.addAll(c);88return s;89});9091db.addCollection(PriorityQueue::new);9293// ArrayDeque fails some tests since its fail-fast support is weaker94// than other collections and limited to detecting most, but not all,95// removals. It probably requires its own test since it is difficult96// to abstract out the conditions under which it fails-fast.97// db.addCollection(ArrayDeque::new);9899// Maps100101db.addMap(HashMap::new);102103db.addMap(LinkedHashMap::new);104105// This fails when run through jtreg but passes when run through106// ant107// db.addMap(IdentityHashMap::new);108109db.addMap(WeakHashMap::new);110111// @@@ Descending maps etc112db.addMap(TreeMap::new);113114return spliteratorDataProvider = data.toArray(new Object[0][]);115}116117@Test(dataProvider = "Source")118public <T> void testTryAdvance(String description, Supplier<Source<T>> ss) {119{120Source<T> source = ss.get();121Spliterator<T> s = source.spliterator();122123s.tryAdvance(e -> {124});125source.update();126127assertThrowsCME(() -> s.tryAdvance(e -> {128}));129}130131{132Source<T> source = ss.get();133Spliterator<T> s = source.spliterator();134135s.tryAdvance(e -> {136});137source.update();138139assertThrowsCME(() -> s.forEachRemaining(e -> {140}));141}142}143144@Test(dataProvider = "Source")145public <T> void testForEach(String description, Supplier<Source<T>> ss) {146Source<T> source = ss.get();147Spliterator<T> s = source.spliterator();148149assertThrowsCME(() -> s.forEachRemaining(e -> {150source.update();151}));152}153154@Test(dataProvider = "Source")155public <T> void testEstimateSize(String description, Supplier<Source<T>> ss) {156{157Source<T> source = ss.get();158Spliterator<T> s = source.spliterator();159160s.estimateSize();161source.update();162163assertThrowsCME(() -> s.tryAdvance(e -> {164}));165}166167{168Source<T> source = ss.get();169Spliterator<T> s = source.spliterator();170171s.estimateSize();172source.update();173174assertThrowsCME(() -> s.forEachRemaining(e -> {175}));176}177}178179private void assertThrowsCME(ThrowingRunnable r) {180assertThrows(ConcurrentModificationException.class, r);181}182183}184185186