Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/exceptionRequests/excreq002.java
41161 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.EventRequestManager.exceptionRequests;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* EventRequestManager. <BR>39* <BR>40* The test checks that results of the method <BR>41* <code>com.sun.jdi.EventRequestManager.exceptionRequests()</code> <BR>42* complies with its spec. <BR>43* <BR>44* The test checks up on the following assertion: <BR>45* - The list is unmodifiable. <BR>46* - The list of the enabled and disabled exception requests. <BR>47* - This list is changes as requests are added and deleted. <BR>48* <BR>49* The test has three phases and works as follows. <BR>50* <BR>51* In first phase, <BR>52* upon launching debuggee's VM which will be suspended, <BR>53* a debugger waits for the VMStartEvent within a predefined <BR>54* time interval. If no the VMStartEvent received, the test is FAILED. <BR>55* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>56* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>57* and waits for the event within the predefined time interval. <BR>58* If no the ClassPrepareEvent received, the test is FAILED. <BR>59* Upon getting the ClassPrepareEvent, <BR>60* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>61* within debuggee's special methodForCommunication(). <BR>62* <BR>63* In second phase to check the assertion, <BR>64* the debugger and the debuggee perform the following. <BR>65* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>66* - The debuggee prepares the check case and invokes <BR>67* the methodForCommunication to be suspended and <BR>68* to inform the debugger with the event. <BR>69* - Upon getting the BreakpointEvent, <BR>70* the debugger checking up on assertions. <BR>71* <BR>72* In third phase, at the end of the test, the debuggee changes <BR>73* the value of the "instruction" which the debugger and debuggee <BR>74* use to inform each other of needed actions, and both end. <BR>75* <BR>76*/7778public class excreq002 extends JDIBase {7980public static void main (String argv[]) {8182int result = run(argv, System.out);8384System.exit(result + PASS_BASE);85}8687public static int run (String argv[], PrintStream out) {8889int exitCode = new excreq002().runThis(argv, out);9091if (exitCode != PASSED) {92System.out.println("TEST FAILED");93}94return testExitCode;95}9697// ************************************************ test parameters9899private String debuggeeName =100"nsk.jdi.EventRequestManager.exceptionRequests.excreq002a";101102//====================================================== test program103104105private 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 section253254String fieldName = "testField";255256List requests = null;257ListIterator li = null;258259ExceptionRequest request = null;260ExceptionRequest cuRequests[] = { null, null, null, null, null,261null, null, null, null, null };262int listSize;263int flag;264265266log1(" TESTING BEGINS");267268for (int i = 0; ; i++) {269270vm.resume();271breakpointForCommunication();272273int instruction = ((IntegerValue)274(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();275276if (instruction == 0) {277vm.resume();278break;279}280281log1(":::::: case: # " + i);282283//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part284285ReferenceType refType = (ReferenceType)286debuggeeClass.getValue(debuggeeClass.fieldByName(fieldName)).type();287288log2("......creating ExceptionRequests");289for (int i1 = 0; i1 < cuRequests.length; i1++) {290cuRequests[i1] = eventRManager.createExceptionRequest(refType, false, false);291cuRequests[i1].putProperty("number", String.valueOf(i1));292293log2("......checking up on returned List after creating new ExceptionRequest");294requests = eventRManager.exceptionRequests();295listSize = requests.size();296if ( listSize != (i1 + 1) ) {297testExitCode = FAILED;298log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + (i1 + 1));299}300flag = 0;301li = requests.listIterator();302while (li.hasNext()) {303request = (ExceptionRequest) li.next();304if ( !request.isEnabled() ) {305flag++;306if (flag > 1) {307testExitCode = FAILED;308log3("ERROR: # of disabled requests > 1 : " + flag);309}310if ( !request.getProperty("number").equals(String.valueOf(i1)) ) {311testExitCode = FAILED;312log3("ERROR: in the List, disabled is request expected to be enabled : # == " + i1);313}314} else {315if ( request.getProperty("number").equals(String.valueOf(i1)) ) {316testExitCode = FAILED;317log3("ERROR: in the List, enabled is newly created disabled request : # == " + i1);318}319}320}321322log2(" enabling created ExceptionRequest");323cuRequests[i1].enable();324requests = eventRManager.exceptionRequests();325listSize = requests.size();326if ( listSize != (i1 + 1) ) {327testExitCode = FAILED;328log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + (i1 + 1));329}330331li = requests.listIterator();332while (li.hasNext()) {333request = (ExceptionRequest) li.next();334if ( !request.isEnabled() ) {335testExitCode = FAILED;336log3("ERROR: returned List contains disabled request : " + request);337}338}339340log2(" removing item from the List; UnsupportedOperationException is expected");341try {342requests.remove(i1);343testExitCode = FAILED;344log3("ERROR: NO exception");345} catch ( UnsupportedOperationException e ) {346log2(" UnsupportedOperationException ");347}348}349350log2("......deleting ExceptionRequests");351for (int i2 = cuRequests.length -1; i2 >= 0; i2--) {352eventRManager.deleteEventRequest(cuRequests[i2]);353requests = eventRManager.exceptionRequests();354listSize = requests.size();355if ( listSize != i2 ) {356testExitCode = FAILED;357log3("ERROR: size of returned List is not equal to expected : " + listSize + " != " + i2);358}359log2(" removing item from the List; UnsupportedOperationException is expected");360try {361requests.remove(i2);362testExitCode = FAILED;363log3("ERROR: NO exception");364} catch ( UnsupportedOperationException e ) {365log2(" UnsupportedOperationException ");366}367}368369//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~370}371log1(" TESTING ENDS");372return;373}374375}376377378