Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ExceptionRequest/notifyUncaught/notifyuncaught001.java
41162 views
/*1* Copyright (c) 2001, 2020, 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*/2223package nsk.jdi.ExceptionRequest.notifyUncaught;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829import com.sun.jdi.*;30import com.sun.jdi.event.*;31import com.sun.jdi.request.*;3233import java.util.*;34import java.io.*;3536/**37* The test for the implementation of an object of the type <BR>38* ExceptionRequest. <BR>39* <BR>40* The test checks that results of the method <BR>41* <code>com.sun.jdi.ExceptionRequest.notifyUncaught()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks if a boolean value, an argument of the method <BR>45* EventRequestManager.createExceptionRequest(), <BR>46* is equal to one returned by the method <BR>47* ExceptionRequest.notifyUncaught() <BR>48* The cases to check include both true and false. <BR>49* <BR>50* The test has three phases and works as follows. <BR>51* <BR>52* In first phase, <BR>53* upon launching debuggee's VM which will be suspended, <BR>54* a debugger waits for the VMStartEvent within a predefined <BR>55* time interval. If no the VMStartEvent received, the test is FAILED. <BR>56* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>57* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>58* and waits for the event within the predefined time interval. <BR>59* If no the ClassPrepareEvent received, the test is FAILED. <BR>60* Upon getting the ClassPrepareEvent, <BR>61* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>62* within debuggee's special methodForCommunication(). <BR>63* <BR>64* In second phase to check the assertion, <BR>65* the debugger and the debuggee perform the following. <BR>66* - The debugger <BR>67* - sets up two ExceptionRequests with true and false values of <BR>68* the notifyUncaught argument, <BR>69* - gets the values of the Request with the method <BR>70* StepRequest.notifyUncaught(), and <BR>71* - compares the values. <BR>72* <BR>73* In third phase, at the end of the test, the debuggee changes <BR>74* the value of the "instruction" which the debugger and debuggee <BR>75* use to inform each other of needed actions, and both end. <BR>76* <BR>77*/7879public class notifyuncaught001 extends JDIBase {8081public static void main (String argv[]) {8283int result = run(argv, System.out);8485System.exit(result + PASS_BASE);86}8788public static int run (String argv[], PrintStream out) {8990int exitCode = new notifyuncaught001().runThis(argv, out);9192if (exitCode != PASSED) {93System.out.println("TEST FAILED");94}95return testExitCode;96}9798// ************************************************ test parameters99100private String debuggeeName =101"nsk.jdi.ExceptionRequest.notifyUncaught.notifyuncaught001a";102103//====================================================== test program104105private int runThis (String argv[], PrintStream out) {106107argsHandler = new ArgumentHandler(argv);108logHandler = new Log(out, argsHandler);109Binder binder = new Binder(argsHandler, logHandler);110111waitTime = argsHandler.getWaitTime() * 60000;112113try {114log2("launching a debuggee :");115log2(" " + debuggeeName);116if (argsHandler.verbose()) {117debuggee = binder.bindToDebugee(debuggeeName + " -vbs");118} else {119debuggee = binder.bindToDebugee(debuggeeName);120}121if (debuggee == null) {122log3("ERROR: no debuggee launched");123return FAILED;124}125log2("debuggee launched");126} catch ( Exception e ) {127log3("ERROR: Exception : " + e);128log2(" test cancelled");129return FAILED;130}131132debuggee.redirectOutput(logHandler);133134vm = debuggee.VM();135136eventQueue = vm.eventQueue();137if (eventQueue == null) {138log3("ERROR: eventQueue == null : TEST ABORTED");139vm.exit(PASS_BASE);140return FAILED;141}142143log2("invocation of the method runTest()");144switch (runTest()) {145146case 0 : log2("test phase has finished normally");147log2(" waiting for the debuggee to finish ...");148debuggee.waitFor();149150log2("......getting the debuggee's exit status");151int status = debuggee.getStatus();152if (status != PASS_BASE) {153log3("ERROR: debuggee returned UNEXPECTED exit status: " +154status + " != PASS_BASE");155testExitCode = FAILED;156} else {157log2("......debuggee returned expected exit status: " +158status + " == PASS_BASE");159}160break;161162default : log3("ERROR: runTest() returned unexpected value");163164case 1 : log3("test phase has not finished normally: debuggee is still alive");165log2("......forcing: vm.exit();");166testExitCode = FAILED;167try {168vm.exit(PASS_BASE);169} catch ( Exception e ) {170log3("ERROR: Exception : " + e);171}172break;173174case 2 : log3("test cancelled due to VMDisconnectedException");175log2("......trying: vm.process().destroy();");176testExitCode = FAILED;177try {178Process vmProcess = vm.process();179if (vmProcess != null) {180vmProcess.destroy();181}182} catch ( Exception e ) {183log3("ERROR: Exception : " + e);184}185break;186}187188return testExitCode;189}190191192/*193* Return value: 0 - normal end of the test194* 1 - ubnormal end of the test195* 2 - VMDisconnectedException while test phase196*/197198private int runTest() {199200try {201testRun();202203log2("waiting for VMDeathEvent");204getEventSet();205if (eventIterator.nextEvent() instanceof VMDeathEvent)206return 0;207208log3("ERROR: last event is not the VMDeathEvent");209return 1;210} catch ( VMDisconnectedException e ) {211log3("ERROR: VMDisconnectedException : " + e);212return 2;213} catch ( Exception e ) {214log3("ERROR: Exception : " + e);215return 1;216}217218}219220private void testRun()221throws JDITestRuntimeException, Exception {222223eventRManager = vm.eventRequestManager();224225ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();226cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);227cpRequest.addClassFilter(debuggeeName);228229cpRequest.enable();230vm.resume();231getEventSet();232cpRequest.disable();233234ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();235debuggeeClass = event.referenceType();236237if (!debuggeeClass.name().equals(debuggeeName))238throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");239240log2(" received: ClassPrepareEvent for debuggeeClass");241242String bPointMethod = "methodForCommunication";243String lineForComm = "lineForComm";244245ThreadReference mainThread = debuggee.threadByNameOrThrow("main");246247BreakpointRequest bpRequest = settingBreakpoint(mainThread,248debuggeeClass,249bPointMethod, lineForComm, "zero");250bpRequest.enable();251252//------------------------------------------------------ testing section253254log1(" TESTING BEGINS");255256EventRequest eventRequest1 = null;257EventRequest eventRequest2 = null;258259boolean bool;260261262for (int i = 0; ; i++) {263264vm.resume();265266log1(":::::: case: # " + i);267268//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part269270switch (i) {271272case 0:273274275log2("......setting up: eventRequest1 = setting24ExceptionRequest(true, true);");276eventRequest1 = setting24ExceptionRequest(true, true);277278log2("......getting: bool = ((ExceptionRequest) eventRequest1).notifyUncaught();");279bool = ((ExceptionRequest) eventRequest1).notifyUncaught();280281log2(" compareing boolean values");282if (bool != true) {283testExitCode = FAILED;284log3("ERROR: values are not equal : false and true");285}286287log2("......setting up: eventRequest2 = setting24ExceptionRequest(true, false);");288eventRequest2 = setting24ExceptionRequest(true, false);289290log2("......getting: bool = ((ExceptionRequest) eventRequest2).notifyUncaught();");291bool = ((ExceptionRequest) eventRequest2).notifyUncaught();292293log2(" compareing boolean values");294if (bool != false) {295testExitCode = FAILED;296log3("ERROR: values are not equal : true and false");297}298299break;300301default:302throw new JDITestRuntimeException("** default case 2 **");303}304305breakpointForCommunication();306307int instruction = ((IntegerValue)308(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();309310if (instruction == 0) {311vm.resume();312break;313}314315//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~316}317log1(" TESTING ENDS");318return;319}320321// ============================== test's additional methods322323private ExceptionRequest setting24ExceptionRequest ( boolean notifyCaught,324boolean notifyUnaught )325throws JDITestRuntimeException {326try {327log2("......setting up ExceptionRequest:");328329ExceptionRequest330excr = eventRManager.createExceptionRequest(null, notifyCaught, notifyUnaught);331332log2(" ExceptionRequest has been set up");333return excr;334} catch ( Exception e ) {335log3("ERROR: ATTENTION: Exception within settingExceptionRequest() : " + e);336log3(" ExceptionRequest HAS NOT BEEN SET UP");337throw new JDITestRuntimeException("** FAILURE to set up ExceptionRequest **");338}339}340341}342343344