1 /*
2  * Copyright (c) 2007, 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  */
23 
24 /*
25  * @test
26  *
27  * @summary converted from VM Testbase nsk/jdi/ThreadReference/forceEarlyReturn/forceEarlyReturn002.
28  * VM Testbase keywords: [quick, jpda, jdi, feature_jdk6_jpda, vm6]
29  * VM Testbase readme:
30  * DESCRIPTION
31  *         The test checks that a result of the method com.sun.jdi.forceEarlyReturn(Value value)
32  *         complies with its specification. The test checks:
33  *                 - attempt to call forceEarlyReturn for the type which has not yet been loaded throws ClassNotLoadedException
34  *                 - MethodExitEvent is generated as it would be in a normal return
35  *         Test scenario:
36  *         Debugger VM enable breakpoint in test method which return type is 'nsk.share.jdi.TestClass1',
37  *         force debugee call this method and when debugee VM stop at breakpoint, call forceEarlyReturn().
38  *         ClassNotLoadedException should be thrown (expect that nsk.share.jdi.TestClass1 isn't loaded in debuggee VM).
39  *         Debugger VM force debuggee VM create instance of 'nsk.share.jdi.TestClass1' and call test method again.
40  *         When debugee VM stop at breakpoint, call forceEarlyReturn() and check that no exception is thrown.
41  *         Debugee checks that correct value is returned from test method after force return.
42  *         Debugger checks that MethodExitEvent is generated after forceEarlyReturn.
43  *
44  * @library /vmTestbase
45  *          /test/lib
46  * @run driver jdk.test.lib.FileInstaller . .
47  * @build nsk.jdi.ThreadReference.forceEarlyReturn.forceEarlyReturn002.forceEarlyReturn002
48  *        nsk.jdi.ThreadReference.forceEarlyReturn.forceEarlyReturn002.forceEarlyReturn002a
49  * @run main/othervm PropertyResolvingWrapper
50  *      nsk.jdi.ThreadReference.forceEarlyReturn.forceEarlyReturn002.forceEarlyReturn002
51  *      -verbose
52  *      -arch=${os.family}-${os.simpleArch}
53  *      -waittime=5
54  *      -debugee.vmkind=java
55  *      -transport.address=dynamic
56  *      "-debugee.vmkeys=${test.vm.opts} ${test.java.opts}"
57  */
58 
59 package nsk.jdi.ThreadReference.forceEarlyReturn.forceEarlyReturn002;
60 
61 import java.io.PrintStream;
62 import com.sun.jdi.*;
63 import com.sun.jdi.request.*;
64 import com.sun.jdi.event.*;
65 import nsk.share.Consts;
66 import nsk.share.jdi.ForceEarlyReturnDebugger;
67 
68 public class forceEarlyReturn002 extends ForceEarlyReturnDebugger {
main(String argv[])69     public static void main(String argv[]) {
70         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
71     }
72 
debuggeeClassName()73     public String debuggeeClassName() {
74         return forceEarlyReturn002a.class.getName();
75     }
76 
run(String argv[], PrintStream out)77     public static int run(String argv[], PrintStream out) {
78         return new forceEarlyReturn002().runIt(argv, out);
79     }
80 
doTest()81     public void doTest() {
82         // initialize breakpoint
83 
84         ReferenceType referenceType = debuggee.classByName(ClassUsingTestClass.class.getName());
85 
86         BreakpointRequest breakpointRequest = debuggee.makeBreakpoint(referenceType,
87                 ClassUsingTestClass.breakpointMethodName,
88                 ClassUsingTestClass.breakpointLine);
89 
90         breakpointRequest.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
91         breakpointRequest.enable();
92 
93         pipe.println(forceEarlyReturn002a.COMMAND_CALL_OBJECT_METHOD);
94 
95         BreakpointEvent breakPointEvent = waitForBreakpoint(breakpointRequest);
96 
97         // if no breakpoint happened then test failed, stop testing
98         if (breakPointEvent == null)
99             return;
100 
101         /*
102          * Test can't guarantee that TestClass1 isn't loaded in the debuggee VM (it isn't loaded
103          * if VM implements lazy loading). Here test checks that TestClass1 isn't loaded and
104          * if class is really absent in the debuggee VM it is possible to check ClassNotLoadedException.
105          */
106         boolean testClassIsLoaded = false;
107 
108         ClassLoaderReference classLoader = debuggee.classByName(forceEarlyReturn002a.class.getName()).classLoader();
109         for (ReferenceType loadedClass : classLoader.visibleClasses()) {
110             if (loadedClass.name().equals("nsk.share.jdi.TestClass1")) {
111                 log.display("WARNING: TestClass1 is loaded in the debuggee VM, can't test ClassNotLoadedException");
112                 testClassIsLoaded = true;
113                 break;
114             }
115         }
116 
117         ThreadReference threadReference = debuggee.threadByName(forceEarlyReturn002a.mainThreadName);
118 
119         try {
120             if (testClassIsLoaded) {
121                 threadReference.forceEarlyReturn(null);
122             } else {
123                 try {
124                     threadReference.forceEarlyReturn(threadReference);
125                     setSuccess(false);
126                     log.complain("Expected 'ClassNotLoadedException' was not thrown");
127                 } catch (ClassNotLoadedException e) {
128                     log.display("Got expected ClassNotLoadedException");
129                 }
130             }
131         } catch (Exception e) {
132             unexpectedException(e);
133         }
134 
135         debuggee.resume();
136 
137         if (!isDebuggeeReady())
138             return;
139 
140         // after this command test class should be loaded
141         pipe.println(forceEarlyReturn002a.COMMAND_LOAD_CLASS_AND_CALL_OBJECT_METHOD);
142 
143         breakPointEvent = waitForBreakpoint(breakpointRequest);
144 
145         // if no breakpoint happened then test failed, stop testing
146         if (breakPointEvent == null)
147             return;
148 
149         // get value for early return
150         ObjectReference returnValue = (ObjectReference) referenceType.getValue(referenceType.fieldByName("expectedValue"));
151 
152         try {
153             // don't expect any exception
154             threadReference.forceEarlyReturn(returnValue);
155         } catch (Exception e) {
156             setSuccess(false);
157             log.complain("Unexpected exception: " + e);
158             e.printStackTrace(log.getOutStream());
159         }
160 
161         testMethodExitEvent(threadReference, ClassUsingTestClass.breakpointMethodName);
162 
163         if (!isDebuggeeReady())
164             return;
165     }
166 }
167