Path: blob/master/test/jdk/java/util/AbstractList/CheckForIndexOutOfBoundsException.java
41152 views
/*1* Copyright (c) 2020, 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 816155826* @summary ListIterator should not discard cause on exception27* @run testng CheckForIndexOutOfBoundsException28*/2930import java.util.List;31import java.util.AbstractList;32import java.util.Iterator;33import java.util.ListIterator;34import java.util.NoSuchElementException;3536import org.testng.annotations.Test;3738import static org.testng.Assert.assertNotNull;39import static org.testng.Assert.assertTrue;40import static org.testng.Assert.fail;4142// Fixed size list containing two elements4344class MyList extends AbstractList<String> {4546private static final int SIZE = 2;4748public String get(int i) {49if (i >= 0 && i < SIZE) {50return "x";51} else {52throw new IndexOutOfBoundsException(i);53}54}5556public int size() {57return SIZE;58}59}6061@Test62public class CheckForIndexOutOfBoundsException {6364List<String> list = new MyList();656667@Test68public void checkIteratorNext() {69var iterator = list.iterator(); // position at start70try {71for (int i = 0; i <= list.size(); i++) {72iterator.next();73}74fail("Failing checkIteratorNext() - NoSuchElementException should have been thrown");75} catch (NoSuchElementException e) {76checkAssertOnException(e);77}78}7980@Test81public void checkListIteratorNext() {82var iterator = list.listIterator(list.size()); // position at end83try {84iterator.next();85fail("Failing checkListIteratorNext() - NoSuchElementException should have been thrown");86} catch (NoSuchElementException e) {87checkAssertOnException(e);88}89}9091@Test92public void checkListIteratorPrevious() {93var iterator = list.listIterator(0); // position at start94try {95iterator.previous();96fail("Failing checkListIteratorPrevious() - NoSuchElementException should have been thrown");97} catch (NoSuchElementException e) {98checkAssertOnException(e);99}100}101102private void checkAssertOnException(NoSuchElementException e) {103var cause = e.getCause();104assertNotNull(cause, "Exception.getCause()");105assertTrue(cause instanceof IndexOutOfBoundsException, "Exception.getCause() should be an " +106"IndexOutOfBoundsException");107var msg = e.getMessage();108assertNotNull(msg, "Exception.getMessage()");109assertTrue(msg.contains("IndexOutOfBoundsException"), "Exception.getMessage() should " +110"contain the string 'IndexOutOfBoundsException'");111}112}113114115116