// Arguments to the jar command has this format: // "-cfm TESTCLASSES/Agent.jar TESTSRC/agent.mf -C TESTCLASSES Agent.class"
RunnerUtil.createJar("-cfm", jar, manifest, "-C", testClasses, clazz);
}
System.out.println(" - Attaching to application ...");
VirtualMachine vm = VirtualMachine.attach(pid);
// Test 1 - read the system properties from the target VM and // check that property is set
System.out.println(" - Test: system properties in target VM");
Properties props = vm.getSystemProperties();
String value = props.getProperty("attach.test"); if (value == null || !value.equals("true")) { thrownew RuntimeException("attach.test property not set");
}
System.out.println(" - attach.test property set as expected");
// Test 1a - read the agent properties from the target VM. // By default, the agent property contains "sun.java.command", // "sun.jvm.flags", and "sun.jvm.args". // Just sanity check - make sure not empty.
System.out.println(" - Test: agent properties in target VM");
props = vm.getAgentProperties(); if (props == null || props.size() == 0) { thrownew RuntimeException("Agent properties is empty");
}
System.out.println(" - agent properties non-empty as expected");
// Test 2 - attempt to load an agent that does not exist
System.out.println(" - Test: Load an agent that does not exist"); try {
vm.loadAgent("SilverBullet.jar");
} catch (AgentLoadException x) {
System.out.println(" - AgentLoadException thrown as expected!");
}
// Test 3 - load an "bad" agent (agentmain throws an exception)
System.out.println(" - Test: Load a bad agent");
System.out.println("INFO: This test will cause error messages "
+ "to appear in the application log about SilverBullet.jar "
+ "not being found and an agent failing to start."); try {
vm.loadAgent(badagent); thrownew RuntimeException( "AgentInitializationException not thrown as expected!");
} catch (AgentInitializationException x) {
System.out.println( " - AgentInitializationException thrown as expected!");
}
// Test 4 - detach from the VM and attempt a load (should throw IOE)
System.out.println(" - Test: Detach from VM");
System.out.println("INFO: This test will cause error messages "
+ "to appear in the application log about a BadAgent including "
+ "a RuntimeException and an InvocationTargetException.");
vm.detach(); try {
vm.loadAgent(agent); thrownew RuntimeException("loadAgent did not throw an exception!!");
} catch (IOException ioe) {
System.out.println(" - IOException as expected");
}
// Test 5 - functional "end-to-end" test. // Create a listener socket. Load Agent.jar into the target VM passing // it the port number of our listener. When agent loads it should connect // back to the tool.
System.out.println(" - Re-attaching to application ...");
vm = VirtualMachine.attach(pid);
System.out.println(" - Test: End-to-end connection with agent");
ServerSocket ss = new ServerSocket(0); int port = ss.getLocalPort();
System.out.println(" - Loading Agent.jar into target VM ...");
vm.loadAgent(agent, Integer.toString(port));
System.out.println(" - Waiting for agent to connect back to tool ...");
Socket s = ss.accept();
System.out.println(" - Connected to agent.");
// Test 5b - functional "end-to-end" test. // Now with an agent that does redefine.
System.out.println(" - Re-attaching to application ...");
vm = VirtualMachine.attach(pid);
System.out.println(" - Test: End-to-end connection with RedefineAgent");
ServerSocket ss2 = new ServerSocket(0); int port2 = ss2.getLocalPort();
System.out.println(" - Loading RedefineAgent.jar into target VM ...");
vm.loadAgent(redefineagent, Integer.toString(port2));
System.out.println(" - Waiting for RedefineAgent to connect back to tool ...");
Socket s2 = ss2.accept();
System.out.println(" - Connected to RedefineAgent.");
// Test 6 - list method should list the target VM
System.out.println(" - Test: VirtualMachine.list");
List<VirtualMachineDescriptor> l = VirtualMachine.list(); boolean found = false; for (VirtualMachineDescriptor vmd: l) { if (vmd.id().equals(pid)) {
found = true; break;
}
} if (found) {
System.out.println(" - " + pid + " found.");
} else { thrownew RuntimeException(pid + " not found in VM list");
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.