-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: go back to hooking org.junit.jupiter.api.Test
JUnit 5 test execution listeners get notified before @beforeeach methods, and after @AfterEach methods. This causes some problems (e.g. a temporary recording file can be deleted before it gets moved to its final destination), but is also not what we want. By design, none of the other agents capture test setup and teardown, so we shouldn't either. These changes go back to just hooking JUnit 5's Test class. The change to have instrumentation catch Throwable instead of exception was a prerequisite for these changes, because AssertionError (raised when an assertion fails) is an Error, not an Exception. Also, change .metadata.framework to .metadata.frameworks so it matches the spec.
- Loading branch information
Showing
25 changed files
with
238 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
agent/src/main/java/com/appland/appmap/process/hooks/test/JUnit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
39 changes: 0 additions & 39 deletions
39
agent/src/main/java/com/appland/appmap/process/hooks/test/JUnit4.java
This file was deleted.
Oops, something went wrong.
208 changes: 0 additions & 208 deletions
208
agent/src/main/java/com/appland/appmap/process/hooks/test/JUnit5.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.