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