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