8139430: Refactor test library to decrease module dependencies of tests
authorshurailine
Mon, 23 Nov 2015 11:49:04 -0800
changeset 34327 50693d25f4a7
parent 34326 2bb3b3aea3c5
child 34328 0534cb944d0e
8139430: Refactor test library to decrease module dependencies of tests Reviewed-by: mchung, alanb
jdk/test/java/util/logging/TestLoggerWeakRefLeak.java
jdk/test/lib/testlibrary/jdk/testlibrary/InputArguments.java
jdk/test/lib/testlibrary/jdk/testlibrary/JcmdBase.java
jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java
jdk/test/lib/testlibrary/jdk/testlibrary/TestThread.java
jdk/test/lib/testlibrary/jdk/testlibrary/management/InputArguments.java
jdk/test/lib/testlibrary/jdk/testlibrary/management/ThreadMXBeanTool.java
jdk/test/sun/tools/jcmd/TestJcmdSanity.java
jdk/test/sun/tools/jinfo/JInfoHelper.java
jdk/test/sun/tools/jmap/BasicJMapTest.java
jdk/test/sun/tools/jps/JpsBase.java
jdk/test/sun/tools/jstack/BasicJStackTest.java
--- a/jdk/test/java/util/logging/TestLoggerWeakRefLeak.java	Mon Nov 23 10:36:15 2015 -0800
+++ b/jdk/test/java/util/logging/TestLoggerWeakRefLeak.java	Mon Nov 23 11:49:04 2015 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2015, 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
@@ -128,7 +128,7 @@
         int instanceCount = 0;
 
         HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine
