Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
<artifactId>byte-buddy-agent</artifactId>
<version>1.14.10</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -56,6 +62,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
</plugins>
</build>
</project>
54 changes: 54 additions & 0 deletions src/test/java/LogInjectionAgentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import net.bytebuddy.agent.ByteBuddyAgent;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Field;

import static org.junit.jupiter.api.Assertions.*;

public class LogInjectionAgentTest {

private Instrumentation getInstrumentation() {
return ByteBuddyAgent.install();
}

@AfterEach
public void tearDown() throws Exception {
// Stop the HTTP server and reset static fields
Field serverField = LogInjectionAgent.class.getDeclaredField("apiServer");
serverField.setAccessible(true);
Object server = serverField.get(null);
if (server != null) {
server.getClass().getMethod("stop").invoke(server);
}
Field managerField = LogInjectionAgent.class.getDeclaredField("logPointManager");
managerField.setAccessible(true);
managerField.set(null, null);
serverField.set(null, null);
}

@Test
public void testPremainInitializesManager() {
Instrumentation inst = getInstrumentation();
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream original = System.out;
System.setOut(new PrintStream(out));
try {
LogInjectionAgent.premain("apiPort=0", inst);
} finally {
System.setOut(original);
}
assertTrue(out.toString().contains("Starting Log Injection Agent"));
assertNotNull(LogInjectionAgent.getLogPointManager());
}

@Test
public void testAgentmainDelegatesToPremain() {
Instrumentation inst = getInstrumentation();
LogInjectionAgent.agentmain("apiPort=0", inst);
assertNotNull(LogInjectionAgent.getLogPointManager());
}
}