jdk/test/jdk/modules/etc/VerifyModuleDelegation.java
author mchung
Thu, 15 Jun 2017 11:54:49 -0700
changeset 45643 8e4ef8645d00
parent 44545 83b611b88ac8
permissions -rw-r--r--
8182032: Make java.compiler upgradeable Reviewed-by: alanb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
45643
8e4ef8645d00 8182032: Make java.compiler upgradeable
mchung
parents: 44545
diff changeset
     2
 * Copyright (c) 2013, 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
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Verify the defining class loader of each module never delegates
42693
6645de32a866 8171201: Drop java.compact$N aggregator modules
mchung
parents: 42338
diff changeset
    27
 *          to its child class loader.
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36928
diff changeset
    28
 * @run testng/othervm --add-modules=ALL-SYSTEM VerifyModuleDelegation
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Set;
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    33
import static java.util.stream.Collectors.toSet;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import org.testng.annotations.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
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
public class VerifyModuleDelegation {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
    private static final String JAVA_BASE = "java.base";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
    private static final ModuleDescriptor BASE
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42693
diff changeset
    43
        = ModuleDescriptor.newModule(JAVA_BASE).build();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    45
    private static final Set<ModuleDescriptor> MREFS
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43712
diff changeset
    46
            = ModuleLayer.boot().modules().stream().map(Module::getDescriptor)
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    47
                .collect(toSet());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
    private void check(ModuleDescriptor md, ModuleDescriptor ref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        assertTrue(md.requires().size() == ref.requires().size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        assertTrue(md.requires().containsAll(ref.requires()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    public void checkJavaBase() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        ModuleDescriptor md =
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    57
                MREFS.stream()
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
                     .filter(d -> d.name().equals(JAVA_BASE))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
                     .findFirst().orElseThrow(Error::new);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        check(md, BASE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    public void checkLoaderDelegation() {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43712
diff changeset
    66
        ModuleLayer boot = ModuleLayer.boot();
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    67
        MREFS.stream()
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
             .forEach(md -> md.requires().stream().forEach(req ->
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
                 {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
                     // check if M requires D and D's loader must be either the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
                     // same or an ancestor of M's loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
                     ClassLoader loader1 = boot.findLoader(md.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
                     ClassLoader loader2 = boot.findLoader(req.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
                     if (loader1 != loader2 && !isAncestor(loader2, loader1)) {
45643
8e4ef8645d00 8182032: Make java.compiler upgradeable
mchung
parents: 44545
diff changeset
    75
                         throw new Error(loader1.getName() + "/" + md.name() +
8e4ef8645d00 8182032: Make java.compiler upgradeable
mchung
parents: 44545
diff changeset
    76
                             " can't delegate to find classes from " +
8e4ef8645d00 8182032: Make java.compiler upgradeable
mchung
parents: 44545
diff changeset
    77
                             loader2.getName() + "/" + req.name());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
                     }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
                 }));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    // Returns true if p is an ancestor of cl i.e. class loader 'p' can
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    // be found in the cl's delegation chain
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    private static boolean isAncestor(ClassLoader p, ClassLoader cl) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        if (p != null && cl == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        ClassLoader acl = cl;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        do {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            acl = acl.getParent();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            if (p == acl) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        } while (acl != null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
}