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.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 - 0 interfaces
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() != 0) {
185                             log3("ERROR : iface0list.size() != 0 in case: Class0ForCheck");
186                             expresult = 1;
187                             break;
188                         }
189 
190                         break ;
191 
192                 case 1:         // Class1forCheck - 1 direct and 1 indirect 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() != 2) {
202                             log3("ERROR : iface1list.size() != 2 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");
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                                 log3("ERROR :name2: !name.equals('.Iface1' or '.Iface2') in Class1forCheck");
221                                 expresult = 1;
222                                 break;
223                             }
224                         }
225                         break;
226 
227                 case 2:         // Class2ForCheck - 1 direct and 2 indirect interfaces
228 
229                         List list2 = vm.classesByName(mName + ".Class2ForCheck");
230 
231                         classRefType  = (ReferenceType) list2.get(0);
232 
233                         List iface2list =
234                              ((ClassType) classRefType).allInterfaces();
235 
236                         if (iface2list.size() != 3) {
237                             log3("ERROR : iface2list.size() != 3 in case: Class2forCheck");
238                             expresult = 1;
239                             break;
240                         }
241 
242                         reftype = (ReferenceType) iface2list.get(0);
243                         name = reftype.name();
244                         if (!name.equals(mName + ".Iface1")) {
245                             if (!name.equals(mName + ".Iface2")) {
246                                 if (!name.equals(mName + ".Iface3")) {
247                                     log3("ERROR : name1: !name.equals('.Iface1' or '.Iface3' or 'Iface3) in Class2forCheck");
248                                     expresult = 1;
249                                     break;
250                                 }
251                             }
252                         }
253                         reftype = (ReferenceType) iface2list.get(1);
254                         name = reftype.name();
255                         if (!name.equals(mName + ".Iface1")) {
256                             if (!name.equals(mName + ".Iface2")) {
257                                 if (!name.equals(mName + ".Iface3")) {
258                                     log3("ERROR : name1: !name.equals('.Iface1' or '.Iface3' or 'Iface3) in Class2forCheck");
259                                     expresult = 1;
260                                     break;
261                                 }
262                             }
263                         }
264                         reftype = (ReferenceType) iface2list.get(2);
265                         name = reftype.name();
266                         if (!name.equals(mName + ".Iface1")) {
267                             if (!name.equals(mName + ".Iface2")) {
268                                 if (!name.equals(mName + ".Iface3")) {
269                                     log3("ERROR : name1: !name.equals('.Iface1' or '.Iface3' or 'Iface3) in Class2forCheck");
270                                     expresult = 1;
271                                     break;
272                                 }
273                             }
274                         }
275                         break;
276 
277                 case 3:         // Class3ForCheck - 1 direct and 2 indirect interfaces
278 
279                         List list3 = vm.classesByName(mName + ".Class3ForCheck");
280 
281                         classRefType  = (ReferenceType) list3.get(0);
282 
283                         List iface3list =
284                              ((ClassType) classRefType).allInterfaces();
285 
286                         if (iface3list.size() != 3) {
287                             log3("ERROR : iface3list.size() != 3 in case: Class3forCheck");
288                             expresult = 1;
289                             break;
290                         }
291 
292                         reftype = (ReferenceType) iface3list.get(0);
293                         name = reftype.name();
294                         if (!name.equals(mName + ".Iface1")) {
295                             if (!name.equals(mName + ".Iface2")) {
296                                 if (!name.equals(mName + ".Iface3")) {
297                                     log3("ERROR : name1: !name.equals('.Iface1' or '.Iface3' or 'Iface3) in Class3forCheck");
298                                     expresult = 1;
299                                     break;
300                                 }
301                             }
302                         }
303                         reftype = (ReferenceType) iface3list.get(1);
304                         name = reftype.name();
305                         if (!name.equals(mName + ".Iface1")) {
306                             if (!name.equals(mName + ".Iface2")) {
307                                 if (!name.equals(mName + ".Iface3")) {
308                                     log3("ERROR : name1: !name.equals('.Iface1' or '.Iface3' or 'Iface3) in Class3forCheck");
309                                     expresult = 1;
310                                     break;
311                                 }
312                             }
313                         }
314                         reftype = (ReferenceType) iface3list.get(2);
315                         name = reftype.name();
316                         if (!name.equals(mName + ".Iface1")) {
317                             if (!name.equals(mName + ".Iface2")) {
318                                 if (!name.equals(mName + ".Iface3")) {
319                                     log3("ERROR : name1: !name.equals('.Iface1' or '.Iface3' or 'Iface3) in Class3forCheck");
320                                     expresult = 1;
321                                     break;
322                                 }
323                             }
324                         }
325                         break;
326 
327 
328                 default: expresult = 2;
329                          break ;
330                 }
331 
332                 if (expresult == 2) {
333                     log2("      test cases finished");
334                     break ;
335                 } else if (expresult == 1) {
336                     log3("ERROR: expresult != true;  check # = " + i);
337                     testExitCode = FAILED;
338                 }
339             }
340             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
341         }
342         log1("      TESTING ENDS");
343 
344     //--------------------------------------------------   test summary section
345     //-------------------------------------------------    standard end section
346 
347         pipe.println("quit");
348         log2("waiting for the debuggee to finish ...");
349         debuggee.waitFor();
350 
351         int status = debuggee.getStatus();
352         if (status != PASSED + PASS_BASE) {
353             log3("debuggee returned UNEXPECTED exit status: " +
354                     status + " != PASS_BASE");
355             testExitCode = FAILED;
356         } else {
357             log2("debuggee returned expected exit status: " +
358                     status + " == PASS_BASE");
359         }
360 
361         if (testExitCode != PASSED) {
362             logHandler.complain("TEST FAILED");
363         }
364         return testExitCode;
365     }
366 }