Path: blob/master/test/jdk/com/sun/jdi/AfterThreadDeathTest.java
41149 views
/*1* Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 436467126* @summary Creating a StepRequest on a nonexistant thread fails27* @author jjh28*29* @run build TestScaffold VMConnection TargetListener TargetAdapter30* @run compile -g AfterThreadDeathTest.java31* @run driver AfterThreadDeathTest32*/33import com.sun.jdi.*;34import com.sun.jdi.event.*;35import com.sun.jdi.request.*;3637import java.util.*;3839/********** target program **********/4041class AfterDeathTarg {42public static void main(String[] args){43System.out.println("Howdy!");44System.out.println("Goodbye from AfterDeathTarg!");45}46}4748/********** test program **********/4950public class AfterThreadDeathTest extends TestScaffold {51ReferenceType targetClass;52ThreadReference mainThread;53StepRequest stepRequest = null;54EventRequestManager erm;55boolean mainIsDead;5657AfterThreadDeathTest (String args[]) {58super(args);59}6061public static void main(String[] args) throws Exception {62new AfterThreadDeathTest(args).startTests();63}6465/********** event handlers **********/6667public void threadStarted(ThreadStartEvent event) {68println("Got ThreadStartEvent: " + event);6970if (stepRequest != null) {71erm.deleteEventRequest(stepRequest);72stepRequest = null;73println("Deleted stepRequest");74}7576if (mainIsDead) {77// Here is the odd thing about this test; whatever thread this event78// is for, we do a step on the mainThread. If the mainThread is79// already dead, we should get the exception. Note that we don't80// come here for the start of the main thread.81stepRequest = erm.createStepRequest(mainThread,82StepRequest.STEP_LINE,83StepRequest.STEP_OVER);84stepRequest.addCountFilter(1);85stepRequest.setSuspendPolicy (EventRequest.SUSPEND_ALL);86try {87stepRequest.enable();88} catch (IllegalThreadStateException ee) {89println("Ok; got expected IllegalThreadStateException");90return;91} catch (Exception ee) {92failure("FAILED: Did not get expected"93+ " IllegalThreadStateException"94+ " on a StepRequest.enable(). \n"95+ " Got this exception instead: " + ee);96return;97}98failure("FAILED: Did not get expected IllegalThreadStateException"99+ " on a StepRequest.enable()");100}101}102103public void threadDied(ThreadDeathEvent event) {104println("Got ThreadDeathEvent: " + event);105if (! mainIsDead) {106if (mainThread.equals(event.thread())) {107mainIsDead = true;108}109}110}111112public void vmDied(VMDeathEvent event) {113println("Got VMDeathEvent");114}115116public void vmDisconnected(VMDisconnectEvent event) {117println("Got VMDisconnectEvent");118}119120/********** test core **********/121122protected void runTests() throws Exception {123/*124* Get to the top of main()125* to determine targetClass and mainThread126*/127BreakpointEvent bpe = startToMain("AfterDeathTarg");128targetClass = bpe.location().declaringType();129mainThread = bpe.thread();130erm = vm().eventRequestManager();131132/*133* Set event requests134*/135ThreadStartRequest request = erm.createThreadStartRequest();136request.setSuspendPolicy(EventRequest.SUSPEND_ALL);137request.enable();138139ThreadDeathRequest request1 = erm.createThreadDeathRequest();140request1.setSuspendPolicy(EventRequest.SUSPEND_NONE);141request1.enable();142143/*144* resume the target listening for events145*/146listenUntilVMDisconnect();147148/*149* deal with results of test150* if anything has called failure("foo") testFailed will be true151*/152if (!testFailed) {153println("AfterThreadDeathTest: passed");154} else {155throw new Exception("AfterThreadDeathTest: failed");156}157}158}159160161