jdk/test/com/sun/jdi/CountFilterTest.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 24973 8c4bc3fa4c4e
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *  @bug 4321339
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary Check correct processing of filters after a count filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  @author Robert Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run compile -g CountFilterTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main CountFilterTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /********** target program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class CountFilterTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    void thisOne() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    void butNotThisOne() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    void norThisOne() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    void butThisOne() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static void main(String[] args){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        CountFilterTarg cft = new CountFilterTarg();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        System.out.println("Hi! Hi! Hello...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        cft.thisOne();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        cft.butNotThisOne();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        cft.norThisOne();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        cft.butThisOne();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        System.out.println("Goodbye from CountFilterTarg!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class CountFilterTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    ReferenceType targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    EventRequestManager erm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    Map whereMap = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    CountFilterTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        new CountFilterTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /********** event handlers **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public void breakpointReached(BreakpointEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        println("Got BreakpointEvent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        locatableEvent(event, event.location());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public void methodEntered(MethodEntryEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        println("Got MethodEntryEvent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        locatableEvent(event, event.location());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void methodExited(MethodExitEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        println("Got MethodExitEvent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        locatableEvent(event, event.location());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public void locatableEvent(Event event, Location loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        String got = loc.method().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        String expected = (String)whereMap.get(event.request());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (!got.equals(expected)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            failure("FAIL: expected event in " + expected +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    " but it occurred in " + got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /********** test assist*****/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    BreakpointRequest breakpointAtMethod(String methodName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                           throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        List meths = targetClass.methodsByName(methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (meths.size() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new Exception("test error: should be one " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Method meth = (Method)meths.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return erm.createBreakpointRequest(meth.location());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * Get to the top of main()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         * to determine targetClass and mainThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        BreakpointEvent bpe = startToMain("CountFilterTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        targetClass = bpe.location().declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        erm = vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        ThreadReference otherThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        /* find a thread that isn't mainThread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for (Iterator it = vm().allThreads().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                       it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            ThreadReference tr = (ThreadReference)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (!tr.equals(mainThread)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                otherThread = tr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (otherThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new Exception("test error: couldn't find " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                "other thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * Set event requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        MethodEntryRequest meRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            erm.createMethodEntryRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        meRequest.addClassFilter("CountFilterTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        meRequest.addCountFilter(5);  // incl constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        meRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        whereMap.put(meRequest, "butThisOne");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        MethodExitRequest mxRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            erm.createMethodExitRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        mxRequest.addCountFilter(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        mxRequest.addClassFilter("borp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        mxRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        whereMap.put(mxRequest, "nowhere (from method exit)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        BreakpointRequest thisOneRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            breakpointAtMethod("thisOne");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        thisOneRequest.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        thisOneRequest.addThreadFilter(mainThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        thisOneRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        whereMap.put(thisOneRequest, "thisOne");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        BreakpointRequest butNotThisOneRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            breakpointAtMethod("butNotThisOne");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        butNotThisOneRequest.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        butNotThisOneRequest.addThreadFilter(otherThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        butNotThisOneRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        whereMap.put(butNotThisOneRequest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                     "nowhere (post filter)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        BreakpointRequest norThisOneRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            breakpointAtMethod("norThisOne");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        norThisOneRequest.addThreadFilter(otherThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        norThisOneRequest.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        norThisOneRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        whereMap.put(norThisOneRequest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                     "nowhere (pre filter)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        BreakpointRequest butThisOneRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            breakpointAtMethod("butThisOne");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        butThisOneRequest.addThreadFilter(mainThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        butThisOneRequest.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        butThisOneRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        whereMap.put(butThisOneRequest, "butThisOne");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * resume the target listening for events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            println("CountFilterTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new Exception("CountFilterTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}