jdk/test/java/lang/reflect/Proxy/ProxyLayerTest.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 43712 5dfd0950317c
child 45393 de4e1efc8eec
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
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.InvocationHandler;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.lang.reflect.Proxy;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import static jdk.testlibrary.ProcessTools.executeTestJava;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import org.testng.annotations.BeforeTest;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import org.testng.annotations.Test;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import static org.testng.Assert.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * @library /lib/testlibrary
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 * @modules jdk.compiler
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * @build ProxyTest CompilerUtils jdk.testlibrary.ProcessTools
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 * @run testng ProxyLayerTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 * @summary Test proxies to implement interfaces in a layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
public class ProxyLayerTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
    private static final String TEST_SRC = System.getProperty("test.src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
    private static final String TEST_CLASSES = System.getProperty("test.classes");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    private static final Path MODS_DIR = Paths.get("mods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    private static final Path CPATH_DIR = Paths.get(TEST_CLASSES);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    // the names of the modules in this test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    private static String[] modules = new String[] {"m1", "m2", "m3"};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
     * Compiles all modules used by the test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    @BeforeTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    public void compileAll() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        for (String mn : modules) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            Path msrc = SRC_DIR.resolve(mn);
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
    67
            assertTrue(CompilerUtils.compile(msrc, MODS_DIR, "--module-source-path", SRC_DIR.toString()));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        }
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
    /**
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43712
diff changeset
    72
     * Test proxy implementing interfaces in a layer defined in
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
     * an unnamed module
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 testProxyInUnnamed() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        ModuleFinder finder = ModuleFinder.of(MODS_DIR);
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43712
diff changeset
    78
        ModuleLayer bootLayer = ModuleLayer.boot();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        Configuration cf = bootLayer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
                .configuration()
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 40261
diff changeset
    81
                .resolveAndBind(ModuleFinder.of(), finder, Arrays.asList(modules));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        ClassLoader scl = ClassLoader.getSystemClassLoader();
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43712
diff changeset
    83
        ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        ClassLoader loader = layer.findLoader("m1");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        assertTrue(layer.findModule("m1").isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        assertTrue(layer.findModule("m2").isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        assertTrue(layer.findModule("m3").isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        Class<?>[] interfaces = new Class<?>[] {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            Class.forName("p.one.I", false, loader),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
            Class.forName("p.two.A", false, loader),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            Class.forName("p.three.P", false, loader),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        Object o = Proxy.newProxyInstance(loader, interfaces, handler);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        Class<?> proxyClass = o.getClass();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        Package pkg = proxyClass.getPackage();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        assertFalse(proxyClass.getModule().isNamed());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        assertFalse(pkg.isSealed());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        assertEquals(proxyClass.getModule().getLayer(), null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
     * Test proxy implementing interfaces in a Layer and defined in a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
     * dynamic module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    public void testProxyInDynamicModule() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        ModuleFinder finder = ModuleFinder.of(MODS_DIR);
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43712
diff changeset
   112
        ModuleLayer bootLayer = ModuleLayer.boot();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        Configuration cf = bootLayer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                .configuration()
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 40261
diff changeset
   115
                .resolveAndBind(ModuleFinder.of(), finder, Arrays.asList(modules));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        ClassLoader scl = ClassLoader.getSystemClassLoader();
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43712
diff changeset
   117
        ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        ClassLoader loader = layer.findLoader("m1");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        assertTrue(layer.findModule("m1").isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        assertTrue(layer.findModule("m2").isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        assertTrue(layer.findModule("m3").isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        Class<?>[] interfaces = new Class<?>[] {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
            Class.forName("p.one.internal.J", false, loader),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        Object o = Proxy.newProxyInstance(loader, interfaces, handler);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        Class<?> proxyClass = o.getClass();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        Package pkg = proxyClass.getPackage();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        assertTrue(proxyClass.getModule().isNamed());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        assertTrue(pkg.isSealed());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        assertEquals(proxyClass.getModule().getLayer(), null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
     * Test proxy implementing interfaces that the target module has no access
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
    public void testNoReadAccess() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        ModuleFinder finder = ModuleFinder.of(MODS_DIR);
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43712
diff changeset
   142
        ModuleLayer bootLayer = ModuleLayer.boot();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        Configuration cf = bootLayer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                .configuration()
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 40261
diff changeset
   145
                .resolveAndBind(ModuleFinder.of(), finder, Arrays.asList(modules));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        ClassLoader scl = ClassLoader.getSystemClassLoader();
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43712
diff changeset
   147
        ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        ClassLoader loader = layer.findLoader("m1");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        assertTrue(layer.findModule("m1").isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        assertTrue(layer.findModule("m2").isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        assertTrue(layer.findModule("m3").isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        Class<?>[] interfaces = new Class<?>[] {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                Class.forName("p.one.I", false, loader),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                Class.forName("p.two.B", false, loader)   // non-public interface but exported package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        checkIAE(loader, interfaces);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
    private void checkIAE(ClassLoader loader, Class<?>[] interfaces) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
            Proxy.getProxyClass(loader, interfaces);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            throw new RuntimeException("Expected IllegalArgumentException thrown");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        } catch (IllegalArgumentException e) {}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
            Proxy.newProxyInstance(loader, interfaces, handler);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
            throw new RuntimeException("Expected IllegalArgumentException thrown");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        } catch (IllegalArgumentException e) {}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
    private final static InvocationHandler handler =
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            (proxy, m, params) -> { throw new RuntimeException(m.toString()); };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
}