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