Path: blob/master/test/jdk/java/lang/ModuleTests/AddExportsTest.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @modules java.base/jdk.internal.misc26* java.desktop27* @run main/othervm --add-exports=java.desktop/sun.awt=java.base AddExportsTest28* @run main/othervm --add-exports=java.desktop/sun.awt=ALL-UNNAMED AddExportsTest29* @summary Test Module isExported methods with exports changed by -AddExportsTest30*/3132import java.util.Optional;33import java.util.stream.Stream;3435import jdk.internal.misc.VM;3637public class AddExportsTest {38/*39* jtreg sets -Dtest.modules system property to the internal APIs40* specified at @modules tag. The test will exclude --add-exports set41* for @modules.42*/43private static final String TEST_MODULES = System.getProperty("test.modules");4445public static void main(String[] args) {4647Optional<String> oaddExports = Stream.of(VM.getRuntimeArguments())48.filter(arg -> arg.startsWith("--add-exports="))49.filter(arg -> !arg.equals("--add-exports=" + TEST_MODULES + "=ALL-UNNAMED"))50.map(arg -> arg.substring("--add-exports=".length(), arg.length()))51.findFirst();5253assertTrue(oaddExports.isPresent());5455ModuleLayer bootLayer = ModuleLayer.boot();5657Module unnamedModule = AddExportsTest.class.getModule();58assertFalse(unnamedModule.isNamed());5960for (String expr : oaddExports.get().split(",")) {6162String[] s = expr.split("=");63assertTrue(s.length == 2);6465// $MODULE/$PACKAGE66String[] moduleAndPackage = s[0].split("/");67assertTrue(moduleAndPackage.length == 2);6869String mn = moduleAndPackage[0];70String pn = moduleAndPackage[1];7172// source module73Module source;74Optional<Module> om = bootLayer.findModule(mn);75assertTrue(om.isPresent(), mn + " not in boot layer");76source = om.get();7778// package should not be exported unconditionally79assertFalse(source.isExported(pn),80pn + " should not be exported unconditionally");8182// $TARGET83String tn = s[1];84if ("ALL-UNNAMED".equals(tn)) {8586// package is exported to all unnamed modules87assertTrue(source.isExported(pn, unnamedModule),88pn + " should be exported to all unnamed modules");8990} else {9192om = bootLayer.findModule(tn);93assertTrue(om.isPresent());94Module target = om.get();9596// package should be exported to target module97assertTrue(source.isExported(pn, target),98pn + " should be exported to " + target);99100// package should not be exported to unnamed modules101assertFalse(source.isExported(pn, unnamedModule),102pn + " should not be exported to unnamed modules");103104}105106}107}108109static void assertTrue(boolean cond) {110if (!cond) throw new RuntimeException();111}112113static void assertTrue(boolean cond, String msg) {114if (!cond) throw new RuntimeException(msg);115}116117static void assertFalse(boolean cond) {118if (cond) throw new RuntimeException();119}120121static void assertFalse(boolean cond, String msg) {122if (cond) throw new RuntimeException(msg);123}124125}126127128