jdk/test/jdk/modules/etc/VerifyModuleDelegation.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 40531 d1fd060ce6b7
child 42693 6645de32a866
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
     2
 * Copyright (c) 2013, 2016, 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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 *          to its child class loader. Also sanity check java.compact2
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 *          requires.
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    29
 * @modules java.compact2
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36928
diff changeset
    30
 * @run testng/othervm --add-modules=ALL-SYSTEM VerifyModuleDelegation
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.module.ModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.lang.reflect.Layer;
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    37
import java.lang.reflect.Module;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Set;
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    39
import static java.util.stream.Collectors.toSet;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import static java.lang.module.ModuleDescriptor.Requires.Modifier.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import org.testng.annotations.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import static org.testng.Assert.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
public class VerifyModuleDelegation {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
    private static final String JAVA_BASE = "java.base";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
    private static final String JAVA_COMPACT1 = "java.compact1";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
    private static final String JAVA_COMPACT2 = "java.compact2";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    private static final ModuleDescriptor BASE
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40531
diff changeset
    53
        = ModuleDescriptor.module(JAVA_BASE).build();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    private static final ModuleDescriptor COMPACT2
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40531
diff changeset
    56
        = ModuleDescriptor.module(JAVA_COMPACT2)
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36928
diff changeset
    57
            .requires(Set.of(MANDATED), JAVA_BASE)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40531
diff changeset
    58
            .requires(Set.of(TRANSITIVE), JAVA_COMPACT1)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40531
diff changeset
    59
            .requires(Set.of(TRANSITIVE), "java.rmi")
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40531
diff changeset
    60
            .requires(Set.of(TRANSITIVE), "java.sql")
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40531
diff changeset
    61
            .requires(Set.of(TRANSITIVE), "java.xml")
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
            .build();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    64
    private static final Set<ModuleDescriptor> MREFS
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    65
            = Layer.boot().modules().stream().map(Module::getDescriptor)
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    66
                .collect(toSet());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    private void check(ModuleDescriptor md, ModuleDescriptor ref) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        assertTrue(md.requires().size() == ref.requires().size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        assertTrue(md.requires().containsAll(ref.requires()));
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
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    public void checkJavaBase() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        ModuleDescriptor md =
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    76
                MREFS.stream()
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
                     .filter(d -> d.name().equals(JAVA_BASE))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
                     .findFirst().orElseThrow(Error::new);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        check(md, BASE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    public void checkCompact2() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        ModuleDescriptor md =
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    85
                MREFS.stream()
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                     .filter(d -> d.name().equals(JAVA_COMPACT2))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                     .findFirst().orElseThrow(Error::new);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        check(md, COMPACT2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    public void checkLoaderDelegation() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        Layer boot = Layer.boot();
40531
d1fd060ce6b7 8163126: Fix @modules in some of jdk/* tests
shurailine
parents: 40261
diff changeset
    94
        MREFS.stream()
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
             .forEach(md -> md.requires().stream().forEach(req ->
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                 {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                     // check if M requires D and D's loader must be either the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                     // same or an ancestor of M's loader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                     ClassLoader loader1 = boot.findLoader(md.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                     ClassLoader loader2 = boot.findLoader(req.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                     if (loader1 != loader2 && !isAncestor(loader2, loader1)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                         throw new Error(md.name() + " can't delegate to " +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
                                         "find classes from " + req.name());
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
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    // Returns true if p is an ancestor of cl i.e. class loader 'p' can
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
    // be found in the cl's delegation chain
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    private static boolean isAncestor(ClassLoader p, ClassLoader cl) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        if (p != null && cl == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        ClassLoader acl = cl;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        do {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            acl = acl.getParent();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            if (p == acl) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        } while (acl != null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
}