< prev index next >

test/jdk/tools/launcher/ArgsEnvVar.java

Print this page




  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8170832 8180447
  27  * @summary Arguments passed in environment variable
  28  * @modules jdk.compiler
  29  *          jdk.zipfs
  30  * @build TestHelper
  31  * @run main ArgsEnvVar
  32  */
  33 import java.io.File;
  34 import java.io.IOException;
  35 import java.util.ArrayList;
  36 import java.util.HashMap;
  37 import java.util.List;
  38 import java.util.Map;
  39 import java.util.regex.Pattern;


  40 
  41 public class ArgsEnvVar extends TestHelper {
  42     private static File testJar = null;
  43     private static Map<String, String> env = new HashMap<>();
  44 
  45     private static String JDK_JAVA_OPTIONS = "JDK_JAVA_OPTIONS";
  46 
  47     static void init() throws IOException {
  48         if  (testJar != null) {
  49             return;
  50         }
  51         testJar = new File("test.jar");
  52         StringBuilder tsrc = new StringBuilder();
  53         tsrc.append("public static void main(String... args) {\n");
  54         tsrc.append("   for (String x : args) {\n");
  55         tsrc.append("        System.out.println(x);\n");
  56         tsrc.append("   }\n");
  57         tsrc.append("}\n");
  58         createJar(testJar, new File("Foo"), tsrc.toString());
  59 


 136 
 137     private TestResult testInEnv(List<String> options) {
 138         env.put(JDK_JAVA_OPTIONS, String.join(" ", options));
 139         return doExec(env, javaCmd, "-jar", "test.jar");
 140     }
 141 
 142     private TestResult testInEnvAsArgFile(List<String> options) throws IOException {
 143         File argFile = createArgFile("argFile", options);
 144         env.put(JDK_JAVA_OPTIONS, "@argFile");
 145         TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");
 146         argFile.delete();
 147         return tr;
 148     }
 149 
 150     @Test
 151     public void noTerminalOpt() throws IOException {
 152         List<List<String>> terminal_opts = List.of(
 153                 List.of("-jar", "test.jar"),
 154                 List.of("-m", "test/Foo"),
 155                 List.of("--module", "test/Foo"),

 156                 List.of("--dry-run"),
 157                 List.of("-h"),
 158                 List.of("-?"),
 159                 List.of("-help"),
 160                 List.of("--help"),
 161                 List.of("-X"),
 162                 List.of("--help-extra"),
 163                 List.of("-version"),
 164                 List.of("--version"),
 165                 List.of("-fullversion"),
 166                 List.of("--full-version"));
 167 
 168         for (List<String> options: terminal_opts) {
 169             // terminal opt in environment variable
 170             TestResult tr = testInEnv(options);
 171             tr.checkNegative();
 172             if (!tr.testStatus) {
 173                 System.out.println(tr);
 174                 throw new RuntimeException("test fails");
 175             }


 222         verifyOptions(List.of("-cp", "*", "-jar", "test.jar"), tr);
 223 
 224         env.put(JDK_JAVA_OPTIONS, "-p ?");
 225         tr = doExec(env, javaCmd, "-jar", "test.jar", "one", "two");
 226         verifyOptions(List.of("-p", "?", "-jar", "test.jar", "one", "two"), tr);
 227     }
 228 
 229     @Test
 230     public void testTrailingSpaces() {
 231         env.put(JDK_JAVA_OPTIONS, "--add-exports java.base/jdk.internal.misc=ALL-UNNAMED ");
 232         TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");
 233         verifyOptions(List.of("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-jar", "test.jar"), tr);
 234 
 235         env.put(JDK_JAVA_OPTIONS, "--class-path ' '");
 236         tr = doExec(env, javaCmd, "-jar", "test.jar");
 237         verifyOptions(List.of("--class-path", " ", "-jar", "test.jar"), tr);
 238 
 239         env.put(JDK_JAVA_OPTIONS, "  --add-exports java.base/jdk.internal.misc=ALL-UNNAMED ");
 240         tr = doExec(env, javaCmd, "-jar", "test.jar");
 241         verifyOptions(List.of("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-jar", "test.jar"), tr);































































 242     }
 243 
 244     public static void main(String... args) throws Exception {
 245         init();
 246         ArgsEnvVar a = new ArgsEnvVar();
 247         a.run(args);
 248         if (testExitValue > 0) {
 249             System.out.println("Total of " + testExitValue + " failed");
 250             System.exit(1);
 251         } else {
 252             System.out.println("All tests pass");
 253         }
 254     }
 255 }


  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8170832 8180447
  27  * @summary Arguments passed in environment variable
  28  * @modules jdk.compiler
  29  *          jdk.zipfs
  30  * @build TestHelper
  31  * @run main ArgsEnvVar
  32  */
  33 import java.io.File;
  34 import java.io.IOException;
  35 import java.util.ArrayList;
  36 import java.util.HashMap;
  37 import java.util.List;
  38 import java.util.Map;
  39 import java.util.regex.Pattern;
  40 import java.nio.file.Paths;
  41 import java.nio.file.Path;
  42 
  43 public class ArgsEnvVar extends TestHelper {
  44     private static File testJar = null;
  45     private static Map<String, String> env = new HashMap<>();
  46 
  47     private static String JDK_JAVA_OPTIONS = "JDK_JAVA_OPTIONS";
  48 
  49     static void init() throws IOException {
  50         if  (testJar != null) {
  51             return;
  52         }
  53         testJar = new File("test.jar");
  54         StringBuilder tsrc = new StringBuilder();
  55         tsrc.append("public static void main(String... args) {\n");
  56         tsrc.append("   for (String x : args) {\n");
  57         tsrc.append("        System.out.println(x);\n");
  58         tsrc.append("   }\n");
  59         tsrc.append("}\n");
  60         createJar(testJar, new File("Foo"), tsrc.toString());
  61 


 138 
 139     private TestResult testInEnv(List<String> options) {
 140         env.put(JDK_JAVA_OPTIONS, String.join(" ", options));
 141         return doExec(env, javaCmd, "-jar", "test.jar");
 142     }
 143 
 144     private TestResult testInEnvAsArgFile(List<String> options) throws IOException {
 145         File argFile = createArgFile("argFile", options);
 146         env.put(JDK_JAVA_OPTIONS, "@argFile");
 147         TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");
 148         argFile.delete();
 149         return tr;
 150     }
 151 
 152     @Test
 153     public void noTerminalOpt() throws IOException {
 154         List<List<String>> terminal_opts = List.of(
 155                 List.of("-jar", "test.jar"),
 156                 List.of("-m", "test/Foo"),
 157                 List.of("--module", "test/Foo"),
 158                 List.of("--module=test/Foo"),
 159                 List.of("--dry-run"),
 160                 List.of("-h"),
 161                 List.of("-?"),
 162                 List.of("-help"),
 163                 List.of("--help"),
 164                 List.of("-X"),
 165                 List.of("--help-extra"),
 166                 List.of("-version"),
 167                 List.of("--version"),
 168                 List.of("-fullversion"),
 169                 List.of("--full-version"));
 170 
 171         for (List<String> options: terminal_opts) {
 172             // terminal opt in environment variable
 173             TestResult tr = testInEnv(options);
 174             tr.checkNegative();
 175             if (!tr.testStatus) {
 176                 System.out.println(tr);
 177                 throw new RuntimeException("test fails");
 178             }


 225         verifyOptions(List.of("-cp", "*", "-jar", "test.jar"), tr);
 226 
 227         env.put(JDK_JAVA_OPTIONS, "-p ?");
 228         tr = doExec(env, javaCmd, "-jar", "test.jar", "one", "two");
 229         verifyOptions(List.of("-p", "?", "-jar", "test.jar", "one", "two"), tr);
 230     }
 231 
 232     @Test
 233     public void testTrailingSpaces() {
 234         env.put(JDK_JAVA_OPTIONS, "--add-exports java.base/jdk.internal.misc=ALL-UNNAMED ");
 235         TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");
 236         verifyOptions(List.of("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-jar", "test.jar"), tr);
 237 
 238         env.put(JDK_JAVA_OPTIONS, "--class-path ' '");
 239         tr = doExec(env, javaCmd, "-jar", "test.jar");
 240         verifyOptions(List.of("--class-path", " ", "-jar", "test.jar"), tr);
 241 
 242         env.put(JDK_JAVA_OPTIONS, "  --add-exports java.base/jdk.internal.misc=ALL-UNNAMED ");
 243         tr = doExec(env, javaCmd, "-jar", "test.jar");
 244         verifyOptions(List.of("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-jar", "test.jar"), tr);
 245     }
 246 
 247 
 248     @Test
 249     // That that we can correctly handle the module longform argument option
 250     // when supplied in an argument file
 251     public void modulesInArgsFile() throws IOException {
 252         File cwd = new File(".");
 253         File testModuleDir = new File(cwd, "modules_test");
 254 
 255         createEchoArgumentsModule(testModuleDir);
 256 
 257         Path SRC_DIR = Paths.get(testModuleDir.getAbsolutePath(), "src");
 258         Path MODS_DIR = Paths.get(testModuleDir.getAbsolutePath(), "mods");
 259 
 260         // test module / main class
 261         String MODULE_OPTION = "--module=test/launcher.Main";
 262         String TEST_MODULE = "test";
 263 
 264         // javac -d mods/test src/test/**
 265         TestResult tr = doExec(
 266             javacCmd,
 267             "-d", MODS_DIR.toString(),
 268             "--module-source-path", SRC_DIR.toString(),
 269             "--module", TEST_MODULE);
 270 
 271         if (!tr.isOK()) {
 272             System.out.println("test did not compile");
 273             throw new RuntimeException("Error: modules test did not compile");
 274         }
 275 
 276         // verify the terminating ability of --module= through environment variables
 277         File argFile = createArgFile("cmdargs", List.of("--module-path", MODS_DIR.toString(), MODULE_OPTION, "--hello"));
 278         env.put(JDK_JAVA_OPTIONS, "@cmdargs");
 279         tr = doExec(env, javaCmd);
 280         tr.checkNegative();
 281         tr.contains("Error: Option " + MODULE_OPTION + " in @cmdargs is not allowed in environment variable JDK_JAVA_OPTIONS");
 282         if (!tr.testStatus) {
 283             System.out.println(tr);
 284             throw new RuntimeException("test fails");
 285         }
 286 
 287         // check that specifying --module and --module-path with file works
 288         tr = doExec(javaCmd, "-Dfile.encoding=UTF-8", "\"@cmdargs\"");
 289         tr.contains("[--hello]");
 290         if (!tr.testStatus) {
 291             System.out.println(tr);
 292             throw new RuntimeException("test fails");
 293         }
 294 
 295         // check with reversed --module-path and --module in the arguments file, this will fail, --module= is terminating
 296         File argFile1 = createArgFile("cmdargs1", List.of(MODULE_OPTION, "--module-path", MODS_DIR.toString(), "--hello"));
 297         tr = doExec(javaCmd, "-Dfile.encoding=UTF-8", "\"@cmdargs1\"");
 298         tr.checkNegative();
 299         if (!tr.testStatus) {
 300             System.out.println(tr);
 301             throw new RuntimeException("test fails");
 302         }
 303 
 304         // clean-up
 305         argFile.delete();
 306         argFile1.delete();
 307         recursiveDelete(testModuleDir);
 308     }
 309 
 310     public static void main(String... args) throws Exception {
 311         init();
 312         ArgsEnvVar a = new ArgsEnvVar();
 313         a.run(args);
 314         if (testExitValue > 0) {
 315             System.out.println("Total of " + testExitValue + " failed");
 316             System.exit(1);
 317         } else {
 318             System.out.println("All tests pass");
 319         }
 320     }
 321 }
< prev index next >