jdk/test/java/lang/ModuleTests/BasicModuleTest.java
changeset 44545 83b611b88ac8
child 45652 33342314ce89
equal deleted inserted replaced
44480:2c33418a6d57 44545:83b611b88ac8
       
     1 /*
       
     2  * Copyright (c) 2014, 2017, 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 import java.lang.module.ModuleDescriptor.Exports;
       
    25 import java.lang.module.ResolvedModule;
       
    26 import java.nio.file.spi.FileSystemProvider;  // service type in java.base
       
    27 import java.util.function.Predicate;
       
    28 import java.util.stream.Stream;
       
    29 import javax.print.PrintServiceLookup;        // service type in java.desktop
       
    30 
       
    31 import org.testng.annotations.Test;
       
    32 import static org.testng.Assert.*;
       
    33 
       
    34 /*
       
    35  * @test
       
    36  * @summary Basic test of java.lang.Module
       
    37  * @modules java.desktop java.xml
       
    38  * @run testng BasicModuleTest
       
    39  */
       
    40 
       
    41 public class BasicModuleTest {
       
    42 
       
    43     /**
       
    44      * Tests that the given module reads all modules in the boot layer.
       
    45      */
       
    46     private void testReadsAllBootModules(Module m) {
       
    47         ModuleLayer bootLayer = ModuleLayer.boot();
       
    48         bootLayer.configuration()
       
    49             .modules()
       
    50             .stream()
       
    51             .map(ResolvedModule::name)
       
    52             .map(bootLayer::findModule)
       
    53             .forEach(target -> assertTrue(m.canRead(target.get())));
       
    54     }
       
    55 
       
    56     /**
       
    57      * Returns a {@code Predicate} to test if a package is exported.
       
    58      */
       
    59     private Predicate<Exports> doesExport(String pn) {
       
    60         return e -> (e.source().equals(pn) && !e.isQualified());
       
    61     }
       
    62 
       
    63 
       
    64 
       
    65     @Test
       
    66     public void testThisModule() {
       
    67         Module thisModule = BasicModuleTest.class.getModule();
       
    68         Module baseModule = Object.class.getModule();
       
    69 
       
    70         assertFalse(thisModule.isNamed());
       
    71         assertTrue(thisModule.getName() == null);
       
    72         assertTrue(thisModule.getDescriptor() == null);
       
    73         assertTrue(thisModule.getLayer() == null);
       
    74         assertTrue(thisModule.toString().startsWith("unnamed module "));
       
    75 
       
    76         ClassLoader thisLoader = BasicModuleTest.class.getClassLoader();
       
    77         assertTrue(thisLoader == thisModule.getClassLoader());
       
    78         assertTrue(thisLoader.getUnnamedModule() == thisModule);
       
    79 
       
    80         // unnamed modules read all other modules
       
    81         ClassLoader cl;
       
    82         cl = ClassLoader.getPlatformClassLoader();
       
    83         assertTrue(thisModule.canRead(cl.getUnnamedModule()));
       
    84         cl = ClassLoader.getSystemClassLoader();
       
    85         assertTrue(thisModule.canRead(cl.getUnnamedModule()));
       
    86         testReadsAllBootModules(thisModule);
       
    87 
       
    88         // unnamed modules export all packages
       
    89         assertTrue(thisModule.isExported(""));
       
    90         assertTrue(thisModule.isExported("", thisModule));
       
    91         assertTrue(thisModule.isExported("", baseModule));
       
    92         assertTrue(thisModule.isExported("p"));
       
    93         assertTrue(thisModule.isExported("p", thisModule));
       
    94         assertTrue(thisModule.isExported("p", baseModule));
       
    95 
       
    96         // this test is in the unnamed package
       
    97         assertTrue(thisModule.getPackages().contains(""));
       
    98     }
       
    99 
       
   100 
       
   101     @Test
       
   102     public void testUnnamedModules() {
       
   103         Module thisModule = BasicModuleTest.class.getModule();
       
   104         Module baseModule = Object.class.getModule();
       
   105 
       
   106         ClassLoader loader1 = ClassLoader.getSystemClassLoader();
       
   107         ClassLoader loader2 = loader1.getParent();
       
   108 
       
   109         Module m1 = loader1.getUnnamedModule();
       
   110         Module m2 = loader2.getUnnamedModule();
       
   111 
       
   112         assertTrue(m1 != m2);
       
   113 
       
   114         assertFalse(m1.isNamed());
       
   115         assertFalse(m2.isNamed());
       
   116 
       
   117         assertTrue(m1.getLayer() == null);
       
   118         assertTrue(m2.getLayer() == null);
       
   119 
       
   120         assertTrue(m1.toString().startsWith("unnamed module "));
       
   121         assertTrue(m2.toString().startsWith("unnamed module "));
       
   122 
       
   123         // unnamed module reads all modules
       
   124         assertTrue(m1.canRead(m2));
       
   125         assertTrue(m2.canRead(m1));
       
   126 
       
   127         testReadsAllBootModules(m1);
       
   128         testReadsAllBootModules(m2);
       
   129 
       
   130         assertTrue(m1.isExported(""));
       
   131         assertTrue(m1.isExported("", thisModule));
       
   132         assertTrue(m1.isExported("", baseModule));
       
   133         assertTrue(m1.isExported("p"));
       
   134         assertTrue(m1.isExported("p", thisModule));
       
   135         assertTrue(m1.isExported("p", baseModule));
       
   136     }
       
   137 
       
   138 
       
   139 
       
   140     @Test
       
   141     public void testBaseModule() {
       
   142         Module base = Object.class.getModule();
       
   143         Module thisModule = BasicModuleTest.class.getModule();
       
   144 
       
   145         // getName
       
   146         assertTrue(base.getName().equals("java.base"));
       
   147 
       
   148         // getDescriptor
       
   149         assertTrue(base.getDescriptor().exports().stream()
       
   150                 .anyMatch(doesExport("java.lang")));
       
   151 
       
   152         // getClassLoader
       
   153         assertTrue(base.getClassLoader() == null);
       
   154 
       
   155         // getLayer
       
   156         assertTrue(base.getLayer() == ModuleLayer.boot());
       
   157 
       
   158         // toString
       
   159         assertEquals(base.toString(), "module java.base");
       
   160 
       
   161         // getPackages
       
   162         assertTrue(base.getPackages().contains("java.lang"));
       
   163 
       
   164         // canRead
       
   165         assertTrue(base.canRead(base));
       
   166         assertFalse(base.canRead(thisModule));
       
   167 
       
   168         // addReads
       
   169         try {
       
   170             base.addReads(thisModule);
       
   171             assertTrue(false);
       
   172         } catch (IllegalCallerException expected) { }
       
   173         assertFalse(base.canRead(thisModule));
       
   174 
       
   175         // isExported
       
   176         assertTrue(base.isExported("java.lang"));
       
   177         assertTrue(base.isExported("java.lang", thisModule));
       
   178         assertTrue(base.isExported("java.lang", base));
       
   179         assertFalse(base.isExported("jdk.internal.misc"));
       
   180         assertFalse(base.isExported("jdk.internal.misc", thisModule));
       
   181         assertTrue(base.isExported("jdk.internal.misc", base));
       
   182         assertFalse(base.isExported("java.wombat"));
       
   183         assertFalse(base.isExported("java.wombat", thisModule));
       
   184         assertFalse(base.isExported("java.wombat", base));
       
   185 
       
   186         // addExports
       
   187         try {
       
   188             base.addExports("java.lang", thisModule);
       
   189             assertTrue(false);
       
   190         } catch (IllegalCallerException expected) { }
       
   191         try {
       
   192             base.addExports("jdk.internal.misc", thisModule);
       
   193             assertTrue(false);
       
   194         } catch (IllegalCallerException expected) { }
       
   195         assertFalse(base.isExported("jdk.internal.misc"));
       
   196         assertFalse(base.isExported("jdk.internal.misc", thisModule));
       
   197 
       
   198         // isOpen
       
   199         assertFalse(base.isOpen("java.lang"));
       
   200         assertFalse(base.isOpen("java.lang", thisModule));
       
   201         assertTrue(base.isOpen("java.lang", base));
       
   202         assertFalse(base.isOpen("jdk.internal.misc"));
       
   203         assertFalse(base.isOpen("jdk.internal.misc", thisModule));
       
   204         assertTrue(base.isOpen("jdk.internal.misc", base));
       
   205         assertFalse(base.isOpen("java.wombat"));
       
   206         assertFalse(base.isOpen("java.wombat", thisModule));
       
   207         assertFalse(base.isOpen("java.wombat", base));
       
   208 
       
   209         // addOpens
       
   210         try {
       
   211             base.addOpens("jdk.internal.misc", thisModule);
       
   212             assertTrue(false);
       
   213         } catch (IllegalCallerException expected) { }
       
   214         assertFalse(base.isOpen("jdk.internal.misc"));
       
   215         assertFalse(base.isOpen("jdk.internal.misc", thisModule));
       
   216 
       
   217         // canUse
       
   218         assertTrue(base.canUse(FileSystemProvider.class));
       
   219         assertFalse(base.canUse(Thread.class));
       
   220 
       
   221         // addUses
       
   222         try {
       
   223             base.addUses(FileSystemProvider.class);
       
   224             assertTrue(false);
       
   225         } catch (IllegalCallerException expected) { }
       
   226         try {
       
   227             base.addUses(Thread.class);
       
   228             assertTrue(false);
       
   229         } catch (IllegalCallerException expected) { }
       
   230         assertFalse(base.canUse(Thread.class));
       
   231     }
       
   232 
       
   233 
       
   234     @Test
       
   235     public void testDesktopModule() {
       
   236         Module desktop = java.awt.Component.class.getModule();
       
   237         Module base = Object.class.getModule();
       
   238         Module xml = javax.xml.XMLConstants.class.getModule();
       
   239         Module thisModule = BasicModuleTest.class.getModule();
       
   240 
       
   241         // name
       
   242         assertTrue(desktop.getName().equals("java.desktop"));
       
   243 
       
   244         // descriptor
       
   245         assertTrue(desktop.getDescriptor().exports().stream()
       
   246                    .anyMatch(doesExport("java.awt")));
       
   247 
       
   248         // getClassLoader
       
   249         assertTrue(desktop.getClassLoader() == null);
       
   250 
       
   251         // getLayer
       
   252         assertTrue(desktop.getLayer() == ModuleLayer.boot());
       
   253 
       
   254         // toString
       
   255         assertEquals(desktop.toString(), "module java.desktop");
       
   256 
       
   257         // getPackages
       
   258         assertTrue(desktop.getPackages().contains("java.awt"));
       
   259         assertTrue(desktop.getPackages().contains("sun.awt"));
       
   260 
       
   261         // canRead
       
   262         assertTrue(desktop.canRead(base));
       
   263         assertTrue(desktop.canRead(xml));
       
   264 
       
   265         // addReads
       
   266         try {
       
   267             desktop.addReads(thisModule);
       
   268             assertTrue(false);
       
   269         } catch (IllegalCallerException expected) { }
       
   270         assertFalse(desktop.canRead(thisModule));
       
   271 
       
   272         // isExported
       
   273         assertTrue(desktop.isExported("java.awt"));
       
   274         assertTrue(desktop.isExported("java.awt", thisModule));
       
   275         assertFalse(desktop.isExported("sun.awt"));
       
   276         assertFalse(desktop.isExported("sun.awt", thisModule));
       
   277         assertTrue(desktop.isExported("sun.awt", desktop));
       
   278         assertFalse(desktop.isExported("java.wombat"));
       
   279         assertFalse(desktop.isExported("java.wombat", thisModule));
       
   280         assertFalse(desktop.isExported("java.wombat", base));
       
   281 
       
   282         // addExports
       
   283         try {
       
   284             desktop.addExports("java.awt", thisModule);
       
   285             assertTrue(false);
       
   286         } catch (IllegalCallerException expected) { }
       
   287         try {
       
   288             desktop.addExports("sun.awt", thisModule);
       
   289             assertTrue(false);
       
   290         } catch (IllegalCallerException expected) { }
       
   291         assertFalse(desktop.isExported("sun.awt"));
       
   292         assertFalse(desktop.isExported("sun.awt", thisModule));
       
   293 
       
   294         // isOpen
       
   295         assertFalse(desktop.isOpen("java.awt"));
       
   296         assertFalse(desktop.isOpen("java.awt", thisModule));
       
   297         assertTrue(desktop.isOpen("java.awt", desktop));
       
   298         assertFalse(desktop.isOpen("sun.awt"));
       
   299         assertFalse(desktop.isOpen("sun.awt", thisModule));
       
   300         assertTrue(desktop.isOpen("sun.awt", desktop));
       
   301         assertFalse(desktop.isOpen("java.wombat"));
       
   302         assertFalse(desktop.isOpen("java.wombat", thisModule));
       
   303         assertFalse(desktop.isOpen("java.wombat", desktop));
       
   304 
       
   305         // addOpens
       
   306         try {
       
   307             base.addOpens("sun.awt", thisModule);
       
   308             assertTrue(false);
       
   309         } catch (IllegalCallerException expected) { }
       
   310         assertFalse(desktop.isOpen("sun.awt"));
       
   311         assertFalse(desktop.isOpen("sun.awt", thisModule));
       
   312 
       
   313         // canUse
       
   314         assertTrue(base.canUse(FileSystemProvider.class));
       
   315         assertFalse(base.canUse(Thread.class));
       
   316 
       
   317         // addUses
       
   318         try {
       
   319             desktop.addUses(PrintServiceLookup.class);
       
   320             assertTrue(false);
       
   321         } catch (IllegalCallerException expected) { }
       
   322         try {
       
   323             desktop.addUses(Thread.class);
       
   324             assertTrue(false);
       
   325         } catch (IllegalCallerException expected) { }
       
   326         assertFalse(desktop.canUse(Thread.class));
       
   327     }
       
   328 
       
   329 }