jdk/test/java/lang/reflect/Proxy/ProxyClassAccessTest.java
author iignatyev
Tue, 06 Jun 2017 19:54:08 -0700
changeset 45393 de4e1efc8eec
parent 44545 83b611b88ac8
permissions -rw-r--r--
8181335: remove packageless CompilerUtils Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
45393
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 44545
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
import java.lang.module.Configuration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import java.lang.module.ModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.lang.reflect.Proxy;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
45393
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 44545
diff changeset
    32
import jdk.test.lib.compiler.CompilerUtils;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import static jdk.testlibrary.ProcessTools.executeTestJava;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import org.testng.annotations.BeforeTest;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import org.testng.annotations.Test;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import static org.testng.Assert.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * @test
45393
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 44545
diff changeset
    41
 * @library /lib/testlibrary /test/lib
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * @modules jdk.compiler
45393
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 44545
diff changeset
    43
 * @build ProxyClassAccessTest q.NP jdk.testlibrary.*
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 44545
diff changeset
    44
 *        jdk.test.lib.compiler.CompilerUtils
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 * @run testng ProxyClassAccessTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 * @summary Driver for testing proxy class doesn't have access to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 *          types referenced by proxy interfaces
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
public class ProxyClassAccessTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    private static final String TEST_SRC = System.getProperty("test.src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    private static final String TEST_CLASSES = System.getProperty("test.classes");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    private static final Path MODS_DIR = Paths.get("mods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    // the names of the modules in this test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    private static List<String> modules = Arrays.asList("m1", "m2", "m3", "test");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
     * Compiles all modules used by the test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    @BeforeTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    public void compileAll() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        for (String mn : modules) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            Path msrc = SRC_DIR.resolve(mn);
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
    68
            assertTrue(CompilerUtils.compile(msrc, MODS_DIR, "--module-source-path", SRC_DIR.toString()));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
     * Run the modular test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    public void runTest() throws Exception {
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
    77
        int exitValue = executeTestJava("--module-path", MODS_DIR.toString(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
                                        "-m", "test/jdk.test.ProxyClassAccess")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
                            .outputTo(System.out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
                            .errorTo(System.out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
                            .getExitValue();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        assertTrue(exitValue == 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
     * Test unnamed module has no access to other proxy interface
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    public void testNoReadAccess() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        ModuleFinder finder = ModuleFinder.of(MODS_DIR);
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43712
diff changeset
    92
        ModuleLayer bootLayer = ModuleLayer.boot();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        Configuration cf = bootLayer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                .configuration()
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 40261
diff changeset
    95
                .resolveAndBind(ModuleFinder.of(), finder, modules);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        ClassLoader parentLoader = this.getClass().getClassLoader();
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43712
diff changeset
    97
        ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, parentLoader);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        ClassLoader loader = layer.findLoader("m1");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        Class<?>[] interfaces = new Class<?>[] {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                Class.forName("p.one.I", false, loader),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                Class.forName("q.NP", false, loader)     // non-public interface in unnamed module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        checkIAE(loader, interfaces);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    private void checkIAE(ClassLoader loader, Class<?>[] interfaces) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
            Proxy.getProxyClass(loader, interfaces);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
            throw new RuntimeException("Expected IllegalArgumentException thrown");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        } catch (IllegalArgumentException e) {}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            Proxy.newProxyInstance(loader, interfaces,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                (proxy, m, params) -> { throw new RuntimeException(m.toString()); });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            throw new RuntimeException("Expected IllegalArgumentException thrown");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        } catch (IllegalArgumentException e) {}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
}