1 /* 2 * Copyright (c) 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 * @modules java.base/jdk.internal.misc 27 * java.desktop 28 * @run main/othervm --add-exports=java.desktop/sun.awt=java.base AddExportsTest 29 * @run main/othervm --add-exports=java.desktop/sun.awt=ALL-UNNAMED AddExportsTest 30 * @summary Test Module isExported methods with exports changed by -AddExportsTest 31 */ 32 33 import java.lang.reflect.Layer; 34 import java.lang.reflect.Module; 35 import java.util.Optional; 36 import java.util.stream.Stream; 37 38 import jdk.internal.misc.VM; 39 40 public class AddExportsTest { 41 /* 42 * jtreg sets -Dtest.modules system property to the internal APIs 43 * specified at @modules tag. The test will exclude --add-exports set 44 * for @modules. 45 */ 46 private static final String TEST_MODULES = System.getProperty("test.modules"); 47 48 public static void main(String[] args) { 49 50 Optional<String> oaddExports = Stream.of(VM.getRuntimeArguments()) 51 .filter(arg -> arg.startsWith("--add-exports=")) 52 .filter(arg -> !arg.equals("--add-exports=" + TEST_MODULES + "=ALL-UNNAMED")) 53 .map(arg -> arg.substring("--add-exports=".length(), arg.length())) 54 .findFirst(); 55 56 assertTrue(oaddExports.isPresent()); 57 58 Layer bootLayer = Layer.boot(); 59 60 Module unnamedModule = AddExportsTest.class.getModule(); 61 assertFalse(unnamedModule.isNamed()); 62 63 for (String expr : oaddExports.get().split(",")) { 64 65 String[] s = expr.split("="); 66 assertTrue(s.length == 2); 67 68 // $MODULE/$PACKAGE 69 String[] moduleAndPackage = s[0].split("/"); 70 assertTrue(moduleAndPackage.length == 2); 71 72 String mn = moduleAndPackage[0]; 73 String pn = moduleAndPackage[1]; 74 75 // source module 76 Module source; 77 Optional<Module> om = bootLayer.findModule(mn); 78 assertTrue(om.isPresent(), mn + " not in boot layer"); 79 source = om.get(); 80 81 // package should not be exported unconditionally 82 assertFalse(source.isExported(pn), 83 pn + " should not be exported unconditionally"); 84 85 // $TARGET 86 String tn = s[1]; 87 if ("ALL-UNNAMED".equals(tn)) { 88 89 // package is exported to all unnamed modules 90 assertTrue(source.isExported(pn, unnamedModule), 91 pn + " should be exported to all unnamed modules"); 92 93 } else { 94 95 om = bootLayer.findModule(tn); 96 assertTrue(om.isPresent()); 97 Module target = om.get(); 98 99 // package should be exported to target module 100 assertTrue(source.isExported(pn, target), 101 pn + " should be exported to " + target); 102 103 // package should not be exported to unnamed modules 104 assertFalse(source.isExported(pn, unnamedModule), 105 pn + " should not be exported to unnamed modules"); 106 107 } 108 109 } 110 } 111 112 static void assertTrue(boolean cond) { 113 if (!cond) throw new RuntimeException(); 114 } 115 116 static void assertTrue(boolean cond, String msg) { 117 if (!cond) throw new RuntimeException(msg); 118 } 119 120 static void assertFalse(boolean cond) { 121 if (cond) throw new RuntimeException(); 122 } 123 124 static void assertFalse(boolean cond, String msg) { 125 if (cond) throw new RuntimeException(msg); 126 } 127 128 }