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.VoidValue.toString;
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 import com.sun.jdi.connect.*;
34 import java.io.*;
35 import java.util.*;
36 
37 /**
38  * The debugger application of the test.
39  */
40 public class tostring001 {
41 
42     //------------------------------------------------------- immutable common fields
43 
44     final static String SIGNAL_READY = "ready";
45     final static String SIGNAL_GO    = "go";
46     final static String SIGNAL_QUIT  = "quit";
47 
48     private static int waitTime;
49     private static int exitStatus;
50     private static ArgumentHandler     argHandler;
51     private static Log                 log;
52     private static Debugee             debuggee;
53     private static ReferenceType       debuggeeClass;
54 
55     //------------------------------------------------------- mutable common fields
56 
57     private final static String prefix = "nsk.jdi.VoidValue.toString.";
58     private final static String className = "tostring001";
59     private final static String debuggerName = prefix + className;
60     private final static String debuggeeName = debuggerName + "a";
61 
62     //------------------------------------------------------- test specific fields
63 
64     /** debuggee's methods for check **/
65     private final static String checkedMethods[] = {
66         "Mv" ,  "MvS",  "MvI",  "MvY",  "MvU",
67         "MvR",  "MvP",  "MvSM", "MvIM", "MvYM", "MvPM"
68                                                    };
69 
70     //------------------------------------------------------- immutable common methods
71 
main(String argv[])72     public static void main(String argv[]) {
73         System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));
74     }
75 
display(String msg)76     private static void display(String msg) {
77         log.display("debugger > " + msg);
78     }
79 
complain(String msg)80     private static void complain(String msg) {
81         log.complain("debugger FAILURE > " + msg);
82     }
83 
run(String argv[], PrintStream out)84     public static int run(String argv[], PrintStream out) {
85 
86         exitStatus = Consts.TEST_PASSED;
87 
88         argHandler = new ArgumentHandler(argv);
89         log = new Log(out, argHandler);
90         waitTime = argHandler.getWaitTime() * 60000;
91 
92         debuggee = Debugee.prepareDebugee(argHandler, log, debuggeeName);
93 
94         debuggeeClass = debuggee.classByName(debuggeeName);
95         if ( debuggeeClass == null ) {
96             complain("Class '" + debuggeeName + "' not found.");
97             exitStatus = Consts.TEST_FAILED;
98         }
99 
100         execTest();
101 
102         debuggee.quit();
103 
104         return exitStatus;
105     }
106 
107     //------------------------------------------------------ mutable common method
108 
execTest()109     private static void execTest() {
110 
111         BreakpointRequest brkp = debuggee.setBreakpoint(debuggeeClass,
112                                                     tostring001a.brkpMethodName,
113                                                     tostring001a.brkpLineNumber);
114         debuggee.resume();
115 
116         debuggee.sendSignal(SIGNAL_GO);
117         Event event = null;
118 
119         // waiting the breakpoint event
120         try {
121             event = debuggee.waitingEvent(brkp, waitTime);
122         } catch (InterruptedException e) {
123             throw new Failure("unexpected InterruptedException while waiting for Breakpoint event");
124         }
125         if (!(event instanceof BreakpointEvent)) {
126             debuggee.resume();
127             throw new Failure("BreakpointEvent didn't arrive");
128         }
129 
130         ThreadReference thread = ((BreakpointEvent)event).thread();
131         List<Value> params = new Vector<Value>();
132         ClassType testedClass = (ClassType)debuggeeClass;
133         ObjectReference testedObject = null;
134 
135         // Finding of debuggee's class constructor
136         Method ctor = debuggee.methodByName(debuggeeClass, "<init>");
137 
138         try {
139             testedObject = testedClass.newInstance(thread, ctor, params, 0);
140         } catch (Exception e) {
141             throw new Failure("unexpected " + e + " when invoking debuggee's constructor");
142         }
143 
144         display("Checking toString() method for debuggee's void value methods...");
145 
146         // Check all methods from debuggee
147         for (int i = 0; i < checkedMethods.length-1; i++) {
148 
149             VoidValue voidValue = null;
150             Method method;
151 
152             method = debuggee.methodByName(debuggeeClass, checkedMethods[i]);
153             try {
154                 if (method.isStatic()) {
155                     voidValue = (VoidValue) testedClass.invokeMethod(thread, method, params, 0);
156                 } else {
157                     voidValue = (VoidValue) testedObject.invokeMethod(thread, method, params, 0);
158                 }
159 
160                 String str = voidValue.toString();
161                 if (str == null) {
162                     complain("toString() returns null for VoidValue of debugges's method: " + checkedMethods[i]);
163                     exitStatus = Consts.TEST_FAILED;
164                 } else if (str.length() == 0) {
165                     complain("toString() returns empty string for VoidValue of debugges's method: " + checkedMethods[i]);
166                     exitStatus = Consts.TEST_FAILED;
167                 } else {
168                     display("toString() returns for VoidValue of debugges's method " + checkedMethods[i] + " : " + str);
169                 }
170             } catch(Exception e) {
171                 complain("Unexpected " + e + " when invoking debuggee's method: " + checkedMethods[i]);
172                 exitStatus = Consts.TEST_FAILED;
173             }
174 
175         }
176 
177         display("Checking of debuggee's void value methods completed!");
178         debuggee.resume();
179     }
180 
181     //--------------------------------------------------------- test specific methods
182 }
183 //--------------------------------------------------------- test specific classes
184