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