From 7e3d3a961ed45a2932438898793b9c908c6842a7 Mon Sep 17 00:00:00 2001 From: sla Date: Thu, 14 Nov 2013 12:35:34 +0100 Subject: [PATCH] 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 --- .../instrument/PremainClass/NoPremainAgent.sh | 74 ------------------- .../PremainClass/NoPremainAgentTest.java | 58 +++++++++++++++ .../PremainClass/PremainClassTest.java | 58 +++++++++++++++ .../PremainClass/PremainClassTest.sh | 61 --------------- .../PremainClass/ZeroArgPremainAgent.sh | 74 ------------------- .../PremainClass/ZeroArgPremainAgentTest.java | 58 +++++++++++++++ .../testlibrary/jdk/testlibrary/Utils.java | 73 ++++++++++++++---- 7 files changed, 231 insertions(+), 225 deletions(-) delete mode 100644 test/java/lang/instrument/PremainClass/NoPremainAgent.sh create mode 100644 test/java/lang/instrument/PremainClass/NoPremainAgentTest.java create mode 100644 test/java/lang/instrument/PremainClass/PremainClassTest.java delete mode 100644 test/java/lang/instrument/PremainClass/PremainClassTest.sh delete mode 100644 test/java/lang/instrument/PremainClass/ZeroArgPremainAgent.sh create mode 100644 test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java diff --git a/test/java/lang/instrument/PremainClass/NoPremainAgent.sh b/test/java/lang/instrument/PremainClass/NoPremainAgent.sh deleted file mode 100644 index 3f8c745dd..000000000 --- a/test/java/lang/instrument/PremainClass/NoPremainAgent.sh +++ /dev/null @@ -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 diff --git a/test/java/lang/instrument/PremainClass/NoPremainAgentTest.java b/test/java/lang/instrument/PremainClass/NoPremainAgentTest.java new file mode 100644 index 000000000..9d08a7bd9 --- /dev/null +++ b/test/java/lang/instrument/PremainClass/NoPremainAgentTest.java @@ -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"); + } + } +} diff --git a/test/java/lang/instrument/PremainClass/PremainClassTest.java b/test/java/lang/instrument/PremainClass/PremainClassTest.java new file mode 100644 index 000000000..9ebe4a68d --- /dev/null +++ b/test/java/lang/instrument/PremainClass/PremainClassTest.java @@ -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!"); + } +} diff --git a/test/java/lang/instrument/PremainClass/PremainClassTest.sh b/test/java/lang/instrument/PremainClass/PremainClassTest.sh deleted file mode 100644 index e4cd42b1f..000000000 --- a/test/java/lang/instrument/PremainClass/PremainClassTest.sh +++ /dev/null @@ -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 diff --git a/test/java/lang/instrument/PremainClass/ZeroArgPremainAgent.sh b/test/java/lang/instrument/PremainClass/ZeroArgPremainAgent.sh deleted file mode 100644 index 3fc7e4863..000000000 --- a/test/java/lang/instrument/PremainClass/ZeroArgPremainAgent.sh +++ /dev/null @@ -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 diff --git a/test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java b/test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java new file mode 100644 index 000000000..7b67e4c31 --- /dev/null +++ b/test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java @@ -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"); + } + } +} diff --git a/test/lib/testlibrary/jdk/testlibrary/Utils.java b/test/lib/testlibrary/jdk/testlibrary/Utils.java index f8da7cf66..387c0e338 100644 --- a/test/lib/testlibrary/jdk/testlibrary/Utils.java +++ b/test/lib/testlibrary/jdk/testlibrary/Utils.java @@ -31,6 +31,8 @@ import java.net.ServerSocket; 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 @@ public final class Utils { /** * 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 @@ public final class Utils { * @return List of VM options */ public static List getVmOptions() { - return getVmOptions(false); + return Arrays.asList(safeSplitString(VM_OPTIONS)); } /** @@ -67,24 +74,58 @@ public final class Utils { * @return The list of VM options with -J prefix */ public static List 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); } - private static List getVmOptions(boolean forward) { - List 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); - } - } + /** + * 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 opts = new ArrayList(); + Collections.addAll(opts, safeSplitString(VM_OPTIONS)); + Collections.addAll(opts, safeSplitString(JAVA_OPTIONS)); + return opts.toArray(new String[0]); + } + + /** + * 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 opts = new ArrayList(); + 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(); } /** -- GitLab