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 * @bug 8168836 27 * @summary Basic argument validation for --add-exports 28 * @library ../../../lib /lib/testlibrary 29 * @modules jdk.compiler 30 * @build AddExportsTestWarningError CompilerUtils jdk.testlibrary.* 31 * @run testng AddExportsTestWarningError 32 */ 33 34 import java.io.BufferedOutputStream; 35 import java.io.ByteArrayOutputStream; 36 import java.io.PrintStream; 37 import java.nio.file.Path; 38 import java.nio.file.Paths; 39 import java.util.Arrays; 40 import java.util.stream.Stream; 41 42 import jdk.testlibrary.OutputAnalyzer; 43 import static jdk.testlibrary.ProcessTools.*; 44 45 import org.testng.annotations.BeforeTest; 46 import org.testng.annotations.DataProvider; 47 import org.testng.annotations.Test; 48 import static org.testng.Assert.*; 49 50 51 @Test 52 public class AddExportsTestWarningError { 53 54 private static final Path MODS_DIR = Paths.get("mods"); 55 private static final Path SRC_DIR = Paths.get("src"); 56 private static final String M1_MAIN = "m1/p1.C1"; 57 private static final String M3_MAIN = "m3/p3.C3"; 58 59 @BeforeTest 60 public void setup() throws Exception { 61 ExplodedModuleBuilder builder = new ExplodedModuleBuilder(SRC_DIR); 62 builder.writeJavaFiles("m1", 63 "module m1 { }", 64 "package p1; public class C1 { " + 65 " public static void main(String... args) {}" + 66 "}"); 67 68 builder.writeJavaFiles("m2", 69 "module m2 { requires m1; exports p2; }", 70 "package p2; public class C2 { private p1.C1 c1; }"); 71 72 builder.writeJavaFiles("m3", 73 "module m3 { requires m2; }", 74 "package p3; class C3 { " + 75 " p1.C1 c; " + 76 " public static void main(String... args) { new p2.C2(); }" + 77 "}"); 78 79 builder.compile("m1", MODS_DIR); 80 builder.compile("m2", MODS_DIR, "--add-exports", "m1/p1=m2"); 81 builder.compile("m3", MODS_DIR, "--add-exports", "m1/p1=m3"); 82 } 83 84 85 @DataProvider(name = "goodcases") 86 public Object[][] goodCases() { 87 return new Object[][]{ 88 89 // empty items 90 { "m1/p1=,m2,m3", null }, 91 { "m1/p1=m2,,m3", null }, 92 { "m1/p1=m2,m3,", null }, 93 94 // duplicates 95 { "m1/p1=m2,m2,m3,,", null }, 96 97 }; 98 } 99 100 101 @Test(dataProvider = "goodcases") 102 public void test(String value, String ignore) throws Exception { 103 testNoWarning(value); 104 } 105 106 107 @DataProvider(name = "illFormedAddExports") 108 public Object[][] illFormedAddExports() { 109 return new Object[][]{ 110 { "m1", "Unable to parse --add-exports <module>=<value>: m1"}, 111 112 // missing source part 113 { "=m2", "Unable to parse --add-exports <module>=<value>: =m2"}, 114 { "/=m2", "Unable to parse --add-exports <module>/<package>: /" }, 115 { "m1=m2", "Unable to parse --add-exports <module>/<package>: m1" }, 116 { "/p1=m2", "Unable to parse --add-exports <module>/<package>: /p1" }, 117 { "m1p1=m2", "Unable to parse --add-exports <module>/<package>: m1p1" }, 118 119 // empty list, missing target 120 { "m1/p1=", "Unable to parse --add-exports <module>=<value>: m1/p1=" }, 121 { "m1/p1=,,", "Target must be specified: --add-exports m1/p1=,," }, 122 }; 123 } 124 125 @Test(dataProvider = "illFormedAddExports") 126 public void testIllFormedAddExports(String value, String msg) throws Exception { 127 testError(value, msg); 128 } 129 130 131 @DataProvider(name = "unknownNames") 132 public Object[][] unknownNames() { 133 return new Object[][]{ 134 135 // source not found 136 {"DoesNotExist/p=m1", "WARNING: Unknown module: DoesNotExist specified in --add-exports"}, 137 {"m1/DoesNotExist=m2", "WARNING: package DoesNotExist not in m1"}, 138 139 // target not found 140 {"m1/p1=DoesNotExist", "WARNING: Unknown module: DoesNotExist specified in --add-exports"}, 141 142 // bad names 143 {"m*/p1=m2", "WARNING: Unknown module: m* specified in --add-exports"}, 144 {"m1/p!=m2", "WARNING: package p! not in m1"}, 145 {"m1/p1=m!", "WARNING: Unknown module: m! specified in --add-exports"}, 146 147 }; 148 } 149 150 151 @Test(dataProvider = "unknownNames") 152 public void testUnknownNames(String value, String msg) throws Exception { 153 testWarning(value, msg); 154 } 155 156 157 @DataProvider(name = "missingArguments") 158 public Object[][] missingArguments() { 159 return new Object[][]{ 160 { new String[] { "--add-exports" }, 161 "Error: --add-exports requires modules to be specified"}, 162 163 { new String[] { "--add-exports=" }, 164 "Error: --add-exports= requires modules to be specified" }, 165 166 { new String[] { "--add-exports", "" }, 167 "Error: --add-exports requires modules to be specified"} 168 169 }; 170 } 171 172 173 @Test(dataProvider = "missingArguments") 174 public void testMissingArguments(String[] options, String msg) throws Exception { 175 String[] args = Stream.concat(Arrays.stream(options), 176 Stream.of("-version")) 177 .toArray(String[]::new); 178 int exitValue = executeTestJava(args) 179 .outputTo(System.out) 180 .errorTo(System.out) 181 .shouldContain(msg) 182 .getExitValue(); 183 184 assertTrue(exitValue != 0); 185 } 186 187 private void testWarning(String value, String msg) throws Exception { 188 int exitValue = 189 executeTestJava("--add-exports", value, 190 "--module-path", MODS_DIR.toString(), 191 "-m", M1_MAIN) 192 .outputTo(System.out) 193 .errorTo(System.out) 194 .shouldContain(msg) 195 .getExitValue(); 196 197 assertTrue(exitValue == 0); 198 } 199 200 private void testError(String value, String msg) throws Exception { 201 int exitValue = 202 executeTestJava("--add-exports", value, 203 "--module-path", MODS_DIR.toString(), 204 "-m", M1_MAIN) 205 .outputTo(System.out) 206 .errorTo(System.out) 207 .shouldContain(msg) 208 .getExitValue(); 209 210 assertTrue(exitValue != 0); 211 } 212 213 private void testNoWarning(String value) throws Exception { 214 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 215 PrintStream ps = new PrintStream(new BufferedOutputStream(baos)); 216 OutputAnalyzer outputAnalyzer = 217 executeTestJava("--add-exports", value, 218 "--module-path", MODS_DIR.toString(), 219 "-m", M3_MAIN) 220 .outputTo(ps) 221 .errorTo(ps); 222 223 assertTrue(outputAnalyzer.getExitValue() == 0); 224 225 System.out.println(baos.toString()); 226 String[] output = baos.toString().split("\\R"); 227 assertFalse(Arrays.stream(output) 228 .filter(s -> !s.matches("WARNING: Module name .* may soon be illegal")) 229 .filter(s -> s.startsWith("WARNING:")) 230 .findAny().isPresent()); 231 232 } 233 }