Path: blob/master/test/jdk/java/util/AbstractList/FailFastIterator.java
41152 views
/*1* Copyright (c) 1999, 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 418989626* @summary AbstractList iterators previously checked for co-modification27* *after* the set/add/remove operations were performed.28*/2930import java.util.ArrayList;31import java.util.ConcurrentModificationException;32import java.util.List;33import java.util.ListIterator;3435public class FailFastIterator {36public static void main(String[] args) throws Exception {37List orig = new ArrayList(100);38for (int i=0; i<100; i++)39orig.add(new Integer(i));4041List copy = new ArrayList(orig);42try {43ListIterator i = copy.listIterator();44i.next();45copy.remove(99);46copy.add(new Integer(99));47i.remove();48throw new Exception("remove: iterator didn't fail fast");49} catch (ConcurrentModificationException e) {50}51if (!copy.equals(orig))52throw new Exception("remove: iterator didn't fail fast enough");5354try {55ListIterator i = copy.listIterator();56i.next();57copy.remove(99);58copy.add(new Integer(99));59i.set(new Integer(666));60throw new Exception("set: iterator didn't fail fast");61} catch (ConcurrentModificationException e) {62}63if (!copy.equals(orig))64throw new Exception("set: iterator didn't fail fast enough");6566try {67ListIterator i = copy.listIterator();68copy.remove(99);69copy.add(new Integer(99));70i.add(new Integer(666));71throw new Exception("add: iterator didn't fail fast");72} catch (ConcurrentModificationException e) {73}74if (!copy.equals(orig))75throw new Exception("add: iterator didn't fail fast enough");76}77}787980