duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4882798 |
| 27 | * @summary test that redefineClasses and isRedefineClassesSupported behave correctly when redefine is not enabled |
| 28 | * @author Robert Field, Sun Microsystems -- as modified from the work of Gabriel Adauto, Wily Technology |
| 29 | * |
| 30 | * @run build RedefineClassesDisabledTest |
| 31 | * @run shell RedefineSetUp.sh |
| 32 | * @run shell MakeJAR.sh basicAgent |
| 33 | * @run main/othervm -javaagent:basicAgent.jar RedefineClassesDisabledTest RedefineClassesDisabledTest |
| 34 | */ |
| 35 | |
| 36 | import java.io.*; |
| 37 | import java.lang.instrument.*; |
| 38 | import java.lang.reflect.*; |
| 39 | public class |
| 40 | RedefineClassesDisabledTest |
| 41 | extends ASimpleInstrumentationTestCase |
| 42 | { |
| 43 | |
| 44 | /** |
| 45 | * Constructor for RedefineClassesDisabledTest. |
| 46 | * @param name |
| 47 | */ |
| 48 | public RedefineClassesDisabledTest(String name) |
| 49 | { |
| 50 | super(name); |
| 51 | } |
| 52 | |
| 53 | public static void |
| 54 | main (String[] args) |
| 55 | throws Throwable { |
| 56 | ATestCaseScaffold test = new RedefineClassesDisabledTest(args[0]); |
| 57 | test.runTest(); |
| 58 | } |
| 59 | |
| 60 | protected final void |
| 61 | doRunTest() |
| 62 | throws Throwable { |
| 63 | testIsRedefineClassesSupported(); |
| 64 | testSimpleRedefineClasses(); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | public void |
| 69 | testIsRedefineClassesSupported() |
| 70 | { |
| 71 | boolean canRedef = fInst.isRedefineClassesSupported(); |
| 72 | assertTrue("Can redefine classes flag set incorrectly (true)", !canRedef); |
| 73 | } |
| 74 | |
| 75 | public void |
| 76 | testSimpleRedefineClasses() |
| 77 | throws Throwable |
| 78 | { |
| 79 | // first load the class and prove that it is the right one |
| 80 | ExampleRedefine ex = new ExampleRedefine(); |
| 81 | |
| 82 | // with this version of the class, doSomething is a nop |
| 83 | int firstGet = ex.get(); |
| 84 | ex.doSomething(); |
| 85 | int secondGet = ex.get(); |
| 86 | |
| 87 | assertEquals(firstGet, secondGet); |
| 88 | |
| 89 | // now redefine the class. This will change doSomething to be an increment |
| 90 | |
| 91 | // this class is stored in a different place (scratch directory) to avoid collisions |
| 92 | File f = new File("Different_ExampleRedefine.class"); |
| 93 | System.out.println("Reading test class from " + f); |
| 94 | InputStream redefineStream = new FileInputStream(f); |
| 95 | |
| 96 | byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream); |
| 97 | |
| 98 | ClassDefinition redefineParamBlock = new ClassDefinition( ExampleRedefine.class, |
| 99 | redefineBuffer); |
| 100 | |
| 101 | // test that the redefine fails with an UnsupportedOperationException |
| 102 | boolean caught = false; |
| 103 | try { |
| 104 | fInst.redefineClasses(new ClassDefinition[] {redefineParamBlock}); |
| 105 | } catch (UnsupportedOperationException uoe) { |
| 106 | caught = true; |
| 107 | } |
| 108 | assertTrue(caught); |
| 109 | } |
| 110 | |
| 111 | } |