1 /*
2  * Copyright (c) 2001, 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 package nsk.jdi.Method.variables;
24 
25 import nsk.share.*;
26 import nsk.share.jpda.*;
27 import nsk.share.jdi.*;
28 
29 import com.sun.jdi.*;
30 import java.util.*;
31 import java.io.*;
32 
33 /**
34  * The test for the implementation of an object of the type     <BR>
35  * Method.                                                      <BR>
36  *                                                              <BR>
37  * The test checks up that results of the method                <BR>
38  * <code>com.sun.jdi.Method.variables()</code>                  <BR>
39  * complies with its spec if a debugged program is compiled with<BR>
40  * "-g" option, hence, no exception AbsentInformationException  <BR>
41  * is expected.                                                 <BR>
42  * <BR>
43  * Cases for testing are as follows:                            <BR>
44  *   - non-native method with arguments and variables           <BR>
45  *   - non-native method without arguments and variables        <BR>
46  *   - native method                                            <BR>
47  */
48 
49 public class variables001 {
50 
51     //----------------------------------------------------- templete section
52     static final int PASSED = 0;
53     static final int FAILED = 2;
54     static final int PASS_BASE = 95;
55 
56     //----------------------------------------------------- templete parameters
57     static final String
58     sHeader1 = "\n==> nsk/jdi/Method/variables/variables001",
59     sHeader2 = "--> variables001: ",
60     sHeader3 = "##> variables001: ";
61 
62     //----------------------------------------------------- main method
63 
main(String argv[])64     public static void main (String argv[]) {
65         int result = run(argv, System.out);
66         System.exit(result + PASS_BASE);
67     }
68 
run(String argv[], PrintStream out)69     public static int run (String argv[], PrintStream out) {
70         return new variables001().runThis(argv, out);
71     }
72 
73      //--------------------------------------------------   log procedures
74 
75     //private static boolean verbMode = false;
76 
77     private static Log  logHandler;
78 
log1(String message)79     private static void log1(String message) {
80         logHandler.display(sHeader1 + message);
81     }
log2(String message)82     private static void log2(String message) {
83         logHandler.display(sHeader2 + message);
84     }
log3(String message)85     private static void log3(String message) {
86         logHandler.complain(sHeader3 + message);
87     }
88 
89     //  ************************************************    test parameters
90 
91     private String debuggeeName =
92         "nsk.jdi.Method.variables.variables001a";
93 
94     String mName = "nsk.jdi.Method.variables";
95 
96     //====================================================== test program
97 
98     static ArgumentHandler      argsHandler;
99     static int                  testExitCode = PASSED;
100 
101     //------------------------------------------------------ common section
102 
runThis(String argv[], PrintStream out)103     private int runThis (String argv[], PrintStream out) {
104 
105         Debugee debuggee;
106 
107         argsHandler     = new ArgumentHandler(argv);
108         logHandler      = new Log(out, argsHandler);
109         Binder binder   = new Binder(argsHandler, logHandler);
110 
111         if (argsHandler.verbose()) {
112             debuggee = binder.bindToDebugee(debuggeeName + " -vbs");  // *** tp
113         } else {
114             debuggee = binder.bindToDebugee(debuggeeName);            // *** tp
115         }
116 
117         IOPipe pipe     = new IOPipe(debuggee);
118 
119         debuggee.redirectStderr(out);
120         log2("variables001a debuggee launched");
121         debuggee.resume();
122 
123         String line = pipe.readln();
124         if ((line == null) || !line.equals("ready")) {
125             log3("signal received is not 'ready' but: " + line);
126             return FAILED;
127         } else {
128             log2("'ready' recieved");
129         }
130 
131         VirtualMachine vm = debuggee.VM();
132 
133     //------------------------------------------------------  testing section
134         log1("      TESTING BEGINS");
135 
136         for (int i = 0; ; i++) {
137         pipe.println("newcheck");
138             line = pipe.readln();
139 
140             if (line.equals("checkend")) {
141                 log2("     : returned string is 'checkend'");
142                 break ;
143             } else if (!line.equals("checkready")) {
144                 log3("ERROR: returned string is not 'checkready'");
145                 testExitCode = FAILED;
146                 break ;
147             }
148 
149             log1("new check: #" + i);
150 
151             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part
152 
153             List listOfDebuggeeClasses = vm.classesByName(mName + ".variables001aTestClass");
154             if (listOfDebuggeeClasses.size() != 1) {
155                 testExitCode = FAILED;
156                 log3("ERROR: listOfDebuggeeClasses.size() != 1");
157                 break ;
158             }
159 
160             List   methods   = null;
161             Method m         = null;
162             List   argsList  = null;
163 
164 
165             //  method with arguments and variables
166 
167             methods = ((ReferenceType) listOfDebuggeeClasses.get(0)).
168                            methodsByName("primitiveargsmethod");
169             m = (Method) methods.get(0);
170 
171       check:{
172                 try {
173                    argsList = m.variables();
174                    if (argsList.size() != 12) {
175                        log3("ERROR: for method with args and vars: argsList# != 12   :"
176                             + argsList.size() );
177                        testExitCode = FAILED;
178                        break check;
179                    }
180                    try {
181                        int i1names = 0;
182                        int i2names = 0;
183                        int i3names = 0;
184 
185                        for (int i2 = 0; i2 < argsList.size(); i2++) {
186                            LocalVariable lv = (LocalVariable) argsList.get(i2);
187                            if (lv.name().equals("i1")) i1names++;
188                            if (lv.name().equals("i2")) i2names++;
189                            if (lv.name().equals("i3")) i3names++;
190                        }
191                        if (i1names != 1) {
192                            log3("ERROR: for method with args and vars: mirrors of 'i1' != 1  :"
193                                  + i1names);
194                            testExitCode = FAILED;
195                        }
196                        if (i2names != 2) {
197                            log3("ERROR: for method with args and vars: mirrors of 'i2' != 2  :"
198                                  + i2names);
199                            testExitCode = FAILED;
200                        }
201                        if (i3names != 1) {
202                            log3("ERROR: for method with args and vars: mirrors of 'i3' != 1  :"
203                                  + i3names);
204                            testExitCode = FAILED;
205                        }
206                    } catch ( ClassCastException e ) {
207                        log3("ERROR: ClassCastException for lv = (LocalVariable) arglist.get(0);");
208                        testExitCode = FAILED;
209                    }
210                 } catch ( AbsentInformationException e ) {
211                        log3("       AbsentInformationException for method with arguments");
212                        testExitCode = FAILED;
213                 }
214             } // check:
215 
216 
217             // method without arguments and variables
218 
219             methods = ((ReferenceType) listOfDebuggeeClasses.get(0)).
220                            methodsByName("vd");
221             m = (Method) methods.get(0);
222 
223             try {
224                 argsList = m.variables();
225                 if (argsList.size() != 0) {
226                     log3("ERROR: for method without args and vars: argsList# != 0    :"
227                             + argsList.size() );
228                     testExitCode = FAILED;
229                 }
230             } catch ( AbsentInformationException e ) {
231                 log3("       AbsentInformationException for method without arguments");
232                  testExitCode = FAILED;
233             }
234 
235 
236             // native method
237 
238             methods = ((ReferenceType) listOfDebuggeeClasses.get(0)).
239                            methodsByName("nvd");
240             m = (Method) methods.get(0);
241 
242             try {
243                 log2("......testing native method; AbsentInformationException is expected");
244                 argsList = m.variables();
245                 log3("ERROR: no AbsentInformationException");
246                 testExitCode = FAILED;
247             } catch ( AbsentInformationException e ) {
248                 log2("       AbsentInformationException");
249             }
250 
251             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
252         }
253         log1("      TESTING ENDS");
254 
255     //--------------------------------------------------   test summary section
256     //-------------------------------------------------    standard end section
257 
258         pipe.println("quit");
259         log2("waiting for the debuggee to finish ...");
260         debuggee.waitFor();
261 
262         int status = debuggee.getStatus();
263         if (status != PASSED + PASS_BASE) {
264             log3("debuggee returned UNEXPECTED exit status: " +
265                     status + " != PASS_BASE");
266             testExitCode = FAILED;
267         } else {
268             log2("debuggee returned expected exit status: " +
269                     status + " == PASS_BASE");
270         }
271 
272         if (testExitCode != PASSED) {
273             logHandler.complain("TEST FAILED");
274         }
275         return testExitCode;
276     }
277 }
278