1 /*
2 * Copyright (c) 2015, 2017, 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 *
79
80 // Try to load the specified class with the default ClassLoader.
81 Class<?> clazz = null;
82 try {
83 clazz = Class.forName(className);
84 } catch (ClassNotFoundException e) {
85 System.out.println(e);
86 }
87
88 if (clazz != null) {
89 // class loaded
90 if (shouldLoad) {
91 // Make sure we got the expected defining ClassLoader
92 ClassLoader actualLoader = clazz.getClassLoader();
93 if (actualLoader != expectedLoader) {
94 throw new RuntimeException(testName + " FAILED: " + clazz + " loaded by " + actualLoader +
95 ", expected " + expectedLoader);
96 }
97 // Make sure we got the right version of the class. toString() of an instance
98 // of the overridden version of the class should return "hi".
99 String s = clazz.newInstance().toString();
100 if (!s.equals("hi")) {
101 throw new RuntimeException(testName + " FAILED: toString() returned \"" + s
102 + "\" instead of \"hi\"" );
103 }
104 System.out.println(testName + " PASSED: class loaded as expected.");
105 } else {
106 throw new RuntimeException(testName + " FAILED: class loaded, but should have failed to load.");
107 }
108 } else {
109 // class did not load
110 if (shouldLoad) {
111 throw new RuntimeException(testName + " FAILED: class failed to load.");
112 } else {
113 System.out.println(testName + " PASSED: ClassNotFoundException thrown as expected");
114 }
115 }
116 }
117
118 static void assertTrue(boolean expr) {
119 if (!expr)
120 throw new RuntimeException("assertion failed");
121 }
122 }
|
1 /*
2 * Copyright (c) 2015, 2018, 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 *
79
80 // Try to load the specified class with the default ClassLoader.
81 Class<?> clazz = null;
82 try {
83 clazz = Class.forName(className);
84 } catch (ClassNotFoundException e) {
85 System.out.println(e);
86 }
87
88 if (clazz != null) {
89 // class loaded
90 if (shouldLoad) {
91 // Make sure we got the expected defining ClassLoader
92 ClassLoader actualLoader = clazz.getClassLoader();
93 if (actualLoader != expectedLoader) {
94 throw new RuntimeException(testName + " FAILED: " + clazz + " loaded by " + actualLoader +
95 ", expected " + expectedLoader);
96 }
97 // Make sure we got the right version of the class. toString() of an instance
98 // of the overridden version of the class should return "hi".
99 if (actualLoader == SYS_LOADER) {
100 String s = clazz.newInstance().toString();
101 if (!s.equals("hi")) {
102 throw new RuntimeException(testName + " FAILED: toString() returned \"" + s
103 + "\" instead of \"hi\"" );
104 }
105 }
106 System.out.println(testName + " PASSED: class loaded as expected.");
107 } else {
108 throw new RuntimeException(testName + " FAILED: class loaded, but should have failed to load.");
109 }
110 } else {
111 // class did not load
112 if (shouldLoad) {
113 throw new RuntimeException(testName + " FAILED: class failed to load.");
114 } else {
115 System.out.println(testName + " PASSED: ClassNotFoundException thrown as expected");
116 }
117 }
118 }
119
120 static void assertTrue(boolean expr) {
121 if (!expr)
122 throw new RuntimeException("assertion failed");
123 }
124 }
|