test/hotspot/jtreg/vmTestbase/nsk/jdi/VirtualMachineManager/connectedVirtualMachines/convm003.java
changeset 50018 3ba0d8631f24
child 53869 34906de6c017
equal deleted inserted replaced
50017:88cc95780b6e 50018:3ba0d8631f24
       
     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.VirtualMachineManager.connectedVirtualMachines;
       
    25 
       
    26 import nsk.share.*;
       
    27 import nsk.share.jpda.*;
       
    28 import nsk.share.jdi.*;
       
    29 
       
    30 import com.sun.jdi.*;
       
    31 import com.sun.jdi.connect.*;
       
    32 import java.util.*;
       
    33 import java.io.*;
       
    34 
       
    35 import com.sun.jdi.event.*;
       
    36 import com.sun.jdi.request.*;
       
    37 
       
    38 /**
       
    39  * The test for the implementation of an object of the type     <BR>
       
    40  * VirtualMachineManager.                                       <BR>
       
    41  *                                                              <BR>
       
    42  * The test checks up that results of the method                <BR>
       
    43  * <code>com.sun.jdi.VirtualMachineManager.connectedVirtualMachines()</code>                    <BR>
       
    44  * complies with its specification.                             <BR>
       
    45  * <BR>
       
    46  * Test case includes only one debuggee.                        <BR>
       
    47  * The test checks up that :                                    <BR>
       
    48  * - after launching debugged Virtual Machine the tested method <BR>
       
    49  *   returns List containing one element of                     <BR>
       
    50  *   a VirtualMachine type which is equal to a mirror of        <BR>
       
    51  *   the debuggee already got after launching;                  <BR>
       
    52  * - after a debugger invalidates the debuggee                  <BR>
       
    53  *   with the method VirtualMachine.dispose(),                  <BR>
       
    54  *   the call to the method returns an empty List.              <BR>
       
    55  */
       
    56 
       
    57 public class convm003 {
       
    58 
       
    59     //----------------------------------------------------- templete section
       
    60     static final int PASSED = 0;
       
    61     static final int FAILED = 2;
       
    62     static final int PASS_BASE = 95;
       
    63 
       
    64     //----------------------------------------------------- templete parameters
       
    65     static final String
       
    66     sHeader1 = "\n==> nsk/jdi/VirtualMachineManager/connectedVirtualMachines/convm003  ",
       
    67     sHeader2 = "--> debugger: ",
       
    68     sHeader3 = "##> debugger: ";
       
    69 
       
    70     //----------------------------------------------------- main method
       
    71 
       
    72     public static void main (String argv[]) {
       
    73         int result = run(argv, System.out);
       
    74         System.exit(result + PASS_BASE);
       
    75     }
       
    76 
       
    77     public static int run (String argv[], PrintStream out) {
       
    78         return new convm003().runThis(argv, out);
       
    79     }
       
    80 
       
    81     //--------------------------------------------------   log procedures
       
    82 
       
    83     private static Log  logHandler;
       
    84 
       
    85     private static void log1(String message) {
       
    86         logHandler.display(sHeader1 + message);
       
    87     }
       
    88     private static void log2(String message) {
       
    89         logHandler.display(sHeader2 + message);
       
    90     }
       
    91     private static void log3(String message) {
       
    92         logHandler.complain(sHeader3 + message);
       
    93     }
       
    94 
       
    95     //  ************************************************    test parameters
       
    96 
       
    97     private String debuggeeName =
       
    98         "nsk.jdi.VirtualMachineManager.connectedVirtualMachines.convm003a";
       
    99 
       
   100     //String mName = "nsk.jdi.VirtualMachineManager.connectedVirtualMachines";
       
   101 
       
   102     //====================================================== test program
       
   103     //------------------------------------------------------ common section
       
   104 
       
   105     static ArgumentHandler      argsHandler;
       
   106 
       
   107     static int waitTime;
       
   108 
       
   109     static VirtualMachine      vm            = null;
       
   110 
       
   111     static EventRequestManager eventRManager = null;
       
   112     static EventQueue          eventQueue    = null;
       
   113     static EventSet            eventSet      = null;
       
   114 
       
   115     static int  testExitCode = PASSED;
       
   116 
       
   117     static final int returnCode0 = 0;
       
   118     static final int returnCode1 = 1;
       
   119     static final int returnCode2 = 2;
       
   120     static final int returnCode3 = 3;
       
   121     static final int returnCode4 = 4;
       
   122 
       
   123     //------------------------------------------------------ methods
       
   124 
       
   125     private int runThis (String argv[], PrintStream out) {
       
   126 
       
   127         Debugee debuggee;
       
   128 
       
   129         argsHandler     = new ArgumentHandler(argv);
       
   130         logHandler      = new Log(out, argsHandler);
       
   131         Binder binder   = new Binder(argsHandler, logHandler);
       
   132 
       
   133         if (argsHandler.verbose()) {
       
   134             debuggee = binder.bindToDebugee(debuggeeName + " -vbs");
       
   135         } else {
       
   136             debuggee = binder.bindToDebugee(debuggeeName);
       
   137         }
       
   138 
       
   139         waitTime = argsHandler.getWaitTime();
       
   140 
       
   141 
       
   142         IOPipe pipe     = new IOPipe(debuggee);
       
   143 
       
   144         debuggee.redirectStderr(out);
       
   145         log2("issuspended002a debuggee launched");
       
   146         debuggee.resume();
       
   147 
       
   148         String line = pipe.readln();
       
   149         if ((line == null) || !line.equals("ready")) {
       
   150             log3("signal received is not 'ready' but: " + line);
       
   151             return FAILED;
       
   152         } else {
       
   153             log2("'ready' recieved");
       
   154         }
       
   155 
       
   156         vm = debuggee.VM();
       
   157 
       
   158     //------------------------------------------------------  testing section
       
   159         log1("      TESTING BEGINS");
       
   160 
       
   161         VirtualMachineManager vmm = null;
       
   162 
       
   163         List connectedVM;
       
   164         VirtualMachine vm1 = null;
       
   165 
       
   166         int size = 0;
       
   167 
       
   168 
       
   169         //for (int i = 0; ; i++) {
       
   170 
       
   171         label0 :
       
   172         {
       
   173             pipe.println("newcheck");
       
   174             line = pipe.readln();
       
   175 
       
   176             if (line.equals("checkend")) {
       
   177                 log2("     : returned string is 'checkend'");
       
   178                 break label0;
       
   179             } else if (!line.equals("checkready")) {
       
   180                 log3("ERROR: returned string is not 'checkready'");
       
   181                 testExitCode = FAILED;
       
   182                 break label0;
       
   183             }
       
   184 
       
   185             //log1("new checkready: #" + i);
       
   186 
       
   187             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part
       
   188             log2("......call to Bootstrap.virtualMachineManager()");
       
   189 
       
   190             vmm = Bootstrap.virtualMachineManager();
       
   191             if (vmm == null) {
       
   192                 log3("ERROR: null returned");
       
   193                 testExitCode = FAILED;
       
   194             } else {
       
   195 
       
   196                 log2("......call to vmm.connectedVirtualMachines()");
       
   197                 connectedVM = vmm.connectedVirtualMachines();
       
   198 
       
   199                 size = connectedVM.size();
       
   200                 if (size != 1) {
       
   201                     log3("ERROR: connectedVM.size() != 1 : " + size);
       
   202                     testExitCode = FAILED;
       
   203                 } else {
       
   204                     log2("       connectedVM.size() == 1");
       
   205                     log2("......getting: VirtualMachine vm1 = connectedVM.get(0);");
       
   206                     vm1 = (VirtualMachine) connectedVM.get(0);
       
   207 
       
   208                     log2("......comparing: vm.equals(vm1)");
       
   209                     if (!vm.equals(vm1)) {
       
   210                         log3("ERROR: !vm.equals(vm1)");
       
   211                         testExitCode = FAILED;
       
   212                     }
       
   213                 }
       
   214             }
       
   215 
       
   216             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
   217         }
       
   218         //log1("      TESTING ENDS");
       
   219 
       
   220     //--------------------------------------------------   test summary section
       
   221     //-------------------------------------------------    standard end section
       
   222 
       
   223         eventQueue = vm.eventQueue();
       
   224         eventSet = null;
       
   225 
       
   226         log2("......vm1.dispose()");
       
   227         vm1.dispose();
       
   228 
       
   229         if (vmDisconnect() == 0 ) {
       
   230             log2("......connectedVM = vmm.connectedVirtualMachines();");
       
   231             connectedVM = vmm.connectedVirtualMachines();
       
   232 
       
   233             size = connectedVM.size();
       
   234             if (size != 0) {
       
   235                 log3("ERROR: connectedVM.size() != 0 : " + size);
       
   236                 testExitCode = FAILED;
       
   237 
       
   238                 log2("......since a returned list is not empty");
       
   239                 log2("......try to get first element and compare to the mirror of the vm tested");
       
   240                 try {
       
   241                     log2("        getting: vm1 = (VirtualMachine) connectedVM.get(0);");
       
   242                     vm1 = (VirtualMachine) connectedVM.get(0);
       
   243 
       
   244                     log2("         vm.name()== " + vm1.name());
       
   245                     if (vm.equals(vm1))
       
   246                         log2("vm.equals(vm1)");
       
   247                     else
       
   248                         log2("!vm.equals(vm1)");
       
   249                 } catch ( Exception e ) {
       
   250                     log2("......failure because of Exception:  " + e);
       
   251                 }
       
   252 
       
   253             } else
       
   254                 log2("       connectedVM.size() == 0");
       
   255         } else {
       
   256             log3("ERROR: threadDeath != 0");
       
   257             testExitCode = FAILED;
       
   258         }
       
   259 
       
   260         log1("      TESTING ENDS");
       
   261 
       
   262         pipe.println("quit");
       
   263         log2("waiting for the debuggee to finish ...");
       
   264         debuggee.waitFor();
       
   265 
       
   266         int status = debuggee.getStatus();
       
   267         if (status != PASSED + PASS_BASE) {
       
   268             log3("debuggee returned UNEXPECTED exit status: " +
       
   269                     status + " != PASS_BASE");
       
   270             testExitCode = FAILED;
       
   271         } else {
       
   272             log2("debuggee returned expected exit status: " +
       
   273                     status + " == PASS_BASE");
       
   274         }
       
   275 
       
   276         if (testExitCode != PASSED) {
       
   277             logHandler.complain("TEST FAILED");
       
   278         }
       
   279         return testExitCode;
       
   280     }
       
   281 
       
   282 
       
   283     private int vmDisconnect () {
       
   284 
       
   285         int returnCode = returnCode0;
       
   286 
       
   287         log2("       waiting for VMDisconnectEvent");
       
   288 
       
   289         labelBP:
       
   290             for (;;) {
       
   291 
       
   292                 log2("       new:  eventSet = eventQueue.remove();");
       
   293                 try {
       
   294                     eventSet = eventQueue.remove(waitTime*60000);
       
   295                     if (eventSet == null) {
       
   296                         log3("ERROR:  timeout for waiting for a ThreadDeathEvent");
       
   297                         returnCode = returnCode3;
       
   298                         break labelBP;
       
   299                     }
       
   300                 } catch ( Exception e ) {
       
   301                     log3("ERROR: Exception for  eventSet = eventQueue.remove(); : " + e);
       
   302                     returnCode = 1;
       
   303                     break labelBP;
       
   304                 }
       
   305 
       
   306                 if (eventSet != null) {
       
   307 
       
   308                     log2("     :  eventSet != null;  size == " + eventSet.size());
       
   309 
       
   310                     EventIterator eIter = eventSet.eventIterator();
       
   311                     Event         ev    = null;
       
   312 
       
   313                     for (; eIter.hasNext(); ) {
       
   314 
       
   315                         if (returnCode != returnCode0)
       
   316                             break;
       
   317 
       
   318                         ev = eIter.nextEvent();
       
   319 
       
   320                     ll: for (int ifor =0;  ; ifor++) {
       
   321 
       
   322                         try {
       
   323                           switch (ifor) {
       
   324 
       
   325                           case 0:  AccessWatchpointEvent awe = (AccessWatchpointEvent) ev;
       
   326                                    log2("      AccessWatchpointEvent removed");
       
   327                                    break ll;
       
   328                           case 1:  BreakpointEvent be = (BreakpointEvent) ev;
       
   329                                    log2("      BreakpointEvent removed");
       
   330                                    break ll;
       
   331                           case 2:  ClassPrepareEvent cpe = (ClassPrepareEvent) ev;
       
   332                                    log2("      ClassPreparEvent removed");
       
   333                                    break ll;
       
   334                           case 3:  ClassUnloadEvent cue = (ClassUnloadEvent) ev;
       
   335                                    log2("      ClassUnloadEvent removed");
       
   336                                    break ll;
       
   337                           case 4:  ExceptionEvent ee = (ExceptionEvent) ev;
       
   338                                    log2("      ExceptionEvent removed");
       
   339                                    break ll;
       
   340                           case 5:  MethodEntryEvent mene = (MethodEntryEvent) ev;
       
   341                                    log2("      MethodEntryEvent removed");
       
   342                                    break ll;
       
   343                           case 6:  MethodExitEvent mexe = (MethodExitEvent) ev;
       
   344                                    log2("      MethodExiEvent removed");
       
   345                                    break ll;
       
   346                           case 7:  ModificationWatchpointEvent mwe = (ModificationWatchpointEvent) ev;
       
   347                                    log2("      ModificationWatchpointEvent removed");
       
   348                                    break ll;
       
   349                           case 8:  StepEvent se = (StepEvent) ev;
       
   350                                    log2("      StepEvent removed");
       
   351                                    break ll;
       
   352                           case 9:  ThreadDeathEvent tde = (ThreadDeathEvent) ev;
       
   353                                    log2("      ThreadDeathEvent removed");
       
   354                                    break ll;
       
   355                           case 10: ThreadStartEvent tse = (ThreadStartEvent) ev;
       
   356                                    log2("      ThreadStartEvent removed");
       
   357                                    break ll;
       
   358                           case 11: VMDeathEvent vmde = (VMDeathEvent) ev;
       
   359                                    log2("      VMDeathEvent removed");
       
   360                                    break ll;
       
   361                           case 12: VMStartEvent vmse = (VMStartEvent) ev;
       
   362                                    log2("      VMStartEvent removed");
       
   363                                    break ll;
       
   364                           case 13: WatchpointEvent we = (WatchpointEvent) ev;
       
   365                                    log2("      WatchpointEvent removed");
       
   366                                    break ll;
       
   367 
       
   368                           case 14: VMDisconnectEvent wmde = (VMDisconnectEvent) ev;
       
   369                                    log2("      VMDisconnectEvent removed");
       
   370                                    break labelBP;
       
   371 
       
   372                           default: log3("ERROR:  default case for casting event");
       
   373                                    returnCode = returnCode3;
       
   374                                    break ll;
       
   375                           } // switch
       
   376                         } catch ( ClassCastException e ) {
       
   377                         }   // try
       
   378                     }       // ll: for (int ifor =0;  ; ifor++)
       
   379                 }           // for (; ev.hasNext(); )
       
   380             }
       
   381         }
       
   382         if (returnCode == returnCode0)
       
   383             log2("     :  eventSet == null:  EventQueue is empty");
       
   384 
       
   385         return returnCode;
       
   386     }
       
   387 
       
   388 }