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