1 /* 2 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved. 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 * 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. 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 * @modules java.instrument 31 * @run build RedefineClassesDisabledTest 32 * @run shell RedefineSetUp.sh 33 * @run shell MakeJAR.sh basicAgent 34 * @run main/othervm -javaagent:basicAgent.jar RedefineClassesDisabledTest RedefineClassesDisabledTest 35 */ 36 37 import java.io.*; 38 import java.lang.instrument.*; 39 import java.lang.reflect.*; 40 public class 41 RedefineClassesDisabledTest 42 extends ASimpleInstrumentationTestCase 43 { 44 45 /** 46 * Constructor for RedefineClassesDisabledTest. 47 * @param name 48 */ 49 public RedefineClassesDisabledTest(String name) 50 { 51 super(name); 52 } 53 54 public static void 55 main (String[] args) 56 throws Throwable { 57 ATestCaseScaffold test = new RedefineClassesDisabledTest(args[0]); 58 test.runTest(); 59 } 60 61 protected final void 62 doRunTest() 63 throws Throwable { 64 testIsRedefineClassesSupported(); 65 testSimpleRedefineClasses(); 66 } 67 68 69 public void 70 testIsRedefineClassesSupported() 71 { 72 boolean canRedef = fInst.isRedefineClassesSupported(); 73 assertTrue("Can redefine classes flag set incorrectly (true)", !canRedef); 74 } 75 76 public void 77 testSimpleRedefineClasses() 78 throws Throwable 79 { 80 // first load the class and prove that it is the right one 81 ExampleRedefine ex = new ExampleRedefine(); 82 83 // with this version of the class, doSomething is a nop 84 int firstGet = ex.get(); 85 ex.doSomething(); 86 int secondGet = ex.get(); 87 88 assertEquals(firstGet, secondGet); 89 90 // now redefine the class. This will change doSomething to be an increment 91 92 // this class is stored in a different place (scratch directory) to avoid collisions 93 File f = new File("Different_ExampleRedefine.class"); 94 System.out.println("Reading test class from " + f); 95 InputStream redefineStream = new FileInputStream(f); 96 97 byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream); 98 99 ClassDefinition redefineParamBlock = new ClassDefinition( ExampleRedefine.class, 100 redefineBuffer); 101 102 // test that the redefine fails with an UnsupportedOperationException 103 boolean caught = false; 104 try { 105 fInst.redefineClasses(new ClassDefinition[] {redefineParamBlock}); 106 } catch (UnsupportedOperationException uoe) { 107 caught = true; 108 } 109 assertTrue(caught); 110 } 111 112 }