All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.rules.JUnit38ScopeRunnerAgentRule Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 0.15.1-beta.2
Show newest version
package com.undefinedlabs.scope.rules;

import static com.undefinedlabs.scope.agent.ScopeClassLoaderMatcher.hasClassesNamed;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

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.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

public class JUnit38ScopeRunnerAgentRule extends AbstractScopeRunnerAgentRule {

  @Override
  protected ElementMatcher typeMatcher() {
    return named("junit.framework.TestCase");
  }

  @Override
  protected ElementMatcher classLoaderMatcher() {
    return hasClassesNamed("org.junit.runners.BlockJUnit4ClassRunner");
  }

  @Override
  protected Map, String> transformers() {
    final Map, String> transformers = new HashMap<>();
    transformers.put(
        named("run").and(takesArguments(1)),
        JUnit38ScopeRunnerAgentRule.class.getName() + "$JUnit38RunnerAdvice");
    return transformers;
  }

  public static class JUnit38RunnerAdvice {

    @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class)
    public static Object enter(@Advice.This final Object thiz) {
      final TestCase testCase = (TestCase) thiz;

      final String testName = testCase.getName();
      final String testSuite = testCase.getClass().getName();
      final ScopeRunner runner = ScopeGlobalRunner.get();

      if (!runner.getTestStatuses(testSuite, testName).contains(TestStatus.CACHED)) {
        return null;
      } else {
        final Tracer tracer = ScopeGlobalTracer.get();
        final Span span =
            tracer
                .buildSpan(testName)
                .ignoreActiveSpan()
                .withTag(TagKeys.COMPONENT, TagValues.Test.Framework.JUNIT_4)
                .withTag(TagKeys.SPAN_KIND, TagValues.SPAN_KIND_TEST)
                .withTag(TagKeys.Test.TEST_NAME, testName)
                .withTag(TagKeys.Test.TEST_SUITE, testSuite)
                .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(
            TagValues.Test.Framework.JUNIT_4, testSuite, testName);
        return new Object();
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy