1 /*
2  * Copyright (c) 2001, 2015, 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 4321339
27  * @summary Check correct processing of filters after a count filter
28  * @author Robert Field
29  *
30  * @run build TestScaffold VMConnection TargetListener TargetAdapter
31  * @run compile -g CountFilterTest.java
32  * @run driver CountFilterTest
33  */
34 import com.sun.jdi.*;
35 import com.sun.jdi.event.*;
36 import com.sun.jdi.request.*;
37 
38 import java.util.*;
39 
40     /********** target program **********/
41 
42 class CountFilterTarg {
43 
thisOne()44     void thisOne() {
45     }
46 
butNotThisOne()47     void butNotThisOne() {
48     }
49 
norThisOne()50     void norThisOne() {
51     }
52 
butThisOne()53     void butThisOne() {
54     }
55 
main(String[] args)56     public static void main(String[] args){
57         CountFilterTarg cft = new CountFilterTarg();
58         System.out.println("Hi! Hi! Hello...");
59         cft.thisOne();
60         cft.butNotThisOne();
61         cft.norThisOne();
62         cft.butThisOne();
63         System.out.println("Goodbye from CountFilterTarg!");
64     }
65 }
66 
67     /********** test program **********/
68 
69 public class CountFilterTest extends TestScaffold {
70     ReferenceType targetClass;
71     ThreadReference mainThread;
72     EventRequestManager erm;
73     Map whereMap = new HashMap();
74 
CountFilterTest(String args[])75     CountFilterTest (String args[]) {
76         super(args);
77     }
78 
main(String[] args)79     public static void main(String[] args)      throws Exception {
80         new CountFilterTest(args).startTests();
81     }
82 
83     /********** event handlers **********/
84 
breakpointReached(BreakpointEvent event)85     public void breakpointReached(BreakpointEvent event) {
86         println("Got BreakpointEvent");
87         locatableEvent(event, event.location());
88     }
89 
methodEntered(MethodEntryEvent event)90     public void methodEntered(MethodEntryEvent event) {
91         println("Got MethodEntryEvent");
92         locatableEvent(event, event.location());
93     }
94 
methodExited(MethodExitEvent event)95     public void methodExited(MethodExitEvent event) {
96         println("Got MethodExitEvent");
97         locatableEvent(event, event.location());
98     }
99 
locatableEvent(Event event, Location loc)100     public void locatableEvent(Event event, Location loc) {
101         String got = loc.method().name();
102         String expected = (String)whereMap.get(event.request());
103         if (!got.equals(expected)) {
104             failure("FAIL: expected event in " + expected +
105                     " but it occurred in " + got);
106         }
107     }
108 
109     /********** test assist*****/
110 
breakpointAtMethod(String methodName)111     BreakpointRequest breakpointAtMethod(String methodName)
112                                            throws Exception {
113         List meths = targetClass.methodsByName(methodName);
114         if (meths.size() != 1) {
115             throw new Exception("test error: should be one " +
116                                 methodName);
117         }
118         Method meth = (Method)meths.get(0);
119         return erm.createBreakpointRequest(meth.location());
120     }
121 
122     /********** test core **********/
123 
runTests()124     protected void runTests() throws Exception {
125         /*
126          * Get to the top of main()
127          * to determine targetClass and mainThread
128          */
129         BreakpointEvent bpe = startToMain("CountFilterTarg");
130         targetClass = bpe.location().declaringType();
131         mainThread = bpe.thread();
132         erm = vm().eventRequestManager();
133         ThreadReference otherThread = null;
134 
135         /* find a thread that isn't mainThread */
136         for (Iterator it = vm().allThreads().iterator();
137                        it.hasNext(); ) {
138             ThreadReference tr = (ThreadReference)it.next();
139             if (!tr.equals(mainThread)) {
140                 otherThread = tr;
141                 break;
142             }
143         }
144         if (otherThread == null) {
145             throw new Exception("test error: couldn't find " +
146                                 "other thread");
147         }
148 
149         /*
150          * Set event requests
151          */
152         MethodEntryRequest meRequest =
153             erm.createMethodEntryRequest();
154         meRequest.addClassFilter("CountFilterTarg");
155         meRequest.addCountFilter(5);  // incl constructor
156         meRequest.enable();
157         whereMap.put(meRequest, "butThisOne");
158 
159         MethodExitRequest mxRequest =
160             erm.createMethodExitRequest();
161         mxRequest.addCountFilter(2);
162         mxRequest.addClassFilter("borp");
163         mxRequest.enable();
164         whereMap.put(mxRequest, "nowhere (from method exit)");
165 
166         BreakpointRequest thisOneRequest =
167             breakpointAtMethod("thisOne");
168         thisOneRequest.addCountFilter(1);
169         thisOneRequest.addThreadFilter(mainThread);
170         thisOneRequest.enable();
171         whereMap.put(thisOneRequest, "thisOne");
172 
173         BreakpointRequest butNotThisOneRequest =
174             breakpointAtMethod("butNotThisOne");
175         butNotThisOneRequest.addCountFilter(1);
176         butNotThisOneRequest.addThreadFilter(otherThread);
177         butNotThisOneRequest.enable();
178         whereMap.put(butNotThisOneRequest,
179                      "nowhere (post filter)");
180 
181         BreakpointRequest norThisOneRequest =
182             breakpointAtMethod("norThisOne");
183         norThisOneRequest.addThreadFilter(otherThread);
184         norThisOneRequest.addCountFilter(1);
185         norThisOneRequest.enable();
186         whereMap.put(norThisOneRequest,
187                      "nowhere (pre filter)");
188 
189         BreakpointRequest butThisOneRequest =
190             breakpointAtMethod("butThisOne");
191         butThisOneRequest.addThreadFilter(mainThread);
192         butThisOneRequest.addCountFilter(1);
193         butThisOneRequest.enable();
194         whereMap.put(butThisOneRequest, "butThisOne");
195 
196         /*
197          * resume the target listening for events
198          */
199         listenUntilVMDisconnect();
200 
201         /*
202          * deal with results of test
203          * if anything has called failure("foo") testFailed will be true
204          */
205         if (!testFailed) {
206             println("CountFilterTest: passed");
207         } else {
208             throw new Exception("CountFilterTest: failed");
209         }
210     }
211 }
212