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