1 /*
   2  * Copyright (c) 2001, 2015, 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 /**
  25  *  @test
  26  *  @bug 4419314
  27  *  @author Robert Field
  28  *
  29  *  @modules jdk.jdi
  30  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  31  *  @run compile -g HelloWorld.java
  32  *  @run build VMDeathRequestTest
  33  *  @run driver VMDeathRequestTest
  34  *
  35  * @summary VMDeathRequestTest checks to see that
  36  * VMDisconnectedException is never thrown before VMDisconnectEvent.
  37  *
  38  * Failure mode for this test is throwing VMDisconnectedException
  39  * on vm.eventQueue().remove();
  40  * Does not use a scaffold since we don't want that hiding the exception.
  41  */
  42 import com.sun.jdi.*;
  43 import com.sun.jdi.event.*;
  44 import com.sun.jdi.request.*;
  45 
  46 import java.util.*;
  47 
  48 
  49     /********** test program **********/
  50 
  51 public class VMDeathRequestTest extends TestScaffold {
  52     boolean requestedVMDeathOccurred = false;
  53     boolean defaultVMDeathOccurred = false;
  54     Object syncer = new Object();
  55     boolean disconnected = false;
  56     VMDeathRequest deathRequest;
  57     EventSet currentEventSet;
  58 
  59     VMDeathRequestTest (String args[]) {
  60         super(args);
  61     }
  62 
  63     public static void main(String[] args)      throws Exception {
  64         new VMDeathRequestTest(args).startTests();
  65     }
  66 
  67     /********** event handlers **********/
  68 
  69     public void eventSetReceived(EventSet set) {
  70         currentEventSet = set;
  71     }
  72 
  73     public void vmDied(VMDeathEvent event) {
  74         if (event.request() == deathRequest) {
  75             requestedVMDeathOccurred = true;
  76             println("Got requested VMDeathEvent");
  77             if (currentEventSet.suspendPolicy() !=
  78                                    EventRequest.SUSPEND_ALL) {
  79                 failure("failure: wrong suspend policy");
  80             }
  81         } else if (event.request() == null) {
  82             defaultVMDeathOccurred = true;
  83             println("Got default VMDeathEvent");
  84         } else {
  85             failure("failure: Unexpected type of VMDeathEvent occurred");
  86         }
  87     }
  88 
  89     public void vmDisconnected(VMDisconnectEvent event) {
  90         println("Got VMDisconnectEvent");
  91         disconnected = true;
  92         synchronized (syncer) {
  93             syncer.notifyAll();
  94         }
  95     }
  96 
  97     /**
  98      * Turn off default VMDeath handling
  99      */
 100     protected void createDefaultVMDeathRequest() {
 101     }
 102 
 103     /********** test core **********/
 104 
 105     protected void runTests() throws Exception {
 106 
 107         startToMain("HelloWorld");
 108 
 109         deathRequest = eventRequestManager().createVMDeathRequest();
 110         deathRequest.enable();
 111 
 112         /*
 113          * Static tests
 114          */
 115         List reqs = eventRequestManager().vmDeathRequests();
 116         if (reqs.size() != 1 || reqs.get(0) != deathRequest) {
 117             failure("failure: vmDeathRequests()");
 118         }
 119         if (!vm().canRequestVMDeathEvent()) {
 120             failure("failure: canRequestVMDeathEvent() returned false");
 121         }
 122 
 123         /*
 124          * Event tests
 125          */
 126         addListener(this);
 127         synchronized (syncer) {
 128             vm().resume();
 129             while (!disconnected) {
 130                 try {
 131                     syncer.wait();
 132                 } catch (InterruptedException e) {
 133                 }
 134             }
 135         }
 136 
 137         /*
 138          * Failure analysis
 139          */
 140         if (!requestedVMDeathOccurred) {
 141             failure("failure: didn't get requested VMDeathEvent");
 142         }
 143         if (!defaultVMDeathOccurred) {
 144             failure("failure: didn't get default VMDeathEvent");
 145         }
 146 
 147         if (!testFailed) {
 148             println("VMDeathRequestTest: passed");
 149         } else {
 150             throw new Exception("VMDeathRequestTest: failed");
 151         }
 152     }
 153 }