jdk/test/com/sun/tools/attach/BasicTests.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 28760 b83d51c77b31
child 30820 0d4717a011d3
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 28760
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import com.sun.tools.attach.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    30
import java.io.File;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    31
import jdk.testlibrary.OutputAnalyzer;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    32
import jdk.testlibrary.JDKToolLauncher;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    33
import jdk.testlibrary.ProcessTools;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    34
import jdk.testlibrary.ProcessThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    36
/*
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    37
 * @test
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    38
 * @bug 6173612 6273707 6277253 6335921 6348630 6342019 6381757
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    39
 * @summary Basic unit tests for the VM attach mechanism.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    40
 * @library /lib/testlibrary
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 28760
diff changeset
    41
 * @modules java.instrument
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 28760
diff changeset
    42
 *          java.management
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 28760
diff changeset
    43
 *          jdk.jartool/sun.tools.jar
25182
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    44
 * @run build jdk.testlibrary.* Agent BadAgent RedefineAgent Application RedefineDummy RunnerUtil
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    45
 * @run main BasicTests
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    46
 *
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    47
 * This test will perform a number of basic attach tests.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    48
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class BasicTests {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    51
    /*
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    52
     * The actual test is in the nested class TestMain.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    53
     * The responsibility of this class is to:
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    54
     * 1. Build all needed jars.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    55
     * 2. Start the Application class in a separate process.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    56
     * 3. Find the pid and shutdown port of the running Application.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    57
     * 4. Launches the tests in nested class TestMain that will attach to the Application.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    58
     * 5. Shut down the Application.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    59
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    60
    public static void main(String args[]) throws Throwable {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    61
        ProcessThread processThread = null;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    62
        try {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    63
            buildJars();
25182
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    64
            processThread = RunnerUtil.startApplication();
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    65
            runTests(processThread.getPid());
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    66
        } catch (Throwable t) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    67
            System.out.println("TestBasic got unexpected exception: " + t);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    68
            t.printStackTrace();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    69
            throw t;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    70
        } finally {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    71
            // Make sure the Application process is stopped.
25182
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    72
            RunnerUtil.stopApplication(processThread);
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    73
        }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    74
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    76
    /**
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    77
     * Runs the actual tests in nested class TestMain.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    78
     * The reason for running the tests in a separate process
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    79
     * is that we need to modify the class path.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    80
     */
