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 4331522
  27  * @summary addClassFilter("Foo") acts like "Foo*"
  28  * @author Robert Field/Jim Holmlund
  29  *
  30  * @run build JDIScaffold VMConnection
  31  * @run compile -g HelloWorld.java
  32  * @run driver FilterMatch
  33  */
  34 
  35 /* Look at patternMatch in JDK file:
  36  *    .../src/share/back/eventHandler.c
  37  */
  38 
  39 /*
  40  *  This test tests patterns passed to addClassFilter that do match
  41  *  the classname of the event.  See also testcase FilterNoMatch.java.
  42  */
  43 
  44 import java.util.*;
  45 import com.sun.jdi.*;
  46 import com.sun.jdi.event.*;
  47 import com.sun.jdi.request.*;
  48 
  49 public class FilterMatch extends JDIScaffold {
  50 
  51     static boolean listenCalled;
  52 
  53     public static void main(String args[]) throws Exception {
  54         new FilterMatch().startTests();
  55     }
  56 
  57     public FilterMatch() {
  58         super();
  59     }
  60 
  61     private void listen() {
  62         TargetAdapter adapter = new TargetAdapter() {
  63             EventSet set = null;
  64 
  65             public boolean eventSetReceived(EventSet set) {
  66                 this.set = set;
  67                 return false;
  68             }
  69 
  70             // This gets called if all filters match.
  71             public boolean stepCompleted(StepEvent event) {
  72                 listenCalled = true;
  73                 System.out.println("listen: line#=" + event.location().lineNumber()
  74                                    + " event=" + event);
  75                 // disable the step and then run to completion
  76                 StepRequest str= (StepRequest)event.request();
  77                 str.disable();
  78                 set.resume();
  79                 return false;
  80             }
  81         };
  82         listenCalled = false;
  83         addListener(adapter);
  84     }
  85 
  86     protected void runTests() throws Exception {
  87         String[] args = new String[2];
  88         args[0] = "-connect";
  89         args[1] = "com.sun.jdi.CommandLineLaunch:main=HelloWorld";
  90 
  91         connect(args);
  92         waitForVMStart();
  93 
  94         // VM has started, but hasn't started running the test program yet.
  95         EventRequestManager requestManager = vm().eventRequestManager();
  96         ReferenceType referenceType = resumeToPrepareOf("HelloWorld").referenceType();
  97 
  98         // The debuggee is stopped
  99         // I don't think we really have to set a bkpt and then do a step,
 100         // we should just be able to do a step.  Problem is the
 101         // createStepRequest call needs a thread and I don't know
 102         // yet where to get one other than from the bkpt handling :-(
 103         Location location = findLocation(referenceType, 3);
 104         BreakpointRequest request
 105             = requestManager.createBreakpointRequest(location);
 106 
 107         request.enable();
 108 
 109         // This does a resume, so we shouldn't come back to it until
 110         // the debuggee has run and hit the bkpt.
 111         BreakpointEvent event = (BreakpointEvent)waitForRequestedEvent(request);
 112 
 113         // The bkpt was hit; remove it.
 114         requestManager.deleteEventRequest(request);  // remove BP
 115 
 116         StepRequest request1 = requestManager.createStepRequest(event.thread(),
 117                                   StepRequest.STEP_LINE,StepRequest.STEP_OVER);
 118 
 119         // These patterns all match HelloWorld.  Since they all match, our
 120         // listener should get called and the test will pass.  If any of them
 121         // are erroneously determined to _not_ match, then our listener will
 122         // not get called and the test will fail.
 123         request1.addClassFilter("*");
 124 
 125         request1.addClassFilter("H*");
 126         request1.addClassFilter("He*");
 127         request1.addClassFilter("HelloWorld*");
 128 
 129         request1.addClassFilter("*d");
 130         request1.addClassFilter("*ld");
 131         request1.addClassFilter("*HelloWorld");
 132 
 133         request1.addClassFilter("HelloWorld");
 134 
 135         // As a test, uncomment this line and the test should fail.
 136         //request1.addClassFilter("x");
 137 
 138         request1.enable();
 139         listen();
 140 
 141         vm().resume();
 142 
 143         waitForVMDeath();
 144 
 145         if ( !listenCalled){
 146             throw new Exception( "Failed: Event filtered out.");
 147         }
 148         System.out.println( "Passed: Event not filtered out.");
 149     }
 150 }