LookupTypeTest.java revision 12657:6ef01bd40ce2
1132451Sroberto/*
2132451Sroberto * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3132451Sroberto * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4132451Sroberto *
5132451Sroberto * This code is free software; you can redistribute it and/or modify it
6132451Sroberto * under the terms of the GNU General Public License version 2 only, as
7132451Sroberto * published by the Free Software Foundation.
8132451Sroberto *
9132451Sroberto * This code is distributed in the hope that it will be useful, but WITHOUT
10132451Sroberto * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11132451Sroberto * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12132451Sroberto * version 2 for more details (a copy is included in the LICENSE file that
13132451Sroberto * accompanied this code).
14132451Sroberto *
15132451Sroberto * You should have received a copy of the GNU General Public License version
16132451Sroberto * 2 along with this work; if not, write to the Free Software Foundation,
17132451Sroberto * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18132451Sroberto *
19132451Sroberto * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20132451Sroberto * or visit www.oracle.com if you need additional information or have any
21132451Sroberto * questions.
22132451Sroberto */
23132451Sroberto
24132451Sroberto/*
25132451Sroberto * @test
26132451Sroberto * @bug 8136421
27132451Sroberto * @requires vm.jvmci
28132451Sroberto * @library / /test/lib
29132451Sroberto * @library ../common/patches
30132451Sroberto * @modules java.base/jdk.internal.misc
31132451Sroberto * @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot
32132451Sroberto * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
33132451Sroberto * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
34132451Sroberto *                   compiler.jvmci.compilerToVM.LookupTypeTest
35132451Sroberto */
36132451Sroberto
37132451Srobertopackage compiler.jvmci.compilerToVM;
38132451Sroberto
39132451Srobertoimport compiler.jvmci.common.testcases.DoNotExtendClass;
40132451Srobertoimport compiler.jvmci.common.testcases.MultiSubclassedClass;
41132451Srobertoimport compiler.jvmci.common.testcases.SingleSubclass;
42132451Srobertoimport jdk.test.lib.Asserts;
43132451Srobertoimport jdk.test.lib.Utils;
44132451Srobertoimport jdk.vm.ci.hotspot.CompilerToVMHelper;
45132451Srobertoimport jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
46132451Sroberto
47132451Srobertoimport java.util.HashSet;
48132451Srobertoimport java.util.Set;
49132451Sroberto
50132451Srobertopublic class LookupTypeTest {
51132451Sroberto    public static void main(String args[]) {
52132451Sroberto        LookupTypeTest test = new LookupTypeTest();
53132451Sroberto        for (TestCase tcase : createTestCases()) {
54132451Sroberto            test.runTest(tcase);
55132451Sroberto        }
56132451Sroberto    }
57132451Sroberto
58132451Sroberto    private static Set<TestCase> createTestCases() {
59132451Sroberto        Set<TestCase> result = new HashSet<>();
60132451Sroberto        // a primitive class
61132451Sroberto        result.add(new TestCase(Utils.toJVMTypeSignature(int.class),
62132451Sroberto                LookupTypeTest.class, true, false, InternalError.class));
63132451Sroberto        // lookup not existing class
64132451Sroberto        result.add(new TestCase("Lsome_not_existing;", LookupTypeTest.class,
65132451Sroberto                true, false, ClassNotFoundException.class));
66132451Sroberto        // lookup invalid classname
67132451Sroberto        result.add(new TestCase("L!@#$%^&**()[]{}?;", LookupTypeTest.class,
68285612Sdelphij                true, false, ClassNotFoundException.class));
69285612Sdelphij        // lookup package private class
70132451Sroberto        result.add(new TestCase(
71182007Sroberto                "Lcompiler/jvmci/compilerToVM/testcases/PackagePrivateClass;",
72285612Sdelphij                LookupTypeTest.class, true, false,
73182007Sroberto                ClassNotFoundException.class));
74132451Sroberto        // lookup usual class with resolve=true
75285612Sdelphij        result.add(new TestCase(Utils.toJVMTypeSignature(SingleSubclass.class),
76132451Sroberto                LookupTypeTest.class, true, true));
77285612Sdelphij        // lookup usual class with resolve=false
78285612Sdelphij        result.add(new TestCase(
79285612Sdelphij                Utils.toJVMTypeSignature(DoNotExtendClass.class),
80285612Sdelphij                LookupTypeTest.class, false, true));
81285612Sdelphij        // lookup usual class with null accessor
82285612Sdelphij        result.add(new TestCase(
83285612Sdelphij                Utils.toJVMTypeSignature(MultiSubclassedClass.class), null,
84285612Sdelphij                false, false, NullPointerException.class));
85285612Sdelphij        return result;
86285612Sdelphij    }
87285612Sdelphij
88285612Sdelphij    private void runTest(TestCase tcase) {
89285612Sdelphij        System.out.println(tcase);
90285612Sdelphij        HotSpotResolvedObjectType metaspaceKlass;
91285612Sdelphij        try {
92285612Sdelphij            metaspaceKlass = CompilerToVMHelper.lookupType(tcase.className,
93285612Sdelphij                    tcase.accessing, tcase.resolve);
94285612Sdelphij        } catch (Throwable t) {
95285612Sdelphij            Asserts.assertNotNull(tcase.expectedException,
96285612Sdelphij                    "Assumed no exception, but got " + t);
97285612Sdelphij            Asserts.assertFalse(tcase.isPositive,
98285612Sdelphij                    "Got unexpected exception " + t);
99182007Sroberto            Asserts.assertEQ(t.getClass(), tcase.expectedException,
100182007Sroberto                    "Unexpected exception");
101182007Sroberto            // passed
102182007Sroberto            return;
103132451Sroberto        }
104285612Sdelphij        if (tcase.expectedException != null) {
105285612Sdelphij            throw new AssertionError("Expected exception was not thrown: "
106132451Sroberto                    + tcase.expectedException.getName());
107132451Sroberto        }
108182007Sroberto        if (tcase.isPositive) {
109132451Sroberto            Asserts.assertNotNull(metaspaceKlass,
110285612Sdelphij                    "Unexpected null metaspace klass");
111285612Sdelphij            Asserts.assertEQ(metaspaceKlass.getName(), tcase.className,
112285612Sdelphij                    "Got unexpected resolved class name");
113285612Sdelphij        } else {
114285612Sdelphij            Asserts.assertNull(metaspaceKlass, "Unexpected metaspace klass");
115182007Sroberto        }
116285612Sdelphij    }
117285612Sdelphij
118285612Sdelphij    private static class TestCase {
119182007Sroberto        public final String className;
120285612Sdelphij        public final Class<?> accessing;
121182007Sroberto        public final boolean resolve;
122132451Sroberto        public final boolean isPositive;
123285612Sdelphij        public final Class<? extends Throwable> expectedException;
124285612Sdelphij
125285612Sdelphij        public TestCase(String className, Class<?> accessing, boolean resolve,
126285612Sdelphij                boolean isPositive,
127132451Sroberto                Class<? extends Throwable> expectedException) {
128182007Sroberto            this.className = className;
129182007Sroberto            this.accessing = accessing;
130132451Sroberto            this.resolve = resolve;
131182007Sroberto            this.isPositive = isPositive;
132182007Sroberto            this.expectedException = expectedException;
133182007Sroberto        }
134285612Sdelphij
135182007Sroberto        public TestCase(String className, Class<?> accessing, boolean resolve,
136182007Sroberto                boolean isPositive) {
137132451Sroberto            this.className = className;
138182007Sroberto            this.accessing = accessing;
139182007Sroberto            this.resolve = resolve;
140182007Sroberto            this.isPositive = isPositive;
141182007Sroberto            this.expectedException = null;
142182007Sroberto        }
143182007Sroberto
144182007Sroberto        @Override
145182007Sroberto        public String toString() {
146132451Sroberto            return String.format("CASE: class=%s, accessing=%s,"
147182007Sroberto                + " resolve=%s, positive=%s, expectedException=%s", className,
148182007Sroberto                accessing, resolve, isPositive, expectedException);
149132451Sroberto        }
150132451Sroberto    }
151182007Sroberto}
152182007Sroberto