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
24package jdk.test;
25
26import java.net.URL;
27import java.net.URLClassLoader;
28import static jdk.test.ProxyTest.*;
29
30public class Main {
31    public static void main(String... args) {
32        ProxyTest ptest = new jdk.test.ProxyTest();
33        for (Data d : proxiesForExportedTypes()) {
34            ptest.test(d);
35        }
36
37        for (Data d : proxiesForPackagePrivateTypes()) {
38            ptest.test(d);
39        }
40
41        for (Data d : proxiesForModulePrivateTypes()) {
42            ptest.test(d);
43        }
44
45        for (Data d : proxiesWithAddReads()) {
46            ptest.test(d);
47        }
48
49        for (Data d : proxiesWithAddExports()) {
50            ptest.test(d);
51        }
52    }
53
54    private final static Module m1 = p.one.I.class.getModule();
55    private final static Module m2 = p.two.A.class.getModule();
56    private final static Module m3 = p.three.P.class.getModule();
57    private final static Module test = Main.class.getModule();
58    private final static Class<?> unnamedModuleClass = classForName("q.U");
59    private final static Class<?> m3InternalType = classForName("p.three.internal.Q");
60
61    /*
62     * Test cases for proxy class to implement exported proxy interfaces
63     * will result in the unnamed module.
64     *
65     * The proxy class is accessible to unnamed module.
66     */
67    static Data[] proxiesForExportedTypes() {
68        ClassLoader ld = Main.class.getClassLoader();
69        Module unnamed = ld.getUnnamedModule();
70        ClassLoader ld2 = new URLClassLoader(new URL[0], ld);
71        Module unnamed2 = ld2.getUnnamedModule();
72
73        return new Data[] {
74            new Data(unnamed,  ld, Runnable.class),
75            new Data(unnamed,  ld, p.one.I.class),
76            new Data(unnamed,  ld, p.one.I.class, p.two.A.class),
77            new Data(unnamed,  ld, p.one.I.class, unnamedModuleClass),
78            new Data(unnamed2, ld2, Runnable.class),
79            new Data(unnamed2, ld2, p.one.I.class),
80            new Data(unnamed2, ld2, p.one.I.class, p.two.A.class),
81            new Data(unnamed2, ld2, p.one.I.class, unnamedModuleClass),
82            new Data(unnamed, m1.getClassLoader(), p.one.I.class),
83            new Data(unnamed, m2.getClassLoader(), p.two.A.class),
84            new Data(unnamed, m3.getClassLoader(), p.three.P.class),
85        };
86    }
87
88    /*
89     * Test cases for proxy class to implement package-private proxy interface
90     * will result in same runtime package and same module as the proxy interface
91     *
92     * The proxy class is accessible to classes in the same runtime package
93     */
94    static Data[] proxiesForPackagePrivateTypes() {
95        Class<?> bClass = classForName("p.two.B");  // package-private type
96
97        return new Data[] {
98            new Data(m2, m2.getClassLoader(), p.two.A.class, bClass),
99            new Data(m2, m2.getClassLoader(), p.two.A.class, bClass, p.two.internal.C.class)
100        };
101    }
102
103    /*
104     * Test cases for proxy class to implement one or more module-private types.
105     *
106     * This will result in a dynamic module which can read the modules of the interfaces
107     * and their dependences and also qualified exports to the module-private packages.
108     * The proxy class is not accessible to any module.
109     */
110    static Data[] proxiesForModulePrivateTypes() {
111        ClassLoader ld = Main.class.getClassLoader();
112        ClassLoader customLoader = new URLClassLoader(new URL[0], ld);
113        return new Data[] {
114            // different loaders
115            new Data(m1.getClassLoader(), p.one.internal.J.class),
116            new Data(customLoader, p.one.internal.J.class),
117            new Data(m2.getClassLoader(), p.two.internal.C.class, Runnable.class),
118            // interfaces from m2 only
119            new Data(m2.getClassLoader(), p.two.internal.C.class, p.two.A.class),
120            // p.two.A is accessible to m3
121            new Data(m3.getClassLoader(), m3InternalType, p.two.A.class),
122            new Data(customLoader, unnamedModuleClass, jdk.test.internal.R.class, p.one.I.class),
123            // two module-private types in two different modules
124            new Data(m3.getClassLoader(), p.two.internal.C.class, m3InternalType),
125            new Data(m3.getClassLoader(), p.three.P.class, m3InternalType, jdk.test.internal.R.class),
126        };
127    }
128
129    /*
130     * Test cases for proxy class to implement accessible proxy interfaces
131     * after addReads. That does not change the target module.
132     */
133    static Data[] proxiesWithAddReads() {
134        Module unnamed = test.getClassLoader().getUnnamedModule();
135        test.addReads(unnamed);
136        return new Data[] {
137             new Data(test.getClassLoader(),
138                      unnamedModuleClass, p.one.I.class,
139                      jdk.test.internal.R.class), // module-private interface in test
140        };
141    }
142
143    /*
144     * Test cases for proxy class to implement accessible proxy interfaces
145     * after addExports.  That does not change the target module.
146     */
147    static Data[] proxiesWithAddExports() {
148        return new Data[] {
149                new Data(test.getClassLoader(),
150                         p.one.internal.J.class,
151                         p.two.internal.C.class), // module-private interfaces in m2 and m3
152        };
153    }
154
155    static Class<?> classForName(String cn) {
156        try {
157            return Class.forName(cn);
158        } catch (ClassNotFoundException e) {
159            throw new RuntimeException(e);
160        }
161    }
162}
163