0N/A/*
2362N/A * Copyright (c) 2003, 2005, Oracle and/or its affiliates. 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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4882798 5067523
0N/A * @summary insure redefine is supported. exercise a class, then redefine it and do it again
0N/A * @author Gabriel Adauto, Wily Technology
0N/A *
0N/A * @run build RedefineClassesTests
0N/A * @run shell RedefineSetUp.sh
0N/A * @run shell MakeJAR.sh redefineAgent
0N/A * @run main/othervm -javaagent:redefineAgent.jar RedefineClassesTests RedefineClassesTests
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.lang.instrument.*;
0N/Aimport java.lang.reflect.*;
0N/Apublic class
0N/ARedefineClassesTests
0N/A extends ASimpleInstrumentationTestCase
0N/A{
0N/A
0N/A /**
0N/A * Constructor for RedefineClassesTests.
0N/A * @param name
0N/A */
0N/A public RedefineClassesTests(String name)
0N/A {
0N/A super(name);
0N/A }
0N/A
0N/A public static void
0N/A main (String[] args)
0N/A throws Throwable {
0N/A ATestCaseScaffold test = new RedefineClassesTests(args[0]);
0N/A test.runTest();
0N/A }
0N/A
0N/A protected final void
0N/A doRunTest()
0N/A throws Throwable {
0N/A testIsRedefineClassesSupported();
0N/A testSimpleRedefineClasses();
0N/A testUnmodifiableClassException();
0N/A }
0N/A
0N/A
0N/A public void
0N/A testIsRedefineClassesSupported()
0N/A {
0N/A boolean canRedef = fInst.isRedefineClassesSupported();
0N/A assertTrue("Cannot redefine classes", canRedef);
0N/A }
0N/A
0N/A public void
0N/A testSimpleRedefineClasses()
0N/A throws Throwable
0N/A {
0N/A // first load the class and prove that it is the right one
0N/A ExampleRedefine ex = new ExampleRedefine();
0N/A
0N/A // with this version of the class, doSomething is a nop
0N/A int firstGet = ex.get();
0N/A ex.doSomething();
0N/A int secondGet = ex.get();
0N/A
0N/A assertEquals(firstGet, secondGet);
0N/A
0N/A // now redefine the class. This will change doSomething to be an increment
0N/A
0N/A // this class is stored in a different place (scratch directory) to avoid collisions
0N/A File f = new File("Different_ExampleRedefine.class");
0N/A System.out.println("Reading test class from " + f);
0N/A InputStream redefineStream = new FileInputStream(f);
0N/A
0N/A byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream);
0N/A
0N/A ClassDefinition redefineParamBlock = new ClassDefinition( ExampleRedefine.class,
0N/A redefineBuffer);
0N/A
0N/A fInst.redefineClasses(new ClassDefinition[] {redefineParamBlock});
0N/A
0N/A int thirdGet = ex.get();
0N/A ex.doSomething();
0N/A int fourthGet = ex.get();
0N/A assertEquals(thirdGet + 1, fourthGet);
0N/A }
0N/A
0N/A public void
0N/A testUnmodifiableClassException()
0N/A throws Throwable
0N/A {
0N/A System.out.println("Testing UnmodifiableClassException");
0N/A
0N/A // Load any class
0N/A File f = new File("Different_ExampleRedefine.class");
0N/A InputStream redefineStream = new FileInputStream(f);
0N/A byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream);
0N/A
0N/A System.out.println("Try to redefine class for primitive type");
0N/A try {
0N/A ClassDefinition redefineParamBlock =
0N/A new ClassDefinition( byte.class, redefineBuffer );
0N/A fInst.redefineClasses(new ClassDefinition[] {redefineParamBlock});
0N/A fail();
0N/A } catch (UnmodifiableClassException x) {
0N/A }
0N/A
0N/A System.out.println("Try to redefine class for array type");
0N/A try {
0N/A ClassDefinition redefineParamBlock =
0N/A new ClassDefinition( byte[].class, redefineBuffer );
0N/A fInst.redefineClasses(new ClassDefinition[] {redefineParamBlock});
0N/A fail();
0N/A } catch (UnmodifiableClassException x) {
0N/A }
0N/A
0N/A }
0N/A
0N/A}