Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ClassLoaderReference/visibleClasses/visibleclasses001.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.ClassLoaderReference.visibleClasses;2425import nsk.share.*;26import nsk.share.jdi.*;2728import com.sun.jdi.*;29import com.sun.jdi.event.*;30import com.sun.jdi.request.*;3132import java.util.*;33import java.io.*;3435/**36* The test for the implementation of an object of the type <BR>37* ClassLoader. <BR>38* <BR>39* The test checks up that results of the method <BR>40* <code>com.sun.jdi.ClassLoader.visibleClasses()</code> <BR>41* complies with its spec. <BR>42* <BR>43* The test checks up on the following assertion: <BR>44* Returns a list of all classes for which this class loader <BR>45* has been recorded as the initiating loader in the target VM.<BR>46* The list contains ReferenceTypes defined directly by <BR>47* this loader (as returned by definedClasses()) and <BR>48* any types for which loading was delegated by <BR>49* this class loader to another class loader. <BR>50* <BR>51* The case to check includes a class loader with no <BR>52* delegated types, that is, the expected returned value should <BR>53* be equal to one returned by ClassLoader.definedClasses(), <BR>54* except primitive arrays defined by bootstrap class loader <BR>55* and visible. <BR>56* <BR>57* The test has three phases and works as follows. <BR>58* <BR>59* In first phase, <BR>60* upon launching debuggee's VM which will be suspended, <BR>61* a debugger waits for the VMStartEvent within a predefined <BR>62* time interval. If no the VMStartEvent received, the test is FAILED. <BR>63* Upon getting the VMStartEvent, it makes the request for debuggee's <BR>64* ClassPrepareEvent with SUSPEND_EVENT_THREAD, resumes the VM, <BR>65* and waits for the event within the predefined time interval. <BR>66* If no the ClassPrepareEvent received, the test is FAILED. <BR>67* Upon getting the ClassPrepareEvent, <BR>68* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD <BR>69* within debuggee's special methodForCommunication(). <BR>70* <BR>71* In second phase to check the assetion, <BR>72* the debugger and the debuggee perform the following. <BR>73* - The debugger resumes the debuggee and waits for the BreakpointEvent.<BR>74* - The debuggee prepares a ClassLoader object and invokes <BR>75* the methodForCommunication to be suspended and <BR>76* to inform the debugger with the event. <BR>77* - Upon getting the BreakpointEvent, <BR>78* the debugger performs the check. <BR>79* Note. To inform each other of needed actions, the debugger and <BR>80* and the debuggee use debuggeee's variable "instruction". <BR>81* <BR>82* In third phase when at the end, <BR>83* the debuggee changes the value of the "instruction" <BR>84* to inform the debugger of checks finished, and both end. <BR>85* <BR>86*/8788public class visibleclasses001 extends JDIBase {8990public static void main (String argv[]) {9192int result = run(argv, System.out);9394System.exit(result + PASS_BASE);95}9697public static int run (String argv[], PrintStream out) {9899int exitCode = new visibleclasses001().runThis(argv, out);100101if (exitCode != PASSED) {102System.out.println("TEST FAILED");103}104return testExitCode;105}106107// ************************************************ test parameters108109private String debuggeeName =110"nsk.jdi.ClassLoaderReference.visibleClasses.visibleclasses001a";111112//====================================================== test program113114static List<String> primitiveArraysNamesPatterns = Arrays.asList(new String[] {115"^boolean(\\[\\])+", "^byte(\\[\\])+", "^char(\\[\\])+", "^int(\\[\\])+",116"^short(\\[\\])+", "^long(\\[\\])+", "^float(\\[\\])+", "^double(\\[\\])+"});117118private int runThis (String argv[], PrintStream out) {119120argsHandler = new ArgumentHandler(argv);121logHandler = new Log(out, argsHandler);122Binder binder = new Binder(argsHandler, logHandler);123124waitTime = argsHandler.getWaitTime() * 60000;125126try {127log2("launching a debuggee :");128log2(" " + debuggeeName);129if (argsHandler.verbose()) {130debuggee = binder.bindToDebugee(debuggeeName + " -vbs");131} else {132debuggee = binder.bindToDebugee(debuggeeName);133}134if (debuggee == null) {135log3("ERROR: no debuggee launched");136return FAILED;137}138log2("debuggee launched");139} catch ( Exception e ) {140log3("ERROR: Exception : " + e);141log2(" test cancelled");142return FAILED;143}144145debuggee.redirectOutput(logHandler);146147vm = debuggee.VM();148149eventQueue = vm.eventQueue();150if (eventQueue == null) {151log3("ERROR: eventQueue == null : TEST ABORTED");152vm.exit(PASS_BASE);153return FAILED;154}155156log2("invocation of the method runTest()");157switch (runTest()) {158159case 0 : log2("test phase has finished normally");160log2(" waiting for the debuggee to finish ...");161debuggee.waitFor();162163log2("......getting the debuggee's exit status");164int status = debuggee.getStatus();165if (status != PASS_BASE) {166log3("ERROR: debuggee returned UNEXPECTED exit status: " +167status + " != PASS_BASE");168testExitCode = FAILED;169} else {170log2("......debuggee returned expected exit status: " +171status + " == PASS_BASE");172}173break;174175default : log3("ERROR: runTest() returned unexpected value");176177case 1 : log3("test phase has not finished normally: debuggee is still alive");178log2("......forcing: vm.exit();");179testExitCode = FAILED;180try {181vm.exit(PASS_BASE);182} catch ( Exception e ) {183log3("ERROR: Exception : e");184}185break;186187case 2 : log3("test cancelled due to VMDisconnectedException");188log2("......trying: vm.process().destroy();");189testExitCode = FAILED;190try {191Process vmProcess = vm.process();192if (vmProcess != null) {193vmProcess.destroy();194}195} catch ( Exception e ) {196log3("ERROR: Exception : e");197}198break;199}200201return testExitCode;202}203204205/*206* Return value: 0 - normal end of the test207* 1 - ubnormal end of the test208* 2 - VMDisconnectedException while test phase209*/210211private int runTest() {212213try {214testRun();215216log2("waiting for VMDeathEvent");217getEventSet();218if (eventIterator.nextEvent() instanceof VMDeathEvent)219return 0;220221log3("ERROR: last event is not the VMDeathEvent");222return 1;223} catch ( VMDisconnectedException e ) {224log3("ERROR: VMDisconnectedException : " + e);225return 2;226} catch ( Exception e ) {227log3("ERROR: Exception : " + e);228return 1;229}230231}232233private void testRun()234throws JDITestRuntimeException, Exception {235236eventRManager = vm.eventRequestManager();237238ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();239cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);240cpRequest.addClassFilter(debuggeeName);241242cpRequest.enable();243vm.resume();244getEventSet();245cpRequest.disable();246247ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();248debuggeeClass = event.referenceType();249250if (!debuggeeClass.name().equals(debuggeeName))251throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");252253log2(" received: ClassPrepareEvent for debuggeeClass");254255String bPointMethod = "methodForCommunication";256String lineForComm = "lineForComm";257BreakpointRequest bpRequest;258259bpRequest = settingBreakpoint(debuggee.threadByNameOrThrow("main"),260debuggeeClass,261bPointMethod, lineForComm, "zero");262bpRequest.enable();263264//------------------------------------------------------ testing section265266log1(" 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: # 0");282283//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part284285log2("......getting: List classes = vm.allClasses();");286List<ReferenceType> classes = vm.allClasses();287log2(" classes.size() == " + classes.size());288289vm.resume();290breakpointForCommunication();291292log1(":::::: case: # 1");293294String ClassLoaderObjName = "classLoader";295296log2("......getting: Value val = debuggeeClass.getValue(debuggeeClass.fieldByName(ClassLoaderObjName));");297Value val = debuggeeClass.getValue(debuggeeClass.fieldByName(ClassLoaderObjName));298299log2("......getting: ClassLoaderReference clRef = (ClassLoaderReference) val;");300ClassLoaderReference clRef = (ClassLoaderReference) val;301302log2("......getting: List definedClasses = clRef.definedClasses();");303List<ReferenceType> definedClasses = clRef.definedClasses();304305log2("......getting: List visibleClasses = clRef.visibleClasses();");306List<ReferenceType> visibleClasses = clRef.visibleClasses();307308log2("......checking up on: visibleClasses.size() == definedClasses.size()");309310if (visibleClasses.size() != definedClasses.size()) {311log2(" : visibleClasses.size() != definedClasses.size()");312log2(" : definedClasses.size() == " + definedClasses.size());313log2(" : visibleClasses.size() == " + visibleClasses.size());314315for (ReferenceType vcl : visibleClasses) {316317int vclIndex = visibleClasses.indexOf(vcl);318String vclName = vcl.name();319320if (primitiveArraysNamesPatterns.stream().anyMatch(vcl.name()::matches)) {321log2(" : visibleClasses[" + vclIndex + "].name() == " + vclName +322" Correct - primitive arrays are visible for class loader");323} else {324log3(" : visibleClasses[" + vclIndex + "].name() == " + vclName);325326classes.stream().filter(cl -> cl.name().equals(vclName))327.forEach(cl -> log3(" : List classes contains an object with the name: " + cl.name()));328329testExitCode = FAILED;330}331}332}333334//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~335}336log1(" TESTING ENDS");337return;338}339340}341342343