1 /* 2 * Copyright (c) 2020, 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.AbstractList; 33 import java.util.Iterator; 34 import java.util.ListIterator; 35 import java.util.NoSuchElementException; 36 37 import org.testng.annotations.Test; 38 39 import static org.testng.Assert.assertNotNull; 40 import static org.testng.Assert.assertTrue; 41 import static org.testng.Assert.fail; 42 43 // Fixed size list containing two elements 44 45 class MyList extends AbstractList<String> { 46 47 private static final int SIZE = 2; 48 49 public String get(int i) { 50 if (i >= 0 && i < SIZE) { 51 return "x"; 52 } else { 53 throw new IndexOutOfBoundsException(); 54 } 55 } 56 57 public int size() { 58 return SIZE; 59 } 60 } 61 62 @Test 63 public class CheckForIndexOutOfBoundsException { 64 65 List<String> list = new MyList(); 66 67 68 @Test 69 public void checkIteratorNext() { 70 var iterator = list.iterator(); // position at start 71 try { 72 for (int i = 0; i <= list.size(); i++) { 73 iterator.next(); 74 } 75 fail("Failing checkIteratorNext() - NoSuchElementException should have been thrown"); 76 } catch (NoSuchElementException e) { 77 checkAssertOnException(e); 78 } 79 } 80 81 @Test 82 public void checkListIteratorNext() { 83 var iterator = list.listIterator(list.size()); // position at end 84 try { 85 iterator.next(); 86 fail("Failing checkListIteratorNext() - NoSuchElementException should have been thrown"); 87 } catch (NoSuchElementException e) { 88 checkAssertOnException(e); 89 } 90 } 91 92 @Test 93 public void checkListIteratorPrevious() { 94 var iterator = list.listIterator(0); // position at start 95 try { 96 iterator.previous(); 97 fail("Failing checkListIteratorPrevious() - NoSuchElementException should have been thrown"); 98 } catch (NoSuchElementException e) { 99 checkAssertOnException(e); 100 } 101 } 102 103 private void checkAssertOnException(NoSuchElementException e) { 104 var cause = e.getCause(); 105 assertNotNull(cause, "Exception.getCause()"); 106 assertTrue(cause instanceof IndexOutOfBoundsException, "Exception.getCause() should be an " + 107 "IndexOutOfBoundsException"); 108 var msg = e.getMessage(); 109 assertNotNull(msg, "Exception.getMessage()"); 110 assertTrue(msg.contains("IndexOutOfBoundsException"), "Exception.getMessage() should " + 111 "contain the string 'IndexOutOfBoundsException'"); 112 } 113 } 114