-                .attach(Integer.toString(ProcessTools.getProcessId()));
+                .attach(Long.toString(ProcessTools.getProcessId()));
         try {
             try (InputStream heapHistoStream = vm.heapHisto("-live");
                     BufferedReader in = new BufferedReader(new InputStreamReader(heapHistoStream))) {
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/InputArguments.java	Mon Nov 23 10:36:15 2015 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,88 +0,0 @@
-/*
- * 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.
- */
-
-package jdk.testlibrary;
-
-import java.lang.management.RuntimeMXBean;
-import java.lang.management.ManagementFactory;
-import java.util.List;
-
-/**
- * This class provides access to the input arguments to the VM.
- */
-public class InputArguments {
-    private static final List<String> args;
-
-    static {
-        RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
-        args = runtimeMxBean.getInputArguments();
-    }
-
-    /**
-     * Returns true if {@code arg} is an input argument to the VM.
-     *
-     * This is useful for checking boolean flags such as -XX:+UseSerialGC or
-     * -XX:-UsePerfData.
-     *
-     * @param arg The name of the argument.
-     * @return {@code true} if the given argument is an input argument,
-     *         otherwise {@code false}.
-     */
-    public static boolean contains(String arg) {
-        return args.contains(arg);
-    }
-
-    /**
-     * Returns true if {@code prefix} is the start of an input argument to the
-     * VM.
-     *
-     * This is useful for checking if flags describing a quantity, such as
-     * -XX:+MaxMetaspaceSize=100m, is set without having to know the quantity.
-     * To check if the flag -XX:MaxMetaspaceSize is set, use
-     * {@code InputArguments.containsPrefix("-XX:MaxMetaspaceSize")}.
-     *
-     * @param prefix The start of the argument.
-     * @return {@code true} if the given argument is the start of an input
-     *         argument, otherwise {@code false}.
-     */
-    public static boolean containsPrefix(String prefix) {
-        for (String arg : args) {
-            if (arg.startsWith(prefix)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
-    /**
-     * Get the string containing input arguments passed to the VM
-     */
-    public static String getInputArguments() {
-        StringBuilder result = new StringBuilder();
-        for (String arg : args)
-            result.append(arg).append(' ');
-
-        return result.toString();
-    }
-
-}
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/JcmdBase.java	Mon Nov 23 10:36:15 2015 -0800
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/JcmdBase.java	Mon Nov 23 11:49:04 2015 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2015, 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
@@ -99,7 +99,7 @@
             }
         }
         if (requestToCurrentProcess) {
-            launcher.addToolArg(Integer.toString(ProcessTools.getProcessId()));
+            launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));
         }
         if (jcmdArgs != null) {
             for (String toolArg : jcmdArgs) {
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Mon Nov 23 10:36:15 2015 -0800
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Mon Nov 23 11:49:04 2015 -0800
@@ -27,8 +27,6 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
-import java.lang.management.ManagementFactory;
-import java.lang.management.RuntimeMXBean;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
@@ -253,11 +251,8 @@
      *
      * @return Process id
      */
-    public static int getProcessId() throws Exception {
-        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
-        int pid = Integer.parseInt(runtime.getName().split("@")[0]);
-
-        return pid;
+    public static long getProcessId() {
+        return ProcessHandle.current().getPid();
     }
 
     /**
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/TestThread.java	Mon Nov 23 10:36:15 2015 -0800
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/TestThread.java	Mon Nov 23 11:49:04 2015 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2015, 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
@@ -23,9 +23,6 @@
 
 package jdk.testlibrary;
 
-import java.lang.management.ManagementFactory;
-import java.lang.management.ThreadInfo;
-import java.lang.management.ThreadMXBean;
 import java.util.concurrent.TimeoutException;
 
 /**
@@ -204,46 +201,4 @@
         }
         return null;
     }
-
-    /**
-     * Waits until {@link TestThread} is in the certain {@link State}
-     * and blocking on {@code object}.
-     *
-     * @param state The thread state
-     * @param object The object to block on
-     */
-    public void waitUntilBlockingOnObject(Thread.State state, Object object) {
-        String want = object == null ? null : object.getClass().getName() + '@'
-                + Integer.toHexString(System.identityHashCode(object));
-        ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
-        while (isAlive()) {
-            ThreadInfo ti = tmx.getThreadInfo(getId());
-            if (ti.getThreadState() == state
-                    && (want == null || want.equals(ti.getLockName()))) {
-                return;
-            }
-            try {
-                Thread.sleep(1);
-            } catch (InterruptedException e) {
-            }
-        }
-    }
-
-    /**
-     * Waits until {@link TestThread} is in native.
-     */
-    public void waitUntilInNative() {
-        ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
-        while (isAlive()) {
-            ThreadInfo ti = tmx.getThreadInfo(getId());
-            if (ti.isInNative()) {
-                return;
-            }
-            try {
-                Thread.sleep(1);
-            } catch (InterruptedException e) {
-            }
-        }
-    }
-
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/management/InputArguments.java	Mon Nov 23 11:49:04 2015 -0800
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2013, 2015, 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.
+ */
+
+package jdk.testlibrary.management;
+
+import java.lang.management.RuntimeMXBean;
+import java.lang.management.ManagementFactory;
+import java.util.List;
+
+/**
+ * This class provides access to the input arguments to the VM.
+ */
+public class InputArguments {
+    private static final List<String> args;
+
+    static {
+        RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
+        args = runtimeMxBean.getInputArguments();
+    }
+
+    /**
+     * Returns true if {@code arg} is an input argument to the VM.
+     *
+     * This is useful for checking boolean flags such as -XX:+UseSerialGC or
+     * -XX:-UsePerfData.
+     *
+     * @param arg The name of the argument.
+     * @return {@code true} if the given argument is an input argument,
+     *         otherwise {@code false}.
+     */
+    public static boolean contains(String arg) {
+        return args.contains(arg);
+    }
+
+    /**
+     * Returns true if {@code prefix} is the start of an input argument to the
+     * VM.
+     *
+     * This is useful for checking if flags describing a quantity, such as
+     * -XX:+MaxMetaspaceSize=100m, is set without having to know the quantity.
+     * To check if the flag -XX:MaxMetaspaceSize is set, use
+     * {@code InputArguments.containsPrefix("-XX:MaxMetaspaceSize")}.
+     *
+     * @param prefix The start of the argument.
+     * @return {@code true} if the given argument is the start of an input
+     *         argument, otherwise {@code false}.
+     */
+    public static boolean hasArgStartingWith(String prefix) {
+        for (String arg : args) {
+            if (arg.startsWith(prefix)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Get the string containing input arguments passed to the VM
+     */
+    public static String getInputArguments() {
+        StringBuilder result = new StringBuilder();
+        for (String arg : args)
+            result.append(arg).append(' ');
+
+        return result.toString();
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/management/ThreadMXBeanTool.java	Mon Nov 23 11:49:04 2015 -0800
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2015, 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.
+ */
+
+package jdk.testlibrary.management;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
+import java.lang.management.ThreadMXBean;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * A few utility methods to use ThreadMXBean.
+ */
+public final class ThreadMXBeanTool {
+
+    /**
+     * Waits until {@link Thread} is in the certain {@link State}
+     * and blocking on {@code object}.
+     *
+     * @param state The thread state
+     * @param object The object to block on
+     */
+    public static void waitUntilBlockingOnObject(Thread thread, Thread.State state, Object object)
+        throws InterruptedException {
+        String want = object == null ? null : object.getClass().getName() + '@'
+                + Integer.toHexString(System.identityHashCode(object));
+        ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
+        while (thread.isAlive()) {
+            ThreadInfo ti = tmx.getThreadInfo(thread.getId());
+            if (ti.getThreadState() == state
+                    && (want == null || want.equals(ti.getLockName()))) {
+                return;
+            }
+            Thread.sleep(1);
+        }
+    }
+
+    /**
+     * Waits until {@link Thread} is in native.
+     */
+    public static void waitUntilInNative(Thread thread) throws InterruptedException {
+        ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
+        while (thread.isAlive()) {
+            ThreadInfo ti = tmx.getThreadInfo(thread.getId());
+            if (ti.isInNative()) {
+                return;
+            }
+            Thread.sleep(1);
+        }
+    }
+
+}
--- a/jdk/test/sun/tools/jcmd/TestJcmdSanity.java	Mon Nov 23 10:36:15 2015 -0800
+++ b/jdk/test/sun/tools/jcmd/TestJcmdSanity.java	Mon Nov 23 11:49:04 2015 -0800
@@ -71,7 +71,7 @@
 
         output.shouldHaveExitValue(0);
         output.shouldNotContain("Exception");
-        output.shouldContain(Integer.toString(ProcessTools.getProcessId()) + ":");
+        output.shouldContain(Long.toString(ProcessTools.getProcessId()) + ":");
         matchJcmdCommands(output);
         output.shouldContain("For more information about a specific command use 'help <command>'.");
     }
--- a/jdk/test/sun/tools/jinfo/JInfoHelper.java	Mon Nov 23 10:36:15 2015 -0800
+++ b/jdk/test/sun/tools/jinfo/JInfoHelper.java	Mon Nov 23 11:49:04 2015 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2015, 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
@@ -60,7 +60,7 @@
             }
         }
         if (toPid) {
-            launcher.addToolArg(Integer.toString(ProcessTools.getProcessId()));
+            launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));
         }
 
         ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
--- a/jdk/test/sun/tools/jmap/BasicJMapTest.java	Mon Nov 23 10:36:15 2015 -0800
+++ b/jdk/test/sun/tools/jmap/BasicJMapTest.java	Mon Nov 23 11:49:04 2015 -0800
@@ -111,7 +111,7 @@
                 launcher.addToolArg(toolArg);
             }
         }
-        launcher.addToolArg(Integer.toString(ProcessTools.getProcessId()));
+        launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));
 
         processBuilder.command(launcher.getCommand());
         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
