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.ArrayType.newInstance;
  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  * ArrayType.                                                   <BR>
  37  *                                                              <BR>
  38  * The test checks up that results of the method                <BR>
  39  * <code>com.sun.jdi.ArrayType.newInstance()</code>             <BR>
  40  * complies with its spec when componentType of an ArrayType    <BR>
  41  * object used as original for newInstance() is InterfeaceType. <BR>
  42  * The test works as follows.                                   <BR>
  43  *                                                              <BR>
  44  * For given debuggee's array whose components                  <BR>
  45  * are objects of a class type implementing the interface,      <BR>
  46  * a debugger forms corresponding ArrayType object              <BR>
  47  * to which it applies newInstance() method in order to get     <BR>
  48  * an ArrayReference object, newarray.                          <BR>
  49  * Then the debugger checks up that:                            <BR>
  50  * - newInstance() method's returned value != null;             <BR>
  51  * - length of newly created array is equal to 'arg' in         <BR>
  52  *   newInstance(int arg) method invocation;                    <BR>
  53  * - newarray.type().name().equals(array.componentType().name());<BR>
  54  * - all components of new array are nulls                      <BR>
  55  *   as its default values at the moment of the array creation. <BR>
  56  * <BR>
  57  */
  58 
  59 public class newinstance003 {
  60 
  61     //----------------------------------------------------- templete section
  62     static final int PASSED = 0;
  63     static final int FAILED = 2;
  64     static final int PASS_BASE = 95;
  65 
  66     //----------------------------------------------------- templete parameters
  67     static final String
  68     sHeader1 = "\n==> nsk/jdi/ArrayType/newInstance/newinstance003  ",
  69     sHeader2 = "--> newinstance003: ",
  70     sHeader3 = "##> newinstance003: ";
  71 
  72     //----------------------------------------------------- main method
  73 
  74     public static void main (String argv[]) {
  75         int result = run(argv, System.out);
  76         System.exit(result + PASS_BASE);
  77     }
  78 
  79     public static int run (String argv[], PrintStream out) {
  80         return new newinstance003().runThis(argv, out);
  81     }
  82 
  83      //--------------------------------------------------   log procedures
  84 
  85     private static boolean verbMode = false;
  86 
  87     private static Log  logHandler;
  88 
  89     private static void log1(String message) {
  90         logHandler.display(sHeader1 + message);
  91     }
  92     private static void log2(String message) {
  93         logHandler.display(sHeader2 + message);
  94     }
  95     private static void log3(String message) {
  96         logHandler.complain(sHeader3 + message);
  97     }
  98 
  99     //  ************************************************    test parameters
 100 
 101     private String debuggeeName =
 102         "nsk.jdi.ArrayType.newInstance.newinstance003a";
 103 
 104     String mName = "nsk.jdi.ArrayType.newInstance";
 105 
 106     //====================================================== test program
 107 
 108     static ArgumentHandler      argsHandler;
 109     static int                  testExitCode = PASSED;
 110 
 111     //------------------------------------------------------ common section
 112 
 113     private int runThis (String argv[], PrintStream out) {
 114 
 115         Debugee debuggee;
 116 
 117         argsHandler     = new ArgumentHandler(argv);
 118         logHandler      = new Log(out, argsHandler);
 119         Binder binder   = new Binder(argsHandler, logHandler);
 120 
 121         if (argsHandler.verbose()) {
 122             debuggee = binder.bindToDebugee(debuggeeName + " -vbs");  // *** tp
 123         } else {
 124             debuggee = binder.bindToDebugee(debuggeeName);            // *** tp
 125         }
 126 
 127         IOPipe pipe     = new IOPipe(debuggee);
 128 
 129         debuggee.redirectStderr(out);
 130         log2("newinstance003a debuggee launched");
 131         debuggee.resume();
 132 
 133         String line = pipe.readln();
 134         if ((line == null) || !line.equals("ready")) {
 135             log3("signal received is not 'ready' but: " + line);
 136             return FAILED;
 137         } else {
 138             log2("'ready' recieved");
 139         }
 140 
 141         VirtualMachine vm = debuggee.VM();
 142 
 143     //------------------------------------------------------  testing section
 144         log1("      TESTING BEGINS");
 145 
 146         for (int i = 0; ; i++) {
 147         pipe.println("newcheck");
 148             line = pipe.readln();
 149 
 150             if (line.equals("checkend")) {
 151                 log2("     : returned string is 'checkend'");
 152                 break ;
 153             } else if (!line.equals("checkready")) {
 154                 log3("ERROR: returned string is not 'checkready'");
 155                 testExitCode = FAILED;
 156                 break ;
 157             }
 158 
 159             log1("new check: #" + i);
 160 
 161             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part
 162 
 163             List listOftestedClasses = vm.classesByName(mName + ".newinstance003aTestClass");
 164             if (listOftestedClasses.size() != 1) {
 165                 log3("ERROR: listOftestedClasses.size() != 1");
 166                 testExitCode = FAILED;
 167                 continue;
 168             }
 169             ReferenceType testedClass =
 170                         (ReferenceType) listOftestedClasses.get(0);
 171 
 172             Field fieldArray = testedClass.fieldByName("iface");
 173             if (fieldArray == null) {
 174                 log3("ERROR: fieldArray == null");
 175                 testExitCode = FAILED;
 176                 continue;
 177             }
 178 
 179             final int arraylength = 2;
 180 
 181             ArrayType ifaceArray =
 182                             (ArrayType) testedClass.getValue(fieldArray).type();
 183 
 184             ArrayReference newifaceArray = null;
 185             try {
 186                 newifaceArray = ifaceArray.newInstance(arraylength);
 187             } catch ( Throwable e ) {
 188                 log3 ("ERROR: Exception: " + e);
 189                 testExitCode = FAILED;
 190                 continue;
 191             }
 192             if (newifaceArray == null) {
 193                 log3("ERROR: newifaceArray == null");
 194                 testExitCode = FAILED;
 195                 continue;
 196             }
 197 
 198             try {
 199                 if ( newifaceArray.type().name().equals(ifaceArray.componentType().name()) ) {
 200                     log3("ERROR : types are not equal :\n" +
 201                          "newifaceArray.type().name()      =" + newifaceArray.type().name() + "\n" +
 202                          "ifaceArray.componentType().name()=" + ifaceArray.componentType().name() );
 203                     testExitCode = FAILED;
 204                     continue;
 205                 }
 206             } catch ( ClassNotLoadedException e ) {
 207                 log3("ERROR: ClassNotLoadedException for newifaceArray.type().name().equals(..");
 208                 testExitCode = FAILED;
 209                 continue;
 210             }
 211 
 212             if (newifaceArray.length() != arraylength) {
 213                 log3("ERROR : newifaceArray.length() != arraylength   " + newifaceArray.length());
 214                 testExitCode = FAILED;
 215                 continue;
 216             }
 217 
 218             if (newifaceArray.getValue(0) != null ||
 219                            newifaceArray.getValue(1) != null) {
 220                 log3("ERROR: newifaceArray.getValue() != null  ");
 221                 testExitCode = FAILED;
 222                 continue;
 223             }
 224 
 225             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 226         }
 227         log1("      TESTING ENDS");
 228 
 229     //--------------------------------------------------   test summary section
 230     //-------------------------------------------------    standard end section
 231 
 232         pipe.println("quit");
 233         log2("waiting for the debuggee to finish ...");
 234         debuggee.waitFor();
 235 
 236         int status = debuggee.getStatus();
 237         if (status != PASSED + PASS_BASE) {
 238             log3("debuggee returned UNEXPECTED exit status: " +
 239                     status + " != PASS_BASE");
 240             testExitCode = FAILED;
 241         } else {
 242             log2("debuggee returned expected exit status: " +
 243                     status + " == PASS_BASE");
 244         }
 245 
 246         if (testExitCode != PASSED) {
 247             System.out.println("TEST FAILED");
 248         }
 249         return testExitCode;
 250     }
 251 }