1 /*
2  * Copyright (c) 2002, 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 package nsk.jdi.ClassType.invokeMethod;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 import com.sun.jdi.*;
31 import com.sun.jdi.request.*;
32 import com.sun.jdi.event.*;
33 
34 import java.util.*;
35 import java.io.*;
36 
37 /**
38  * Test checks up the following assertions:                           <br>
39  *     1. The specified method can be defined in this class, or in
40  *     a superclass.                                                  <br>
41  *     2. <code>IllegalArgumentException</code> is thrown if          <br>
42  *       - the method is not a member of this class or a superclass;  <br>
43  *       - the size of the argument list does not match the number of declared
44  *         arguemnts for the method.                                  <br>
45  * The first case considers the invokations for the <code>private</code>,
46  * <code>protected</code> and <code>public</code> methods. In this case
47  * no exceptions are expected.<br>
48  */
49 
50 public class invokemethod003 {
51 
52     private final static String prefix = "nsk.jdi.ClassType.invokeMethod.";
53     private final static String debuggerName = prefix + "invokemethod003";
54     private final static String debugeeName = debuggerName + "a";
55 
56     public final static String SGNL_READY = "ready";
57     public final static String SGNL_QUIT = "quit";
58 
59     private static int exitStatus;
60     private static Log log;
61     private static Debugee debugee;
62     private static long waitTime;
63 
64     private ClassType testedClass;
65     private ReferenceType anotherClass;
66     private ThreadReference thread;
67 
68     class TestRuntimeException extends RuntimeException {
TestRuntimeException(String msg)69         TestRuntimeException(String msg) {
70             super("TestRuntimeException: " + msg);
71         }
72     }
73 
74     public final static String [] methods2Invoke = {
75                 "publicFromParent",
76                 "protectFromParent",
77                 "privateFromParent",
78                 "fromChild"
79     };
80 
display(String msg)81     private static void display(String msg) {
82         log.display(msg);
83     }
84 
complain(String msg)85     private static void complain(String msg) {
86         log.complain("debugger FAILURE> " + msg + "\n");
87     }
88 
main(String argv[])89     public static void main(String argv[]) {
90         System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));
91     }
92 
run(String argv[], PrintStream out)93     public static int run(String argv[], PrintStream out) {
94 
95         exitStatus = Consts.TEST_PASSED;
96 
97         invokemethod003 thisTest = new invokemethod003();
98 
99         ArgumentHandler argHandler = new ArgumentHandler(argv);
100         log = new Log(out, argHandler);
101 
102         waitTime = argHandler.getWaitTime() * 60000;
103 
104         Binder binder = new Binder(argHandler, log);
105         debugee = binder.bindToDebugee(debugeeName);
106 
107         thisTest.execTest();
108         display("Test finished. exitStatus = " + exitStatus);
109 
110         return exitStatus;
111     }
112 
execTest()113     private void execTest() {
114 
115         try {
116             prepareTestCase();
117         } catch(InterruptedException e) {
118             complain("InterruptedException occurs");
119             exitStatus = Consts.TEST_FAILED;
120             return;
121         } catch(TestRuntimeException e) {
122             complain(" " + e);
123             for (int i = 0; i < e.getStackTrace().length; i++) {
124                 display("\t" + e.getStackTrace()[i]);
125             }
126             if ( debugee.getIOPipe() == null ) {
127                 debugee.createIOPipe();
128             }
129             debugee.receiveExpectedSignal(SGNL_READY);
130             debugee.quit();
131             exitStatus = Consts.TEST_FAILED;
132             return;
133         }
134 
135         display("\nTEST BEGINS");
136         display("===========");
137 
138         Value retValue, value = null,
139               expectedValue = debugee.VM().mirrorOf(6L);
140         List<com.sun.jdi.Value> params = createParams(3),
141              params4 = createParams(4);
142 
143         Method method;
144 
145         // checking up IllegalArgumentException, when the method is not a member
146         // of this class or a superclass
147         display("\nthe method is not a member of this class or a superclass");
148         display("--------------------------------------------------------");
149         method = debugee.methodByName(anotherClass, "run");
150         try {
151             retValue = invokeMethod(thread, method, params, expectedValue);
152             complain("***IllegalArgumentException is not thrown***");
153             exitStatus = Consts.TEST_FAILED;
154         } catch(IllegalArgumentException e) {
155             display(">expected " + e);
156         } catch(Exception e) {
157             complain(" unexpected " + e);
158             exitStatus = Consts.TEST_FAILED;
159         }
160         display("");
161 
162         // checking up for public, protected, private methods of the superclass
163         display("\npublic, protected, private methods of the superclass");
164         display("----------------------------------------------------");
165         for (int j = 0; j < methods2Invoke.length; j++) {
166             method = debugee.methodByName(testedClass, methods2Invoke[j]);
167             try {
168                 retValue = invokeMethod(thread, method, params, expectedValue);
169             } catch(Exception e) {
170                 complain("***unexpected " + e + "***");
171                 exitStatus = Consts.TEST_FAILED;
172             }
173         }
174         display("");
175 
176         // checking up IllegalArgumentException, wrong size of the argument list
177         display("wrong size of the argument list: " + params4.size()
178                         + "(it should be 3)");
179         display("--------------------------------------------------");
180         method = debugee.methodByName(testedClass, methods2Invoke[0]);
181         try {
182             retValue = invokeMethod(thread, method, params4, expectedValue);
183             complain("***IllegalArgumentException is not thrown***");
184             exitStatus = Consts.TEST_FAILED;
185         } catch(IllegalArgumentException e) {
186             display(">expected " + e);
187         } catch(Exception e) {
188             complain(" unexpected " + e);
189             exitStatus = Consts.TEST_FAILED;
190         }
191         display("");
192 
193         display("=============");
194         display("TEST FINISHES\n");
195 
196         debugee.resume();
197         debugee.quit();
198     }
199 
prepareTestCase()200     private void prepareTestCase() throws InterruptedException {
201         Event event = null;
202 
203         ClassPrepareRequest cprep
204                     = debugee.getEventRequestManager().createClassPrepareRequest();
205         cprep.addClassFilter(prefix + invokemethod003a.class2Check);
206         cprep.enable();
207 
208         debugee.resume();
209 
210         // waiting ClassPrepareEvent for debugeeName
211         event = debugee.waitingEvent(cprep, waitTime);
212         if (!(event instanceof ClassPrepareEvent)) {
213             debugee.resume();
214             throw new TestRuntimeException("ClassPrepareEvent didn't arrive");
215         }
216 
217 
218         testedClass = (ClassType )debugee.classByName(prefix
219                                                     + invokemethod003a.class2Check);
220 
221         ReferenceType debugeeClass = debugee.classByName(debugeeName);
222         BreakpointRequest brkp = debugee.setBreakpoint(debugeeClass,
223                                                     invokemethod003a.brkpMethodName,
224                                                     invokemethod003a.brkpLineNumber);
225 
226         debugee.resume();
227 
228         debugee.createIOPipe();
229         debugee.redirectStdout(log,"");
230         debugee.redirectStderr(log,"");
231         debugee.receiveExpectedSignal(SGNL_READY);
232 
233         anotherClass = debugee.classByName(prefix
234                                                 + invokemethod003a.anotherClassName);
235         if (anotherClass == null) {
236             debugee.resume();
237             throw new TestRuntimeException(prefix
238                             + invokemethod003a.anotherClassName
239                             + " has not been loaded yet");
240         }
241 
242 
243         // waiting the breakpoint event
244         event = debugee.waitingEvent(brkp, waitTime);
245         if (!(event instanceof BreakpointEvent )) {
246             debugee.resume();
247             throw new TestRuntimeException("BreakpointEvent didn't arrive");
248         }
249 
250         BreakpointEvent brkpEvent = (BreakpointEvent )event;
251         if (brkpEvent == null) {
252             debugee.resume();
253             throw new TestRuntimeException("No breakpoint events");
254         }
255 
256         thread = brkpEvent.thread();
257     }
258 
invokeMethod(ThreadReference thread, Method method, List<Value> params, Value expectedValue)259     private Value invokeMethod(ThreadReference thread, Method method, List<Value> params,
260                                     Value expectedValue) {
261         Value returnedValue = null,
262               param;
263         try {
264             display("Method      : " + method);
265             for (int i = 0; i < params.size(); i++) {
266                 param = params.get(i);
267                 display("Parameters  : " + param + "(" + param.type() + ")");
268             }
269             returnedValue = testedClass.invokeMethod(thread, method, params,
270                                                     ClassType.INVOKE_SINGLE_THREADED);
271         } catch(InvalidTypeException e) {
272             complain("exception: " + e);
273             exitStatus = Consts.TEST_FAILED;
274         } catch(ClassNotLoadedException e) {
275             complain("exception: " + e);
276             exitStatus = Consts.TEST_FAILED;
277         } catch(IncompatibleThreadStateException e) {
278             complain("exception: " + e);
279             exitStatus = Consts.TEST_FAILED;
280         } catch(InvocationException e) {
281             complain("exception: " + e);
282             exitStatus = Consts.TEST_FAILED;
283         }
284 
285         String retType = returnedValue != null ? returnedValue.type().toString()
286                                                   : "";
287         display("Return value: " + returnedValue + "(" + retType + ")");
288 
289         if (!returnedValue.equals(expectedValue)) {
290             String expType = expectedValue.type().toString() ;
291             complain("***wrong the return value***");
292             complain("expected value        : " + expectedValue + "("
293                                             + expType + ")");
294             exitStatus = Consts.TEST_FAILED;
295         }
296         display("");
297         return returnedValue;
298     }
299 
createParams(int size)300     private List<com.sun.jdi.Value> createParams(int size) {
301         Vector<com.sun.jdi.Value> params = new Vector<com.sun.jdi.Value>();
302         for (int i = 0; i < size; i++) {
303             params.add(debugee.VM().mirrorOf(i + 1));
304         }
305         return params;
306     }
307 }
308