Skip to content

Commit

Permalink
Merge pull request #234 from getappmap/junit_20231020
Browse files Browse the repository at this point in the history
Support test_status and test_failure for both JUnit 4 and Junit 5
  • Loading branch information
apotterri authored Oct 24, 2023
2 parents a17ca6e + f15dc6d commit d0f5a22
Show file tree
Hide file tree
Showing 34 changed files with 261 additions and 362 deletions.
2 changes: 2 additions & 0 deletions agent/bin/test_run
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ set -x
./test/petclinic/test
if [[ $JAVA_VERSION == 17.* ]]; then
./test/petclinic-fw/test
else
./test/junit4/test
fi
./test/httpcore/test
./test/http_client/test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public Event setReturnValue(Object val) {
* @return {@code this}
* @see <a href="https://github.com/applandinc/appmap#common-attributes-1">GitHub: AppMap - Common attributes</a>
*/
public Event setException(Exception exception) {
public Event setException(Throwable exception) {
if (exception != null) {
this.exceptions = new ArrayList<>();
Throwable t = exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MethodException {

@ArgumentArray
@HookCondition(value = ConfigCondition.class, methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void handle(Event event, Object self, Exception exception, Object[] args) {
public static void handle(Event event, Object self, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class RecordMethod {
@ExcludeReceiver
@HookCondition(RecordCondition.class)
public static void record(Event event, Object[] args) {
RecordingSupport.startRecording(event, "record_process", "process");
RecordingSupport.startRecording(event, new Recorder.Metadata("record_process", "process"));
}

@ArgumentArray
Expand All @@ -28,7 +28,7 @@ public static void record(Event event, Object returnValue, Object[] args) {
@ArgumentArray
@ExcludeReceiver
@HookCondition(value = RecordCondition.class, methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void record(Event event, Exception exception, Object[] args) {
public static void record(Event event, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
StackTraceElement ste = exception.getStackTrace()[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ public TestDetails(Event event) {
}
}

public static void startRecording(Event event, String recorderName, String recorderType) {
startRecording(new TestDetails(event), recorderName, recorderType);
public static void startRecording(Event event, Recorder.Metadata metadata) {
startRecording(new TestDetails(event), metadata);
}

public static void startRecording(TestDetails details, String recorderName, String recorderType) {
public static void startRecording(TestDetails details, Recorder.Metadata metadata) {
logger.debug("Recording started for {}", canonicalName(details.definedClass, details.isStatic, details.methodId));
try {
Recorder.Metadata metadata = new Recorder.Metadata(recorderName, recorderType);
final String feature = identifierToSentence(details.methodId);
final String featureGroup = identifierToSentence(details.definedClass);
metadata.scenarioName = String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,49 +314,49 @@ public static void executeUpdate(Event event, Statement s, Object returnValue, S

@ArgumentArray
@HookClass(value = "java.sql.Connection", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void nativeSQL(Event event, Connection c, Exception exception, Object[] args) {
public static void nativeSQL(Event event, Connection c, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
}

@ArgumentArray
@HookClass(value = "java.sql.Connection", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void prepareCall(Event event, Connection c, Exception exception, Object[] args) {
public static void prepareCall(Event event, Connection c, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
}

@ArgumentArray
@HookClass(value = "java.sql.Connection", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void prepareStatement(Event event, Connection c, Exception exception, Object[] args) {
public static void prepareStatement(Event event, Connection c, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
}

@ArgumentArray
@HookClass(value = "java.sql.Statement", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void addBatch(Event event, Statement s, Exception exception, Object[] args) {
public static void addBatch(Event event, Statement s, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
}

@ArgumentArray
@HookClass(value = "java.sql.Statement", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void execute(Event event, Statement s, Exception exception, Object[] args) {
public static void execute(Event event, Statement s, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
}

@ArgumentArray
@HookClass(value = "java.sql.Statement", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void executeQuery(Event event, Statement s, Exception exception, Object[] args) {
public static void executeQuery(Event event, Statement s, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
}

@ArgumentArray
@HookClass(value = "java.sql.Statement", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void executeUpdate(Event event, Statement s, Exception exception, Object[] args) {
public static void executeUpdate(Event event, Statement s, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static void recordHttpServerResponse(Event event, HttpServletRequest req,
recorder.add(event);
}

private static void recordHttpServerException(Event event, HttpServletRequest req, Exception exception) {
private static void recordHttpServerException(Event event, HttpServletRequest req, Throwable exception) {
event.setException(exception);
clearLastEvent(req, event);
recorder.add(event);
Expand Down Expand Up @@ -151,7 +151,7 @@ private static void doServiceReturn(Event event, Object[] args) {
@ArgumentArray
@ExcludeReceiver
@HookClass(value = "javax.servlet.http.HttpServlet", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void service(Event event, Exception exception, Object[] args) {
public static void service(Event event, Throwable exception, Object[] args) {
if (args.length != 2) {
return;
}
Expand All @@ -163,7 +163,7 @@ public static void service(Event event, Exception exception, Object[] args) {
@ArgumentArray
@ExcludeReceiver
@HookClass(value = "jakarta.servlet.http.HttpServlet", method = "service", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void serviceJakarta(Event event, Exception exception, Object[] args) {
public static void serviceJakarta(Event event, Throwable exception, Object[] args) {
if (args.length != 2) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.appland.appmap.process.hooks.test;

import com.appland.appmap.output.v1.Event;
import com.appland.appmap.process.hooks.RecordingSupport;
import com.appland.appmap.record.Recorder;
import com.appland.appmap.transform.annotations.ArgumentArray;
import com.appland.appmap.transform.annotations.ExcludeReceiver;
import com.appland.appmap.transform.annotations.HookAnnotated;
import com.appland.appmap.transform.annotations.MethodEvent;

public class JUnit {
private static final Recorder recorder = Recorder.getInstance();
static final String JUNIT_NAME = "junit";

@ArgumentArray
@ExcludeReceiver
@HookAnnotated("org.junit.Test")
public static void junit(Event event, Object[] args) {
Recorder.Metadata metadata = new Recorder.Metadata(JUNIT_NAME, TestSupport.TEST_RECORDER_TYPE);
metadata.frameworks.add(new Recorder.Framework("JUnit", "4"));
RecordingSupport.startRecording(event, metadata);
}

@ArgumentArray
@ExcludeReceiver
@HookAnnotated(value = "org.junit.Test", methodEvent = MethodEvent.METHOD_RETURN)
public static void junit(Event event, Object returnValue, Object[] args) {
RecordingSupport.stopRecording(event, true);
}

@ArgumentArray
@HookAnnotated(value = "org.junit.Test", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void junit(Event event, Object self, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
StackTraceElement ste = findErrorFrame(self, exception);
RecordingSupport.stopRecording(new RecordingSupport.TestDetails(event), false, exception.getMessage(),
ste.getLineNumber());
}

@ArgumentArray
@ExcludeReceiver
@HookAnnotated("org.junit.jupiter.api.Test")
public static void junit5Test(Event event, Object[] args) {
Recorder.Metadata metadata = new Recorder.Metadata(JUNIT_NAME, TestSupport.TEST_RECORDER_TYPE);
metadata.frameworks.add(new Recorder.Framework("JUnit", "5"));
RecordingSupport.startRecording(event, metadata);
}

@ArgumentArray
@ExcludeReceiver
@HookAnnotated(value = "org.junit.jupiter.api.Test", methodEvent = MethodEvent.METHOD_RETURN)
public static void junit5Test(Event event, Object returnValue, Object[] args) {
RecordingSupport.stopRecording(event, true);
}

@ArgumentArray
@HookAnnotated(value = "org.junit.jupiter.api.Test", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void junit5Test(Event event, Object self, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
StackTraceElement errorFrame = findErrorFrame(self, exception);
RecordingSupport.stopRecording(new RecordingSupport.TestDetails(event), false, exception.getMessage(),
errorFrame.getLineNumber());
}

private static StackTraceElement findErrorFrame(Object self, Throwable exception) throws InternalError {
String selfClass = self.getClass().getName();
StackTraceElement errorFrame = null;
for (StackTraceElement frame : exception.getStackTrace()) {
if (frame.getClassName().equals(selfClass)) {
errorFrame = frame;
break;
}
}
if (errorFrame == null) {
throw new InternalError("no stack frame matched test class");
}
return errorFrame;
}
}

This file was deleted.

Loading

0 comments on commit d0f5a22

Please sign in to comment.