RedefineTest.java revision 0
0N/A/*
2273N/A * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1472N/A * CA 95054 USA or visit www.sun.com if you need additional information or
1472N/A * have any questions.
0N/A */
0N/A
0N/A/**
1879N/A * @test
1879N/A * @bug 4628726
1879N/A * @summary Test class redefinition - method data line numbers and local vars,
1879N/A *
1879N/A * @author Robert Field
1879N/A *
1879N/A * @library ..
1879N/A * @run build TestScaffold VMConnection TargetListener TargetAdapter
1879N/A * @run compile -g RedefineTest.java
1879N/A * @run shell RedefineSetUp.sh
1879N/A * @run main RedefineTest -repeat 3
1879N/A * @run main RedefineTest
0N/A */
107N/Aimport com.sun.jdi.*;
107N/Aimport com.sun.jdi.event.*;
107N/Aimport com.sun.jdi.request.*;
107N/Aimport java.util.*;
107N/Aimport java.io.*;
107N/A
107N/A /********** target program **********/
107N/A
107N/Aclass RedefineTarg {
107N/A public static void main(String[] args){
107N/A RedefineSubTarg.stemcp();
107N/A RedefineSubTarg sub = new RedefineSubTarg();
107N/A sub.bottom();
107N/A RedefineSubTarg.stnemcp();
0N/A RedefineSubTarg.stemcp();
0N/A }
0N/A}
0N/A
0N/A /********** test program **********/
0N/A
0N/Apublic class RedefineTest extends TestScaffold {
0N/A static int redefineRepeat = 1;
0N/A int bpCnt = 0;
0N/A
0N/A // isObsolete, linenumber, lv name, lv value, lv isArg
0N/A String[] before = {
0N/A "+ 3",
0N/A "+ 6 eights 888 T",
0N/A "+ 11 rot 4 F",
0N/A "+ 15",
1349N/A "+ 20 myArg 56 T paramy 12 F",
107N/A "+ 24",
1349N/A "+ 28",
1349N/A "+ 33" };
1349N/A String[] after = {
0N/A "+ 5",
0N/A "O",
0N/A "O",
0N/A "+ 16",
107N/A "+ 21 whoseArg 56 T parawham 12 F",
107N/A "+ 25",
107N/A "O",
0N/A "+ 34" };
0N/A String[] shorter = {
107N/A "+ 5",
0N/A "+ 9 eights 88 T",
107N/A "+ 13",
0N/A "+ 16",
107N/A "+ 21 whoseArg 56 T parawham 12 F",
0N/A "+ 25" };
107N/A String[] refresh = {
0N/A "+ 5",
0N/A "+ 9 eights 88 T",
0N/A "+ 13",
0N/A "+ 16",
0N/A "+ 21 whoseArg 56 T parawham 12 F",
0N/A "+ 25",
0N/A "+ 29",
0N/A "+ 34" };
0N/A int[] bps = {7, 12, 16, 21, 25, 30, 34};
0N/A String[][] bpPlaces = {
0N/A {"+ 16"},
0N/A {"+ 21 myArg 56 T paramy 12 F"},
0N/A {"+ 25"},
0N/A {"+ 34"} };
0N/A
0N/A static String[] processArgs(String args[]) {
0N/A if (args.length > 0 && args[0].equals("-repeat")) {
0N/A redefineRepeat = Integer.decode(args[1]).intValue();
0N/A String[] args2 = new String[args.length - 2];
0N/A System.arraycopy(args, 2, args2, 0, args.length - 2);
0N/A return args2;
0N/A } else {
0N/A return args;
0N/A }
0N/A }
0N/A
0N/A RedefineTest (String args[]) {
107N/A super(args);
0N/A }
107N/A
107N/A public static void main(String[] args) throws Exception {
0N/A new RedefineTest(processArgs(args)).startTests();
107N/A }
0N/A
0N/A
0N/A /********** event handlers **********/
0N/A
107N/A public void breakpointReached(BreakpointEvent event) {
0N/A println("Got BreakpointEvent - " + event);
0N/A try {
0N/A checkFrames(event.thread(), bpPlaces[bpCnt++]);
0N/A if (bpCnt >= bpPlaces.length) {
107N/A eventRequestManager().deleteAllBreakpoints();
107N/A }
107N/A } catch (Exception exc) {
1349N/A failure("FAIL: breakpoint checking threw " + exc);
107N/A }
107N/A }
1349N/A
1349N/A /********** test assists **********/
1349N/A
1349N/A // isObsolete, linenumber, lv name, lv value, lv isArg
107N/A // equals: ref type (always), method (not obsolete)
1349N/A void checkFrames(ThreadReference thread, String[] matchList) throws Exception {
107N/A for (int i = 0; i < matchList.length; ++i) {
1349N/A String match = matchList[i];
1349N/A StackFrame frame = thread.frame(i);
0N/A Location loc = frame.location();
0N/A ReferenceType refType = loc.declaringType();
107N/A Method meth = loc.method();
1349N/A String errInfo = "\nframe " + i + ": " + loc + "\n match: " + match;
1349N/A if (!findReferenceType("RedefineSubTarg").equals(refType)) {
1349N/A failure("FAIL: Bad reference type - " + errInfo);
1349N/A return; // might be bad class, but might have run past bottom
1349N/A }
1349N/A StringTokenizer st = new StringTokenizer(match);
1349N/A boolean expectObs = st.nextToken().equals("O");
1349N/A println("Frame " + i + ": " + meth);
1349N/A if (meth.isObsolete()) {
1349N/A if (!expectObs) {
1349N/A failure("FAIL: Method should NOT be obsolete - " + errInfo);
1349N/A }
1349N/A } else {
1349N/A if (expectObs) {
1349N/A failure("FAIL: Method should be obsolete - " + errInfo);
1349N/A break; // no more data to read
1349N/A }
1349N/A if (!findMethod(refType, meth.name(), meth.signature()).equals(meth)) {
107N/A failure("FAIL: Non matching method - " + errInfo);
0N/A }
0N/A int line = loc.lineNumber();
1349N/A if (line != Integer.parseInt(st.nextToken())) {
0N/A failure("FAIL: Unexpected line number: " + errInfo);
0N/A }
0N/A // local var matching
0N/A int lvCnt = 0;
0N/A while (st.hasMoreTokens()) {
0N/A ++lvCnt;
0N/A String lvName = st.nextToken();
0N/A int lvValue = Integer.parseInt(st.nextToken());
107N/A boolean isArg = st.nextToken().equals("T");
1349N/A LocalVariable lv = frame.visibleVariableByName(lvName);
1349N/A if (lv == null) {
1349N/A failure("FAIL: local var not found: '" + lvName +
0N/A "' -- " + errInfo);
0N/A } else {
0N/A Value val = frame.getValue(lv);
0N/A int ival = ((IntegerValue)val).value();
107N/A if (ival != lvValue) {
107N/A failure("FAIL: expected value: '" + lvValue +
107N/A "' got: '" + ival + "' -- " + errInfo);
107N/A }
107N/A if (lv.isArgument() != isArg) {
107N/A failure("FAIL: expected argument: '" + isArg +
107N/A "' got: '" + lv.isArgument() + "' -- " + errInfo);
107N/A }
107N/A }
107N/A }
107N/A List locals = frame.visibleVariables();
107N/A if (locals.size() != lvCnt) {
107N/A failure("FAIL: expected '" + lvCnt +
107N/A "' locals were '" + locals.size() +
107N/A "' -- " + errInfo + "' -- " + locals);
107N/A }
107N/A }
107N/A }
107N/A }
107N/A
107N/A
107N/A void doRedefine(String fileName) throws Exception {
107N/A File phyl = new File(fileName);
107N/A byte[] bytes = new byte[(int)phyl.length()];
107N/A InputStream in = new FileInputStream(phyl);
107N/A in.read(bytes);
107N/A in.close();
107N/A
107N/A Map map = new HashMap();
107N/A map.put(findReferenceType("RedefineSubTarg"), bytes);
107N/A
107N/A try {
107N/A for (int i = 0; i < redefineRepeat; ++i) {
107N/A vm().redefineClasses(map);
107N/A }
107N/A } catch (Exception thr) {
107N/A failure("FAIL: unexpected exception: " + thr);
107N/A }
107N/A }
107N/A
107N/A ThreadReference toTop() {
107N/A BreakpointEvent bpe = resumeTo("RedefineSubTarg", "top", "()V");
107N/A return bpe.thread();
107N/A }
0N/A
107N/A void setBP(int line) {
0N/A try {
0N/A Location loc = findLocation(findReferenceType("RedefineSubTarg"), line);
0N/A final BreakpointRequest request =
0N/A eventRequestManager().createBreakpointRequest(loc);
107N/A request.enable();
107N/A } catch (Exception exc) {
107N/A failure("FAIL: Attempt to set BP at line " + line + " threw " + exc);
107N/A }
107N/A }
107N/A
107N/A /********** test core **********/
107N/A
107N/A protected void runTests() throws Exception {
107N/A
107N/A startToMain("RedefineTarg");
0N/A
107N/A ThreadReference thread = toTop();
107N/A
107N/A println("------ Before Redefine ------");
107N/A checkFrames(thread, before);
107N/A
107N/A println("------ After Redefine ------");
107N/A doRedefine("Different_RedefineSubTarg.class");
107N/A checkFrames(thread, after);
107N/A
107N/A println("------ Static 2 ------");
107N/A toTop();
107N/A checkFrames(thread, shorter);
107N/A
107N/A println("------ Instance ------");
107N/A toTop();
107N/A checkFrames(thread, shorter);
107N/A
107N/A println("------ Re-entered ------");
107N/A toTop();
0N/A checkFrames(thread, refresh);
107N/A
107N/A println("------ Breakpoints ------");
107N/A doRedefine("RedefineSubTarg.class");
107N/A for (int i = 0; i < bps.length; ++i) {
107N/A setBP(bps[i]);
107N/A }
107N/A
107N/A /*
107N/A * resume the target listening for events
107N/A */
107N/A listenUntilVMDisconnect();
107N/A
107N/A if (bpCnt != bpPlaces.length) {
107N/A failure("FAIL: Wrong number of breakpoints encountered: " + bpCnt);
107N/A }
107N/A
107N/A /*
107N/A * deal with results of test
107N/A * if anything has called failure("foo") testFailed will be true
107N/A */
0N/A if (!testFailed) {
0N/A println("RedefineTest(method): passed");
107N/A } else {
107N/A throw new Exception("RedefineTest(method): failed");
0N/A }
0N/A }
107N/A}
0N/A