Path: blob/master/test/jdk/com/sun/jdi/ClassesByName2Test.java
41149 views
/*1* Copyright (c) 2001, 2016, 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*/2223/**24* @test25* @bug 4406439 492574026* @summary ClassesByName2 verifies that all the classes in the loaded class list can be found with classesByName..27* @author Tim Bell (based on ClassesByName by Robert Field)28*29* @modules jdk.jdi30* java.desktop31*32* @run build TestScaffold VMConnection TargetListener TargetAdapter33* @run compile -g ClassesByName2Test.java34* @run driver ClassesByName2Test35*/36import com.sun.jdi.*;37import com.sun.jdi.event.*;38import com.sun.jdi.request.*;3940import java.util.*;4142/********** target program **********/4344class ClassesByName2Targ {45static void bkpt() {46}4748public static void main(String[] args){49System.out.println("Howdy!");50try {5152Thread zero = new Thread ("ZERO") {53public void run () {54System.setProperty("java.awt.headless", "true");55java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();5657}58};5960Thread one = new Thread ("ONE") {61public void run () {62try {63java.security.KeyPairGenerator keyGen =64java.security.KeyPairGenerator.getInstance("DSA", "SUN");65} catch (Exception e) {66e.printStackTrace();67}68}69};7071Thread two = new Thread ("TWO") {72public void run () {73try {74String s = String.format("%02x", 0xff);75} catch (Exception e) {76e.printStackTrace();77}78}79};8081two.start();82one.start();83zero.start();8485try {86zero.join();87System.out.println("zero joined");88one.join();89System.out.println("one joined");90two.join();91System.out.println("two joined");92} catch (InterruptedException iex) {93iex.printStackTrace();94}95} catch (Exception e) {96e.printStackTrace();97}98bkpt();99System.out.println("Goodbye from ClassesByName2Targ!");100}101}102103/********** test program **********/104105public class ClassesByName2Test extends TestScaffold {106volatile boolean stop = false;107108ClassesByName2Test (String args[]) {109super(args);110}111112public void breakpointReached(BreakpointEvent event) {113System.out.println("Got BreakpointEvent: " + event);114stop = true;115}116117public void eventSetComplete(EventSet set) {118// Don't resume.119}120121public static void main(String[] args) throws Exception {122new ClassesByName2Test(args).startTests();123}124125void breakpointAtMethod(ReferenceType ref, String methodName)126throws Exception {127List meths = ref.methodsByName(methodName);128if (meths.size() != 1) {129throw new Exception("test error: should be one " +130methodName);131}132Method meth = (Method)meths.get(0);133BreakpointRequest bkptReq = vm().eventRequestManager().134createBreakpointRequest(meth.location());135bkptReq.enable();136try {137addListener (this);138} catch (Exception ex){139ex.printStackTrace();140failure("failure: Could not add listener");141throw new Exception("ClassesByname2Test: failed");142}143}144145protected void runTests() throws Exception {146BreakpointEvent bpe = startToMain("ClassesByName2Targ");147148/*149Bug 6263966 - Don't just resume because the debuggee can150complete and disconnect while the following loop is151accessing it.152*/153breakpointAtMethod(bpe.location().declaringType(), "bkpt");154vm().resume();155156/* The test of 'stop' is so that we stop when the debuggee hits157the bkpt. The 150 is so we stop if the debuggee158is slow (eg, -Xcomp -server) - we don't want to159spend all day waiting for it to get to the bkpt.160*/161for (int i = 0; i < 150 && !stop; i++) {162List all = vm().allClasses();163System.out.println("\n++++ Lookup number: " + i + ". allClasses() returned " +164all.size() + " classes.");165for (Iterator it = all.iterator(); it.hasNext(); ) {166ReferenceType cls = (ReferenceType)it.next();167String name = cls.name();168List found = vm().classesByName(name);169if (found.contains(cls)) {170//System.out.println("Found class: " + name);171} else {172System.out.println("CLASS NOT FOUND: " + name);173throw new Exception("CLASS NOT FOUND (by classesByName): " +174name);175}176}177}178179// In case of a slow debuggee, we don't want to resume the debuggee and wait180// for it to complete.181vm().exit(0);182183/*184* deal with results of test185* if anything has called failure("foo") testFailed will be true186*/187if (!testFailed) {188println("ClassesByName2Test: passed");189} else {190throw new Exception("ClassesByName2Test: failed");191}192}193}194195196