8023138: [TEST_BUG] java/lang/instrument/PremainClass/NoPremainAgent.sh fails intermittently
Summary: Port tests for java/lang/instrument/PremainClass from script to java
Reviewed-by: sla
Contributed-by: mattias.tobiasson@oracle.com
--- a/jdk/test/java/lang/instrument/PremainClass/NoPremainAgent.sh Thu Nov 14 16:08:28 2013 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,74 +0,0 @@
-#
-# Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
-# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
-#
-# This code is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License version 2 only, as
-# published by the Free Software Foundation.
-#
-# This code is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
-# version 2 for more details (a copy is included in the LICENSE file that
-# accompanied this code).
-#
-# You should have received a copy of the GNU General Public License version
-# 2 along with this work; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-#
-# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
-# or visit www.oracle.com if you need additional information or have any
-# questions.
-#
-
-# @test
-# @bug 6289149
-# @summary test when the agent's class is missing the premain() function.
-# @author Daniel D. Daugherty, Sun Microsystems
-#
-# @run build DummyMain
-# @run shell ../MakeJAR3.sh NoPremainAgent
-# @run shell NoPremainAgent.sh
-#
-
-if [ "${TESTJAVA}" = "" ]
-then
- echo "TESTJAVA not set. Test cannot execute. Failed."
- exit 1
-fi
-
-if [ "${COMPILEJAVA}" = "" ]
-then
- COMPILEJAVA="${TESTJAVA}"
-fi
-echo "COMPILEJAVA=${COMPILEJAVA}"
-
-if [ "${TESTSRC}" = "" ]
-then
- echo "TESTSRC not set. Test cannot execute. Failed."
- exit 1
-fi
-
-if [ "${TESTCLASSES}" = "" ]
-then
- echo "TESTCLASSES not set. Test cannot execute. Failed."
- exit 1
-fi
-
-JAVAC="${COMPILEJAVA}"/bin/javac
-JAVA="${TESTJAVA}"/bin/java
-
-"${JAVA}" ${TESTVMOPTS} -javaagent:NoPremainAgent.jar \
- -classpath "${TESTCLASSES}" DummyMain > output.log 2>&1
-cat output.log
-
-MESG="java.lang.NoSuchMethodException"
-grep "$MESG" output.log
-result=$?
-if [ "$result" = 0 ]; then
- echo "PASS: found '$MESG' in the test output"
-else
- echo "FAIL: did NOT find '$MESG' in the test output"
-fi
-
-exit $result
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/instrument/PremainClass/NoPremainAgentTest.java Thu Nov 14 12:35:34 2013 +0100
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import jdk.testlibrary.OutputAnalyzer;
+import jdk.testlibrary.ProcessTools;
+import jdk.testlibrary.Utils;
+
+/*
+ * @test
+ * @bug 6289149
+ * @summary test when the agent's class is missing the premain() function.
+ * @library /lib/testlibrary
+ * @run build DummyMain
+ * @run shell ../MakeJAR3.sh NoPremainAgent
+ * @run main NoPremainAgentTest
+ */
+public class NoPremainAgentTest {
+ // Use a javaagent without the premain() function.
+ // Verify that we get the correct exception.
+ public static void main(String[] a) throws Exception {
+ String testArgs = String.format(
+ "-javaagent:NoPremainAgent.jar -classpath %s DummyMain",
+ System.getProperty("test.classes", "."));
+
+ ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
+ Utils.addTestJavaOpts(testArgs.split("\\s+")));
+ System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));
+
+ OutputAnalyzer output = new OutputAnalyzer(pb.start());
+ System.out.println("testjvm.stdout:" + output.getStdout());
+ System.out.println("testjvm.stderr:" + output.getStderr());
+
+ output.stderrShouldContain("java.lang.NoSuchMethodException");
+ if (0 == output.getExitValue()) {
+ throw new RuntimeException("Expected error but got exit value 0");
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/instrument/PremainClass/PremainClassTest.java Thu Nov 14 12:35:34 2013 +0100
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import jdk.testlibrary.OutputAnalyzer;
+import jdk.testlibrary.ProcessTools;
+import jdk.testlibrary.Utils;
+
+/*
+ * @test
+ * @bug 5055293
+ * @summary Test non ascii characters in the Premain-Class attribute.
+ * @library /lib/testlibrary
+ * @run build DummyMain
+ * @run main PremainClassTest
+ */
+public class PremainClassTest {
+ // Use a javaagent where the manifest Premain-Class contains
+ // a non ascii character.
+ // Verify that the premain() function is executed correctly.
+ public static void main(String[] a) throws Exception {
+ String testArgs = String.format(
+ "-javaagent:%s/Agent.jar -classpath %s DummyMain",
+ System.getProperty("test.src"),
+ System.getProperty("test.classes", "."));
+
+ ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
+ Utils.addTestJavaOpts(testArgs.split("\\s+")));
+ System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));
+
+ OutputAnalyzer output = new OutputAnalyzer(pb.start());
+ System.out.println("testjvm.stdout:" + output.getStdout());
+ System.out.println("testjvm.stderr:" + output.getStderr());
+
+ output.shouldHaveExitValue(0);
+ output.stdoutShouldContain("premain running");
+ output.stdoutShouldContain("Hello from DummyMain!");
+ }
+}
--- a/jdk/test/java/lang/instrument/PremainClass/PremainClassTest.sh Thu Nov 14 16:08:28 2013 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-#
-# Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
-# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
-#
-# This code is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License version 2 only, as
-# published by the Free Software Foundation.
-#
-# This code is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
-# version 2 for more details (a copy is included in the LICENSE file that
-# accompanied this code).
-#
-# You should have received a copy of the GNU General Public License version
-# 2 along with this work; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-#
-# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
-# or visit www.oracle.com if you need additional information or have any
-# questions.
-#
-
-# @test
-# @bug 5055293
-# @summary Test non US-ASCII characters in the value of the Premain-Class
-# attribute.
-
-if [ "${TESTJAVA}" = "" ]
-then
- echo "TESTJAVA not set. Test cannot execute. Failed."
- exit 1
-fi
-
-if [ "${COMPILEJAVA}" = "" ]
-then
- COMPILEJAVA="${TESTJAVA}"
-fi
-echo "COMPILEJAVA=${COMPILEJAVA}"
-
-if [ "${TESTSRC}" = "" ]
-then
- echo "TESTSRC not set. Test cannot execute. Failed."
- exit 1
-fi
-
-if [ "${TESTCLASSES}" = "" ]
-then
- echo "TESTCLASSES not set. Test cannot execute. Failed."
- exit 1
-fi
-
-JAVAC="${COMPILEJAVA}"/bin/javac
-JAVA="${TESTJAVA}"/bin/java
-
-"$JAVAC" ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d "${TESTCLASSES}" "${TESTSRC}"/DummyMain.java
-
-"${JAVA}" ${TESTVMOPTS} -javaagent:"${TESTSRC}"/Agent.jar -classpath "${TESTCLASSES}" DummyMain
-result=$?
-
-exit $result
--- a/jdk/test/java/lang/instrument/PremainClass/ZeroArgPremainAgent.sh Thu Nov 14 16:08:28 2013 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,74 +0,0 @@
-#
-# Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
-# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
-#
-# This code is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License version 2 only, as
-# published by the Free Software Foundation.
-#
-# This code is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
-# version 2 for more details (a copy is included in the LICENSE file that
-# accompanied this code).
-#
-# You should have received a copy of the GNU General Public License version
-# 2 along with this work; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-#
-# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
-# or visit www.oracle.com if you need additional information or have any
-# questions.
-#
-
-# @test
-# @bug 6289149
-# @summary test when the agent's class has a zero arg premain() function.
-# @author Daniel D. Daugherty, Sun Microsystems
-#
-# @run build DummyMain
-# @run shell ../MakeJAR3.sh ZeroArgPremainAgent
-# @run shell ZeroArgPremainAgent.sh
-#
-
-if [ "${TESTJAVA}" = "" ]
-then
- echo "TESTJAVA not set. Test cannot execute. Failed."
- exit 1
-fi
-
-if [ "${COMPILEJAVA}" = "" ]
-then
- COMPILEJAVA="${TESTJAVA}"
-fi
-echo "COMPILEJAVA=${COMPILEJAVA}"
-
-if [ "${TESTSRC}" = "" ]
-then
- echo "TESTSRC not set. Test cannot execute. Failed."
- exit 1
-fi
-
-if [ "${TESTCLASSES}" = "" ]
-then
- echo "TESTCLASSES not set. Test cannot execute. Failed."
- exit 1
-fi
-
-JAVAC="${COMPILEJAVA}"/bin/javac
-JAVA="${TESTJAVA}"/bin/java
-
-"${JAVA}" ${TESTVMOPTS} -javaagent:ZeroArgPremainAgent.jar \
- -classpath "${TESTCLASSES}" DummyMain > output.log 2>&1
-cat output.log
-
-MESG="java.lang.NoSuchMethodException"
-grep "$MESG" output.log
-result=$?
-if [ "$result" = 0 ]; then
- echo "PASS: found '$MESG' in the test output"
-else
- echo "FAIL: did NOT find '$MESG' in the test output"
-fi
-
-exit $result
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java Thu Nov 14 12:35:34 2013 +0100
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import jdk.testlibrary.OutputAnalyzer;
+import jdk.testlibrary.ProcessTools;
+import jdk.testlibrary.Utils;
+
+/*
+ * @test
+ * @bug 6289149
+ * @summary test when the agent's class has a zero arg premain() function.
+ * @library /lib/testlibrary
+ * @run build DummyMain
+ * @run shell ../MakeJAR3.sh ZeroArgPremainAgent
+ * @run main ZeroArgPremainAgentTest
+ */
+public class ZeroArgPremainAgentTest {
+ // Use a javaagent with a zero argument premain() function.
+ // Verify that we get the correct exception.
+ public static void main(String[] a) throws Exception {
+ String testArgs = String.format(
+ "-javaagent:ZeroArgPremainAgent.jar -classpath %s DummyMain",
+ System.getProperty("test.classes", "."));
+
+ ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
+ Utils.addTestJavaOpts(testArgs.split("\\s+")));
+ System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));
+
+ OutputAnalyzer output = new OutputAnalyzer(pb.start());
+ System.out.println("testjvm.stdout:" + output.getStdout());
+ System.out.println("testjvm.stderr:" + output.getStderr());
+
+ output.stderrShouldContain("java.lang.NoSuchMethodException");
+ if (0 == output.getExitValue()) {
+ throw new RuntimeException("Expected error but got exit value 0");
+ }
+ }
+}
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/Utils.java Thu Nov 14 16:08:28 2013 -0800
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/Utils.java Thu Nov 14 12:35:34 2013 +0100
@@ -31,6 +31,8 @@
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
+import java.util.Arrays;
+import java.util.Collections;
/**
* Common library for various test helper functions.
@@ -45,7 +47,12 @@
/**
* Returns the value of 'test.vm.opts'system property.
*/
- public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "");
+ public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
+
+ /**
+ * Returns the value of 'test.java.opts'system property.
+ */
+ public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();
private Utils() {
@@ -58,7 +65,7 @@
* @return List of VM options
*/
public static List<String> getVmOptions() {
- return getVmOptions(false);
+ return Arrays.asList(safeSplitString(VM_OPTIONS));
}
/**
@@ -67,24 +74,58 @@
* @return The list of VM options with -J prefix
*/
public static List<String> getForwardVmOptions() {
- return getVmOptions(true);
+ String[] opts = safeSplitString(VM_OPTIONS);
+ for (int i = 0; i < opts.length; i++) {
+ opts[i] = "-J" + opts[i];
+ }
+ return Arrays.asList(opts);
+ }
+
+ /**
+ * Returns the default JTReg arguments for a jvm running a test.
+ * This is the combination of JTReg arguments test.vm.opts and test.java.opts.
+ * @return An array of options, or an empty array if no opptions.
+ */
+ public static String[] getTestJavaOpts() {
+ List<String> opts = new ArrayList<String>();
+ Collections.addAll(opts, safeSplitString(VM_OPTIONS));
+ Collections.addAll(opts, safeSplitString(JAVA_OPTIONS));
+ return opts.toArray(new String[0]);
}
- private static List<String> getVmOptions(boolean forward) {
- List<String> optionsList = new ArrayList<>();
- String options = VM_OPTIONS.trim();
- if (!options.isEmpty()) {
- options = options.replaceAll("\\s+", " ");
- for (String option : options.split(" ")) {
- if (forward) {
- optionsList.add("-J" + option);
- } else {
- optionsList.add(option);
- }
- }
+ /**
+ * Combines given arguments with default JTReg arguments for a jvm running a test.
+ * This is the combination of JTReg arguments test.vm.opts and test.java.opts
+ * @return The combination of JTReg test java options and user args.
+ */
+ public static String[] addTestJavaOpts(String... userArgs) {
+ List<String> opts = new ArrayList<String>();
+ Collections.addAll(opts, getTestJavaOpts());
+ Collections.addAll(opts, userArgs);
+ return opts.toArray(new String[0]);
+ }
+
+ /**
+ * Splits a string by white space.
+ * Works like String.split(), but returns an empty array
+ * if the string is null or empty.
+ */
+ private static String[] safeSplitString(String s) {
+ if (s == null || s.trim().isEmpty()) {
+ return new String[] {};
}
+ return s.trim().split("\\s+");
+ }
- return optionsList;
+ /**
+ * @return The full command line for the ProcessBuilder.
+ */
+ public static String getCommandLine(ProcessBuilder pb) {
+ StringBuilder cmd = new StringBuilder();
+ for (String s : pb.command()) {
+ cmd.append(s).append(" ");
+ }
+ return cmd.toString();
}
/**