Skip to content

Commit

Permalink
fixup! feat: add NoAppMap to control test-case recording
Browse files Browse the repository at this point in the history
  • Loading branch information
apotterri committed Nov 1, 2023
1 parent 5e40c2e commit 4b930e3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.appland.appmap.process.hooks;

import java.lang.reflect.Method;

import com.appland.appmap.output.v1.Event;
import com.appland.appmap.process.conditions.RecordCondition;
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.HookCondition;
import com.appland.appmap.transform.annotations.MethodEvent;
import com.appland.appmap.util.ClassUtil;

public class RecordMethod {
private static final Recorder recorder = Recorder.getInstance();
Expand All @@ -18,12 +15,7 @@ public class RecordMethod {
@ExcludeReceiver
@HookCondition(RecordCondition.class)
public static void record(Event event, Object[] args) {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
// stack[0] is getStackTrace, stack[1] is junit5Test, so stack[2] is the test
// method:
Method testMethod = ClassUtil.findMethod(stack[2]);

RecordingSupport.startRecording(event, new Recorder.Metadata("record_process", "process"), testMethod);
RecordingSupport.startRecording(event, new Recorder.Metadata("record_process", "process"));
}

@ArgumentArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static com.appland.appmap.util.StringUtil.decapitalize;
import static com.appland.appmap.util.StringUtil.identifierToSentence;

import java.lang.reflect.Method;
import java.util.Objects;

import org.tinylog.TaggedLogger;
Expand All @@ -15,7 +14,6 @@
import com.appland.appmap.record.Recorder;
import com.appland.appmap.record.Recorder.Metadata;
import com.appland.appmap.record.Recording;
import com.appland.appmap.transform.annotations.AnnotationUtil;

public class RecordingSupport {
private static final TaggedLogger logger = AppMapConfig.getLogger(null);
Expand All @@ -42,15 +40,11 @@ public TestDetails(Event event) {
}
}

public static void startRecording(Event event, Recorder.Metadata metadata, Method testMethod) {
startRecording(new TestDetails(event), metadata, testMethod);
public static void startRecording(Event event, Recorder.Metadata metadata) {
startRecording(new TestDetails(event), metadata);
}

public static void startRecording(TestDetails details, Recorder.Metadata metadata, Method testMethod) {
if (!isRecordingEnabled(testMethod)) {
return;
}

public static void startRecording(TestDetails details, Recorder.Metadata metadata) {
logger.debug("Recording started for {}", canonicalName(details.definedClass, details.isStatic, details.methodId));
try {
final String feature = identifierToSentence(details.methodId);
Expand All @@ -68,13 +62,6 @@ public static void startRecording(TestDetails details, Recorder.Metadata metadat
}
}

private static boolean isRecordingEnabled(Method testMethod) {
String noAppMap = "com.appland.appmap.annotation.NoAppMap";
boolean methodAnnotated = AnnotationUtil.hasAnnotation(noAppMap, testMethod);
boolean classAnnotated = AnnotationUtil.hasAnnotation(noAppMap, testMethod.getDeclaringClass());
return !methodAnnotated && !classAnnotated;
}

public static void stopRecording(Event event) {
RecordingSupport.stopRecording(new TestDetails(event), true, null, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.appland.appmap.process.hooks.test;

import java.lang.reflect.Method;

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;
import com.appland.appmap.util.ClassUtil;

public class JUnit {
private static final Recorder recorder = Recorder.getInstance();
Expand All @@ -19,26 +16,21 @@ public class JUnit {
@ExcludeReceiver
@HookAnnotated("org.junit.Test")
public static void junitTest(Event event, Object[] args) {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
// stack[0] is getStackTrace, stack[1] is junitTest, so stack[2] is the test
// method:
Method testMethod = ClassUtil.findMethod(stack[2]);

Recorder.Metadata metadata = new Recorder.Metadata(JUNIT_NAME, TestSupport.TEST_RECORDER_TYPE);
metadata.frameworks.add(new Recorder.Framework("JUnit", "4"));
RecordingSupport.startRecording(event, metadata, testMethod);
TestSupport.startRecording(event, metadata);
}

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

@ArgumentArray
@HookAnnotated(value = "org.junit.Test", methodEvent = MethodEvent.METHOD_EXCEPTION)
public static void junitTestExc(Event event, Object self, Throwable exception, Object[] args) {
public static void junitTest(Event event, Object self, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
StackTraceElement ste = findErrorFrame(self, exception);
Expand All @@ -50,26 +42,21 @@ public static void junitTestExc(Event event, Object self, Throwable exception, O
@ExcludeReceiver
@HookAnnotated("org.junit.jupiter.api.Test")
public static void junit5Test(Event event, Object[] args) {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
// stack[0] is getStackTrace, stack[1] is junit5Test, so stack[2] is the test
// method:
Method testMethod = ClassUtil.findMethod(stack[2]);

Recorder.Metadata metadata = new Recorder.Metadata(JUNIT_NAME, TestSupport.TEST_RECORDER_TYPE);
metadata.frameworks.add(new Recorder.Framework("JUnit", "5"));
RecordingSupport.startRecording(event, metadata, testMethod);
TestSupport.startRecording(event, metadata);
}

@ArgumentArray
@ExcludeReceiver
@HookAnnotated(value = "org.junit.jupiter.api.Test", methodEvent = MethodEvent.METHOD_RETURN)
public static void junit5TestRet(Event event, Object returnValue, Object[] args) {
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 junit5TestExc(Event event, Object self, Throwable exception, Object[] args) {
public static void junit5Test(Event event, Object self, Throwable exception, Object[] args) {
event.setException(exception);
recorder.add(event);
StackTraceElement errorFrame = findErrorFrame(self, exception);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.appland.appmap.process.hooks.test;

import java.lang.reflect.Method;

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;
import com.appland.appmap.util.ClassUtil;

public class TestNG {
private static final String TESTNG_NAME = "testng";
Expand All @@ -18,13 +15,7 @@ public class TestNG {
@ExcludeReceiver
@HookAnnotated("org.testng.annotations.Test")
public static void testng(Event event, Object[] args) {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
// stack[0] is getStackTrace, stack[1] is junitng, so stack[2] is the test
// method:
Method testMethod = ClassUtil.findMethod(stack[2]);

RecordingSupport.startRecording(event, new Recorder.Metadata(TESTNG_NAME, TestSupport.TEST_RECORDER_TYPE),
testMethod);
TestSupport.startRecording(event, new Recorder.Metadata(TESTNG_NAME, TestSupport.TEST_RECORDER_TYPE));
}

@ArgumentArray
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
package com.appland.appmap.process.hooks.test;

import java.lang.reflect.Method;

import com.appland.appmap.output.v1.Event;
import com.appland.appmap.process.hooks.RecordingSupport;
import com.appland.appmap.process.hooks.RecordingSupport.TestDetails;
import com.appland.appmap.record.Recorder;
import com.appland.appmap.transform.annotations.AnnotationUtil;
import com.appland.appmap.util.ClassUtil;

class TestSupport {
static final String TEST_RECORDER_TYPE = "tests";

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

static void startRecording(TestDetails details, Recorder.Metadata metadata) {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
// stack[0] is getStackTrace, stack[1] is startReording, stack[2] is the
// hook method, so stack[3] is the test method:
Method testMethod = ClassUtil.findMethod(stack[3]);

if (!isRecordingEnabled(testMethod)) {
return;
}

RecordingSupport.startRecording(details, metadata);
}

private static boolean isRecordingEnabled(Method testMethod) {
String noAppMap = "com.appland.appmap.annotation.NoAppMap";
boolean methodAnnotated = AnnotationUtil.hasAnnotation(noAppMap, testMethod);
boolean classAnnotated = AnnotationUtil.hasAnnotation(noAppMap, testMethod.getDeclaringClass());
return !methodAnnotated && !classAnnotated;
}
}

0 comments on commit 4b930e3

Please sign in to comment.