1 /*
   2  * Copyright (c) 2001, 2013, 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  *
  29  *  @author Robert Field/Jim Holmlund
  30  *
  31  *  @library scaffold
  32  *  @run build JDIScaffold VMConnection
  33  *  @run compile -g HelloWorld.java
  34  *  @run driver FilterNoMatch
  35  */
  36 
  37 /* This tests the patternMatch function in JDK file:
  38  *    .../src/share/back/eventHandler.c
  39  *
  40  * This test verifies that patterns that do not match really don't.
  41  * See also testcase FilterMatch.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 FilterNoMatch extends JDIScaffold {
  50 
  51     static boolean listenCalled;
  52 
  53     public static void main(String args[]) throws Exception {
  54         new FilterNoMatch().startTests();
  55     }
  56 
  57     public FilterNoMatch() {
  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 no patterns match.  If any
  71             // pattern is erroneously matched, then this method
  72             // will not get called.
  73             public boolean stepCompleted(StepEvent event) {
  74                 listenCalled = true;
  75                 System.out.println("listen: line#=" + event.location().lineNumber()
  76                                    + " event=" + event);
  77                 // disable the step and then run to completion
  78                 StepRequest str= (StepRequest)event.request();
  79                 str.disable();
  80                 set.resume();
  81                 return false;
  82             }
  83         };
  84         listenCalled = false;
  85         addListener(adapter);
  86     }
  87 
  88     protected void runTests() throws Exception {
  89         String[] args = new String[2];
  90         args[0] = "-connect";
  91         args[1] = "com.sun.jdi.CommandLineLaunch:main=HelloWorld";
  92 
  93         connect(args);
  94         waitForVMStart();
  95 
  96         // VM has started, but hasn't started running the test program yet.
  97         EventRequestManager requestManager = vm().eventRequestManager();
  98         ReferenceType referenceType = resumeToPrepareOf("HelloWorld").referenceType();
  99 
 100         // The debuggee is stopped
 101         // I don't think we really have to set a bkpt and then do a step,
 102         // we should just be able to do a step.  Problem is the
 103         // createStepRequest call needs a thread and I don't know
 104         // yet where to get one other than from the bkpt handling :-(
 105         Location location = findLocation(referenceType, 3);
 106         BreakpointRequest request
 107             = requestManager.createBreakpointRequest(location);
 108 
 109         request.enable();
 110 
 111         // This does a resume, so we shouldn't come back to it until
 112         // the debuggee has run and hit the bkpt.
 113         BreakpointEvent event = (BreakpointEvent)waitForRequestedEvent(request);
 114 
 115         // The bkpt was hit; remove it.
 116         requestManager.deleteEventRequest(request);
 117 
 118         StepRequest request1 = requestManager.createStepRequest(event.thread(),
 119                                   StepRequest.STEP_LINE,StepRequest.STEP_OVER);
 120 
 121 
 122         // We have to filter out all these so that they don't cause the
 123         // listener to be called.
 124         request1.addClassExclusionFilter("java.*");
 125         request1.addClassExclusionFilter("javax.*");
 126         request1.addClassExclusionFilter("sun.*");
 127         request1.addClassExclusionFilter("com.sun.*");
 128         request1.addClassExclusionFilter("com.oracle.*");
 129         request1.addClassExclusionFilter("oracle.*");
 130         request1.addClassExclusionFilter("jdk.internal.*");
 131 
 132         // We want our listener to be called if a pattern does not match.
 133         // So, here we want patterns that do not match HelloWorld.
 134         // If any pattern here erroneously matches, then our listener
 135         // will not get called and the test will fail.
 136         request1.addClassExclusionFilter("H");
 137         request1.addClassExclusionFilter("HelloWorl");
 138         request1.addClassExclusionFilter("HelloWorldx");
 139         request1.addClassExclusionFilter("xHelloWorld");
 140 
 141         request1.addClassExclusionFilter("*elloWorldx");
 142         request1.addClassExclusionFilter("*elloWorl");
 143         request1.addClassExclusionFilter("*xHelloWorld");
 144         request1.addClassExclusionFilter("elloWorld*");
 145         request1.addClassExclusionFilter("HelloWorldx*");
 146         request1.addClassExclusionFilter("xHelloWorld*");
 147 
 148         // As a test, uncomment this line and this test should fail.
 149         //request1.addClassExclusionFilter("*elloWorld");
 150 
 151 
 152         request1.enable();
 153         listen();
 154 
 155         vm().resume();
 156 
 157         waitForVMDeath();
 158 
 159         if ( !listenCalled){
 160             throw new Exception( "Failed: .");
 161         }
 162         System.out.println( "Passed: ");
 163     }
 164 }