Path: blob/master/test/jdk/java/util/ArrayList/Bug6533203.java
41149 views
/*1* Copyright (c) 2007, 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 653320326* @summary AbstractList.ListItr.add might corrupt iterator state if enclosing add throws27*/2829import java.util.ArrayList;30import java.util.List;31import java.util.ListIterator;3233@SuppressWarnings({"serial","unchecked"})34public class Bug6533203 {35void test(String[] args) throws Throwable {36final List<Integer> superstitious = new ArrayList<>() {37public void add(int index, Integer i) {38if (i == 13) throw new Error("unlucky");39else super.add(index, i); }};40final ListIterator it = superstitious.listIterator(0);41equal(it.nextIndex(), 0);42THROWS(Error.class, new F(){void f(){it.add(13);}});43equal(it.nextIndex(), 0);44}4546//--------------------- Infrastructure ---------------------------47volatile int passed = 0, failed = 0;48void pass() {passed++;}49void fail() {failed++; Thread.dumpStack();}50void fail(String msg) {System.out.println(msg); fail();}51void unexpected(Throwable t) {failed++; t.printStackTrace();}52void check(boolean cond) {if (cond) pass(); else fail();}53void equal(Object x, Object y) {54if (x == null ? y == null : x.equals(y)) pass();55else fail(x + " not equal to " + y);}56public static void main(String[] args) throws Throwable {57new Bug6533203().instanceMain(args);}58void instanceMain(String[] args) throws Throwable {59try {test(args);} catch (Throwable t) {unexpected(t);}60System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);61if (failed > 0) throw new AssertionError("Some tests failed");}62abstract class F {abstract void f() throws Throwable;}63void THROWS(Class<? extends Throwable> k, F... fs) {64for (F f : fs)65try {f.f(); fail("Expected " + k.getName() + " not thrown");}66catch (Throwable t) {67if (k.isAssignableFrom(t.getClass())) pass();68else unexpected(t);}}69}707172