25182
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    81
    private static void runTests(long pid) throws Throwable {
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    82
        final String sep = File.separator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    84
        // Need to add jdk/lib/tools.jar to classpath.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    85
        String classpath =
28760
b83d51c77b31 8067945: SVC jdk/test/* should be cleaned from JRE layout dependency
ykantser
parents: 25182
diff changeset
    86
            System.getProperty("test.class.path", "");
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    87
        String testClassDir = System.getProperty("test.classes", "") + sep;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    88
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    89
        // Argumenta : -classpath cp BasicTests$TestMain pid agent badagent redefineagent
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    90
        String[] args = {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    91
            "-classpath",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    92
            classpath,
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    93
            "BasicTests$TestMain",
25182
1f6766bac82c 8048193: [tests] Replace JPS and stdout based PID retrieval by Process.getPid()
jbachorik
parents: 24366
diff changeset
    94
            Long.toString(pid),
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    95
            testClassDir + "Agent.jar",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    96
            testClassDir + "BadAgent.jar",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    97
            testClassDir + "RedefineAgent.jar" };
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    98
        OutputAnalyzer output = ProcessTools.executeTestJvm(args);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
    99
        output.shouldHaveExitValue(0);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   100
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   102
    /**
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   103
     * Will build all jars needed by the tests.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   104
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   105
    private static void buildJars() throws Throwable {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   106
        String[] jars = {"Agent", "BadAgent", "RedefineAgent", "Application" };
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   107
        for (String jar : jars) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   108
            buildJar(jar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   110
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   112
    /**
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   113
     * Will build a jar with the given name.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   114
     * Class file and manifest must already exist.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   115
     * @param jarName Name of the jar.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   116
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   117
    private static void buildJar(String jarName) throws Throwable {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   118
        String testClasses = System.getProperty("test.classes", "?");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   119
        String testSrc = System.getProperty("test.src", "?");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   120
        String jar = String.format("%s/%s.jar", testClasses, jarName);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   121
        String manifest = String.format("%s/%s.mf", testSrc, jarName.toLowerCase());
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   122
        String clazz = String.format("%s.class", jarName);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   123
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   124
        // Arguments to the jar command has this format:
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   125
        // "-cfm TESTCLASSES/Agent.jar TESTSRC/agent.mf -C TESTCLASSES Agent.class"
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   126
        RunnerUtil.createJar("-cfm", jar, manifest, "-C", testClasses, clazz);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   127
    }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   128
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   129
    /**
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   130
     * This is the actual test. It will attach to the running Application
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   131
     * and perform a number of basic attach tests.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   132
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   133
    public static class TestMain {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   134
        public static void main(String args[]) throws Exception {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   135
            String pid = args[0];
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   136
            String agent = args[1];
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   137
            String badagent = args[2];
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   138
            String redefineagent = args[3];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   140
            System.out.println(" - Attaching to application ...");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   141
            VirtualMachine vm = VirtualMachine.attach(pid);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   142
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   143
            // Test 1 - read the system properties from the target VM and
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   144
            // check that property is set
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   145
            System.out.println(" - Test: system properties in target VM");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   146
            Properties props = vm.getSystemProperties();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   147
            String value = props.getProperty("attach.test");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   148
            if (value == null || !value.equals("true")) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   149
                throw new RuntimeException("attach.test property not set");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   150
            }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   151
            System.out.println(" - attach.test property set as expected");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   153
            // Test 1a - read the agent properties from the target VM.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   154
            // By default, the agent property contains "sun.java.command",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   155
            // "sun.jvm.flags", and "sun.jvm.args".
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   156
            // Just sanity check - make sure not empty.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   157
            System.out.println(" - Test: agent properties in target VM");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   158
            props = vm.getAgentProperties();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   159
            if (props == null || props.size() == 0) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   160
                throw new RuntimeException("Agent properties is empty");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   161
            }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   162
            System.out.println(" - agent properties non-empty as expected");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   164
            // Test 2 - attempt to load an agent that does not exist
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   165
            System.out.println(" - Test: Load an agent that does not exist");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   166
            try {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   167
                vm.loadAgent("SilverBullet.jar");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   168
            } catch (AgentLoadException x) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   169
                System.out.println(" - AgentLoadException thrown as expected!");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   170
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   172
            // Test 3 - load an "bad" agent (agentmain throws an exception)
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   173
            System.out.println(" - Test: Load a bad agent");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   174
            System.out.println("INFO: This test will cause error messages "
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   175
                + "to appear in the application log about SilverBullet.jar "
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   176
                + "not being found and an agent failing to start.");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   177
            try {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   178
                vm.loadAgent(badagent);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   179
                throw new RuntimeException(
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   180
                    "AgentInitializationException not thrown as expected!");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   181
            } catch (AgentInitializationException x) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   182
                System.out.println(
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   183
                    " - AgentInitializationException thrown as expected!");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   184
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   186
            // Test 4 - detach from the VM and attempt a load (should throw IOE)
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   187
            System.out.println(" - Test: Detach from VM");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   188
            System.out.println("INFO: This test will cause error messages "
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   189
                + "to appear in the application log about a BadAgent including "
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   190
                + "a RuntimeException and an InvocationTargetException.");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   191
            vm.detach();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   192
            try {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   193
                vm.loadAgent(agent);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   194
                throw new RuntimeException("loadAgent did not throw an exception!!");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   195
            } catch (IOException ioe) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   196
                System.out.println(" - IOException as expected");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   197
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   199
            // Test 5 - functional "end-to-end" test.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   200
            // Create a listener socket. Load Agent.jar into the target VM passing
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   201
            // it the port number of our listener. When agent loads it should connect
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   202
            // back to the tool.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   203
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   204
            System.out.println(" - Re-attaching to application ...");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   205
            vm = VirtualMachine.attach(pid);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   206
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   207
            System.out.println(" - Test: End-to-end connection with agent");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   209
            ServerSocket ss = new ServerSocket(0);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   210
            int port = ss.getLocalPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   212
            System.out.println(" - Loading Agent.jar into target VM ...");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   213
            vm.loadAgent(agent, Integer.toString(port));
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   214
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   215
            System.out.println(" - Waiting for agent to connect back to tool ...");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   216
            Socket s = ss.accept();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   217
            System.out.println(" - Connected to agent.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   219
            // Test 5b - functional "end-to-end" test.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   220
            // Now with an agent that does redefine.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   221
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   222
            System.out.println(" - Re-attaching to application ...");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   223
            vm = VirtualMachine.attach(pid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   225
            System.out.println(" - Test: End-to-end connection with RedefineAgent");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   226
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   227
            ServerSocket ss2 = new ServerSocket(0);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   228
            int port2 = ss2.getLocalPort();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   229
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   230
            System.out.println(" - Loading RedefineAgent.jar into target VM ...");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   231
            vm.loadAgent(redefineagent, Integer.toString(port2));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   233
            System.out.println(" - Waiting for RedefineAgent to connect back to tool ...");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   234
            Socket s2 = ss2.accept();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   235
            System.out.println(" - Connected to RedefineAgent.");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   236
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   237
            // Test 6 - list method should list the target VM
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   238
            System.out.println(" - Test: VirtualMachine.list");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   239
            List<VirtualMachineDescriptor> l = VirtualMachine.list();
24268
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   240
            boolean found = false;
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   241
            for (VirtualMachineDescriptor vmd: l) {
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   242
                if (vmd.id().equals(pid)) {
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   243
                    found = true;
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   244
                    break;
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   245
                }
24268
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   246
            }
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   247
            if (found) {
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   248
                System.out.println(" - " + pid + " found.");
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   249
            } else {
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   250
                throw new RuntimeException(pid + " not found in VM list");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   253
            // test 7 - basic hashCode/equals tests
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   254
            System.out.println(" - Test: hashCode/equals");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   256
            VirtualMachine vm1 = VirtualMachine.attach(pid);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   257
            VirtualMachine vm2 = VirtualMachine.attach(pid);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   258
            if (!vm1.equals(vm2)) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   259
                throw new RuntimeException("virtual machines are not equal");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   260
            }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   261
            if (vm.hashCode() != vm.hashCode()) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   262
                throw new RuntimeException("virtual machine hashCodes not equal");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   263
            }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   264
            System.out.println(" - hashCode/equals okay");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   265
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   266
            // ---
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   267
            System.out.println(" - Cleaning up...");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   268
            s.close();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   269
            ss.close();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   270
            s2.close();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 9259
diff changeset
   271
            ss2.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
}