Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjie-fourth committed Jun 2, 2023
1 parent 150228a commit 865174c
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 41 deletions.
Binary file added attacher/lib/jna-5.13.0.jar
Binary file not shown.
Binary file added attacher/lib/jna-platform-5.13.0.jar
Binary file not shown.
File renamed without changes.
16 changes: 15 additions & 1 deletion attacher/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@
<artifactId>tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${basedir}/tools.jar</systemPath>
<systemPath>${basedir}/lib/tools-windows.jar</systemPath>
</dependency>
<dependency>
<groupId>jna</groupId>
<artifactId>code</artifactId>
<version>5.13.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jna-5.13.0.jar</systemPath>
</dependency>
<dependency>
<groupId>jna</groupId>
<artifactId>platform</artifactId>
<version>5.13.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jna-platform-5.13.0.jar</systemPath>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,4 @@ public static void main(String[] args) throws InterruptedException {
ex.printStackTrace();
}
}

private static class LocalDemo {
public static void main(String[] args) {
String pid = "19384";
String agentPathAndOptions = "D:\\project\\myself\\MockSystem\\agent\\target\\agent-1.0.jar";
try {
VirtualMachine virtualMachine = VirtualMachine.attach(pid);
virtualMachine.loadAgent(agentPathAndOptions);
virtualMachine.detach();
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}
}
3 changes: 3 additions & 0 deletions integration-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<artifactId>maven-surefire-plugin</artifactId>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<version>3.1.0</version>
<configuration>
<argLine>-XX:+StartAttachListener</argLine>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package org.github.fourth.mocksystem.integrationtest;

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinNT;
import com.sun.tools.attach.VirtualMachine;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.lang.reflect.Field;
import java.util.logging.Logger;

@DisplayName("模拟测试")
public class IntegrationTest {

private static final String AGENT_JAR_LOCATION = "../agent/target/mocksystem-agent-1.0.0.jar";
public static final String AGENT_JAR_LOCATION = "../agent/target/mocksystem-agent-1.0.0.jar";

private static final String TEST_APPLICATION_LOCATION = "../test-application/target/mocksystem-test-application-1.0.0-jar-with-dependencies.jar";

Expand All @@ -35,42 +39,58 @@ public void startLoadingTest() throws IOException, InterruptedException {
}

// @Test
// @DisplayName("运行时加载测试")
@DisplayName("运行时加载测试")
public void runtimeLoadingTest() throws IOException, InterruptedException {
String command;
if (isLocalMachine() && isMac()) {
if (isMac() && isLocalMachine()) {
command = String.format("%s -jar %s", LOCAL_JAVA_COMMAND_LOCATION, TEST_APPLICATION_LOCATION);
} else {
command = String.format(" java -jar %s", TEST_APPLICATION_LOCATION);
}
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(computeShebang(), executeTheStringAsCommand(), command);

ProcessUtils processUtils = new ProcessUtils(processBuilder);

String pid = String.valueOf(getPid(processUtils.getProcess()));
System.out.println(pid);
try {
VirtualMachine virtualMachine = VirtualMachine.attach(pid);
virtualMachine.loadAgent(AGENT_JAR_LOCATION);
virtualMachine.detach();
} catch (Throwable ex) {
ex.printStackTrace();
}
Thread.sleep(10000);
// todo: 似乎只能通过执行shell脚本来完成
// ProcessUtils processUtils = new ProcessUtils(processBuilder);
// String pid = String.valueOf(getProcessID(process));
// System.out.println(pid);
// try {
// VirtualMachine virtualMachine = VirtualMachine.attach(pid);
// virtualMachine.loadAgent(AGENT_JAR_LOCATION);
// virtualMachine.detach();
// } catch (Throwable ex) {
// ex.printStackTrace();
// }

// Thread.sleep(10000);
}

public static int getPid(Process process) {
public static long getProcessID(Process p) {
long result = -1;
try {
Class<?> cProcessImpl = process.getClass();
Field fPid = cProcessImpl.getDeclaredField("pid");
if (!fPid.isAccessible()) {
fPid.setAccessible(true);
//for windows
if (p.getClass().getName().equals("java.lang.Win32Process") ||
p.getClass().getName().equals("java.lang.ProcessImpl")) {
Field f = p.getClass().getDeclaredField("handle");
f.setAccessible(true);
long handl = f.getLong(p);
Kernel32 kernel = Kernel32.INSTANCE;
WinNT.HANDLE hand = new WinNT.HANDLE();
hand.setPointer(Pointer.createConstant(handl));
result = kernel.GetProcessId(hand);
f.setAccessible(false);
}
//for unix based operating systems
else if (p.getClass().getName().equals("java.lang.UNIXProcess")) {
Field f = p.getClass().getDeclaredField("pid");
f.setAccessible(true);
result = f.getLong(p);
f.setAccessible(false);
}
return fPid.getInt(process);
} catch (Exception e) {
return -1;
} catch (Exception ex) {
result = -1;
}
return result;
}

private static String executeTheStringAsCommand() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package org.github.fourth.mocksystem.integrationtest;

import com.sun.tools.attach.VirtualMachine;
import org.omg.SendingContext.RunTime;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static org.github.fourth.mocksystem.integrationtest.IntegrationTest.AGENT_JAR_LOCATION;
import static org.github.fourth.mocksystem.integrationtest.IntegrationTest.getProcessID;

public class ProcessUtils {

private final Process process;
Expand All @@ -12,21 +18,30 @@ public class ProcessUtils {

public ProcessUtils(ProcessBuilder processBuilder) throws IOException {
this.process = processBuilder.start();
String pid = String.valueOf(getProcessID(process));
System.out.println(pid);
try {
VirtualMachine virtualMachine = VirtualMachine.attach(pid);
virtualMachine.loadAgent(AGENT_JAR_LOCATION);
virtualMachine.detach();
} catch (Throwable ex) {
ex.printStackTrace();
}
BufferedReader outReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuilder output = new StringBuilder();
StringBuilder errorOutput = new StringBuilder();
String line;
while ((line = outReader.readLine()) != null) {
output.append(line).append("\n");
System.out.println(line);
}
outLog = output.toString();
System.out.println(outLog);
while ((line = errorReader.readLine()) != null) {
errorOutput.append(line).append("\n");
System.err.println(line);
}
errorLog = errorOutput.toString();
System.err.println(errorLog);
}

public Process getProcess() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class MyAtm {

public static void withdrawMoney(int amount) throws InterruptedException {
// Thread.sleep(5000);
Thread.sleep(5000);
System.out.println("start==============================");
System.out.printf("[Application] Successful Withdrawal of [{%s}] units!%n", amount);
}
Expand Down

0 comments on commit 865174c

Please sign in to comment.