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