jdk/test/com/sun/jdi/SourceNameFilterTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 828 ad3f54bd6ae8
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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 4836939
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary JDI add addSourceNameFilter to ClassPrepareRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  @author jjh
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 SourceNameFilterTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main SourceNameFilterTest
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 SourceNameFilterTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static  void bkpt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static void main(String[] args){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        System.out.println("Howdy!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        LoadedLater1.doit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        bkpt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        LoadedLater2.doit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        bkpt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        LoadedLater3.doit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        bkpt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("Goodbye from SourceNameFilterTarg!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
class LoadedLater1 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static void doit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        System.out.println("didit1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
class LoadedLater2 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static void doit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.out.println("didit2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
class LoadedLater3 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static void doit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        System.out.println("didit3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
public class SourceNameFilterTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    ReferenceType targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    boolean gotEvent1 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    boolean gotEvent2 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    boolean gotEvent3 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    ClassPrepareRequest cpReq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    boolean shouldResume = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    SourceNameFilterTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        new SourceNameFilterTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public void eventSetComplete(EventSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        //System.out.println("jj: resuming, set = " + set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (shouldResume) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            set.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            shouldResume = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public void classPrepared(ClassPrepareEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        shouldResume = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        ReferenceType rt = event.referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        String rtname = rt.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (rtname.equals("LoadedLater1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            gotEvent1 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (rtname.equals("LoadedLater2")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            gotEvent2 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (rtname.equals("LoadedLater3")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            gotEvent3 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // debug code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            println("Got ClassPrepareEvent for : " + rtname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                println("    sourceName = " + rt.sourceName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            } catch (AbsentInformationException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                failure("failure: absent info on sourceName(): " + ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            String stratum = rt.defaultStratum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            println("    defaultStratum = " + stratum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                println("    sourceNames = " + rt.sourceNames(stratum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } catch (AbsentInformationException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                failure("failure: absent info on sourceNames(): " + ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            println("\nAvailable strata:  " + rt.availableStrata());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         * Get to the top of main()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * to determine targetClass and mainThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        BreakpointEvent bpe = startToMain("SourceNameFilterTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        targetClass = bpe.location().declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        EventRequestManager erm = vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        addListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * Resume the target listening for events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * This should cause a class prepare event for LoadedLater1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        cpReq = erm.createClassPrepareRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        cpReq.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        resumeTo("SourceNameFilterTarg", "bkpt", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         * This should cause us to not get a class prepared for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         * LoadedLater2 since it doesn't come from "jj"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        cpReq.disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        cpReq.addSourceNameFilter("jj");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        cpReq.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        resumeTo("SourceNameFilterTarg", "bkpt", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        cpReq.disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * This should cause us to get a class prepare event for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * LoadedLater3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        cpReq = erm.createClassPrepareRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        cpReq.addSourceNameFilter("SourceNameFilterTest.java");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        cpReq.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        resumeTo("SourceNameFilterTarg", "bkpt", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (!gotEvent1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            failure("failure: Did not get a class prepare request " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    "for Loadedlater1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (gotEvent2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            failure("failure: Did get a class prepare request " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    "for Loadedlater2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (!gotEvent3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            failure("failure: Did not get a class prepare request " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    "for Loadedlater3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
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("SourceNameFilterTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new Exception("SourceNameFilterTest: 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
}