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