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 FilterNoMatch
  33  */
  34 
  35 /* This tests the patternMatch function in JDK file:
  36  *    .../src/share/back/eventHandler.c
  37  *
  38  * This test verifies that patterns that do not match really don't.
  39  * See also testcase FilterMatch.java.
  40  */
  41 
  42 import java.util.*;
  43 import com.sun.jdi.*;
  44 import com.sun.jdi.event.*;
  45 import com.sun.jdi.request.*;
  46 
  47 public class FilterNoMatch extends JDIScaffold {
  48 
  49     static boolean listenCalled;
  50 
  51     public static void main(String args[]) throws Exception {
  52         new FilterNoMatch().startTests();
  53     }
  54 
  55     public FilterNoMatch() {
  56         super();
  57     }
  58 
  59     private void listen() {
  60         TargetAdapter adapter = new TargetAdapter() {
  61             EventSet set = null;
  62 
  63             public boolean eventSetReceived(EventSet set) {
  64                 this.set = set;
  65                 return false;
  66             }
  67 
  68             // This gets called if no patterns match.  If any
  69             // pattern is erroneously matched, then this method
  70             // will not get called.
  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);
 115 
 116         StepRequest request1 = requestManager.createStepRequest(event.thread(),
 117                                   StepRequest.STEP_LINE,StepRequest.STEP_OVER);
 118 
 119 
 120         // We have to filter out all these so that they don't cause the
 121         // listener to be called.
 122         request1.addClassExclusionFilter("java.*");
 123         request1.addClassExclusionFilter("javax.*");
 124         request1.addClassExclusionFilter("sun.*");
 125         request1.addClassExclusionFilter("com.sun.*");
 126         request1.addClassExclusionFilter("com.oracle.*");
 127         request1.addClassExclusionFilter("oracle.*");
 128         request1.addClassExclusionFilter("jdk.internal.*");
 129 
 130         // We want our listener to be called if a pattern does not match.
 131         // So, here we want patterns that do not match HelloWorld.
 132         // If any pattern here erroneously matches, then our listener
 133         // will not get called and the test will fail.
 134         request1.addClassExclusionFilter("H");
 135         request1.addClassExclusionFilter("HelloWorl");
 136         request1.addClassExclusionFilter("HelloWorldx");
 137         request1.addClassExclusionFilter("xHelloWorld");
 138 
 139         request1.addClassExclusionFilter("*elloWorldx");
 140         request1.addClassExclusionFilter("*elloWorl");
 141         request1.addClassExclusionFilter("*xHelloWorld");
 142         request1.addClassExclusionFilter("elloWorld*");
 143         request1.addClassExclusionFilter("HelloWorldx*");
 144         request1.addClassExclusionFilter("xHelloWorld*");
 145 
 146         // As a test, uncomment this line and this test should fail.
 147         //request1.addClassExclusionFilter("*elloWorld");
 148 
 149 
 150         request1.enable();
 151         listen();
 152 
 153         vm().resume();
 154 
 155         waitForVMDeath();
 156 
 157         if ( !listenCalled){
 158             throw new Exception( "Failed: .");
 159         }
 160         System.out.println( "Passed: ");
 161     }
 162 }