jdk/test/com/sun/jdi/redefine/RedefineTest.java
author mikejwre
Thu, 25 Mar 2010 15:05:15 -0700
changeset 5074 9c9bfe8f3a47
parent 4335 365eb4449319
child 5506 202f599c92aa
permissions -rw-r--r--
Added tag jdk7-b87 for changeset 8367da959bab
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 2001-2002 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 4287595
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @bug 4462989
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  @bug 4531511
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  @summary Test class redefinition
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @author Robert Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @library ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *  @run compile -g RedefineTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *  @run shell RedefineSetUp.sh
4335
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 2
diff changeset
    37
 *  @run main/othervm RedefineTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /********** target program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
class RedefineTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void show(String where){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        System.out.println("Returned: " + where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static void lastly(String where){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static void main(String[] args){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        RedefineSubTarg sub = new RedefineSubTarg();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        String where = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        for (int i = 0; i < 5; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            where = sub.foo(where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            show(where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        lastly(where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class RedefineTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    ReferenceType targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static final String expected ="Boring Boring Different Boring Different ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    int repetitionCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    boolean beforeRedefine = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    RedefineTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        new RedefineTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /********** event handlers **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public void methodEntered(MethodEntryEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Method meth = event.location().method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        ThreadReference thread = event.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (meth.name().equals("foo")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ++repetitionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            beforeRedefine = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                expectNonObsolete(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                inspectLineNumber(event, thread.frame(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                doRedefine(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                beforeRedefine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                switch (repetitionCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    expectNonObsolete(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    inspectLineNumber(event, thread.frame(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    expectObsolete(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    inspectLineNumber(event, thread.frame(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                failure("Test Failure: unexpected exception - " + exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                exc.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public void breakpointReached(BreakpointEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        ThreadReference thread = event.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            StackFrame frame = thread.frame(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            LocalVariable lv = frame.visibleVariableByName("where");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            Value vWhere = frame.getValue(lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            String remoteWhere = ((StringReference)vWhere).value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            println("Value of where: " + remoteWhere);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (!remoteWhere.equals(expected)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                failure("FAIL: expected result string: '" + expected +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        "' got: '" + remoteWhere + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } catch (Exception thr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            failure("Test Failure: unexpected exception: " + thr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /********** test assists **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    void expectNonObsolete(ThreadReference thread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (isObsolete(thread)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            failure("FAIL: Method should NOT be obsolete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            println("as it should be, not obsolete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    void expectObsolete(ThreadReference thread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (isObsolete(thread)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            println("obsolete like it should be");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            failure("FAIL: Method should be obsolete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    void inspectLineNumber(LocatableEvent event, StackFrame frame) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         * For each value of repetitionCount, use the beforeRedefine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * boolean to distinguish the time before and after the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * redefinition takes place.  Line numbers are inspected both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * before and after each redefine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int n = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int expectedLine = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        switch (repetitionCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            expectedLine = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            expectedLine = beforeRedefine ? 4:21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            expectedLine = beforeRedefine ? 21:4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            expectedLine = beforeRedefine ? 4:21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            /* The class won't be redefined on this iteration (look
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
             * for a java.lang.UnsupportedOperationException instead)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
             * so expected line stays the same as last successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
             * redefine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            expectedLine = 21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        Method method = event.location().method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (frame.location().method().isObsolete()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
             * Then skip. Obsolete methods are not interesting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
             * inspect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            println("inspectLineNumber skipping obsolete method " + method.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            n = method.location().lineNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            int m = frame.location().lineNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if ((n != expectedLine) || (n != m)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                failure("Test Failure: line number disagreement: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        n + " (event) versus " + m + " (frame) versus " + expectedLine +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        " (expected)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                println("inspectLineNumber in method " + method.name() + " at line " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    boolean isObsolete(ThreadReference thread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        StackFrame frame = thread.frame(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        Method meth = frame.location().method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return meth.isObsolete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    void doRedefine(ThreadReference thread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        Exception receivedException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        String fileName = "notThis";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        switch (repetitionCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            fileName = "RedefineSubTarg.class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            fileName = "Different_RedefineSubTarg.class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            fileName = "RedefineSubTarg.class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            fileName = "Different_RedefineSubTarg.class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            fileName = "SchemaChange_RedefineSubTarg.class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        File phyl = new File(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        byte[] bytes = new byte[(int)phyl.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        InputStream in = new FileInputStream(phyl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        in.read(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        Map map = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        map.put(findReferenceType("RedefineSubTarg"), bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        println(System.getProperty("line.separator") + "Iteration # " + repetitionCount +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                " ------ Redefine as: " + fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            vm().redefineClasses(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } catch (Exception thr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            receivedException = thr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        switch (repetitionCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (receivedException == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                failure("FAIL: no exception; expected: UnsupportedOperationException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            } else if (receivedException instanceof UnsupportedOperationException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                println("Received expected exception: " + receivedException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                failure("FAIL: got exception: " + receivedException +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        ", expected: UnsupportedOperationException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (receivedException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                failure("FAIL: unexpected exception: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        receivedException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        BreakpointEvent bpe = startToMain("RedefineTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        targetClass = bpe.location().declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        EventRequestManager erm = vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         * Method entry in sub targ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        MethodEntryRequest mee = erm.createMethodEntryRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        mee.addClassFilter("RedefineSubTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        mee.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * BP at end to get value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        List lastlys = targetClass.methodsByName("lastly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (lastlys.size() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throw new Exception ("TestFailure: Expected one 'lastly' method, found: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                 lastlys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        Location loc = ((Method)(lastlys.get(0))).location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        EventRequest req = erm.createBreakpointRequest(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        req.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // Allow application to complete and shut down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            println("RedefineTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            throw new Exception("RedefineTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
}