NullRedefineClassesTests.java revision 12973:2e63fa2efdb1
1/*
2 * Copyright (c) 2003, 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 4920005 4882798
27 * @summary make sure redefineClasses throws NullPointerException in the right places.
28 * @author Robert Field as modified from the code of Gabriel Adauto, Wily Technology
29 *
30 * @run build NullRedefineClassesTests
31 * @run shell MakeJAR.sh redefineAgent
32 * @run main/othervm -javaagent:redefineAgent.jar NullRedefineClassesTests NullRedefineClassesTests
33 */
34
35import java.lang.instrument.ClassDefinition;
36import java.lang.instrument.UnmodifiableClassException;
37
38public class
39NullRedefineClassesTests
40    extends ASimpleInstrumentationTestCase
41{
42
43    /**
44     * Constructor for NullRedefineClassesTests.
45     * @param name
46     */
47    public NullRedefineClassesTests(String name) {
48        super(name);
49    }
50
51    public static void
52    main (String[] args) throws Throwable {
53        ATestCaseScaffold   test = new NullRedefineClassesTests(args[0]);
54        test.runTest();
55    }
56
57    protected final void
58    doRunTest() throws ClassNotFoundException, UnmodifiableClassException  {
59        testNullRedefineClasses();
60    }
61
62
63    public void
64    testNullRedefineClasses() throws ClassNotFoundException, UnmodifiableClassException {
65        boolean caught;
66
67        // Test that a null argument throws NullPointerException
68        caught = false;
69        try {
70            fInst.redefineClasses(null);
71        } catch (NullPointerException npe) {
72            caught = true;
73        }
74        assertTrue(caught);
75
76        // Test that a null element throws NullPointerException
77        caught = false;
78        try {
79            fInst.redefineClasses(new ClassDefinition[]{ null });
80        } catch (NullPointerException npe) {
81            caught = true;
82        }
83        assertTrue(caught);
84
85        // Test that a null element amonst others throws NullPointerException
86        caught = false;
87        ClassDefinition cd = new ClassDefinition(DummyClass.class, new byte[] {1, 2, 3});
88        try {
89            fInst.redefineClasses(new ClassDefinition[]{ cd, null });
90        } catch (NullPointerException npe) {
91            caught = true;
92        }
93        assertTrue(caught);
94
95        // Test that a null class throws NullPointerException
96        caught = false;
97        try {
98            new ClassDefinition(null, new byte[] {1, 2, 3});
99        } catch (NullPointerException npe) {
100            caught = true;
101        }
102        assertTrue(caught);
103
104        // Test that a null byte array throws NullPointerException
105        caught = false;
106        try {
107            new ClassDefinition(DummyClass.class, null);
108        } catch (NullPointerException npe) {
109            caught = true;
110        }
111        assertTrue(caught);
112    }
113}
114