blob: d1eaaa8e2cfbc6793da22649603978adc421d980 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 *
26 *
27 * Basic unit test for Instrumentation appendToBootstrapClassLoaderSearch and
28 * appendToSystemClassLoaderSearch methods:
29 *
30 * 1. Attempt to load a class that does not exist.
31 *
32 * 2. Add a jar file to the class path with the missing class.
33 *
34 * 3. Attempt to load the class again - CNF should be thrown as the JVMS requires that
35 * the error from the first resolution attempt should be thrown.
36 */
37import java.lang.instrument.Instrumentation;
38import java.util.jar.JarFile;
39import java.io.IOException;
40
41public class PrematureLoadTest {
42
43 static int failures = 0;
44
45 public static void main(String args[]) throws IOException {
46
47 try {
48 new BootSupport();
49 throw new RuntimeException("Test configuration error - BootSupport loaded unexpectedly!");
50 } catch (NoClassDefFoundError x) {
51 }
52
53 try {
54 new AgentSupport();
55 throw new RuntimeException("Test configuration error - AgentSupport loaded unexpectedly!");
56 } catch (NoClassDefFoundError x) {
57 }
58
59
60 JarFile bootclasses = new JarFile("BootSupport.jar");
61 JarFile agentclasses = new JarFile("AgentSupport.jar");
62
63 Instrumentation ins = Agent.getInstrumentation();
64
65 ins.appendToBootstrapClassLoaderSearch(bootclasses);
66 try {
67 new BootSupport();
68 System.out.println("FAIL: BootSupport resolved");
69 failures++;
70 } catch (NoClassDefFoundError x) {
71 System.out.println("PASS: BootSupport failed to resolve");
72 }
73
74 try {
75 ins.appendToSystemClassLoaderSearch(agentclasses);
76 try {
77 new AgentSupport();
78 System.out.println("FAIL: AgentSupport resolved");
79 failures++;
80 } catch (NoClassDefFoundError x) {
81 System.out.println("PASS: AgentSupport failed to resolve");
82 }
83 } catch (UnsupportedOperationException x) {
84 System.out.println("System class loader does not support adding to class path");
85 }
86
87 if (failures > 0) {
88 throw new RuntimeException(failures + " test(s) failed.");
89 }
90 }
91}