1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8161558 27 * @summary ListIterator should not discard cause on exception 28 * @run testng CheckForIndexOutOfBoundsException 29 */ 30 31 import java.util.List; 32 import java.util.Collections; 33 import java.util.Iterator; 34 import java.util.NoSuchElementException; 35 36 import org.testng.annotations.Test; 37 38 import static org.testng.Assert.assertNotNull; 39 import static org.testng.Assert.assertTrue; 40 import static org.testng.Assert.fail; 41 42 @Test 43 public class CheckForIndexOutOfBoundsException { 44 45 List<String> list = Collections.nCopies(2, "x"); 46 47 @Test 48 public void checkNext() { 49 var iterator = list.listIterator(list.size());; // position at end 50 try { 51 iterator.next(); 52 fail("Failing checkNext Test - NoSuchElementException should have been thrown"); 53 } catch (NoSuchElementException e) { 54 var cause = e.getCause(); 55 assertNotNull(cause, "CheckNext Exception.getCause()"); 56 assertTrue(cause instanceof IndexOutOfBoundsException, "CheckNext Exception.getCause() should be an IndexOutOfBoundsException"); 57 var msg = e.getMessage(); 58 assertNotNull(msg, "CheckNext Exception.getMessage()"); 59 assertTrue(msg.contains("IndexOutOfBoundsException"), "CheckNext Exception.getMessage() should contain the string 'IndexOutOfBoundsException'"); 60 } 61 } 62 63 @Test 64 public void checkPrevious() { 65 var iterator = list.listIterator(0); // position at start 66 try { 67 iterator.previous(); 68 fail("Failing checkPrevious Test - NoSuchElementException should have been thrown"); 69 } catch (NoSuchElementException e) { 70 var cause = e.getCause(); 71 assertNotNull(cause, "CheckPrevious Exception.getCause()"); 72 assertTrue(cause instanceof IndexOutOfBoundsException, "CheckPrevious Exception.getCause() should be an IndexOutOfBoundsException"); 73 var msg = e.getMessage(); 74 assertNotNull(msg, "CheckPrevious Exception.getMessage()"); 75 assertTrue(msg.contains("IndexOutOfBoundsException"), "CheckPrevious Exception.getMessage() should contain the string 'IndexOutOfBoundsException'"); 76 } 77 } 78 }