LookupTypeTest.java revision 9814:22fd02fad88b
1/*
2 * Copyright (c) 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 8136421
27 * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
28 * @library / /testlibrary
29 * @compile ../common/CompilerToVMHelper.java
30 * @build compiler.jvmci.compilerToVM.LookupTypeTest
31 * @run main ClassFileInstaller
32 *      jdk.vm.ci.hotspot.CompilerToVMHelper
33 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockExperimentalVMOptions
34 *     -XX:+EnableJVMCI compiler.jvmci.compilerToVM.LookupTypeTest
35 */
36
37package compiler.jvmci.compilerToVM;
38
39import compiler.jvmci.common.testcases.DoNotExtendClass;
40import compiler.jvmci.common.testcases.MultiSubclassedClass;
41import compiler.jvmci.common.testcases.SingleSubclass;
42import java.util.HashSet;
43import java.util.Set;
44import jdk.vm.ci.hotspot.CompilerToVMHelper;
45import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
46import jdk.test.lib.Asserts;
47import jdk.test.lib.Utils;
48
49public class LookupTypeTest {
50    public static void main(String args[]) {
51        LookupTypeTest test = new LookupTypeTest();
52        for (TestCase tcase : createTestCases()) {
53            test.runTest(tcase);
54        }
55    }
56
57    private static Set<TestCase> createTestCases() {
58        Set<TestCase> result = new HashSet<>();
59        // a primitive class
60        result.add(new TestCase(Utils.toJVMTypeSignature(int.class),
61                LookupTypeTest.class, true, false, InternalError.class));
62        // lookup not existing class
63        result.add(new TestCase("Lsome_not_existing;", LookupTypeTest.class,
64                true, false, ClassNotFoundException.class));
65        // lookup invalid classname
66        result.add(new TestCase("L!@#$%^&**()[]{}?;", LookupTypeTest.class,
67                true, false, ClassNotFoundException.class));
68        // lookup package private class
69        result.add(new TestCase(
70                "Lcompiler/jvmci/compilerToVM/testcases/PackagePrivateClass;",
71                LookupTypeTest.class, true, false,
72                ClassNotFoundException.class));
73        // lookup usual class with resolve=true
74        result.add(new TestCase(Utils.toJVMTypeSignature(SingleSubclass.class),
75                LookupTypeTest.class, true, true));
76        // lookup usual class with resolve=false
77        result.add(new TestCase(
78                Utils.toJVMTypeSignature(DoNotExtendClass.class),
79                LookupTypeTest.class, false, true));
80        // lookup usual class with null accessor
81        result.add(new TestCase(
82                Utils.toJVMTypeSignature(MultiSubclassedClass.class), null,
83                false, false, NullPointerException.class));
84        return result;
85    }
86
87    private void runTest(TestCase tcase) {
88        System.out.println(tcase);
89        HotSpotResolvedObjectType metaspaceKlass;
90        try {
91            metaspaceKlass = CompilerToVMHelper.lookupType(tcase.className,
92                    tcase.accessing, tcase.resolve);
93        } catch (Throwable t) {
94            Asserts.assertNotNull(tcase.expectedException,
95                    "Assumed no exception, but got " + t);
96            Asserts.assertFalse(tcase.isPositive,
97                    "Got unexpected exception " + t);
98            Asserts.assertEQ(t.getClass(), tcase.expectedException,
99                    "Unexpected exception");
100            // passed
101            return;
102        }
103        if (tcase.expectedException != null) {
104            throw new AssertionError("Expected exception was not thrown: "
105                    + tcase.expectedException.getName());
106        }
107        if (tcase.isPositive) {
108            Asserts.assertNotNull(metaspaceKlass,
109                    "Unexpected null metaspace klass");
110            Asserts.assertEQ(metaspaceKlass.getName(), tcase.className,
111                    "Got unexpected resolved class name");
112        } else {
113            Asserts.assertNull(metaspaceKlass, "Unexpected metaspace klass");
114        }
115    }
116
117    private static class TestCase {
118        public final String className;
119        public final Class<?> accessing;
120        public final boolean resolve;
121        public final boolean isPositive;
122        public final Class<? extends Throwable> expectedException;
123
124        public TestCase(String className, Class<?> accessing, boolean resolve,
125                boolean isPositive,
126                Class<? extends Throwable> expectedException) {
127            this.className = className;
128            this.accessing = accessing;
129            this.resolve = resolve;
130            this.isPositive = isPositive;
131            this.expectedException = expectedException;
132        }
133
134        public TestCase(String className, Class<?> accessing, boolean resolve,
135                boolean isPositive) {
136            this.className = className;
137            this.accessing = accessing;
138            this.resolve = resolve;
139            this.isPositive = isPositive;
140            this.expectedException = null;
141        }
142
143        @Override
144        public String toString() {
145            return String.format("CASE: class=%s, accessing=%s,"
146                + " resolve=%s, positive=%s, expectedException=%s", className,
147                accessing, resolve, isPositive, expectedException);
148        }
149    }
150}
151