com.undefinedlabs.scope.rules.JUnit4IgnoredScopeRunnerAgentRule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scope-runner-junit4 Show documentation
Show all versions of scope-runner-junit4 Show documentation
Scope is a APM for tests to give engineering teams unprecedented visibility into their CI process to quickly
identify, troubleshoot and fix failed builds.
This artifact contains the classes to instrument the JUnit4 runner.
package com.undefinedlabs.scope.rules;
import static com.undefinedlabs.scope.agent.ScopeClassLoaderMatcher.hasClassesNamed;
import static net.bytebuddy.matcher.ElementMatchers.named;
import com.undefinedlabs.scope.ScopeGlobalTracer;
import com.undefinedlabs.scope.runner.ScopeGlobalRunner;
import com.undefinedlabs.scope.runner.ScopeRunner;
import com.undefinedlabs.scope.runner.TestStatus;
import com.undefinedlabs.scope.runner.rules.AbstractScopeRunnerAgentRule;
import com.undefinedlabs.scope.statistics.Statistics;
import com.undefinedlabs.scope.utils.SpanUtils;
import com.undefinedlabs.scope.utils.baggage.BaggageKeys;
import com.undefinedlabs.scope.utils.baggage.BaggageValues;
import com.undefinedlabs.scope.utils.tag.TagKeys;
import com.undefinedlabs.scope.utils.tag.TagValues;
import io.opentracing.Span;
import io.opentracing.Tracer;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.junit.Test;
import org.junit.internal.builders.IgnoredClassRunner;
public class JUnit4IgnoredScopeRunnerAgentRule extends AbstractScopeRunnerAgentRule {
@Override
protected ElementMatcher super TypeDescription> typeMatcher() {
return named("org.junit.internal.builders.IgnoredClassRunner");
}
@Override
protected ElementMatcher classLoaderMatcher() {
return hasClassesNamed("org.junit.runners.BlockJUnit4ClassRunner");
}
@Override
protected Map extends ElementMatcher super MethodDescription>, String> transformers() {
final Map, String> transformers = new HashMap<>();
transformers.put(
named("run"),
JUnit4IgnoredScopeRunnerAgentRule.class.getName() + "$IgnoredClassRunnerAdvice");
return transformers;
}
private static Class findTestClassFromIgnoredClassRunner(
final Object runnerObj, final String fieldName) {
try {
final IgnoredClassRunner junitRunner = (IgnoredClassRunner) runnerObj;
final Field fTestClassField = junitRunner.getClass().getDeclaredField(fieldName);
fTestClassField.setAccessible(true);
return (Class>) fTestClassField.get(junitRunner);
} catch (final Exception e) {
return null;
}
}
public static Class findTestClassFromIgnoredClassRunner(final Object runnerObj) {
final Class testClass = findTestClassFromIgnoredClassRunner(runnerObj, "clazz");
if (testClass == null) {
return findTestClassFromIgnoredClassRunner(runnerObj, "fTestClass");
}
return testClass;
}
public static class IgnoredClassRunnerAdvice {
@Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class)
public static Object enter(@Advice.This final Object runnerObj) {
try {
final Class> fTestClass =
JUnit4IgnoredScopeRunnerAgentRule.findTestClassFromIgnoredClassRunner(runnerObj);
if (fTestClass == null) {
return null;
}
final List testMethods =
MethodUtils.getMethodsListWithAnnotation(fTestClass, Test.class);
boolean hasCachedTests = false;
final ScopeRunner scopeRunner = ScopeGlobalRunner.get();
for (int i = 0; i < testMethods.size() && !hasCachedTests; i++) {
final Method testMethod = testMethods.get(i);
hasCachedTests =
scopeRunner
.getTestStatuses(fTestClass.getName(), testMethod.getName())
.contains(TestStatus.CACHED);
}
if (hasCachedTests) {
final Tracer tracer = ScopeGlobalTracer.get();
for (final Method testMethod : testMethods) {
final Span span =
tracer
.buildSpan(testMethod.getName())
.ignoreActiveSpan()
.withTag(TagKeys.COMPONENT, TagValues.Test.Framework.JUNIT_4)
.withTag(TagKeys.SPAN_KIND, TagValues.SPAN_KIND_TEST)
.withTag(TagKeys.Test.TEST_NAME, fTestClass.getName())
.withTag(TagKeys.Test.TEST_SUITE, testMethod.getName())
.withTag(TagKeys.Test.TEST_FRAMEWORK, TagValues.Test.Framework.JUNIT_4)
.withTag(TagKeys.Test.TEST_LANGUAGE, TagValues.Test.Language.JAVA)
.withTag(TagKeys.Test.TEST_STATUS, TagValues.Test.TEST_CACHE)
.withTag(TagKeys.ERROR, false)
.start();
span.setBaggageItem(BaggageKeys.TRACE_KIND, BaggageValues.TRACE_KIND_TEST);
span.finish(SpanUtils.INSTANCE.getStartTimestampMicros(span));
Statistics.INSTANCE.registerRunnerCachedTest(
"Junit4", fTestClass.getName(), testMethod.getName());
}
return new Object();
}
return null;
} catch (final Exception e) {
return null;
}
}
}
}