1 /*
2  * Copyright (c) 2015, 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 8140450
27  * @summary Sanity test for exception cases
28  * @run testng SanityTest
29  */
30 
31 package test.java.lang.StackWalker;
32 
33 
34 import java.util.Collections;
35 import java.util.Set;
36 
37 import org.testng.annotations.Test;
38 
39 public class SanityTest {
40     @Test
testNPE()41     public static void testNPE() {
42         try {
43             StackWalker sw = StackWalker.getInstance((Set<StackWalker.Option>) null);
44             throw new RuntimeException("NPE expected");
45         } catch (NullPointerException e) {}
46 
47         try {
48             StackWalker sw = StackWalker.getInstance((StackWalker.Option) null);
49             throw new RuntimeException("NPE expected");
50         } catch (NullPointerException e) {}
51     }
52 
53     @Test
testUOE()54     public static void testUOE() {
55         try {
56             StackWalker.getInstance().getCallerClass();
57             throw new RuntimeException("UOE expected");
58         } catch (UnsupportedOperationException expected) {}
59     }
60 
61     @Test
testInvalidEstimateDepth()62     public static void testInvalidEstimateDepth() {
63         try {
64             StackWalker sw = StackWalker.getInstance(Collections.emptySet(), 0);
65             throw new RuntimeException("Illegal estimateDepth should throw IAE");
66         } catch (IllegalArgumentException e) {}
67     }
68 
69     @Test
testNullFuncation()70     public static void testNullFuncation() {
71         try {
72             StackWalker.getInstance().walk(null);
73             throw new RuntimeException("NPE expected");
74         } catch (NullPointerException e) {}
75     }
76 
77     @Test
testNullConsumer()78     public static void testNullConsumer() {
79         try {
80             StackWalker.getInstance().forEach(null);
81             throw new RuntimeException("NPE expected");
82         } catch (NullPointerException e) {}
83     }
84 
85 
86     @Test
testUOEFromGetDeclaringClass()87     public static void testUOEFromGetDeclaringClass() {
88         try {
89             StackWalker sw = StackWalker.getInstance();
90             sw.forEach(StackWalker.StackFrame::getDeclaringClass);
91             throw new RuntimeException("UOE expected");
92         } catch (UnsupportedOperationException expected) {
93         }
94     }
95 
96     @Test
testUOEFromGetMethodType()97     public static void testUOEFromGetMethodType() {
98         try {
99             StackWalker sw = StackWalker.getInstance();
100             sw.forEach(StackWalker.StackFrame::getMethodType);
101             throw new RuntimeException("UOE expected");
102         } catch (UnsupportedOperationException expected) {}
103     }
104 }
105