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