1 /*
  2  * Copyright (c) 2001, 2020, 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.allInterfaces;
 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  * ClassType.                                                   <BR>
 37  *                                                              <BR>
 38  * The test checks up that results of the method                <BR>
 39  * <code>com.sun.jdi.ClassType.allInterfaces()</code>           <BR>
 40  * complies with its spec.                                      <BR>
 41  * <BR>
 42  * The cases for testing are as follows.                <BR>
 43  *                                                      <BR>
 44  * 1)    Class0ForCheck doesn't implement interface     <BR>
 45  * 2)    Class1ForCheck implements one direct and       <BR>
 46  *           one indirect interface                     <BR>
 47  * 3)    Class2ForCheck implements one direct and       <BR>
 48  *           two indirect interfaces                    <BR>
 49  * 4)    Class3ForCheck extends Class2ForCheck,         <BR>
 50  *           hence implements one direct and            <BR>
 51  *           two indirect interfaces                    <BR>
 52  * <BR>
 53  */
 54 
 55 public class allinterfaces001 {
 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/ClassType/allInterfaces/allinterfaces001",
 65     sHeader2 = "--> allinterfaces001: ",
 66     sHeader3 = "##> allinterfaces001: ";
 67 
 68     //----------------------------------------------------- main method
 69 
 70     public static void main (String argv[]) {
 71         int result = run(argv, System.out);
 72         System.exit(result + PASS_BASE);
 73     }
 74 
 75     public static int run (String argv[], PrintStream out) {
 76         return new allinterfaces001().runThis(argv, out);
 77     }
 78 
 79      //--------------------------------------------------   log procedures
 80 
 81     private static boolean verbMode = false;
 82 
 83     private static Log  logHandler;
 84 
 85     private static void log1(String message) {
 86         logHandler.display(sHeader1 + message);
 87     }
 88     private static void log2(String message) {
 89         logHandler.display(sHeader2 + message);
 90     }
 91     private static void log3(String message) {
 92         logHandler.complain(sHeader3 + message);
 93     }
 94 
 95     //  ************************************************    test parameters
 96 
 97     private String debuggeeName =
 98         "nsk.jdi.ClassType.allInterfaces.allinterfaces001a";
 99 
100     String mName = "nsk.jdi.ClassType.allInterfaces";
101 
102     //====================================================== test program
103 
104     static ArgumentHandler      argsHandler;
105     static int                  testExitCode = PASSED;
106 
107     //------------------------------------------------------ common section
108 
109     private int runThis (String argv[], PrintStream out) {
110 
111         Debugee debuggee;
112 
113         argsHandler     = new ArgumentHandler(argv);
114         logHandler      = new Log(out, argsHandler);
115         Binder binder   = new Binder(argsHandler, logHandler);
116 
117         if (argsHandler.verbose()) {
118             debuggee = binder.bindToDebugee(debuggeeName + " -vbs");  // *** tp
119         } else {
120             debuggee = binder.bindToDebugee(debuggeeName);            // *** tp
121         }
122 
123         IOPipe pipe     = new IOPipe(debuggee);
124 
125         debuggee.redirectStderr(out);
126         log2("allinterfaces001a debuggee launched");
127         debuggee.resume();
128 
129         String line = pipe.readln();
130         if ((line == null) || !line.equals("ready")) {
131             log3("signal received is not 'ready' but: " + line);
132             return FAILED;
133         } else {
134             log2("'ready' recieved");
135         }
136 
137         VirtualMachine vm = debuggee.VM();
138 
139     //------------------------------------------------------  testing section
140         log1("      TESTING BEGINS");
141 
142         for (int i = 0; ; i++) {
143         pipe.println("newcheck");
144             line = pipe.readln();
145 
146             if (line.equals("checkend")) {
147                 log2("     : returned string is 'checkend'");
148                 break ;
149             } else if (!line.equals("checkready")) {
150                 log3("ERROR: returned string is not 'checkready'");
151                 testExitCode = FAILED;
152                 break ;
153             }
154 
155             log1("new check: #" + i);
156 
157             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part
158 
159             ReferenceType classRefType = null;
160             ReferenceType reftype      = null;
161             List          l            = null;
162             Method        m            = null;
163             String        name         = null;
164 
165             int i2;
166 
167             for (i2 = 0; ; i2++) {
168 
169                 int expresult = 0;
170 
171                 log2("new check: #" + i2);
172 
173                 switch (i2) {
174 
175                 case 0:         // Class0ForCheck - 1 injected interface (java.lang.IdentityObject)
176 
177                         List list0 = vm.classesByName(mName + ".Class0ForCheck");
178 
179                         classRefType  = (ReferenceType) list0.get(0);
180 
181                         List iface0list =
182                              ((ClassType) classRefType).allInterfaces();
183 
184                         if (iface0list.size() != 1) {
185                             log3("ERROR : iface0list.size() != 1 in case: Class0ForCheck");
186                             expresult = 1;
187                             break;
188                         }
189 
190                         break ;
191 
192                 case 1:         // Class1forCheck - 1 direct, 1 indirect, and 1 injected (java.lang.IdentityObject) interfaces
193 
194                         List list1 = vm.classesByName(mName + ".Class1ForCheck");
195 
196                         classRefType  = (ReferenceType) list1.get(0);
197 
198                         List iface1list =
199                              ((ClassType) classRefType).allInterfaces();
200 
201                         if (iface1list.size() != 3) {
202                             log3("ERROR : iface1list.size() != 3 in case: Class1forCheck   :"  + iface1list.size());
203                             expresult = 1;
204                             break;
205                         }
206 
207                         reftype = (ReferenceType) iface1list.get(0);
208                         name = reftype.name();
209                         if (!name.equals(mName + ".Iface1")) {
210                             if (!name.equals(mName + ".Iface2")) {
211                                 log3("ERROR : name1: !name.equals('.Iface1' or '.Iface2') in Class1forCheck, name: " + name);
212                                 expresult = 1;
213                                 break;
214                             }
215                         }
216                         reftype = (ReferenceType) iface1list.get(1);
217                         name = reftype.name();
218                         if (!name.equals(mName + ".Iface1")) {
219                             if (!name.equals(mName + ".Iface2")) {
220                                 if (!name.equals("java.lang.IdentityObject")) {
221                                     log3("ERROR :name2: !name.equals('.Iface1' or '.Iface2 or 'java.lang.IdentityObject') in Class1forCheck, name: " + name);
222                                     expresult = 1;
223                                     break;
224                                 }
225                             }
226                         }
227                         break;
228 
229                 case 2:         // Class2ForCheck - 1 direct, 2 indirect, and 1 injected (java.lang.IdentityObject) interfaces
230 
231                         List list2 = vm.classesByName(mName + ".Class2ForCheck");
232 
233                         classRefType  = (ReferenceType) list2.get(0);
234 
235                         List iface2list =
236                              ((ClassType) classRefType).allInterfaces();
237 
238                         if (iface2list.size() != 4) {
239                             log3("ERROR : iface2list.size() != 4 in case: Class2forCheck");
240                             expresult = 1;
241                             break;
242                         }
243 
244                         reftype = (ReferenceType) iface2list.get(0);
245                         name = reftype.name();
246                         if (!name.equals(mName + ".Iface1")) {
247                             if (!name.equals(mName + ".Iface2")) {
248                                 if (!name.equals(mName + ".Iface3")) {
249                                     log3("ERROR : name1: !name.equals('.Iface1' or '.Iface2' or 'Iface3) in Class2forCheck, name: " + name);
250                                     expresult = 1;
251                                     break;
252                                 }
253                             }
254                         }
255                         reftype = (ReferenceType) iface2list.get(1);
256                         name = reftype.name();
257                         if (!name.equals(mName + ".Iface1")) {
258                             if (!name.equals(mName + ".Iface2")) {
259                                 if (!name.equals(mName + ".Iface3")) {
260                                     if (!name.equals("java.lang.IdentityObject")) {
261                                         log3("ERROR : name1: !name.equals('.Iface1' or '.Iface2' or 'Iface3' or 'java.lang.IdentityObject') in Class2forCheck, name: " + name);
262                                         expresult = 1;
263                                         break;
264                                     }
265                                 }
266                             }
267                         }
268                         reftype = (ReferenceType) iface2list.get(2);
269                         name = reftype.name();
270                         if (!name.equals(mName + ".Iface1")) {
271                             if (!name.equals(mName + ".Iface2")) {
272                                 if (!name.equals(mName + ".Iface3")) {
273                                     log3("ERROR : name1: !name.equals('.Iface1' or '.Iface3' or 'Iface3) in Class2forCheck, name: " + name);
274                                     expresult = 1;
275                                     break;
276                                 }
277                             }
278                         }
279                         break;
280 
281                 case 3:         // Class3ForCheck - 1 direct, 2 indirect, and 1 injected (java.lang.IdentityObject) interfaces
282 
283                         List list3 = vm.classesByName(mName + ".Class3ForCheck");
284 
285                         classRefType  = (ReferenceType) list3.get(0);
286 
287                         List iface3list =
288                              ((ClassType) classRefType).allInterfaces();
289 
290                         if (iface3list.size() != 4) {
291                             log3("ERROR : iface3list.size() != 4 in case: Class3forCheck");
292                             expresult = 1;
293                             break;
294                         }
295 
296                         reftype = (ReferenceType) iface3list.get(0);
297                         name = reftype.name();
298                         if (!name.equals(mName + ".Iface1")) {
299                             if (!name.equals(mName + ".Iface2")) {
300                                 if (!name.equals(mName + ".Iface3")) {
301                                     log3("ERROR : name1: !name.equals('.Iface1' or '.Iface2' or 'Iface3) in Class3forCheck, name: " + name);
302                                     expresult = 1;
303                                     break;
304                                 }
305                             }
306                         }
307                         reftype = (ReferenceType) iface3list.get(1);
308                         name = reftype.name();
309                         if (!name.equals(mName + ".Iface1")) {
310                             if (!name.equals(mName + ".Iface2")) {
311                                 if (!name.equals(mName + ".Iface3")) {
312                                     if (!name.equals("java.lang.IdentityObject")) {
313                                         log3("ERROR : name1: !name.equals('.Iface1' or '.Iface3' or 'Iface3' or 'java.lang.IdentityObject') in Class3forCheck, name: " + name);
314                                         expresult = 1;
315                                         break;
316                                     }
317                                 }
318                             }
319                         }
320                         reftype = (ReferenceType) iface3list.get(2);
321                         name = reftype.name();
322                         if (!name.equals(mName + ".Iface1")) {
323                             if (!name.equals(mName + ".Iface2")) {
324                                 if (!name.equals(mName + ".Iface3")) {
325                                     log3("ERROR : name1: !name.equals('.Iface1' or '.Iface2' or 'Iface3) in Class3forCheck, name: " + name);
326                                     expresult = 1;
327                                     break;
328                                 }
329                             }
330                         }
331                         break;
332 
333 
334                 default: expresult = 2;
335                          break ;
336                 }
337 
338                 if (expresult == 2) {
339                     log2("      test cases finished");
340                     break ;
341                 } else if (expresult == 1) {
342                     log3("ERROR: expresult != true;  check # = " + i);
343                     testExitCode = FAILED;
344                 }
345             }
346             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
347         }
348         log1("      TESTING ENDS");
349 
350     //--------------------------------------------------   test summary section
351     //-------------------------------------------------    standard end section
352 
353         pipe.println("quit");
354         log2("waiting for the debuggee to finish ...");
355         debuggee.waitFor();
356 
357         int status = debuggee.getStatus();
358         if (status != PASSED + PASS_BASE) {
359             log3("debuggee returned UNEXPECTED exit status: " +
360                     status + " != PASS_BASE");
361             testExitCode = FAILED;
362         } else {
363             log2("debuggee returned expected exit status: " +
364                     status + " == PASS_BASE");
365         }
366 
367         if (testExitCode != PASSED) {
368             logHandler.complain("TEST FAILED");
369         }
370         return testExitCode;
371     }
372 }