Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ClassUnloadEvent/classSignature/signature001.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.classSignature;2425import com.sun.jdi.*;26import com.sun.jdi.event.*;27import com.sun.jdi.request.*;2829import java.io.*;30import java.util.List;31import java.util.Iterator;3233import nsk.share.*;34import nsk.share.jpda.*;35import nsk.share.jdi.*;363738// This class is the debugger in the test3940// NOTE: Test does not check array class because of difficulty of41// providing reliable technique for unloading such class.42// So all these testcases are commented in the test.4344public class signature001 {45static final int PASSED = 0;46static final int FAILED = 2;47static final int JCK_STATUS_BASE = 95;4849static final int TIMEOUT_DELTA = 1000; // milliseconds5051static final String COMMAND_READY = "ready";52static final String COMMAND_QUIT = "quit";53static final String COMMAND_LOAD = "load";54static final String COMMAND_LOADED = "loaded";55static final String COMMAND_UNLOAD = "unload";56static final String COMMAND_UNLOADED = "unloaded";5758static final String PREFIX = "nsk.jdi.ClassUnloadEvent.classSignature";59static final String DEBUGGEE_NAME = PREFIX + ".signature001a";60static final String CHECKED_CLASS = PREFIX + ".signature001c";61static final String CHECKED_INTERFACE = PREFIX + ".signature001b";62static final String CHECKED_ARRAY = PREFIX + ".signature001c[]";63static final String KLASSLOADER = ClassUnloader.INTERNAL_CLASS_LOADER_NAME;6465static private Debugee debuggee;66static private VirtualMachine vm;67static private IOPipe pipe;68static private Log log;69static private ArgumentHandler argHandler;70static private EventSet eventSet;7172static private ClassUnloadRequest checkedRequest;7374static private long eventTimeout;75static private boolean testFailed;76static private boolean eventsReceived;77static private boolean eventForClass, eventForInterface, eventForArray;7879public static void main (String args[]) {80System.exit(run(args, System.out) + JCK_STATUS_BASE);81}8283public static int run(final String args[], final PrintStream out) {8485testFailed = false;86eventsReceived = false;8788argHandler = new ArgumentHandler(args);89log = new Log(out, argHandler);90eventTimeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds9192// launch debugee9394Binder binder = new Binder(argHandler, log);95log.display("Connecting to debuggee");96debuggee = binder.bindToDebugee(DEBUGGEE_NAME);97debuggee.redirectStderr(log, "signature001a >");9899pipe = debuggee.createIOPipe();100vm = debuggee.VM();101102// create request and wait for ClassUnloadEvent103104try {105106// resume debugee and wait for it becomes ready107108log.display("Resuming debuggee");109debuggee.resume();110111log.display("Waiting for command: " + COMMAND_READY);112String command = pipe.readln();113if (command == null || !command.equals(COMMAND_READY)) {114throw new Failure("TEST BUG: unexpected debuggee's command: " + command);115}116117// get mirror of debugee class118119ReferenceType rType;120if ((rType = debuggee.classByName(DEBUGGEE_NAME)) == null) {121throw new Failure("TEST BUG: cannot find debuggee's class " + DEBUGGEE_NAME);122}123124// send command to load checked class and waits for a confirmation125126pipe.println(COMMAND_LOAD);127log.display("Waiting for checked class is loaded");128command = pipe.readln();129130if (command == null || !command.equals(COMMAND_LOADED)) {131throw new Failure("TEST BUG: unexpected debuggee's command: " + command);132}133134// checked class has been loaded!135log.display("Checked classes have been loaded in debuggee!");136137// create request for class unload event138139log.display("Creating request for ClassUnloadEvent");140EventRequestManager erManager = vm.eventRequestManager();141if ((checkedRequest = erManager.createClassUnloadRequest()) == null) {142throw new Failure("TEST BUG: unable to create ClassUnloadRequest");143} else {144log.display("ClassUnloadRequest created");145}146// checkedRequest.addClassFilter("nsk.jdi.ClassUnloadEvent.classSignature.*");147148log.display("Enabling event request");149checkedRequest.enable();150151List unloadRequests = vm.eventRequestManager().classUnloadRequests();152if (unloadRequests == null) {153throw new Failure("TEST_BUG: ClassUnloadRequest is not created");154}155156// force checked classes to be unloaded from debuggee157158// turn off pipe pinging159pipe.setPingTimeout(0);160161log.display("Waiting for checked class is unloaded");162pipe.println(COMMAND_UNLOAD);163command = pipe.readln();164165// end test if checked classes have not been actually unloaded or error occurs166167if (command != null && command.equals(COMMAND_LOADED)) {168// throw new Failure("TEST INCOMPLETE: unable to unload classes");169throw new Warning("TEST INCOMPLETE: unable to unload classes");170}171172if (command == null || !command.equals(COMMAND_UNLOADED)) {173throw new Failure("TEST BUG: unexpected debuggee's command: " + command);174}175176// checked classes have been unloaded177log.display("Checked classes forced to be unloaded from debuggee!");178179// waiting for event untill timeout exceeds180log.display("Waiting for ClassUnloadEvent for checked class");181long timeToFinish = System.currentTimeMillis() + eventTimeout;182while (!eventsReceived && System.currentTimeMillis() < timeToFinish) {183184// get next event set185eventSet = null;186try {187eventSet = vm.eventQueue().remove(TIMEOUT_DELTA);188} catch (Exception e) {189throw new Failure("TEST INCOMPLETE: Unexpected exception while getting event: " + e);190}191if (eventSet == null)192continue;193194// handle each event from the event set195EventIterator eventIterator = eventSet.eventIterator();196while (eventIterator.hasNext()) {197198Event event = eventIterator.nextEvent();199log.display("\nEvent received:\n " + event);200201// handle ClassUnloadEvent202if (event instanceof ClassUnloadEvent) {203204ClassUnloadEvent castedEvent = (ClassUnloadEvent)event;205log.display("Received event is ClassUnloadEvent:\n" + castedEvent);206207// check that received event is for checked request208EventRequest eventRequest = castedEvent.request();209if (!(checkedRequest.equals(eventRequest))) {210log.complain("FAILURE 1: eventRequest is not equal to checked request");211testFailed = true;212}213214// check that received event is for checked VM215VirtualMachine eventMachine = castedEvent.virtualMachine();216if (!(vm.equals(eventMachine))) {217log.complain("FAILURE 2: eventVirtualMachine is not equal to checked vm");218testFailed = true;219}220221// test method ClassUnloadEvent.222String refSignature = castedEvent.classSignature();223log.display("ClassUnloadEvent is received for " + refSignature);224225// check that received event is for checked class226if ((refSignature == null) || (refSignature.equals(""))) {227228log.complain("FAILURE 3: ClassUnloadEvent.classSignature() returns null or empty string");229testFailed = true;230231} else if (refSignature.equals("L" + CHECKED_CLASS.replace('.','/') + ";")) {232233// mark that ClassUnloadEvent for checked class received234eventForClass = true;235log.display("Expected ClassUnloadEvent for checked class received!");236237/*238// check that checked class is not included in debuggee's list of loaded classes239List loadedClasses = vm.classesByName(CHECKED_CLASS);240if (loadedClasses != null) {241log.complain("FAILURE 4: Class " + CHECKED_CLASS +242" is not unloaded while ClassUnloadEvent is received");243testFailed = true;244}245*/246247} else if (refSignature.equals("L" + CHECKED_INTERFACE.replace('.','/') + ";")) {248249// mark that ClassUnloadEvent for checked interface received250eventForInterface = true;251log.display("Expected ClassUnloadEvent for checked class received!");252253/*254// check that checked interface is not included in debuggee's list of loaded classes255List loadedClasses = vm.classesByName(CHECKED_INTERFACE);256if (loadedClasses != null) {257log.complain("FAILURE 4: Class " + CHECKED_INTERFACE +258" is not unloaded while ClassUnloadEvent is received");259testFailed = true;260}261*/262263/*264} else if (refSignature.equals("L" + CHECKED_ARRAY.replace('.','/') + ";")) {265266// mark that ClassUnloadEvent for checked array class received267eventForArray = true;268log.display("Expected ClassUnloadEvent for checked array class received!");269270// check that checked array class is not included in debuggee's list of loaded classes271List loadedClasses = vm.classesByName(CHECKED_ARRAY);272if (loadedClasses != null) {273log.complain("FAILURE 4: Class " + CHECKED_ARRAY +274" is not unloaded while ClassUnloadEvent is received");275testFailed = true;276}277278*/279} else {280281// ClassUnloadEvent for another class received; just display it282log.display("ClassUnloadEvent was received for loaded class " + refSignature);283}284}285286// ignore all other events287}288289eventSet.resume();290eventsReceived = eventForClass && eventForInterface /* && eventForArray */;291}292293log.display("");294295// check that expected event has been received for checked class296log.display("Searching checked class in debuggee");297rType = debuggee.classByName(CHECKED_CLASS);298if (rType != null) {299if (eventForClass) {300log.complain("FAILURE 4: ClassUnloadEvent is received for class to be unloaded\n"301+ " but class still presents in the list of all debuggee classes");302testFailed = true;303} else {304log.display("WARNING: Unable to test ClassUnloadEvent because checked class\n"305+ " was not actually unloaded");306}307} else {308if (!eventForClass) {309log.complain("FAILURE 5: ClassUnloadEvent was not received for class to be unloaded\n"310+ " but class no longe presents in the list of all debuggee classes ");311testFailed = true;312}313}314315// check that expected event has been received for checked interface316log.display("Searching checked interface in debuggee");317rType = debuggee.classByName(CHECKED_INTERFACE);318if (rType != null) {319if (eventForInterface) {320log.complain("FAILURE 6: ClassUnloadEvent is received for interface to be unloaded\n"321+ " but class still presents in the list of all debuggee classes");322testFailed = true;323} else {324log.display("WARNING: Unable to test ClassUnloadEvent because checked interface\n"325+ " was not actually unloaded");326}327} else {328if (!eventForInterface) {329log.complain("FAILURE 7: ClassUnloadEvent was not received for interface to be unloaded\n"330+ " but class no longe presents in the list of all debuggee classes ");331testFailed = true;332}333}334335} catch (Warning e) {336log.display("WARNING: " + e.getMessage());337} catch (Failure e) {338log.complain("TEST FAILURE: " + e.getMessage());339testFailed = true;340} catch (Exception e) {341log.complain("Unexpected exception: " + e);342e.printStackTrace(out);343testFailed = true;344} finally {345346// disable event request to prevent appearance of further events347if (checkedRequest != null) {348log.display("Disabling event request");349checkedRequest.disable();350}351352// force debugee to exit353log.display("Sending command: " + COMMAND_QUIT);354pipe.println(COMMAND_QUIT);355356// wait for debugee terminates and check its exit code357log.display("Waiting for debuggee terminating");358int debuggeeStatus = debuggee.endDebugee();359if (debuggeeStatus == PASSED + JCK_STATUS_BASE) {360log.display("Debuggee PASSED with exit code: " + debuggeeStatus);361} else {362log.complain("Debuggee FAILED with exit code: " + debuggeeStatus);363testFailed = true;364}365366}367368// check test results369if (testFailed) {370log.complain("TEST FAILED");371return FAILED;372}373return PASSED;374}375376static class Warning extends Failure {377Warning(String msg) {378super(msg);379}380}381382}383384385