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 4420844 4449394
  27  * @summary Checks that no events are sent after VMDeath, and test vm.canBeModified
  28  * @author Robert Field
  29  *
  30  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  31  * @run compile -g HelloWorld.java
  32  * @run build VMDeathLastTest
  33  * @run driver VMDeathLastTest
  34  */
  35 import com.sun.jdi.*;
  36 import com.sun.jdi.event.*;
  37 import com.sun.jdi.request.*;
  38 
  39 import java.util.*;
  40 
  41     /********** test program **********/
  42 
  43 public class VMDeathLastTest extends TestScaffold {
  44     Object syncer = new Object();
  45     boolean vmDead = false;
  46     boolean disconnected = false;
  47 
  48     VMDeathLastTest (String args[]) {
  49         super(args);
  50     }
  51 
  52     public static void main(String[] args)      throws Exception {
  53         new VMDeathLastTest(args).startTests();
  54     }
  55 
  56     /********** event handlers **********/
  57 
  58     public void methodEntered(MethodEntryEvent event) {
  59         if (vmDead) {
  60             failure("Failure: Got MethodEntryEvent after VM Dead");
  61         }
  62     }
  63 
  64     public void classPrepared(ClassPrepareEvent event) {
  65         if (vmDead) {
  66             failure("Failure: Got ClassPrepareEvent after VM Dead");
  67         }
  68     }
  69 
  70     public void threadDied(ThreadDeathEvent event) {
  71         if (vmDead) {
  72             failure("Failure: Got ThreadDeathEvent after VM Dead");
  73         }
  74     }
  75 
  76     public void vmDied(VMDeathEvent event) {
  77         println("Got VMDeathEvent");
  78         vmDead = true;
  79     }
  80 
  81     public void vmDisconnected(VMDisconnectEvent event) {
  82         println("Got VMDisconnectEvent");
  83         if (!vmDead) {
  84             failure("Test failure: didn't get VMDeath");
  85         }
  86         disconnected = true;
  87         synchronized (syncer) {
  88             syncer.notifyAll();
  89         }
  90     }
  91 
  92     /**
  93      * Turn off default VMDeath handling.
  94      */
  95     protected void createDefaultVMDeathRequest() {
  96     }
  97 
  98     /********** test core **********/
  99 
 100     protected void runTests() throws Exception {
 101         /*
 102          * Get to the top of main()
 103          * to determine targetClass and mainThread
 104          */
 105         startToMain("HelloWorld");
 106         if (!vm().canBeModified()) {
 107             failure("VM says it is read-only");
 108         }
 109         EventRequestManager erm = vm().eventRequestManager();
 110 
 111         /*
 112          * Set event requests
 113          */
 114         erm.createMethodEntryRequest().enable();
 115         erm.createClassPrepareRequest().enable();
 116         erm.createThreadDeathRequest().enable();
 117 
 118         /*
 119          * resume the target listening for events
 120          */
 121         addListener(this);
 122         synchronized (syncer) {
 123             vm().resume();
 124             while (!disconnected) {
 125                 try {
 126                     syncer.wait();
 127                 } catch (InterruptedException e) {
 128                 }
 129             }
 130         }
 131 
 132         /*
 133          * deal with results of test
 134          */
 135         if (!testFailed) {
 136             println("VMDeathLastTest: passed");
 137         } else {
 138             throw new Exception("VMDeathLastTest: failed");
 139         }
 140     }
 141 }