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  * @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         Layer bootLayer = Layer.boot();
  48         bootLayer.configuration().get()
  49             .descriptors()
  50             .stream()
  51             .map(ModuleDescriptor::name)
  52             .map(bootLayer::findModule)
  53             .forEach(target -> assertTrue(m.canRead(target.get())));
  54     }
  55 
  56     /**
  57      * Returns {@code true} if the array contains the given object.
  58      */
  59     private <T> boolean contains(T[] array, T obj) {
  60         return Stream.of(array).anyMatch(obj::equals);
  61     }
  62 
  63     /**
  64      * Returns a {@code Predicate} to test if a package is exported.
  65      */
  66     private Predicate<Exports> doesExport(String pn) {
  67         return e -> (e.source().equals(pn)
  68                 && !e.targets().isPresent());
  69     }
  70 
  71 
  72 
  73     @Test
  74     public void testThisModule() {
  75         Module thisModule = BasicModuleTest.class.getModule();
  76         Module baseModule = Object.class.getModule();
  77 
  78         assertFalse(thisModule.isNamed());
  79         assertTrue(thisModule.getName() == null);
  80         assertTrue(thisModule.getDescriptor() == null);
  81         assertTrue(thisModule.getLayer() == null);
  82 
  83         ClassLoader thisLoader = BasicModuleTest.class.getClassLoader();
  84         assertTrue(thisLoader == thisModule.getClassLoader());
  85         assertTrue(thisLoader.getUnnamedModule() == thisModule);
  86 
  87         // unnamed modules read all other modules
  88         assertTrue(thisModule.canRead(null));
  89         testReadsAllBootModules(thisModule);
  90 
  91         // unnamed modules export all packages
  92         assertTrue(thisModule.isExported(""));
  93         assertTrue(thisModule.isExported("", thisModule));
  94         assertTrue(thisModule.isExported("", baseModule));
  95         assertTrue(thisModule.isExported("p"));
  96         assertTrue(thisModule.isExported("p", thisModule));
  97         assertTrue(thisModule.isExported("p", baseModule));
  98 
  99         // this test is in the unnamed package
 100         assertTrue(contains(thisModule.getPackages(), ""));
 101     }
 102 
 103 
 104     @Test
 105     public void testUnnamedModules() {
 106         Module thisModule = BasicModuleTest.class.getModule();
 107         Module baseModule = Object.class.getModule();
 108 
 109         ClassLoader loader1 = ClassLoader.getSystemClassLoader();
 110         ClassLoader loader2 = loader1.getParent();
 111 
 112         Module m1 = loader1.getUnnamedModule();
 113         Module m2 = loader2.getUnnamedModule();
 114 
 115         assertTrue(m1 != m2);
 116 
 117         assertFalse(m1.isNamed());
 118         assertFalse(m2.isNamed());
 119 
 120         assertTrue(m1.getLayer() == null);
 121         assertTrue(m2.getLayer() == null);
 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() == Layer.boot());
 157 
 158         // getPackages
 159         assertTrue(contains(base.getPackages(), "java.lang"));
 160 
 161         // canRead
 162         assertTrue(base.canRead(base));
 163 
 164         // isExported
 165         assertTrue(base.isExported("java.lang"));
 166         assertTrue(base.isExported("java.lang", thisModule));
 167         assertFalse(base.isExported("java.wombat"));
 168         assertFalse(base.isExported("java.wombat", thisModule));
 169     }
 170 
 171 
 172     @Test
 173     public void testDesktopModule() {
 174         Module desktop = java.awt.Component.class.getModule();
 175         Module base = Object.class.getModule();
 176         Module xml = javax.xml.XMLConstants.class.getModule();
 177         Module thisModule = BasicModuleTest.class.getModule();
 178 
 179         // name
 180         assertTrue(desktop.getName().equals("java.desktop"));
 181 
 182         // descriptor
 183         assertTrue(desktop.getDescriptor().exports().stream()
 184                    .anyMatch(doesExport("java.awt")));
 185 
 186         // getClassLoader
 187         assertTrue(desktop.getClassLoader() == null);
 188 
 189         // getLayer
 190         assertTrue(base.getLayer() == Layer.boot());
 191 
 192         // getPackages
 193         assertTrue(contains(desktop.getPackages(), "java.awt"));
 194         assertTrue(contains(desktop.getPackages(), "sun.awt"));
 195 
 196         // canRead
 197         assertTrue(desktop.canRead(base));
 198         assertTrue(desktop.canRead(xml));
 199 
 200         // isExported
 201         assertTrue(desktop.isExported("java.awt"));
 202         assertTrue(desktop.isExported("java.awt", thisModule));
 203         assertFalse(desktop.isExported("java.wombat"));
 204         assertFalse(desktop.isExported("java.wombat", thisModule));
 205     }
 206 
 207 
 208     @Test(expectedExceptions = { NullPointerException.class })
 209     public void testIsExportedNull() {
 210         Module thisModule = this.getClass().getModule();
 211         thisModule.isExported(null, thisModule);
 212     }
 213 
 214 
 215     @Test(expectedExceptions = { NullPointerException.class })
 216     public void testIsExportedToNull() {
 217         Module thisModule = this.getClass().getModule();
 218         thisModule.isExported("", null);
 219     }
 220 
 221 
 222 }