Path: blob/master/test/jdk/tools/launcher/TestSpecialArgs.java
41144 views
/*1* Copyright (c) 2012, 2021, 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* @bug 7124089 7131021 8042469 8066185 8074373 825891726* @summary Checks for Launcher special flags, such as MacOSX specific flags,27* and JVM NativeMemoryTracking flags.28* @modules jdk.compiler29* jdk.zipfs30* @compile -XDignore.symbol.file TestSpecialArgs.java EnvironmentVariables.java31* @run main TestSpecialArgs32*/33import java.io.File;34import java.io.FileNotFoundException;35import java.util.HashMap;36import java.util.HashSet;37import java.util.Map;38import java.util.Set;3940public class TestSpecialArgs extends TestHelper {4142public static void main(String... args) throws Exception {43new TestSpecialArgs().run(args);44}4546@Test47void testDocking() {48final Map<String, String> envMap = new HashMap<>();49envMap.put("_JAVA_LAUNCHER_DEBUG", "true");50TestResult tr = doExec(envMap, javaCmd, "-XstartOnFirstThread", "-version");51if (isMacOSX) {52if (!tr.contains("In same thread")) {53System.out.println(tr);54throw new RuntimeException("Error: not running in the same thread ?");55}56if (!tr.isOK()) {57System.out.println(tr);58throw new RuntimeException("Error: arg was rejected ????");59}60} else {61if (tr.isOK()) {62System.out.println(tr);63throw new RuntimeException("Error: argument was accepted ????");64}65}6667tr = doExec(javaCmd, "-Xdock:/tmp/not-available", "-version");68if (isMacOSX) {69if (!tr.isOK()) {70System.out.println(tr);71throw new RuntimeException("Error: arg was rejected ????");72}73} else {74if (tr.isOK()) {75System.out.println(tr);76throw new RuntimeException("Error: argument was accepted ????");77}78}79// MacOSX specific tests ensue......80if (!isMacOSX) {81return;82}83Set<String> envToRemove = new HashSet<>();84Map<String, String> map = System.getenv();85for (String s : map.keySet()) {86if (s.startsWith("JAVA_MAIN_CLASS_")87|| s.startsWith("APP_NAME_")88|| s.startsWith("APP_ICON_")) {89envToRemove.add(s);90}91}92runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(),93"EnvironmentVariables", "JAVA_MAIN_CLASS_*",94"EnvironmentVariables");9596runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(),97"-Xdock:name=TestAppName", "EnvironmentVariables",98"APP_NAME_*", "TestAppName");99100runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(),101"-Xdock:icon=TestAppIcon", "EnvironmentVariables",102"APP_ICON_*", "TestAppIcon");103}104105void runTest(Set<String> envToRemove, String... args) {106TestResult tr = doExec(null, envToRemove, args);107if (!tr.isOK()) {108System.err.println(tr.toString());109throw new RuntimeException("Test Fails");110}111}112113@Test114void testNativeMemoryTracking() {115final Map<String, String> envMap = new HashMap<>();116envMap.put("_JAVA_LAUNCHER_DEBUG", "true");117TestResult tr;118/*119* test argument : -XX:NativeMemoryTracking=value120* A JVM flag, comsumed by the JVM, but requiring launcher121* to set an environmental variable if and only if value is supplied.122* Test and order:123* 1) execute with valid parameter: -XX:NativeMemoryTracking=MyValue124* a) check for correct env variable name: "NMT_LEVEL_" + pid125* b) check that "MyValue" was found in local env.126* 2) execute with invalid parameter: -XX:NativeMemoryTracking=127* !) Won't find "NativeMemoryTracking:"128* Code to create env variable not executed.129* 3) execute with invalid parameter: -XX:NativeMemoryTracking130* !) Won't find "NativeMemoryTracking:"131* Code to create env variable not executed.132* 4) give and invalid value and check to make sure JVM commented133*/134String envVarPidString = "TRACER_MARKER: NativeMemoryTracking: env var is NMT_LEVEL_";135String NMT_Option_Value = "off";136String myClassName = "helloworld";137138// === Run the tests ===139// ---Test 1a140tr = doExec(envMap, javaCmd, "-XX:NativeMemoryTracking=" + NMT_Option_Value,141"-version");142143// get the PID from the env var we set for the JVM144String envVarPid = null;145for (String line : tr.testOutput) {146if (line.contains(envVarPidString)) {147int sindex = envVarPidString.length();148envVarPid = line.substring(sindex);149break;150}151}152// did we find envVarPid?153if (envVarPid == null) {154System.out.println(tr);155throw new RuntimeException("Error: failed to find env Var Pid in tracking info");156}157// we think we found the pid string. min test, not "".158if (envVarPid.length() < 1) {159System.out.println(tr);160throw new RuntimeException("Error: env Var Pid in tracking info is empty string");161}162163// --- Test 1b164if (!tr.contains("NativeMemoryTracking: got value " + NMT_Option_Value)) {165System.out.println(tr);166throw new RuntimeException("Error: Valid param failed to set env variable");167}168169// --- Test 2170tr = doExec(envMap, javaCmd, "-XX:NativeMemoryTracking=",171"-version");172if (tr.contains("NativeMemoryTracking:")) {173System.out.println(tr);174throw new RuntimeException("Error: invalid param caused env variable to be erroneously created");175}176if (!tr.contains("Syntax error, expecting -XX:NativeMemoryTracking=")) {177System.out.println(tr);178throw new RuntimeException("Error: invalid param not checked by JVM");179}180181// --- Test 3182tr = doExec(envMap, javaCmd, "-XX:NativeMemoryTracking",183"-version");184if (tr.contains("NativeMemoryTracking:")) {185System.out.println(tr);186throw new RuntimeException("Error: invalid param caused env variable to be erroneously created");187}188if (!tr.contains("Syntax error, expecting -XX:NativeMemoryTracking=")) {189System.out.println(tr);190throw new RuntimeException("Error: invalid param not checked by JVM");191}192// --- Test 4193tr = doExec(envMap, javaCmd, "-XX:NativeMemoryTracking=BADVALUE",194"-version");195if (!tr.contains("expecting -XX:NativeMemoryTracking")) {196System.out.println(tr);197throw new RuntimeException("Error: invalid param did not get JVM Syntax error message");198}199}200201@Test202void testNMArgumentProcessing() throws FileNotFoundException {203TestResult tr;204// the direct invokers of the VM205String options[] = {206"-version", "-fullversion", "-help", "-?", "-X"207};208for (String option : options) {209tr = doExec(javaCmd, option, "-XX:NativeMemoryTracking=summary");210checkTestResult(tr);211}212213// create a test jar214File jarFile = new File("test.jar");215createJar(jarFile, "public static void main(String... args){}");216217// ones that involve main-class of some sort218tr = doExec(javaCmd, "-jar", jarFile.getName(),219"-XX:NativeMemoryTracking=summary");220checkTestResult(tr);221222tr = doExec(javaCmd, "-cp", jarFile.getName(), "Foo",223"-XX:NativeMemoryTracking=summary");224checkTestResult(tr);225226final Map<String, String> envMap = new HashMap<>();227// checkwith CLASSPATH set ie. no -cp or -classpath228envMap.put("CLASSPATH", ".");229tr = doExec(envMap, javaCmd, "Foo", "-XX:NativeMemoryTracking=summary");230checkTestResult(tr);231232// should accept with no warnings233tr = doExec(javaCmd, "-cp", jarFile.getName(),234"-XX:NativeMemoryTracking=summary", "Foo");235ensureNoWarnings(tr);236237// should accept with no warnings238tr = doExec(javaCmd, "-classpath", jarFile.getName(),239"-XX:NativeMemoryTracking=summary", "Foo");240ensureNoWarnings(tr);241242// make sure a missing class is handled correctly, because the class243// resolution is performed by the JVM.244tr = doExec(javaCmd, "AbsentClass", "-XX:NativeMemoryTracking=summary");245if (!tr.contains("Error: Could not find or load main class AbsentClass")) {246throw new RuntimeException("Test Fails");247}248249// Make sure we handle correctly the module long form (--module=)250tr = doExec(javaCmd, "-XX:NativeMemoryTracking=summary", "--module=jdk.compiler/com.sun.tools.javac.Main", "--help");251ensureNoWarnings(tr);252}253254@Test255void testNMTTools() throws FileNotFoundException {256TestResult tr;257// Tools (non-java launchers) should handle NTM (no "wrong launcher" warning).258tr = doExec(jarCmd, "-J-XX:NativeMemoryTracking=summary", "--help");259ensureNoWarnings(tr);260261// And java terminal args (like "--help") don't stop "-J" args parsing.262tr = doExec(jarCmd, "--help", "-J-XX:NativeMemoryTracking=summary");263ensureNoWarnings(tr);264}265266void ensureNoWarnings(TestResult tr) {267checkTestResult(tr);268if (tr.contains("warning: Native Memory Tracking")) {269System.err.println(tr.toString());270throw new RuntimeException("Test Fails");271}272}273274void checkTestResult(TestResult tr) {275if (!tr.isOK()) {276System.err.println(tr.toString());277throw new RuntimeException("Test Fails");278}279}280}281282283