1 /*
   2  * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  *  @test
  26  *  @bug 4406439 4925740
  27  *  @summary ClassesByName2 verifies that all the classes in the loaded class list can be found with classesByName..
  28  *
  29  *  @author Tim Bell (based on ClassesByName by Robert Field)
  30  *
  31  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  32  *  @run compile -g ClassesByName2Test.java
  33  *  @run driver ClassesByName2Test
  34  */
  35 import com.sun.jdi.*;
  36 import com.sun.jdi.event.*;
  37 import com.sun.jdi.request.*;
  38 
  39 import java.util.*;
  40 
  41     /********** target program **********/
  42 
  43 class ClassesByName2Targ {
  44     static void bkpt() {
  45     }
  46 
  47     public static void main(String[] args){
  48         System.out.println("Howdy!");
  49         try {
  50 
  51             Thread zero = new Thread ("ZERO") {
  52                     public void run () {
  53                         System.setProperty("java.awt.headless", "true");
  54                         java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
  55 
  56                     }
  57                 };
  58 
  59             Thread one = new Thread ("ONE") {
  60                     public void run () {
  61                         try {
  62                             java.security.KeyPairGenerator keyGen =
  63                                 java.security.KeyPairGenerator.getInstance("DSA", "SUN");
  64                         } catch (Exception e) {
  65                             e.printStackTrace();
  66                         }
  67                     }
  68                 };
  69 
  70             Thread two = new Thread ("TWO") {
  71                     public void run () {
  72                         javax.rmi.CORBA.Util.getCodebase(this.getClass());
  73                     }
  74                 };
  75 
  76             two.start();
  77             one.start();
  78             zero.start();
  79 
  80             try {
  81                 zero.join();
  82                 System.out.println("zero joined");
  83                 one.join();
  84                 System.out.println("one joined");
  85                 two.join();
  86                 System.out.println("two joined");
  87             } catch (InterruptedException iex) {
  88                 iex.printStackTrace();
  89             }
  90         } catch (Exception e) {
  91             e.printStackTrace();
  92         }
  93         bkpt();
  94         System.out.println("Goodbye from ClassesByName2Targ!");
  95     }
  96 }
  97 
  98     /********** test program **********/
  99 
 100 public class ClassesByName2Test extends TestScaffold {
 101     volatile boolean stop = false;
 102 
 103     ClassesByName2Test (String args[]) {
 104         super(args);
 105     }
 106 
 107     public void breakpointReached(BreakpointEvent event) {
 108         System.out.println("Got BreakpointEvent: " + event);
 109         stop = true;
 110     }
 111 
 112     public void eventSetComplete(EventSet set) {
 113         // Don't resume.
 114     }
 115 
 116     public static void main(String[] args)      throws Exception {
 117         new ClassesByName2Test(args).startTests();
 118     }
 119 
 120     void breakpointAtMethod(ReferenceType ref, String methodName)
 121                                            throws Exception {
 122         List meths = ref.methodsByName(methodName);
 123         if (meths.size() != 1) {
 124             throw new Exception("test error: should be one " +
 125                                 methodName);
 126         }
 127         Method meth = (Method)meths.get(0);
 128         BreakpointRequest bkptReq = vm().eventRequestManager().
 129             createBreakpointRequest(meth.location());
 130         bkptReq.enable();
 131         try {
 132             addListener (this);
 133         } catch (Exception ex){
 134             ex.printStackTrace();
 135             failure("failure: Could not add listener");
 136             throw new Exception("ClassesByname2Test: failed");
 137         }
 138     }
 139 
 140     protected void runTests() throws Exception {
 141         BreakpointEvent bpe = startToMain("ClassesByName2Targ");
 142 
 143         /*
 144           Bug 6263966 - Don't just resume because the debuggee can
 145           complete and disconnect while the following loop is
 146           accessing it.
 147         */
 148         breakpointAtMethod(bpe.location().declaringType(), "bkpt");
 149         vm().resume();
 150 
 151         /* The test of 'stop' is so that we stop when the debuggee hits
 152            the bkpt.  The 150 is so we stop if the debuggee
 153            is slow (eg, -Xcomp -server) - we don't want to
 154            spend all day waiting for it to get to the bkpt.
 155         */
 156         for (int i = 0; i < 150 && !stop; i++) {
 157             List all = vm().allClasses();
 158             System.out.println("\n++++ Lookup number: " + i + ".  allClasses() returned " +
 159                                all.size() + " classes.");
 160             for (Iterator it = all.iterator(); it.hasNext(); ) {
 161                 ReferenceType cls = (ReferenceType)it.next();
 162                 String name = cls.name();
 163                 List found = vm().classesByName(name);
 164                 if (found.contains(cls)) {
 165                     //System.out.println("Found class: " + name);
 166                 } else {
 167                     System.out.println("CLASS NOT FOUND: " + name);
 168                     throw new Exception("CLASS NOT FOUND (by classesByName): " +
 169                                         name);
 170                 }
 171             }
 172         }
 173 
 174         // In case of a slow debuggee, we don't want to resume the debuggee and wait
 175         // for it to complete.
 176         vm().exit(0);
 177 
 178         /*
 179          * deal with results of test
 180          * if anything has called failure("foo") testFailed will be true
 181          */
 182         if (!testFailed) {
 183             println("ClassesByName2Test: passed");
 184         } else {
 185             throw new Exception("ClassesByName2Test: failed");
 186         }
 187     }
 188 }