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 
 149             // There are potentially other non-test Java threads allocating objects and triggering
 150             // GC's so we need to suspend the target VM to avoid the objects created in the test
 151             // from being accidentally GC'ed. However, we need the target VM temporary resumed
 152             // while reading its response. Below we resume the target VM (if required) and suspend
 153             // it only after pipe.readln() returns.
 154 
 155             // On the first iteration the target VM is not suspended yet.
 156             if (i > 0) {
 157                 debuggee.resume();
 158             }
 159             line = pipe.readln();
 160 
 161             // Suspending target VM to prevent other non-test Java threads from triggering GCs.
 162             debuggee.suspend();
 163 
 164             if (line.equals("checkend")) {
 165                 log2("     : returned string is 'checkend'");
 166                 break ;
 167             } else if (!line.equals("checkready")) {
 168                 log3("ERROR: returned string is not 'checkready'");
 169                 testExitCode = FAILED;
 170                 break ;
 171             }
 172 
 173             log1("new check: #" + i);
 174 
 175             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part
 176 
 177             List listOftestedClasses = vm.classesByName(mName + ".newinstance003aTestClass");
 178             if (listOftestedClasses.size() != 1) {
 179                 log3("ERROR: listOftestedClasses.size() != 1");
 180                 testExitCode = FAILED;
 181                 continue;
 182             }
 183             ReferenceType testedClass =
 184                         (ReferenceType) listOftestedClasses.get(0);
 185 
 186             Field fieldArray = testedClass.fieldByName("iface");
 187             if (fieldArray == null) {
 188                 log3("ERROR: fieldArray == null");
 189                 testExitCode = FAILED;
 190                 continue;
 191             }
 192 
 193             final int arraylength = 2;
 194 
 195             ArrayType ifaceArray =
 196                             (ArrayType) testedClass.getValue(fieldArray).type();
 197 
 198             ArrayReference newifaceArray = null;
 199             try {
 200                 newifaceArray = ifaceArray.newInstance(arraylength);
 201             } catch ( Throwable e ) {
 202                 log3 ("ERROR: Exception: " + e);
 203                 testExitCode = FAILED;
 204                 continue;
 205             }
 206             if (newifaceArray == null) {
 207                 log3("ERROR: newifaceArray == null");
 208                 testExitCode = FAILED;
 209                 continue;
 210             }
 211 
 212             try {
 213                 if ( newifaceArray.type().name().equals(ifaceArray.componentType().name()) ) {
 214                     log3("ERROR : types are not equal :\n" +
 215                          "newifaceArray.type().name()      =" + newifaceArray.type().name() + "\n" +
 216                          "ifaceArray.componentType().name()=" + ifaceArray.componentType().name() );
 217                     testExitCode = FAILED;
 218                     continue;
 219                 }
 220             } catch ( ClassNotLoadedException e ) {
 221                 log3("ERROR: ClassNotLoadedException for newifaceArray.type().name().equals(..");
 222                 testExitCode = FAILED;
 223                 continue;
 224             }
 225 
 226             if (newifaceArray.length() != arraylength) {
 227                 log3("ERROR : newifaceArray.length() != arraylength   " + newifaceArray.length());
 228                 testExitCode = FAILED;
 229                 continue;
 230             }
 231 
 232             if (newifaceArray.getValue(0) != null ||
 233                            newifaceArray.getValue(1) != null) {
 234                 log3("ERROR: newifaceArray.getValue() != null  ");
 235                 testExitCode = FAILED;
 236                 continue;
 237             }
 238 
 239             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 240         }
 241         log1("      TESTING ENDS");
 242 
 243     //--------------------------------------------------   test summary section
 244     //-------------------------------------------------    standard end section
 245         debuggee.resume();
 246         pipe.println("quit");
 247         log2("waiting for the debuggee to finish ...");
 248         debuggee.waitFor();
 249 
 250         int status = debuggee.getStatus();
 251         if (status != PASSED + PASS_BASE) {
 252             log3("debuggee returned UNEXPECTED exit status: " +
 253                     status + " != PASS_BASE");
 254             testExitCode = FAILED;
 255         } else {
 256             log2("debuggee returned expected exit status: " +
 257                     status + " == PASS_BASE");
 258         }
 259 
 260         if (testExitCode != PASSED) {
 261             System.out.println("TEST FAILED");
 262         }
 263         return testExitCode;
 264     }
 265 }