1 /*
2  * Copyright (c) 2003, 2016, 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     4712607 6479237
27  * @summary Basic test for StackTraceElementPublic constructor
28  * @author  Josh Bloch
29  */
30 
31 import java.lang.module.ModuleDescriptor;
32 
33 public class PublicConstructor {
main(String... args)34     public static void main(String... args) {
35         testConstructor();
36         testConstructorWithModule();
37     }
38 
testConstructor()39     static void testConstructor() {
40         StackTraceElement ste = new StackTraceElement("com.acme.Widget",
41                                                       "frobnicate",
42                                                       "Widget.java", 42);
43         if (!(ste.getClassName().equals("com.acme.Widget")  &&
44                 ste.getFileName().equals("Widget.java") &&
45                 ste.getMethodName().equals("frobnicate") &&
46                 ste.getLineNumber() == 42))
47             throw new RuntimeException("1");
48 
49         if (ste.isNativeMethod())
50             throw new RuntimeException("2");
51 
52         assertEquals(ste.toString(),
53                      "com.acme.Widget.frobnicate(Widget.java:42)");
54 
55         StackTraceElement ste1 = new StackTraceElement("com.acme.Widget",
56                                                        "frobnicate",
57                                                        "Widget.java",
58                                                        -2);
59         if (!ste1.isNativeMethod())
60             throw new RuntimeException("3");
61 
62         assertEquals(ste1.toString(),
63                      "com.acme.Widget.frobnicate(Native Method)");
64     }
65 
testConstructorWithModule()66     static void testConstructorWithModule() {
67         StackTraceElement ste = new StackTraceElement("app",
68                                                       "jdk.module",
69                                                       "9.0",
70                                                       "com.acme.Widget",
71                                                       "frobnicate",
72                                                       "Widget.java",
73                                                       42);
74         if (!(ste.getClassName().equals("com.acme.Widget")  &&
75                 ste.getModuleName().equals("jdk.module") &&
76                 ste.getModuleVersion().equals("9.0") &&
77                 ste.getClassLoaderName().equals("app") &&
78                 ste.getFileName().equals("Widget.java") &&
79                 ste.getMethodName().equals("frobnicate") &&
80                 ste.getLineNumber() == 42))
81             throw new RuntimeException("3");
82 
83         if (ste.isNativeMethod())
84             throw new RuntimeException("4");
85 
86         assertEquals(ste.toString(),
87                      "app/jdk.module@9.0/com.acme.Widget.frobnicate(Widget.java:42)");
88     }
89 
assertEquals(String s, String expected)90     static void assertEquals(String s, String expected) {
91         if (!s.equals(expected)) {
92             throw new RuntimeException("Expected: " + expected + " but found: " + s);
93         }
94     }
95 }
96