1 /*
   2  * Copyright (c) 2013, 2016, 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 
  24 /**
  25  * @test
  26  * @summary Verify the defining class loader of each module never delegates
  27  *          to its child class loader. Also sanity check java.compact2
  28  *          requires.
  29  * @modules java.compact2
  30  * @run testng/othervm --add-modules=ALL-SYSTEM VerifyModuleDelegation
  31  */
  32 
  33 import java.lang.module.ModuleDescriptor;
  34 import java.lang.module.ModuleFinder;
  35 import java.lang.module.ModuleReference;
  36 import java.lang.reflect.Layer;
  37 import java.lang.reflect.Module;
  38 import java.util.Set;
  39 import static java.util.stream.Collectors.toSet;
  40 
  41 import static java.lang.module.ModuleDescriptor.Requires.Modifier.*;
  42 
  43 import org.testng.annotations.*;
  44 
  45 import static org.testng.Assert.*;
  46 
  47 public class VerifyModuleDelegation {
  48     private static final String JAVA_BASE = "java.base";
  49     private static final String JAVA_COMPACT1 = "java.compact1";
  50     private static final String JAVA_COMPACT2 = "java.compact2";
  51 
  52     private static final ModuleDescriptor BASE
  53         = new ModuleDescriptor.Builder(JAVA_BASE).build();
  54 
  55     private static final ModuleDescriptor COMPACT2
  56         = new ModuleDescriptor.Builder(JAVA_COMPACT2)
  57             .requires(Set.of(MANDATED), JAVA_BASE)
  58             .requires(Set.of(PUBLIC), JAVA_COMPACT1)
  59             .requires(Set.of(PUBLIC), "java.rmi")
  60             .requires(Set.of(PUBLIC), "java.sql")
  61             .requires(Set.of(PUBLIC), "java.xml")
  62             .build();
  63 
  64     private static final Set<ModuleDescriptor> MREFS
  65             = Layer.boot().modules().stream().map(Module::getDescriptor)
  66                 .collect(toSet());
  67 
  68     private void check(ModuleDescriptor md, ModuleDescriptor ref) {
  69         assertTrue(md.requires().size() == ref.requires().size());
  70         assertTrue(md.requires().containsAll(ref.requires()));
  71     }
  72 
  73     @Test
  74     public void checkJavaBase() {
  75         ModuleDescriptor md =
  76                 MREFS.stream()
  77                      .filter(d -> d.name().equals(JAVA_BASE))
  78                      .findFirst().orElseThrow(Error::new);
  79 
  80         check(md, BASE);
  81     }
  82     @Test
  83     public void checkCompact2() {
  84         ModuleDescriptor md =
  85                 MREFS.stream()
  86                      .filter(d -> d.name().equals(JAVA_COMPACT2))
  87                      .findFirst().orElseThrow(Error::new);
  88         check(md, COMPACT2);
  89     }
  90 
  91     @Test
  92     public void checkLoaderDelegation() {
  93         Layer boot = Layer.boot();
  94         MREFS.stream()
  95              .forEach(md -> md.requires().stream().forEach(req ->
  96                  {
  97                      // check if M requires D and D's loader must be either the
  98                      // same or an ancestor of M's loader
  99                      ClassLoader loader1 = boot.findLoader(md.name());
 100                      ClassLoader loader2 = boot.findLoader(req.name());
 101                      if (loader1 != loader2 && !isAncestor(loader2, loader1)) {
 102                          throw new Error(md.name() + " can't delegate to " +
 103                                          "find classes from " + req.name());
 104                      }
 105                  }));
 106     }
 107 
 108     // Returns true if p is an ancestor of cl i.e. class loader 'p' can
 109     // be found in the cl's delegation chain
 110     private static boolean isAncestor(ClassLoader p, ClassLoader cl) {
 111         if (p != null && cl == null) {
 112             return false;
 113         }
 114         ClassLoader acl = cl;
 115         do {
 116             acl = acl.getParent();
 117             if (p == acl) {
 118                 return true;
 119             }
 120         } while (acl != null);
 121         return false;
 122     }
 123 }