Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ClassUnloadEvent/className/classname001.java
41161 views
/*1* Copyright (c) 2001, 2018, 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.ClassUnloadEvent.className;2425import com.sun.jdi.*;26import com.sun.jdi.event.*;27import com.sun.jdi.request.*;2829import java.io.*;30import java.util.List;3132import nsk.share.*;33import nsk.share.jpda.*;34import nsk.share.jdi.*;353637// This class is the debugger application in the test3839public class classname001 {40static final int PASSED = 0;41static final int FAILED = 2;42static final int JCK_STATUS_BASE = 95;4344static final int TIMEOUT_DELTA = 1000; // milliseconds4546static final String COMMAND_READY = "ready";47static final String COMMAND_QUIT = "quit";48static final String COMMAND_LOAD = "load";49static final String COMMAND_LOADED = "loaded";50static final String COMMAND_UNLOAD = "unload";51static final String COMMAND_UNLOADED = "unloaded";5253static final String PREFIX = "nsk.jdi.ClassUnloadEvent.className";54static final String DEBUGGEE_NAME = PREFIX + ".classname001a";55static final String CHECKED_CLASS = PREFIX + ".classname001b";56static final String KLASSLOADER = ClassUnloader.INTERNAL_CLASS_LOADER_NAME;5758static private Debugee debuggee;59static private VirtualMachine vm;60static private IOPipe pipe;61static private Log log;62static private ArgumentHandler argHandler;63static private EventSet eventSet;6465static private ClassUnloadRequest checkedRequest;6667static private long eventTimeout;68static private boolean testFailed;69static private boolean eventReceived;7071public static void main (String args[]) {72System.exit(run(args, System.out) + JCK_STATUS_BASE);73}7475public static int run(final String args[], final PrintStream out) {7677testFailed = false;78eventReceived = false;7980argHandler = new ArgumentHandler(args);81log = new Log(out, argHandler);82eventTimeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds8384// launch debugee8586Binder binder = new Binder(argHandler, log);87log.display("Connecting to debuggee");88debuggee = binder.bindToDebugee(DEBUGGEE_NAME);89debuggee.redirectStderr(log, "classname001a >");9091pipe = debuggee.createIOPipe();92vm = debuggee.VM();9394// create request and wait for ClassUnloadEvent9596try {9798// resume debugee and wait for it becomes ready99log.display("Resuming debuggee");100debuggee.resume();101102log.display("Waiting for command: " + COMMAND_READY);103String command = pipe.readln();104if (command == null || !command.equals(COMMAND_READY)) {105throw new Failure("TEST BUG: unexpected debuggee's command: " + command);106}107108// get mirror of debugee class109ReferenceType rType;110if ((rType = debuggee.classByName(DEBUGGEE_NAME)) == null) {111throw new Failure("TEST BUG: cannot find debuggee's class " + DEBUGGEE_NAME);112}113114// send command to load checked class and waits for a confirmation115pipe.println(COMMAND_LOAD);116log.display("Waiting for checked class is loaded");117command = pipe.readln();118119if (command == null || !command.equals(COMMAND_LOADED)) {120throw new Failure("TEST BUG: unexpected debuggee's command: " + command);121}122123// checked class has been loaded!124log.display("Checked class has been loaded in debuggee!");125126// check that checked class is loaded in debugee127log.display("Finding checked class in debuggee");128if ((rType = debuggee.classByName(CHECKED_CLASS)) == null) {129throw new Failure("TEST BUG: cannot find checked class loaded: " + CHECKED_CLASS);130}131rType = null;132133// check that user-defined classloader is also loaded in debuggee134log.display("Finding user-defined class loader in debuggee");135if ((rType = debuggee.classByName(KLASSLOADER)) == null) {136throw new Failure("TEST BUG: cannot find user-defined classloader loaded: " + KLASSLOADER);137}138rType = null;139140// create request for class unload event141log.display("Creating request for ClassUnloadEvent");142EventRequestManager erManager = vm.eventRequestManager();143if ((checkedRequest = erManager.createClassUnloadRequest()) == null) {144throw new Failure("TEST BUG: unable to create ClassUnloadRequest");145} else {146log.display("ClassUnloadRequest created");147}148// checkedRequest.addClassFilter("nsk.jdi.ClassUnloadEvent.className.*");149150// enable event request151log.display("Enabling event request");152checkedRequest.enable();153154// turn off pipe pinging155pipe.setPingTimeout(0);156157// force checked class to be unloaded from debuggee158log.display("Waiting for checked class is unloaded");159pipe.println(COMMAND_UNLOAD);160command = pipe.readln();161162// end the test if checked class has not been actually unloaded or error occurs163if (command != null && command.equals(COMMAND_LOADED)) {164throw new Warning("TEST INCOMPLETE: unable to unload class");165}166167if (command == null || !command.equals(COMMAND_UNLOADED)) {168throw new Failure("TEST BUG: unexpected debuggee's command: " + command);169}170171// checked class has been unloaded172log.display("Checked class forced to be unloaded from debuggee!");173174// waiting for event until timeout exceeds175log.display("Waiting for ClassUnloadEvent for checked class");176long timeToFinish = System.currentTimeMillis() + eventTimeout;177while (!eventReceived && System.currentTimeMillis() < timeToFinish) {178179// get next event set180eventSet = null;181try {182eventSet = vm.eventQueue().remove(TIMEOUT_DELTA);183} catch (Exception e) {184throw new Failure("TEST INCOMPLETE: Unexpected exception while getting event: " + e);185}186if (eventSet == null)187continue;188189// handle each event from the event set190EventIterator eventIterator = eventSet.eventIterator();191while (eventIterator.hasNext()) {192193Event event = eventIterator.nextEvent();194log.display("\nEvent received:\n " + event);195196// handle ClassUnloadEvent197if (event instanceof ClassUnloadEvent) {198199ClassUnloadEvent castedEvent = (ClassUnloadEvent)event;200log.display("Received event is ClassUnloadEvent:\n " + castedEvent);201202// check that received event is for checked request203EventRequest eventRequest = castedEvent.request();204if (!(checkedRequest.equals(eventRequest))) {205log.complain("FAILURE 1: eventRequest is not equal to checked request");206testFailed = true;207}208209// check that received event is for checked VM210VirtualMachine eventMachine = castedEvent.virtualMachine();211if (!(vm.equals(eventMachine))) {212log.complain("FAILURE 2: eventVirtualMachine is not equal to checked vm");213testFailed = true;214}215216// test the method ClassUnloadEvent.className()217String refName = castedEvent.className();218219// check that received event is for checked class220log.display("ClassUnloadEvent is received for " + refName);221if ((refName == null) || (refName.equals(""))) {222log.complain("FAILURE 3: ClassUnloadEvent.className() returns null or empty string");223testFailed = true;224} else if (refName.equals(CHECKED_CLASS)) {225226// mark that ClassUnloadEvent for checked class received227eventReceived = true;228log.display("Expected ClassUnloadEvent for checked class received!");229230/*231// check that checked class is not included in debuggee's list of loaded classes232List loadedClasses = vm.classesByName(CHECKED_CLASS);233if (loadedClasses != null) {234log.complain("FAILURE 4: ClassUnloadEvent is received for class to be unloaded\n"235+ " but class still presents in the list of all debuggee classes");236testFailed = true;237}238*/239240} else {241242// ClassUnloadEvent for another class received; just display it243List loadedClasses = vm.classesByName(refName);244if (loadedClasses != null) {245log.display("ClassUnloadEvent was received for loaded class " + refName);246}247}248}249250// ignore all other events251}252253log.display("Resuming event set");254eventSet.resume();255}256257log.display("");258259// check that expected event has been received260log.display("Searching checked class in debuggee");261rType = debuggee.classByName(CHECKED_CLASS);262if (rType != null) {263if (eventReceived) {264log.complain("FAILURE 4: ClassUnloadEvent is received for class to be unloaded\n"265+ " but class still presents in the list of all debuggee classes");266testFailed = true;267} else {268log.display("WARNING: Unable to test ClassUnloadEvent because checked class\n"269+ " was not actually unloaded");270}271} else {272if (!eventReceived) {273log.complain("FAILURE 6: ClassUnloadEvent was not received for class to be unloaded\n"274+ " but class no longe presents in the list of all debuggee classes ");275testFailed = true;276}277}278279} catch (Warning e) {280log.display("WARNING: " + e.getMessage());281} catch (Failure e) {282log.complain("TEST FAILURE: " + e.getMessage());283testFailed = true;284} catch (Exception e) {285log.complain("Unexpected exception: " + e);286e.printStackTrace(out);287testFailed = true;288} finally {289290// disable event request to prevent appearance of further events291if (checkedRequest != null) {292log.display("Disabling event request");293checkedRequest.disable();294}295296// force debugee to exit297log.display("Sending command: " + COMMAND_QUIT);298pipe.println(COMMAND_QUIT);299300log.display("Waiting for debuggee terminating");301int debuggeeStatus = debuggee.endDebugee();302if (debuggeeStatus == PASSED + JCK_STATUS_BASE) {303log.display("Debuggee PASSED with exit code: " + debuggeeStatus);304} else {305log.complain("Debuggee FAILED with exit code: " + debuggeeStatus);306testFailed = true;307}308}309310// check test results311if (testFailed) {312log.complain("TEST FAILED");313return FAILED;314}315return PASSED;316}317318static class Warning extends Failure {319Warning(String msg) {320super(msg);321}322}323324}325326327