Path: blob/master/test/jdk/java/util/List/NestedSubList.java
41149 views
/*1* Copyright (c) 2016, 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 807913626* @run testng NestedSubList27* @summary Accessing a nested sublist leads to StackOverflowError28*/2930import java.util.AbstractList;31import java.util.Arrays;32import java.util.ArrayList;33import java.util.Collections;34import java.util.LinkedList;35import java.util.List;36import java.util.Vector;3738import org.testng.annotations.Test;39import org.testng.annotations.DataProvider;40import static org.testng.Assert.fail;4142public class NestedSubList {4344static final int NEST_LIMIT = 65536;4546@Test(dataProvider="lists")47public void testAccessToSublists(List<Integer> list, boolean modifiable) {48Class<?> cls = list.getClass();49for (int i = 0; i < NEST_LIMIT; ++i) {50list = list.subList(0, 1);51}5253try {54list.get(0);55if (modifiable) {56list.remove(0);57list.add(0, 42);58}59} catch (StackOverflowError e) {60fail("failed for " + cls);61}62}6364@DataProvider65public static Object[][] lists() {66final boolean MODIFIABLE = true;67final boolean NON_MODIFIABLE = false;68List<Integer> c = Arrays.asList(42);6970return new Object[][] {71{c, NON_MODIFIABLE},72{new ArrayList<>(c), MODIFIABLE},73{new LinkedList<>(c), MODIFIABLE},74{new MyList(), NON_MODIFIABLE},75{new Vector<>(c), MODIFIABLE},76{Collections.singletonList(42), NON_MODIFIABLE},77{Collections.checkedList(c, Integer.class), NON_MODIFIABLE},78{Collections.checkedList(new ArrayList<>(c), Integer.class), MODIFIABLE},79{Collections.checkedList(new LinkedList<>(c), Integer.class), MODIFIABLE},80{Collections.checkedList(new Vector<>(c), Integer.class), MODIFIABLE},81{Collections.synchronizedList(c), NON_MODIFIABLE},82{Collections.synchronizedList(new ArrayList<>(c)), MODIFIABLE},83{Collections.synchronizedList(new LinkedList<>(c)), MODIFIABLE},84{Collections.synchronizedList(new Vector<>(c)), MODIFIABLE},85{Collections.unmodifiableList(c), NON_MODIFIABLE},86{Collections.unmodifiableList(new ArrayList<>(c)), NON_MODIFIABLE},87{Collections.unmodifiableList(new LinkedList<>(c)), NON_MODIFIABLE},88{Collections.unmodifiableList(new Vector<>(c)), NON_MODIFIABLE},89};90}9192static class MyList extends AbstractList<Integer> {93public Integer get(int index) { return 42; }94public int size() { return 1; }95}96}979899