--- a/jdk/test/sun/tools/jps/JpsBase.java	Mon Nov 23 10:36:15 2015 -0800
+++ b/jdk/test/sun/tools/jps/JpsBase.java	Mon Nov 23 11:49:04 2015 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2015, 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
@@ -59,7 +59,7 @@
     }
 
     public static void main(String[] args) throws Exception {
-        int pid = ProcessTools.getProcessId();
+        long pid = ProcessTools.getProcessId();
 
         List<List<JpsHelper.JpsArg>> combinations = JpsHelper.JpsArg.generateCombinations();
         for (List<JpsHelper.JpsArg> combination : combinations) {
@@ -76,7 +76,7 @@
                     // 30673
                     isQuiet = true;
                     JpsHelper.verifyJpsOutput(output, "^\\d+$");
-                    output.shouldContain(Integer.toString(pid));
+                    output.shouldContain(Long.toString(pid));
                     break;
                 case l:
                     // If '-l' is specified output should contain the full package name for the application's main class
--- a/jdk/test/sun/tools/jstack/BasicJStackTest.java	Mon Nov 23 10:36:15 2015 -0800
+++ b/jdk/test/sun/tools/jstack/BasicJStackTest.java	Mon Nov 23 11:49:04 2015 -0800
@@ -63,7 +63,7 @@
                 launcher.addToolArg(toolArg);
             }
         }
-        launcher.addToolArg(Integer.toString(ProcessTools.getProcessId()));
+        launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));
 
         processBuilder.command(launcher.getCommand());
         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));