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