Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ExceptionRequest/exception/exception001.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.exception;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.exception()</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.exception() <BR>48* The cases to test include a ReferenceType <BR>49* mirroring NullPointerException, and null. <BR>50* <BR>51* The test has three phases and works as follows. <BR>52* <BR>53* In first phase, <BR>54* upon launching debuggee's VM which will be suspended, <BR>55* a debugger waits for the VMStartEvent within a predefined <BR>56* time interval. If no the VMStartEvent received, the test is FAILED. <BR>57* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>58* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>59* and waits for the event within the predefined time interval. <BR>60* If no the ClassPrepareEvent received, the test is FAILED. <BR>61* Upon getting the ClassPrepareEvent, <BR>62* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>63* within debuggee's special methodForCommunication(). <BR>64* <BR>65* In second phase to check the assertion, <BR>66* the debugger and the debuggee perform the following. <BR>67* - The debugger <BR>68* - gets ReferenceType value mirroring NullPointerException, <BR>69* - sets up two ExceptionRequests with the ReferenceType and null <BR>70* values of the exception arguments, <BR>71* - gets the values of the Request with the method <BR>72* StepRequest.exception(), and <BR>73* - compares the values. <BR>74* <BR>75* In third phase, at the end of the test, the debuggee changes <BR>76* the value of the "instruction" which the debugger and debuggee <BR>77* use to inform each other of needed actions, and both end. <BR>78* <BR>79*/8081public class exception001 extends JDIBase {8283public static void main (String argv[]) {8485int result = run(argv, System.out);8687System.exit(result + PASS_BASE);88}8990public static int run (String argv[], PrintStream out) {9192int exitCode = new exception001().runThis(argv, out);9394if (exitCode != PASSED) {95System.out.println("TEST FAILED");96}97return testExitCode;98}99100// ************************************************ test parameters101102private String debuggeeName =103"nsk.jdi.ExceptionRequest.exception.exception001a";104105//====================================================== test program106107private int runThis (String argv[], PrintStream out) {108109argsHandler = new ArgumentHandler(argv);110logHandler = new Log(out, argsHandler);111Binder binder = new Binder(argsHandler, logHandler);112113waitTime = argsHandler.getWaitTime() * 60000;114115try {116log2("launching a debuggee :");117log2(" " + debuggeeName);118if (argsHandler.verbose()) {119debuggee = binder.bindToDebugee(debuggeeName + " -vbs");120} else {121debuggee = binder.bindToDebugee(debuggeeName);122}123if (debuggee == null) {124log3("ERROR: no debuggee launched");125return FAILED;126}127log2("debuggee launched");128} catch ( Exception e ) {129log3("ERROR: Exception : " + e);130log2(" test cancelled");131return FAILED;132}133134debuggee.redirectOutput(logHandler);135136vm = debuggee.VM();137138eventQueue = vm.eventQueue();139if (eventQueue == null) {140log3("ERROR: eventQueue == null : TEST ABORTED");141vm.exit(PASS_BASE);142return FAILED;143}144145log2("invocation of the method runTest()");146switch (runTest()) {147148case 0 : log2("test phase has finished normally");149log2(" waiting for the debuggee to finish ...");150debuggee.waitFor();151152log2("......getting the debuggee's exit status");153int status = debuggee.getStatus();154if (status != PASS_BASE) {155log3("ERROR: debuggee returned UNEXPECTED exit status: " +156status + " != PASS_BASE");157testExitCode = FAILED;158} else {159log2("......debuggee returned expected exit status: " +160status + " == PASS_BASE");161}162break;163164default : log3("ERROR: runTest() returned unexpected value");165166case 1 : log3("test phase has not finished normally: debuggee is still alive");167log2("......forcing: vm.exit();");168testExitCode = FAILED;169try {170vm.exit(PASS_BASE);171} catch ( Exception e ) {172log3("ERROR: Exception : " + e);173}174break;175176case 2 : log3("test cancelled due to VMDisconnectedException");177log2("......trying: vm.process().destroy();");178testExitCode = FAILED;179try {180Process vmProcess = vm.process();181if (vmProcess != null) {182vmProcess.destroy();183}184} catch ( Exception e ) {185log3("ERROR: Exception : " + e);186}187break;188}189190return testExitCode;191}192193194/*195* Return value: 0 - normal end of the test196* 1 - ubnormal end of the test197* 2 - VMDisconnectedException while test phase198*/199200private int runTest() {201202try {203testRun();204205log2("waiting for VMDeathEvent");206getEventSet();207if (eventIterator.nextEvent() instanceof VMDeathEvent)208return 0;209210log3("ERROR: last event is not the VMDeathEvent");211return 1;212} catch ( VMDisconnectedException e ) {213log3("ERROR: VMDisconnectedException : " + e);214return 2;215} catch ( Exception e ) {216log3("ERROR: Exception : " + e);217return 1;218}219220}221222private void testRun()223throws JDITestRuntimeException, Exception {224225eventRManager = vm.eventRequestManager();226227ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();228cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);229cpRequest.addClassFilter(debuggeeName);230231cpRequest.enable();232vm.resume();233getEventSet();234cpRequest.disable();235236ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();237debuggeeClass = event.referenceType();238239if (!debuggeeClass.name().equals(debuggeeName))240throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");241242log2(" received: ClassPrepareEvent for debuggeeClass");243244String bPointMethod = "methodForCommunication";245String lineForComm = "lineForComm";246247ThreadReference mainThread = debuggee.threadByNameOrThrow("main");248249BreakpointRequest bpRequest = settingBreakpoint(mainThread,250debuggeeClass,251bPointMethod, lineForComm, "zero");252bpRequest.enable();253vm.resume();254255//------------------------------------------------------ testing section256257log1(" TESTING BEGINS");258259EventRequest eventRequest1 = null;260EventRequest eventRequest2 = null;261262String fieldName = "obj";263Value value = null;264265ReferenceType refObject = null;266ReferenceType exception = null;267268for (int i = 0; ; i++) {269270breakpointForCommunication();271272int instruction = ((IntegerValue)273(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();274275if (instruction == 0) {276vm.resume();277break;278}279280log1(":::::: case: # " + i);281282//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part283284switch (i) {285286case 0:287log2("......vm.resume();");288vm.resume();289290log2("......getting: refObject = debuggeeClass.getValue(debuggeeClass.fieldByName(fieldName));");291value = debuggeeClass.getValue(debuggeeClass.fieldByName(fieldName));292log2(" getting: refObject = (ReferenceType) value.type();");293refObject = (ReferenceType) value.type();294295log2("......setting up: eventRequest1 = setting25ExceptionRequest(refObject);");296eventRequest1 = setting25ExceptionRequest(refObject);297298log2("......getting: exception = ((ExceptionRequest) eventRequest1).exception();");299exception = ((ExceptionRequest) eventRequest1).exception();300301log2(" comparing ReferenceType objects");302if ( !exception.equals(refObject) ) {303testExitCode = FAILED;304log3("ERROR: objects are not equal : " + exception);305}306307log2("......setting up: eventRequest2 = setting25ExceptionRequest(null);");308eventRequest2 = setting25ExceptionRequest(null);309310log2("......getting: exception = ((ExceptionRequest) eventRequest2).exception();");311exception = ((ExceptionRequest) eventRequest2).exception();312313log2(" comparing to null");314if ( exception != null ) {315testExitCode = FAILED;316log3("ERROR: exception is not the null : " + exception);317}318319break;320321default:322throw new JDITestRuntimeException("** default case 2 **");323}324325326//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~327}328log1(" TESTING ENDS");329return;330}331332// ============================== test's additional methods333334private ExceptionRequest setting25ExceptionRequest ( ReferenceType refType )335throws JDITestRuntimeException {336try {337log2("......setting up ExceptionRequest:");338339ExceptionRequest340excr = eventRManager.createExceptionRequest(refType, true, true);341342log2(" ExceptionRequest has been set up");343return excr;344} catch ( Exception e ) {345log3("ERROR: ATTENTION: Exception within settingExceptionRequest() : " + e);346log3(" ExceptionRequest HAS NOT BEEN SET UP");347throw new JDITestRuntimeException("** FAILURE to set up ExceptionRequest **");348}349}350351}352353354