Path: blob/master/test/jdk/com/sun/jdi/ExceptionEvents.java
41152 views
/*1* Copyright (c) 2001, 2019, 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 440739726* @key intermittent27* @summary Test the requesting of exception events28* @author Robert Field29*30* @run build TestScaffold VMConnection31* @run compile -g ExceptionEvents.java32*33* @run driver ExceptionEvents N A StackOverflowCaughtTarg java.lang.Exception34* @run driver ExceptionEvents C A StackOverflowCaughtTarg null35* @run driver ExceptionEvents C A StackOverflowCaughtTarg java.lang.Error36* @run driver ExceptionEvents C A StackOverflowCaughtTarg java.lang.StackOverflowError37* @run driver ExceptionEvents N A StackOverflowCaughtTarg java.lang.NullPointerException3839* @run driver ExceptionEvents N T StackOverflowCaughtTarg java.lang.Exception40* @run driver ExceptionEvents C T StackOverflowCaughtTarg null41* @run driver ExceptionEvents C T StackOverflowCaughtTarg java.lang.Error42* @run driver ExceptionEvents C T StackOverflowCaughtTarg java.lang.StackOverflowError43* @run driver ExceptionEvents N T StackOverflowCaughtTarg java.lang.NullPointerException4445* @run driver ExceptionEvents N N StackOverflowCaughtTarg java.lang.Exception46* @run driver ExceptionEvents C N StackOverflowCaughtTarg null47* @run driver ExceptionEvents C N StackOverflowCaughtTarg java.lang.Error48* @run driver ExceptionEvents C N StackOverflowCaughtTarg java.lang.StackOverflowError49* @run driver ExceptionEvents N N StackOverflowCaughtTarg java.lang.NullPointerException5051* @run driver ExceptionEvents N A StackOverflowUncaughtTarg java.lang.Exception52* @run driver ExceptionEvents U A StackOverflowUncaughtTarg null53* @run driver ExceptionEvents U A StackOverflowUncaughtTarg java.lang.Error54* @run driver ExceptionEvents U A StackOverflowUncaughtTarg java.lang.StackOverflowError55* @run driver ExceptionEvents N A StackOverflowUncaughtTarg java.lang.NullPointerException5657* @run driver ExceptionEvents N T StackOverflowUncaughtTarg java.lang.NullPointerException58* @run driver ExceptionEvents N N StackOverflowUncaughtTarg java.lang.NullPointerException5960*/61import com.sun.jdi.*;62import com.sun.jdi.event.*;63import com.sun.jdi.request.*;64import java.util.*;6566class StackOverflowCaughtTarg {67public static void main(String[] args) {68try {69throw new StackOverflowError();70} catch (Throwable exc) {71// ignore72}73}74}7576class StackOverflowUncaughtTarg {77public static void main(String[] args) {78thrower();79}80static void thrower() {81throw new StackOverflowError();82}83}8485class StackOverflowIndirectTarg {86public static void main(String[] args) {87try {88thrower();89} catch (Throwable exc) {90System.out.println("Got exception: " + exc);91}92}93static void thrower() {94throw new StackOverflowError();95}96}9798public class ExceptionEvents extends TestScaffold {99static int failureCount = 0;100static StringBuffer tooManySummary = new StringBuffer();101static StringBuffer notSentSummary = new StringBuffer();102static StringBuffer unexpectedSummary = new StringBuffer();103static int globalSuspendPolicy = -1;104static String[] flags;105106final String target;107final String exceptionName;108final boolean caught;109final boolean uncaught;110final int suspendPolicy;111112int eventCount = 0;113ExceptionRequest request;114115ExceptionEvents(String target,116String exceptionName,117boolean caught, boolean uncaught,118int suspendPolicy) {119super(flags);120this.target = target;121this.exceptionName = exceptionName;122this.caught = caught;123this.uncaught = uncaught;124this.suspendPolicy = suspendPolicy;125}126127static void everything() throws Exception {128goNeither("StackOverflowCaughtTarg", "java.lang.Exception");129goCaught("StackOverflowCaughtTarg", null);130goCaught("StackOverflowCaughtTarg", "java.lang.Error");131goCaught("StackOverflowCaughtTarg", "java.lang.StackOverflowError");132goNeither("StackOverflowCaughtTarg", "java.lang.NullPointerException");133134goNeither("StackOverflowUncaughtTarg", "java.lang.Exception");135goUncaught("StackOverflowUncaughtTarg", null);136goUncaught("StackOverflowUncaughtTarg", "java.lang.Error");137goUncaught("StackOverflowUncaughtTarg", "java.lang.StackOverflowError");138goNeither("StackOverflowUncaughtTarg", "java.lang.NullPointerException");139}140141static void usage() throws Exception {142System.err.println("Use either with no arguments or");143System.err.println(" c|u|n a|t|n <TargetClass> <Exception>|null");144System.err.println("or for verbose folk");145System.err.println(" caught|uncaught|neither all|thread|none <TargetClass> <Exception>|null");146throw new IllegalArgumentException("see usage");147}148149public static void main(String[] args) throws Exception {150StringBuffer testName = new StringBuffer("ExceptionEvents(");151List flagList = new ArrayList();152List argList = new ArrayList();153154for (int i = 0; i < args.length; ++i) {155String arg = args[i];156if (arg.startsWith("-")) {157flagList.add(arg);158} else {159argList.add(arg);160}161}162flags = (String[])flagList.toArray(new String[0]);163args = (String[])argList.toArray(new String[0]);164if (args.length == 0) {165everything();166testName.append("Full Test");167} else if (args.length == 4) {168switch (args[1].toLowerCase().charAt(0)) {169case 'a':170globalSuspendPolicy = EventRequest.SUSPEND_ALL;171break;172case 't':173globalSuspendPolicy = EventRequest.SUSPEND_EVENT_THREAD;174break;175case 'n':176globalSuspendPolicy = EventRequest.SUSPEND_NONE;177break;178default:179usage();180}181String excString = args[3];182if (excString.equals("null")) {183excString = null;184}185switch (args[0].toLowerCase().charAt(0)) {186case 'c':187goCaught(args[2], excString);188break;189case 'u':190goUncaught(args[2], excString);191break;192case 'n':193goNeither(args[2], excString);194break;195default:196usage();197}198testName.append(args[0]);199testName.append(" ");200testName.append(args[1]);201testName.append(" ");202testName.append(args[2]);203} else {204usage();205}206testName.append(")");207208summarize(testName.toString());209}210211static void summarize(String testName) throws Exception {212// final analyse213if (tooManySummary.length() > 0) {214System.out.println("\nSummary of tests with too many events:\n" +215tooManySummary.toString());216}217if (notSentSummary.length() > 0) {218System.out.println("\nSummary of tests with expected events not sent:\n" +219notSentSummary.toString());220}221if (unexpectedSummary.length() > 0) {222System.out.println("\nSummary of tests with events when none expected:\n" +223unexpectedSummary.toString());224}225226if (failureCount > 0) {227throw new Exception(testName + " FAILED " +228failureCount + " tests!");229} else {230System.out.println("\n" + testName + " test PASSED");231}232}233234/**235* Target throws caught exception.236* Events if caught enabled.237*/238static void goCaught(String target,239String exceptionName) throws Exception {240goSuspendPolicy(target, true, exceptionName,241true, true);242goSuspendPolicy(target, true, exceptionName,243true, false);244goSuspendPolicy(target, false, exceptionName,245false, true);246goSuspendPolicy(target, false, exceptionName,247false, false);248}249250/**251* Target throws uncaught exception.252* Events if uncaught enabled.253*/254static void goUncaught(String target,255String exceptionName) throws Exception {256goSuspendPolicy(target, true, exceptionName,257true, true);258goSuspendPolicy(target, true, exceptionName,259false, true);260goSuspendPolicy(target, false, exceptionName,261true, false);262goSuspendPolicy(target, false, exceptionName,263false, false);264}265266/**267* Target doesn't throw named exception. No events.268*/269static void goNeither(String target,270String exceptionName) throws Exception {271goSuspendPolicy(target, false, exceptionName,272true, true);273goSuspendPolicy(target, false, exceptionName,274false, true);275goSuspendPolicy(target, false, exceptionName,276true, false);277goSuspendPolicy(target, false, exceptionName,278false, false);279}280281/**282* Suspend policy should make no difference.283* Iterate over all of them.284*/285static void goSuspendPolicy(String target,286boolean expectedEvent,287String exceptionName,288boolean caught, boolean uncaught) throws Exception {289if (globalSuspendPolicy != -1) {290go(target, expectedEvent, exceptionName,291caught, uncaught, globalSuspendPolicy);292} else {293go(target, expectedEvent, exceptionName,294caught, uncaught, EventRequest.SUSPEND_ALL);295go(target, expectedEvent, exceptionName,296caught, uncaught, EventRequest.SUSPEND_EVENT_THREAD);297go(target, expectedEvent, exceptionName,298caught, uncaught, EventRequest.SUSPEND_NONE);299}300}301302static void go(String target,303boolean expectedEvent,304String exceptionName,305boolean caught, boolean uncaught,306int suspendPolicy) throws Exception {307String description = target + " with " +308exceptionName +309"/caught=" + caught +310"/uncaught=" + uncaught +311" suspend=";312switch (suspendPolicy) {313case EventRequest.SUSPEND_ALL:314description += "All";315break;316case EventRequest.SUSPEND_EVENT_THREAD:317description += "Thread";318break;319case EventRequest.SUSPEND_NONE:320description += "None";321break;322default:323throw new Exception("Test failure: bad suspend policy - " +324suspendPolicy);325}326description += "\n";327328System.out.print("\nTesting " + description);329330ExceptionEvents aRun = new ExceptionEvents(331target,332exceptionName, caught, uncaught,333suspendPolicy);334aRun.startTests();335aRun.analyse(expectedEvent, description);336}337338void analyse(boolean expectedEvent, String description) {339if (expectedEvent) {340if (eventCount == 1) {341println("pass: got expected event");342} else if (eventCount > 1) {343failure("FAILURE: expected only one event got: " +344eventCount);345tooManySummary.append(description);346++failureCount;347} else {348failure("FAILURE: expected event not sent");349notSentSummary.append(description);350++failureCount;351}352} else {353if (eventCount > 0) {354failure("FAILURE: unexpected event sent");355unexpectedSummary.append(description);356++failureCount;357} else {358println("pass: as expected no event sent");359}360}361}362363364/********** event handlers **********/365366public void exceptionThrown(ExceptionEvent event) {367if (event.request() == request) {368try {369System.out.print("ExceptionEvent: ");370System.out.print("" + event.exception().referenceType().name());371Location loc = event.location();372System.out.print(" @ " + loc.method().name());373System.out.print(":" + loc.lineNumber());374} catch (VMDisconnectedException exc) {375System.out.print("Oops - " + exc.toString());376}377System.out.println();378eventCount++;379} else {380System.out.print("alien exception: ");381try {382println(event.toString());383} catch (VMDisconnectedException exc) {384println("Oops - " + exc.toString());385}386}387}388389/**390* Turn off default Exception Handling391*/392protected void createDefaultExceptionRequest() {393}394395/********** test core **********/396397protected void runTests() throws Exception {398/*399* Get to the top of main()400*/401startToMain(target);402403ReferenceType exceptionClass;404405if (exceptionName == null) {406exceptionClass = null;407} else {408exceptionClass = findReferenceType(exceptionName);409if (exceptionName == null) {410throw new Exception("test failure - cannot find: " +411exceptionName);412}413}414415request = eventRequestManager().createExceptionRequest(exceptionClass,416caught, uncaught);417request.addClassExclusionFilter("java.*");418request.addClassExclusionFilter("javax.*");419request.addClassExclusionFilter("sun.*");420request.addClassExclusionFilter("com.sun.*");421request.addClassExclusionFilter("com.oracle.*");422request.addClassExclusionFilter("oracle.*");423request.addClassExclusionFilter("jdk.internal.*");424request.addClassExclusionFilter("jdk.vm.ci.hotspot.*");425request.setSuspendPolicy(suspendPolicy);426request.enable();427428listenUntilVMDisconnect();429}430}431432433