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.interfaces;
 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.interfaces()</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 interfaces    <BR>
 45  * 2)    Class1ForCheck implements one interface        <BR>
 46  * 2)    Class2ForCheck implements two interfaces       <BR>
 47  * <BR>
 48  */
 49 
 50 public class interfaces001 {
 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/ClassType/interfaces/interfaces001",
 60     sHeader2 = "--> interfaces001: ",
 61     sHeader3 = "##> interfaces001: ";
 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 interfaces001().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.ClassType.interfaces.interfaces001a";
 94 
 95     String mName = "nsk.jdi.ClassType.interfaces";
 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("interfaces001a 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             Method        m            = null;
158             String        name         = null;
159 
160             int i2;
161 
162             for (i2 = 0; ; i2++) {
163 
164                 int expresult = 0;
165 
166                 log2("new check: #" + i2);
167 
168                 switch (i2) {
169 
170                 case 0:         // Class0ForCheck - 0 interfaces
171 
172                         List list0 = vm.classesByName(mName + ".Class0ForCheck");
173 
174                         classRefType  = (ReferenceType) list0.get(0);
175 
176                         List iface0list =
177                              ((ClassType) classRefType).interfaces();
178 
179                         if (iface0list.size() != 0) {
180                             log3("ERROR : iface0list.size() != 0 in case: Class0ForCheck");
181                             expresult = 1;
182                             break;
183                         }
184 
185                         break;
186 
187                 case 1:         // Class1forCheck - 1 interface
188 
189                         List list1 = vm.classesByName(mName + ".Class1ForCheck");
190 
191                         classRefType  = (ReferenceType) list1.get(0);
192 
193                         List iface1list =
194                              ((ClassType) classRefType).interfaces();
195 
196                         if (iface1list.size() != 1) {
197                             log3("ERROR : iface1list.size() != 1 in case: Class1forCheck");
198                             expresult = 1;
199                             break;
200                         }
201 
202                         reftype = (ReferenceType) iface1list.get(0);
203                         name = reftype.name();
204                         if (!name.equals(mName + ".Iface1")) {
205                             log3("ERROR : !name.equals(mName + '.Iface1') in Class1forCheck");
206                             expresult = 1;
207                             break;
208                         }
209 
210                         break;
211 
212                 case 2:         // Class2ForCheck - 2 interfaces
213 
214                         List list2 = vm.classesByName(mName + ".Class2ForCheck");
215 
216                         classRefType  = (ReferenceType) list2.get(0);
217 
218                         List iface2list =
219                              ((ClassType) classRefType).interfaces();
220 
221                         if (iface2list.size() != 2) {
222                             log3("ERROR : iface1list.size() != 2 in case: Class2forCheck");
223                             expresult = 1;
224                             break;
225                         }
226 
227                         reftype = (ReferenceType) iface2list.get(0);
228                         name = reftype.name();
229                         if (!name.equals(mName + ".Iface2")) {
230                             if (!name.equals(mName + ".Iface3")) {
231                                 log3("ERROR : name1: !name.equals('.Iface1' or '.Iface3') in Class2forCheck");
232                                 expresult = 1;
233                                 break;
234                             }
235                         }
236                         reftype = (ReferenceType) iface2list.get(1);
237                         name = reftype.name();
238                         if (!name.equals(mName + ".Iface2")) {
239                             if (!name.equals(mName + ".Iface3")) {
240                                 log3("ERROR :name2: !name.equals('.Iface1' or '.Iface3') in Class2forCheck");
241                                 expresult = 1;
242                                 break;
243                             }
244                         }
245 
246                         break;
247 
248 
249                 default: expresult = 2;
250                          break ;
251                 }
252 
253                 if (expresult == 2) {
254                     log2("      test cases finished");
255                     break ;
256                 } else if (expresult == 1) {
257                     log3("ERROR: expresult != true;  check # = " + i);
258                     testExitCode = FAILED;
259                 }
260             }
261             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
262         }
263         log1("      TESTING ENDS");
264 
265     //--------------------------------------------------   test summary section
266     //-------------------------------------------------    standard end section
267 
268         pipe.println("quit");
269         log2("waiting for the debuggee to finish ...");
270         debuggee.waitFor();
271 
272         int status = debuggee.getStatus();
273         if (status != PASSED + PASS_BASE) {
274             log3("debuggee returned UNEXPECTED exit status: " +
275                     status + " != PASS_BASE");
276             testExitCode = FAILED;
277         } else {
278             log2("debuggee returned expected exit status: " +
279                     status + " == PASS_BASE");
280         }
281 
282         if (testExitCode != PASSED) {
283             logHandler.complain("TEST FAILED");
284         }
285         return testExitCode;
286     }
287 }