1 /*
   2  * Copyright (c) 2001, 2016, 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  *  @modules jdk.jdi
  32  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  33  *  @run compile -g ClassesByName2Test.java
  34  *  @run driver ClassesByName2Test
  35  */
  36 import com.sun.jdi.*;
  37 import com.sun.jdi.event.*;
  38 import com.sun.jdi.request.*;
  39 
  40 import java.util.*;
  41 
  42     /********** target program **********/
  43 
  44 class ClassesByName2Targ {
  45     static void bkpt() {
  46     }
  47 
  48     public static void main(String[] args){
  49         System.out.println("Howdy!");
  50         try {
  51 
  52             Thread zero = new Thread ("ZERO") {
  53                     public void run () {
  54                         System.setProperty("java.awt.headless", "true");
  55                         java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
  56 
  57                     }
  58                 };
  59 
  60             Thread one = new Thread ("ONE") {
  61                     public void run () {
  62                         try {
  63                             java.security.KeyPairGenerator keyGen =
  64                                 java.security.KeyPairGenerator.getInstance("DSA", "SUN");
  65                         } catch (Exception e) {
  66                             e.printStackTrace();
  67                         }
  68                     }
  69                 };
  70 
  71             Thread two = new Thread ("TWO") {
  72                 public void run () {
  73                     try {
  74                         String s = String.format("%02x", 0xff);
  75                     } catch (Exception e) {
  76                         e.printStackTrace();
  77                     }
  78                 }
  79             };
  80 
  81             two.start();
  82             one.start();
  83             zero.start();
  84 
  85             try {
  86                 zero.join();
  87                 System.out.println("zero joined");
  88                 one.join();
  89                 System.out.println("one joined");
  90                 two.join();
  91                 System.out.println("two joined");
  92             } catch (InterruptedException iex) {
  93                 iex.printStackTrace();
  94             }
  95         } catch (Exception e) {
  96             e.printStackTrace();
  97         }
  98         bkpt();
  99         System.out.println("Goodbye from ClassesByName2Targ!");
 100     }
 101 }
 102 
 103     /********** test program **********/
 104 
 105 public class ClassesByName2Test extends TestScaffold {
 106     volatile boolean stop = false;
 107 
 108     ClassesByName2Test (String args[]) {
 109         super(args);
 110     }
 111 
 112     public void breakpointReached(BreakpointEvent event) {
 113         System.out.println("Got BreakpointEvent: " + event);
 114         stop = true;
 115     }
 116 
 117     public void eventSetComplete(EventSet set) {
 118         // Don't resume.
 119     }
 120 
 121     public static void main(String[] args)      throws Exception {
 122         new ClassesByName2Test(args).startTests();
 123     }
 124 
 125     void breakpointAtMethod(ReferenceType ref, String methodName)
 126                                            throws Exception {
 127         List meths = ref.methodsByName(methodName);
 128         if (meths.size() != 1) {
 129             throw new Exception("test error: should be one " +
 130                                 methodName);
 131         }
 132         Method meth = (Method)meths.get(0);
 133         BreakpointRequest bkptReq = vm().eventRequestManager().
 134             createBreakpointRequest(meth.location());
 135         bkptReq.enable();
 136         try {
 137             addListener (this);
 138         } catch (Exception ex){
 139             ex.printStackTrace();
 140             failure("failure: Could not add listener");
 141             throw new Exception("ClassesByname2Test: failed");
 142         }
 143     }
 144 
 145     protected void runTests() throws Exception {
 146         BreakpointEvent bpe = startToMain("ClassesByName2Targ");
 147 
 148         /*
 149           Bug 6263966 - Don't just resume because the debuggee can
 150           complete and disconnect while the following loop is
 151           accessing it.
 152         */
 153         breakpointAtMethod(bpe.location().declaringType(), "bkpt");
 154         vm().resume();
 155 
 156         /* The test of 'stop' is so that we stop when the debuggee hits
 157            the bkpt.  The 150 is so we stop if the debuggee
 158            is slow (eg, -Xcomp -server) - we don't want to
 159            spend all day waiting for it to get to the bkpt.
 160         */
 161         for (int i = 0; i < 150 && !stop; i++) {
 162             List all = vm().allClasses();
 163             System.out.println("\n++++ Lookup number: " + i + ".  allClasses() returned " +
 164                                all.size() + " classes.");
 165             for (Iterator it = all.iterator(); it.hasNext(); ) {
 166                 ReferenceType cls = (ReferenceType)it.next();
 167                 String name = cls.name();
 168                 List found = vm().classesByName(name);
 169                 if (found.contains(cls)) {
 170                     //System.out.println("Found class: " + name);
 171                 } else {
 172                     System.out.println("CLASS NOT FOUND: " + name);
 173                     throw new Exception("CLASS NOT FOUND (by classesByName): " +
 174                                         name);
 175                 }
 176             }
 177         }
 178 
 179         // In case of a slow debuggee, we don't want to resume the debuggee and wait
 180         // for it to complete.
 181         vm().exit(0);
 182 
 183         /*
 184          * deal with results of test
 185          * if anything has called failure("foo") testFailed will be true
 186          */
 187         if (!testFailed) {
 188             println("ClassesByName2Test: passed");
 189         } else {
 190             throw new Exception("ClassesByName2Test: failed");
 191         }
 192     }
 193 }