Path: blob/master/test/jdk/com/sun/tools/attach/TempDirTest.java
41153 views
/*1* Copyright (c) 2014, 2018, 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*/2223import com.sun.tools.attach.*;2425import java.nio.file.Files;26import java.nio.file.Path;27import java.util.Properties;28import java.util.List;29import java.io.File;3031import jdk.test.lib.thread.ProcessThread;32import jdk.test.lib.process.OutputAnalyzer;33import jdk.test.lib.process.ProcessTools;3435/*36* @test37* @bug 803310438* @summary Test to make sure attach and jvmstat works correctly when java.io.tmpdir is set39*40* @library /test/lib41* @modules jdk.attach42* jdk.jartool/sun.tools.jar43*44* @run build Application RunnerUtil45* @run main/timeout=200 TempDirTest46*/4748/*49* This test runs with an extra long timeout since it takes a really long time with -Xcomp50* when starting many processes.51*/5253public class TempDirTest {5455private static long startTime;5657public static void main(String args[]) throws Throwable {5859startTime = System.currentTimeMillis();6061Path clientTmpDir = Files.createTempDirectory("TempDirTest-client");62clientTmpDir.toFile().deleteOnExit();63Path targetTmpDir = Files.createTempDirectory("TempDirTest-target");64targetTmpDir.toFile().deleteOnExit();6566// run the test with all possible combinations of setting java.io.tmpdir67runExperiment(null, null);68runExperiment(clientTmpDir, null);69runExperiment(clientTmpDir, targetTmpDir);70runExperiment(null, targetTmpDir);7172}7374private static int counter = 0;7576/*77* The actual test is in the nested class TestMain.78* The responsibility of this class is to:79* 1. Start the Application class in a separate process.80* 2. Find the pid and shutdown port of the running Application.81* 3. Launches the tests in nested class TestMain that will attach to the Application.82* 4. Shut down the Application.83*/84public static void runExperiment(Path clientTmpDir, Path targetTmpDir) throws Throwable {8586System.out.print("### Running tests with overridden tmpdir for");87System.out.print(" client: " + (clientTmpDir == null ? "no" : "yes"));88System.out.print(" target: " + (targetTmpDir == null ? "no" : "yes"));89System.out.println(" ###");9091long elapsedTime = (System.currentTimeMillis() - startTime) / 1000;92System.out.println("Started after " + elapsedTime + "s");9394final String pidFile = "TempDirTest.Application.pid-" + counter++;95ProcessThread processThread = null;96try {97String[] tmpDirArg = null;98if (targetTmpDir != null) {99tmpDirArg = new String[] {"-Djava.io.tmpdir=" + targetTmpDir};100}101processThread = RunnerUtil.startApplication(tmpDirArg);102launchTests(processThread.getPid(), clientTmpDir);103} catch (Throwable t) {104System.out.println("TempDirTest got unexpected exception: " + t);105t.printStackTrace();106throw t;107} finally {108// Make sure the Application process is stopped.109RunnerUtil.stopApplication(processThread);110}111112elapsedTime = (System.currentTimeMillis() - startTime) / 1000;113System.out.println("Completed after " + elapsedTime + "s");114115}116117/**118* Runs the actual tests in nested class TestMain.119* The reason for running the tests in a separate process120* is that we need to modify the class path and121* the -Djava.io.tmpdir property.122*/123private static void launchTests(long pid, Path clientTmpDir) throws Throwable {124final String sep = File.separator;125126String classpath =127System.getProperty("test.class.path", "");128129String[] tmpDirArg = null;130if (clientTmpDir != null) {131tmpDirArg = new String [] {"-Djava.io.tmpdir=" + clientTmpDir};132}133134// Arguments : [-Djava.io.tmpdir=] -classpath cp TempDirTest$TestMain pid135String[] args = RunnerUtil.concat(136tmpDirArg,137new String[] {138"-classpath",139classpath,140"TempDirTest$TestMain",141Long.toString(pid) });142OutputAnalyzer output = ProcessTools.executeTestJvm(args);143output.shouldHaveExitValue(0);144}145146/**147* This is the actual test. It will attach to the running Application148* and perform a number of basic attach tests.149*/150public static class TestMain {151public static void main(String args[]) throws Exception {152String pid = args[0];153154// Test 1 - list method should list the target VM155System.out.println(" - Test: VirtualMachine.list");156List<VirtualMachineDescriptor> l = VirtualMachine.list();157boolean found = false;158for (VirtualMachineDescriptor vmd: l) {159if (vmd.id().equals(pid)) {160found = true;161break;162}163}164if (found) {165System.out.println(" - " + pid + " found.");166} else {167throw new RuntimeException(pid + " not found in VM list");168}169170// Test 2 - try to attach and verify connection171172System.out.println(" - Attaching to application ...");173VirtualMachine vm = VirtualMachine.attach(pid);174175System.out.println(" - Test: system properties in target VM");176Properties props = vm.getSystemProperties();177String value = props.getProperty("attach.test");178if (value == null || !value.equals("true")) {179throw new RuntimeException("attach.test property not set");180}181System.out.println(" - attach.test property set as expected");182}183}184}185186187