J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4836939 |
| 27 | * @summary JDI add addSourceNameFilter to ClassPrepareRequest |
| 28 | * |
| 29 | * @author jjh |
| 30 | * |
| 31 | * @run build TestScaffold VMConnection TargetListener TargetAdapter |
| 32 | * @run compile -g SourceNameFilterTest.java |
| 33 | * @run main SourceNameFilterTest |
| 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 SourceNameFilterTarg { |
| 44 | static void bkpt() { |
| 45 | } |
| 46 | |
| 47 | public static void main(String[] args){ |
| 48 | System.out.println("Howdy!"); |
| 49 | |
| 50 | LoadedLater1.doit(); |
| 51 | bkpt(); |
| 52 | |
| 53 | LoadedLater2.doit(); |
| 54 | bkpt(); |
| 55 | |
| 56 | LoadedLater3.doit(); |
| 57 | bkpt(); |
| 58 | System.out.println("Goodbye from SourceNameFilterTarg!"); |
| 59 | } |
| 60 | } |
| 61 | class LoadedLater1 { |
| 62 | public static void doit() { |
| 63 | System.out.println("didit1"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | class LoadedLater2 { |
| 68 | public static void doit() { |
| 69 | System.out.println("didit2"); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | class LoadedLater3 { |
| 74 | public static void doit() { |
| 75 | System.out.println("didit3"); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /********** test program **********/ |
| 80 | |
| 81 | public class SourceNameFilterTest extends TestScaffold { |
| 82 | ReferenceType targetClass; |
| 83 | ThreadReference mainThread; |
| 84 | boolean gotEvent1 = false; |
| 85 | boolean gotEvent2 = false; |
| 86 | boolean gotEvent3 = false; |
| 87 | |
| 88 | ClassPrepareRequest cpReq; |
| 89 | boolean shouldResume = false; |
| 90 | SourceNameFilterTest (String args[]) { |
| 91 | super(args); |
| 92 | } |
| 93 | |
| 94 | public static void main(String[] args) throws Exception { |
| 95 | new SourceNameFilterTest(args).startTests(); |
| 96 | } |
| 97 | public void eventSetComplete(EventSet set) { |
| 98 | //System.out.println("jj: resuming, set = " + set); |
| 99 | if (shouldResume) { |
| 100 | set.resume(); |
| 101 | shouldResume = false; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | public void classPrepared(ClassPrepareEvent event) { |
| 106 | shouldResume = true; |
| 107 | |
| 108 | ReferenceType rt = event.referenceType(); |
| 109 | String rtname = rt.name(); |
| 110 | |
| 111 | if (rtname.equals("LoadedLater1")) { |
| 112 | gotEvent1 = true; |
| 113 | } |
| 114 | |
| 115 | if (rtname.equals("LoadedLater2")) { |
| 116 | gotEvent2 = true; |
| 117 | } |
| 118 | |
| 119 | if (rtname.equals("LoadedLater3")) { |
| 120 | gotEvent3 = true; |
| 121 | } |
| 122 | |
| 123 | // debug code |
| 124 | if (false) { |
| 125 | println("Got ClassPrepareEvent for : " + rtname); |
| 126 | try { |
| 127 | println(" sourceName = " + rt.sourceName()); |
| 128 | } catch (AbsentInformationException ee) { |
| 129 | failure("failure: absent info on sourceName(): " + ee); |
| 130 | } |
| 131 | |
| 132 | String stratum = rt.defaultStratum(); |
| 133 | println(" defaultStratum = " + stratum); |
| 134 | |
| 135 | try { |
| 136 | println(" sourceNames = " + rt.sourceNames(stratum)); |
| 137 | } catch (AbsentInformationException ee) { |
| 138 | failure("failure: absent info on sourceNames(): " + ee); |
| 139 | } |
| 140 | println("\nAvailable strata: " + rt.availableStrata()); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /********** test core **********/ |
| 146 | |
| 147 | protected void runTests() throws Exception { |
| 148 | /* |
| 149 | * Get to the top of main() |
| 150 | * to determine targetClass and mainThread |
| 151 | */ |
| 152 | BreakpointEvent bpe = startToMain("SourceNameFilterTarg"); |
| 153 | targetClass = bpe.location().declaringType(); |
| 154 | mainThread = bpe.thread(); |
| 155 | EventRequestManager erm = vm().eventRequestManager(); |
| 156 | addListener(this); |
| 157 | |
| 158 | /* |
| 159 | * Resume the target listening for events |
| 160 | * This should cause a class prepare event for LoadedLater1 |
| 161 | */ |
| 162 | cpReq = erm.createClassPrepareRequest(); |
| 163 | cpReq.enable(); |
| 164 | resumeTo("SourceNameFilterTarg", "bkpt", "()V"); |
| 165 | |
| 166 | /* |
| 167 | * This should cause us to not get a class prepared for |
| 168 | * LoadedLater2 since it doesn't come from "jj" |
| 169 | */ |
| 170 | cpReq.disable(); |
| 171 | cpReq.addSourceNameFilter("jj"); |
| 172 | cpReq.enable(); |
| 173 | resumeTo("SourceNameFilterTarg", "bkpt", "()V"); |
| 174 | cpReq.disable(); |
| 175 | |
| 176 | /* |
| 177 | * This should cause us to get a class prepare event for |
| 178 | * LoadedLater3 |
| 179 | */ |
| 180 | cpReq = erm.createClassPrepareRequest(); |
| 181 | cpReq.addSourceNameFilter("SourceNameFilterTest.java"); |
| 182 | cpReq.enable(); |
| 183 | resumeTo("SourceNameFilterTarg", "bkpt", "()V"); |
| 184 | |
| 185 | listenUntilVMDisconnect(); |
| 186 | |
| 187 | if (!gotEvent1) { |
| 188 | failure("failure: Did not get a class prepare request " + |
| 189 | "for Loadedlater1"); |
| 190 | } |
| 191 | |
| 192 | if (gotEvent2) { |
| 193 | failure("failure: Did get a class prepare request " + |
| 194 | "for Loadedlater2"); |
| 195 | } |
| 196 | |
| 197 | if (!gotEvent3) { |
| 198 | failure("failure: Did not get a class prepare request " + |
| 199 | "for Loadedlater3"); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * deal with results of test |
| 204 | * if anything has called failure("foo") testFailed will be true |
| 205 | */ |
| 206 | if (!testFailed) { |
| 207 | println("SourceNameFilterTest: passed"); |
| 208 | } else { |
| 209 | throw new Exception("SourceNameFilterTest: failed"); |
| 210 | } |
| 211 | } |
| 212 | } |