1 /*
   2  * Copyright (c) 2014, 2015, 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;
  25 import java.lang.module.ModuleDescriptor.Exports;
  26 import java.lang.reflect.Layer;
  27 import java.lang.reflect.Module;
  28 import java.util.function.Predicate;
  29 import java.util.stream.Stream;
  30 
  31 import org.testng.annotations.Test;
  32 import static org.testng.Assert.*;
  33 
  34 /*
  35  * @test
  36  * @summary Basic test of java.lang.reflect.Module
  37  * @run testng BasicModuleTest
  38  */
  39 
  40 public class BasicModuleTest {
  41 
  42     /**
  43      * Tests that the given module reads all modules in the boot Layer.
  44      */
  45     private void testReadsAllBootModules(Module m) {
  46         Layer bootLayer = Layer.boot();
  47         bootLayer.configuration().get()
  48             .descriptors()
  49             .stream()
  50             .map(ModuleDescriptor::name)
  51             .map(bootLayer::findModule)
  52             .forEach(target -> assertTrue(m.canRead(target.get())));
  53     }
  54 
  55     /**
  56      * Returns {@code true} if the array contains the given object.
  57      */
  58     private <T> boolean contains(T[] array, T obj) {
  59         return Stream.of(array).anyMatch(obj::equals);
  60     }
  61 
  62     /**
  63      * Returns a {@code Predicate} to test if a package is exported.
  64      */
  65     private Predicate<Exports> doesExport(String pn) {
  66         return e -> (e.source().equals(pn)
  67                 && !e.targets().isPresent());
  68     }
  69 
  70 
  71 
  72     @Test
  73     public void testThisModule() {
  74         Module thisModule = BasicModuleTest.class.getModule();
  75         Module baseModule = Object.class.getModule();
  76 
  77         assertFalse(thisModule.isNamed());
  78         assertTrue(thisModule.getName() == null);
  79         assertTrue(thisModule.getDescriptor() == null);
  80         assertTrue(thisModule.getLayer() == null);
  81 
  82         ClassLoader thisLoader = BasicModuleTest.class.getClassLoader();
  83         assertTrue(thisLoader == thisModule.getClassLoader());
  84         assertTrue(thisLoader.getUnnamedModule() == thisModule);
  85 
  86         // unnamed modules read all other modules
  87         assertTrue(thisModule.canRead(null));
  88         testReadsAllBootModules(thisModule);
  89 
  90         // unnamed modules export all packages
  91         assertTrue(thisModule.isExported(""));
  92         assertTrue(thisModule.isExported("", thisModule));
  93         assertTrue(thisModule.isExported("", baseModule));
  94         assertTrue(thisModule.isExported("p"));
  95         assertTrue(thisModule.isExported("p", thisModule));
  96         assertTrue(thisModule.isExported("p", baseModule));
  97 
  98         // this test is in the unnamed package
  99         assertTrue(contains(thisModule.getPackages(), ""));
 100     }
 101 
 102 
 103     @Test
 104     public void testUnnamedModules() {
 105         Module thisModule = BasicModuleTest.class.getModule();
 106         Module baseModule = Object.class.getModule();
 107 
 108         ClassLoader loader1 = ClassLoader.getSystemClassLoader();
 109         ClassLoader loader2 = loader1.getParent();
 110 
 111         Module m1 = loader1.getUnnamedModule();
 112         Module m2 = loader2.getUnnamedModule();
 113 
 114         assertTrue(m1 != m2);
 115 
 116         assertFalse(m1.isNamed());
 117         assertFalse(m2.isNamed());
 118 
 119         assertTrue(m1.getLayer() == null);
 120         assertTrue(m2.getLayer() == null);
 121 
 122         // unnamed module reads all modules
 123         assertTrue(m1.canRead(m2));
 124         assertTrue(m2.canRead(m1));
 125 
 126         testReadsAllBootModules(m1);
 127         testReadsAllBootModules(m2);
 128 
 129         assertTrue(m1.isExported(""));
 130         assertTrue(m1.isExported("", thisModule));
 131         assertTrue(m1.isExported("", baseModule));
 132         assertTrue(m1.isExported("p"));
 133         assertTrue(m1.isExported("p", thisModule));
 134         assertTrue(m1.isExported("p", baseModule));
 135     }
 136 
 137 
 138 
 139     @Test
 140     public void testBaseModule() {
 141         Module base = Object.class.getModule();
 142         Module thisModule = BasicModuleTest.class.getModule();
 143 
 144         // getName
 145         assertTrue(base.getName().equals("java.base"));
 146 
 147         // getDescriptor
 148         assertTrue(base.getDescriptor().exports().stream()
 149                 .anyMatch(doesExport("java.lang")));
 150 
 151         // getClassLoader
 152         assertTrue(base.getClassLoader() == null);
 153 
 154         // getLayer
 155         assertTrue(base.getLayer() == Layer.boot());
 156 
 157         // getPackages
 158         assertTrue(contains(base.getPackages(), "java.lang"));
 159 
 160         // canRead
 161         assertTrue(base.canRead(base));
 162 
 163         // isExported
 164         assertTrue(base.isExported("java.lang"));
 165         assertTrue(base.isExported("java.lang", thisModule));
 166         assertFalse(base.isExported("java.wombat"));
 167         assertFalse(base.isExported("java.wombat", thisModule));
 168     }
 169 
 170 
 171     @Test
 172     public void testDesktopModule() {
 173         Module desktop = java.awt.Component.class.getModule();
 174         Module base = Object.class.getModule();
 175         Module xml = javax.xml.XMLConstants.class.getModule();
 176         Module thisModule = BasicModuleTest.class.getModule();
 177 
 178         // name
 179         assertTrue(desktop.getName().equals("java.desktop"));
 180 
 181         // descriptor
 182         assertTrue(desktop.getDescriptor().exports().stream()
 183                    .anyMatch(doesExport("java.awt")));
 184 
 185         // getClassLoader
 186         assertTrue(desktop.getClassLoader() == null);
 187 
 188         // getLayer
 189         assertTrue(base.getLayer() == Layer.boot());
 190 
 191         // getPackages
 192         assertTrue(contains(desktop.getPackages(), "java.awt"));
 193         assertTrue(contains(desktop.getPackages(), "sun.awt"));
 194 
 195         // canRead
 196         assertTrue(desktop.canRead(base));
 197         assertTrue(desktop.canRead(xml));
 198 
 199         // isExported
 200         assertTrue(desktop.isExported("java.awt"));
 201         assertTrue(desktop.isExported("java.awt", thisModule));
 202         assertFalse(desktop.isExported("java.wombat"));
 203         assertFalse(desktop.isExported("java.wombat", thisModule));
 204     }
 205 
 206 
 207     @Test(expectedExceptions = { NullPointerException.class })
 208     public void testIsExportedNull() {
 209         Module thisModule = this.getClass().getModule();
 210         thisModule.isExported(null, thisModule);
 211     }
 212 
 213 
 214     @Test(expectedExceptions = { NullPointerException.class })
 215     public void testIsExportedToNull() {
 216         Module thisModule = this.getClass().getModule();
 217         thisModule.isExported("", null);
 218     }
 219 
 220 
 221 }