1 /*
2  * Copyright (c) 2001, 2019, 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.ObjectReference.getValues;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 import com.sun.jdi.*;
31 import java.util.*;
32 import java.io.*;
33 
34 /**
35  * The test for the implementation of an object of the type     <BR>
36  * ObjectReference.                                             <BR>
37  *                                                              <BR>
38  * The test checks up that results of the method                <BR>
39  * <code>com.sun.jdi.ObjectReference.getValues()</code>         <BR>
40  * complies with its specification.                             <BR>
41  * <BR>                                                         <BR>
42  * The case for tesing includes fields in two objects of two    <BR>
43  * different classes: getvalues003aTestClass and getvalues003aTestClass1                        <BR>
44  * As List of arguments contains Fileds defined in another class<BR>
45  * throwing  IllegalArgumentException  is expected.             <BR>
46  * <BR>
47  * The test works as follows:                                   <BR>
48  * Upon launch, a debuggee informs a debuggee of creating       <BR>
49  * the tested object.                                           <BR>
50  * The debugger gets a List of fields from the getvalues003aTestClass1 object1<BR>
51  * and uses it as the argument for get.Values() invoked on      <BR>
52  * getvalues003aTestClass object.                                               <BR>
53  */
54 
55 public class getvalues003{
56 
57     //----------------------------------------------------- templete section
58     static final int PASSED = 0;
59     static final int FAILED = 2;
60     static final int PASS_BASE = 95;
61 
62     //----------------------------------------------------- templete parameters
63     static final String
64     sHeader1 = "\n==> nsk/jdi/ObjectReference/getValues/getvalues003 ",
65     sHeader2 = "--> debugger: ",
66     sHeader3 = "##> debugger: ";
67 
68     //----------------------------------------------------- main method
69 
main(String argv[])70     public static void main (String argv[]) {
71         int result = run(argv, System.out);
72         System.exit(result + PASS_BASE);
73     }
74 
run(String argv[], PrintStream out)75     public static int run (String argv[], PrintStream out) {
76         return new getvalues003().runThis(argv, out);
77     }
78 
79     //--------------------------------------------------   log procedures
80 
81     private static Log  logHandler;
82 
log1(String message)83     private static void log1(String message) {
84         logHandler.display(sHeader1 + message);
85     }
log2(String message)86     private static void log2(String message) {
87         logHandler.display(sHeader2 + message);
88     }
log3(String message)89     private static void log3(String message) {
90         logHandler.complain(sHeader3 + message);
91     }
92 
93     //  ************************************************    test parameters
94 
95     private String debuggeeName =
96         "nsk.jdi.ObjectReference.getValues.getvalues003a";
97 
98     private String testedClassName =
99         "nsk.jdi.ObjectReference.getValues.getvalues003aTestClass";
100 
101     private String testedClass1Name =
102         "nsk.jdi.ObjectReference.getValues.getvalues003aTestClass1";
103 
104     //String mName = "nsk.jdi.ObjectReference.getValues";
105 
106     //====================================================== test program
107     //------------------------------------------------------ common section
108 
109     static ArgumentHandler      argsHandler;
110 
111     static int waitTime;
112 
113     static VirtualMachine      vm            = null;
114 
115     static int  testExitCode = PASSED;
116 
117     static final int returnCode0 = 0;
118     static final int returnCode1 = 1;
119     static final int returnCode2 = 2;
120     static final int returnCode3 = 3;
121     static final int returnCode4 = 4;
122 
123     //------------------------------------------------------ methods
124 
runThis(String argv[], PrintStream out)125     private int runThis (String argv[], PrintStream out) {
126 
127         Debugee debuggee;
128 
129         argsHandler     = new ArgumentHandler(argv);
130         logHandler      = new Log(out, argsHandler);
131         Binder binder   = new Binder(argsHandler, logHandler);
132 
133         if (argsHandler.verbose()) {
134             debuggee = binder.bindToDebugee(debuggeeName + " -vbs");
135         } else {
136             debuggee = binder.bindToDebugee(debuggeeName);
137         }
138 
139         waitTime = argsHandler.getWaitTime();
140 
141 
142         IOPipe pipe     = new IOPipe(debuggee);
143 
144         debuggee.redirectStderr(out);
145         log2(debuggeeName + " debuggee launched");
146         debuggee.resume();
147 
148         String line = pipe.readln();
149         if ((line == null) || !line.equals("ready")) {
150             log3("signal received is not 'ready' but: " + line);
151             return FAILED;
152         } else {
153             log2("'ready' recieved");
154         }
155 
156         vm = debuggee.VM();
157 
158     //------------------------------------------------------  testing section
159         log1("      TESTING BEGINS");
160 
161         for (int i = 0; ; i++) {
162 
163             pipe.println("newcheck");
164             line = pipe.readln();
165 
166             if (line.equals("checkend")) {
167                 log2("     : returned string is 'checkend'");
168                 break ;
169             } else if (!line.equals("checkready")) {
170                 log3("ERROR: returned string is not 'checkready'");
171                 testExitCode = FAILED;
172                 break ;
173             }
174 
175             log1("new checkready: #" + i);
176 
177             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part
178 
179             List classes = null;
180 
181             log2("......getting: List classes = vm.classesByName(debuggeeName); expected size == 1");
182             classes = vm.classesByName(debuggeeName);
183             int size = classes.size();
184             if (size != 1) {
185                 log3("ERROR: classes.size() != 1 : " + size);
186                 testExitCode = FAILED;
187                 break ;
188             }
189 
190             log2("      getting ReferenceType and ClassType objects for debuggeeClass");
191             ReferenceType debuggeeClass = (ReferenceType) classes.get(0);
192 
193 
194             Field    field1  = null;
195             Value    val1    = null;
196             List<Field>     fields  = null;
197 
198             String         objName    = "testObj";
199             String         obj1Name   = "testObj1";
200 
201             ObjectReference objRef    = null;
202 
203             ReferenceType testedClass = null;
204 
205 
206             log2("......getting the mirror of tested getvalues003aTestClass obj : ObjectReference objRef");
207 
208             field1  = debuggeeClass.fieldByName(objName);
209             val1    = debuggeeClass.getValue(field1);
210             objRef  = (ObjectReference) val1;
211 
212             log2("......getting the mirror of tested getvalues003aTestClass : ReferenceType classRef");
213 
214             classes      = vm.classesByName(testedClass1Name);
215             testedClass  = (ReferenceType) classes.get(0);
216             fields       = testedClass.visibleFields();
217 
218 
219             log2("......performing the check; IllegalArgumentException is expected");
220             try {
221                 Map<Field, Value> vMap    = objRef.getValues(fields);
222                 log3("ERROR: no IllegalArgumentException");
223                 testExitCode = FAILED;
224             } catch ( IllegalArgumentException e ) {
225                 log2("      IllegalArgumentException");
226             }
227 
228             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
229         }
230         log1("      TESTING ENDS");
231 
232     //--------------------------------------------------   test summary section
233     //-------------------------------------------------    standard end section
234 
235         pipe.println("quit");
236         log2("waiting for the debuggee to finish ...");
237         debuggee.waitFor();
238 
239         int status = debuggee.getStatus();
240         if (status != PASSED + PASS_BASE) {
241             log3("debuggee returned UNEXPECTED exit status: " +
242                     status + " != PASS_BASE");
243             testExitCode = FAILED;
244         } else {
245             log2("debuggee returned expected exit status: " +
246                     status + " == PASS_BASE");
247         }
248 
249         if (testExitCode != PASSED) {
250             logHandler.complain("TEST FAILED");
251         }
252         return testExitCode;
253     }
254 }
255