1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/*
* Copyright (c) 2003, 2024, 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 nsk.jdi.VirtualMachineManager.createVirtualMachine;
import nsk.share.*;
import nsk.share.jdi.*;
import com.sun.jdi.*;
import com.sun.jdi.connect.*;
import com.sun.jdi.connect.spi.*;
import java.util.*;
import java.io.*;
/**
* The test for the <BR>
* virtualMachineManager.createVirtualMachine(...) methods. <BR>
* <BR>
* The test checks up that createVirtualMachine(Connection, Process) <BR>
* method creates the VirtualMachine properly for Process that is NOT <BR>
* null. <BR>
* <BR>
* After the VirtualMachine is created the test checks that: <BR>
* - VirtualMachine.process() method returns the same Process passed <BR>
* to the createVirtualMachine(Connection, Process) method; <BR>
* - after VirtualMachine.resume() the Process should finish for <BR>
* specified time with expected Exit Status value; <BR>
* <BR>
*/
public class createVM003 {
static final int STATUS_PASSED = 0;
static final int STATUS_FAILED = 2;
static final int STATUS_TEMP = 95;
static final String errorLogPrefixHead = "createVM003: ";
static final String errorLogPrefix = " ";
static final String infoLogPrefixHead = "--> createVM003: ";
static final String infoLogPrefix = "--> ";
static final String emptyString = "";
static final String packagePrefix = "nsk.jdi.VirtualMachineManager.createVirtualMachine.";
// static final String packagePrefix = emptyString;
static final String targetVMClassName = packagePrefix + "CreateVM003_TargetVM";
static ArgumentHandler argsHandler;
static Log logHandler;
private static void logOnVerbose(String message) {
logHandler.display(message);
}
private static void logOnError(String message) {
logHandler.complain(message);
}
private static void logAlways(String message) {
logHandler.println(message);
}
public static void main (String argv[]) {
int result = run(argv,System.out);
if (result != 0) {
throw new RuntimeException("TEST FAILED with result " + result);
}
}
public static int run (String argv[], PrintStream out) {
int result = new createVM003().runThis(argv, out);
if ( result == STATUS_FAILED ) {
logAlways("\n##> nsk/jdi/VirtualMachineManager/createVirtualMachine/createVM003 test FAILED");
}
else {
logAlways("\n==> nsk/jdi/VirtualMachineManager/createVirtualMachine/createVM003 test PASSED");
}
return result;
}
private int runThis (String argv[], PrintStream out) {
int testResult = STATUS_PASSED;
argsHandler = new ArgumentHandler(argv);
logHandler = new Log(out, argsHandler);
logAlways("==> nsk/jdi/VirtualMachineManager/createVirtualMachine/createVM003 test...");
logOnVerbose
("==> Test checks that virtualMachineManager.createVirtualMachine(Connection, Process) method");
logOnVerbose
("==> creates Virtual Machine properly for Process that is NOT null.");
VirtualMachineManager virtualMachineManager = Bootstrap.virtualMachineManager();
if (virtualMachineManager == null) {
logOnError(errorLogPrefixHead + "Bootstrap.virtualMachineManager() returns null.");
return STATUS_FAILED;
}
TransportService testTransportService = new CreateVM003_TranspServ();
TransportService.ListenKey testTransportServiceListenKey = null;
logOnVerbose(infoLogPrefixHead + "Start Listening for target VM...");
try {
testTransportServiceListenKey = testTransportService.startListening();
} catch ( IOException ioExcept ) {
// OK. It is possible Exception
logOnVerbose(infoLogPrefixHead
+ "testTransportService.startListening() throws IOException.");
logOnVerbose(infoLogPrefix + "testTransportService = '" + testTransportService + "'");
logOnVerbose(infoLogPrefix + "IOException - '" + ioExcept + "'");
logOnVerbose(infoLogPrefix + "The test is stopped.");
return STATUS_PASSED;
} catch ( Throwable thrown ) {
logOnError(errorLogPrefixHead + "testTransportService.startListening() throws");
logOnError(errorLogPrefix + "unexpected Exception:");
logOnError(errorLogPrefix + "testTransportService = '" + testTransportService + "'");
logOnError(errorLogPrefix + "Exception - '" + thrown + "'");
return STATUS_FAILED;
}
String targetJava = argsHandler.getLaunchExecPath()
+ " " + argsHandler.getLaunchOptions();
String commandToRun = targetJava + " -Xrunjdwp:transport=dt_socket,address=" +
testTransportServiceListenKey.address() + " " + targetVMClassName;
Binder binder = new Binder(argsHandler, logHandler);
Debugee debugee = null;
Process processToRun = null;
Connection testTransportServiceConnection = null;
try {
logOnVerbose(infoLogPrefixHead + "PROCESS is being created:");
logOnVerbose(infoLogPrefix + "Command to run: " + commandToRun);
debugee = binder.startLocalDebugee(commandToRun);
debugee.redirectOutput(logHandler);
processToRun = debugee.getProcess();
logOnVerbose(infoLogPrefixHead + "Accepting launched target VM...");
try {
testTransportServiceConnection
= testTransportService.accept(testTransportServiceListenKey, 0, 0);
} catch ( IOException ioExcept ) {
// OK. It is possible Exception
logOnVerbose(infoLogPrefixHead
+ "testTransportService.accept(testTransportServiceListenKey, 0, 0) throws IOException.");
logOnVerbose(infoLogPrefix + "testTransportService = '" + testTransportService + "'");
logOnVerbose(infoLogPrefix + "testTransportServiceListenKey = '" + testTransportServiceListenKey + "'");
logOnVerbose(infoLogPrefix + "IOException - '" + ioExcept + "'");
logOnVerbose(infoLogPrefix + "The test is stopped.");
return STATUS_PASSED;
} catch ( Throwable thrown ) {
logOnError(errorLogPrefixHead + "testTransportService.accept(testTransportServiceListenKey) throws");
logOnError(errorLogPrefix + "unexpected Exception:");
logOnError(errorLogPrefix + "testTransportService = '" + testTransportService + "'");
logOnError(errorLogPrefix + "testTransportServiceListenKey = '" + testTransportServiceListenKey + "'");
logOnError(errorLogPrefix + "Exception - '" + thrown + "'");
return STATUS_FAILED;
}
try {
testTransportService.stopListening(testTransportServiceListenKey);
} catch ( IOException ioExcept ) {
// OK. It is possible Exception
logOnVerbose(infoLogPrefixHead
+ "testTransportService.stopListening(testTransportServiceListenKey) throws IOException.");
logOnVerbose(infoLogPrefix + "testTransportService = '" + testTransportService + "'");
logOnVerbose(infoLogPrefix + "testTransportServiceListenKey = '" + testTransportServiceListenKey + "'");
logOnVerbose(infoLogPrefix + "IOException - '" + ioExcept + "'");
logOnVerbose(infoLogPrefix + "The test is stopped.");
return STATUS_PASSED;
} catch ( Throwable thrown ) {
logOnError(errorLogPrefixHead
+ "testTransportService.stopListening(testTransportServiceListenKey) throws");
logOnError(errorLogPrefix + "unexpected Exception:");
logOnError(errorLogPrefix + "testTransportService = '" + testTransportService + "'");
logOnError(errorLogPrefix + "testTransportServiceListenKey = '" + testTransportServiceListenKey + "'");
logOnError(errorLogPrefix + "Exception - '" + thrown + "'");
return STATUS_FAILED;
}
logOnVerbose(infoLogPrefixHead + "Creating VirtualMachine for target VM...");
VirtualMachine testVirtualMachine = null;
try {
testVirtualMachine
= virtualMachineManager.createVirtualMachine(testTransportServiceConnection,
processToRun);
} catch ( IOException ioExcept ) {
// OK. It is possible Exception
logOnVerbose(infoLogPrefixHead
+ "VirtualMachineManager.createVirtualMachine(Connection, Process) throws IOException.");
logOnVerbose(infoLogPrefix + "IOException - '" + ioExcept + "'");
logOnVerbose(infoLogPrefix + "The test is stopped.");
return STATUS_PASSED;
} catch ( Throwable thrown ) {
logOnError(errorLogPrefixHead
+ "VirtualMachineManager.createVirtualMachine(Connection, Process) throws");
logOnError(errorLogPrefix + "unexpected Exception:");
logOnError(errorLogPrefix + "Connection = '" + testTransportServiceConnection + "'");
logOnError(errorLogPrefix + "Exception - '" + thrown + "'");
return STATUS_FAILED;
}
Process testVirtualMachineProcess = testVirtualMachine.process();
if ( ! processToRun.equals(testVirtualMachineProcess) ) {
logOnError(errorLogPrefixHead + "Created VirtualMachine returns unexpected Process:");
logOnError(errorLogPrefix + "Expected Process = '" + processToRun + "'");
logOnError(errorLogPrefix + "Returned Process = '" + testVirtualMachineProcess + "'");
testResult = STATUS_FAILED;
}
long timeout = argsHandler.getWaitTime() * 60 * 1000; // milliseconds
logOnVerbose(infoLogPrefixHead + "Managing target VM");
debugee.setupVM(testVirtualMachine);
logOnVerbose(infoLogPrefix + "Waiting for VM initialized");
debugee.waitForVMInit(timeout);
logOnVerbose(infoLogPrefix + "Resuming VM");
debugee.resume();
logOnVerbose(infoLogPrefix + "Waiting for VM exit");
int procExitValue = debugee.waitFor();
logOnVerbose(infoLogPrefix + "VM exit status: " + procExitValue);
if ( procExitValue != (STATUS_PASSED + STATUS_TEMP) ) {
logOnError(errorLogPrefixHead + "Target VM finished unexpectedly:");
logOnError(errorLogPrefix + "Expected Exit status = " + (STATUS_PASSED + STATUS_TEMP));
logOnError(errorLogPrefix + "Actual Exit status = " + procExitValue);
testResult = STATUS_FAILED;
} else {
logOnVerbose(infoLogPrefixHead + "Target VM finished succesfully!");
}
} finally {
logOnVerbose(infoLogPrefixHead + "Closing connection and destroying target VM...");
if (testTransportServiceConnection != null) {
try {
testTransportServiceConnection.close();
} catch (IOException e) {
logAlways("# WARNING: IOException while closing connection:\n\t" + e);
}
}
if (debugee != null) {
debugee.close();
}
}
return testResult;
}
} // end of createVM003 class
|