Apply minimum code coverage assertions
Improved the test coverage slightly in the process.
MODIFY
dd-java-agent-ittests/build.gradle
dd-java-agent-ittests/build.gradle
+// Not adding jacoco here as it inexplicably breaks our tests.
+// apply from: "${rootDir}/gradle/jacoco.gradle"
+
description = 'dd-java-agent-ittests'
dependencies {
testCompile project(':dd-java-agent')

MODIFY
dd-java-agent/build.gradle
dd-java-agent/build.gradle
@@ -4,6 +4,16 @@ plugins {
description = 'dd-java-agent'
+apply from: "${rootDir}/gradle/jacoco.gradle"
+jacocoTestReport.dependsOn project(':dd-java-agent-ittests').test
+whitelistedInstructionClasses += whitelistedBranchClasses += [
+ "com.datadoghq.trace.agent.integration.*",
+ 'com.datadoghq.trace.agent.AnnotationsTracingAgent',
+ 'com.datadoghq.trace.agent.AgentTracerConfig',
+ 'com.datadoghq.trace.agent.TraceAnnotationsManager',
+ 'com.datadoghq.trace.agent.InstrumentationChecker'
+]
+
dependencies {
compile project(':dd-trace')
compile project(':dd-trace-annotations')

MODIFY
dd-trace-annotations/build.gradle
dd-trace-annotations/build.gradle
+apply from: "${rootDir}/gradle/jacoco.gradle"
+
description = 'dd-trace-annotations'
dependencies {
testCompile group: 'junit', name: 'junit', version: '3.8.1'

MODIFY
dd-trace/build.gradle
dd-trace/build.gradle
@@ -4,6 +4,15 @@ plugins {
}
description = 'dd-trace'
+
+apply from: "${rootDir}/gradle/jacoco.gradle"
+minimumBranchCoverage = 0.3
+minimumInstructionCoverage = 0.5
+whitelistedInstructionClasses += whitelistedBranchClasses += [
+ "com.datadoghq.trace.integration.*",
+ 'com.datadoghq.trace.DDTags'
+]
+
dependencies {
compile group: 'io.opentracing', name: 'opentracing-api', version: '0.30.0'
compile group: 'io.opentracing', name: 'opentracing-noop', version: '0.30.0'

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerResolver.java
dd-trace/src/main/java/com/datadoghq/trace/resolver/DDTracerResolver.java
@@ -41,19 +41,10 @@ public class DDTracerResolver extends TracerResolver {
@SuppressWarnings("static-access")
public static Tracer registerTracer() {
-
- ServiceLoader<TracerResolver> RESOLVERS = ServiceLoader.load(TracerResolver.class);
-
- Tracer tracer = null;
- for (TracerResolver value : RESOLVERS) {
- tracer = value.resolveTracer();
- if (tracer != null) {
- break;
- }
- }
+ Tracer tracer = TracerResolver.resolveTracer();
if (tracer == null) {
- tracer = NoopTracerFactory.create();
+ return NoopTracerFactory.create();
}
GlobalTracer.register(tracer);

MODIFY
dd-trace/src/test/java/com/datadoghq/trace/resolver/TracerResolverTest.java
dd-trace/src/test/java/com/datadoghq/trace/resolver/TracerResolverTest.java
@@ -2,9 +2,14 @@ package com.datadoghq.trace.resolver;
import static org.assertj.core.api.Assertions.assertThat;
+import java.lang.reflect.Field;
import java.util.List;
+import io.opentracing.NoopTracerFactory;
+import io.opentracing.Tracer;
+import io.opentracing.contrib.tracerresolver.TracerResolver;
import io.opentracing.tag.Tags;
+import io.opentracing.util.GlobalTracer;
import org.junit.Test;
import com.datadoghq.trace.DDTracer;
@@ -15,7 +20,7 @@ import com.datadoghq.trace.integration.URLAsResourceName;
public class TracerResolverTest {
@Test
- public void test() {
+ public void testResolve() {
DDTracerResolver tracerResolver = new DDTracerResolver();
DDTracer tracer = (DDTracer) tracerResolver.resolve();
@@ -39,4 +44,34 @@ public class TracerResolverTest {
}
+ @Test
+ public void testResolveTracer() throws Exception {
+ Field tracerField = GlobalTracer.class.getDeclaredField("tracer");
+ tracerField.setAccessible(true);
+ tracerField.set(null, NoopTracerFactory.create());
+
+ assertThat(GlobalTracer.isRegistered()).isFalse();
+
+ Tracer tracer = TracerResolver.resolveTracer();
+
+ assertThat(GlobalTracer.isRegistered()).isFalse();
+ assertThat(tracer).isInstanceOf(DDTracer.class);
+ }
+
+ @Test
+ public void testRegisterTracer() throws Exception {
+ Field tracerField = GlobalTracer.class.getDeclaredField("tracer");
+ tracerField.setAccessible(true);
+ tracerField.set(null, NoopTracerFactory.create());
+
+ assertThat(GlobalTracer.isRegistered()).isFalse();
+
+ DDTracerResolver.registerTracer();
+
+ assertThat(GlobalTracer.isRegistered()).isTrue();
+ assertThat(tracerField.get(null)).isInstanceOf(DDTracer.class);
+
+ tracerField.set(null, NoopTracerFactory.create());
+ }
+
}

ADD
gradle/jacoco.gradle
+apply plugin: "jacoco"
+
+jacocoTestReport {
+ dependsOn test
+ reports {
+ xml.enabled true
+ csv.enabled false
+ html.destination file("${buildDir}/reports/jacoco/")
+ }
+}
+
+project.ext.whitelistedBranchClasses = []
+project.ext.whitelistedInstructionClasses = []
+
+project.ext.minimumBranchCoverage = 0.6
+project.ext.minimumInstructionCoverage = 0.6
+
+afterEvaluate {
+ jacocoTestCoverageVerification {
+ violationRules {
+ rule {
+ element = 'CLASS'
+ excludes = project.whitelistedBranchClasses
+ limit {
+ counter = 'BRANCH'
+ minimum = project.minimumBranchCoverage
+ }
+ }
+
+ rule {
+ element = 'CLASS'
+ excludes = project.whitelistedInstructionClasses
+ limit {
+ counter = 'INSTRUCTION'
+ minimum = project.minimumInstructionCoverage
+ }
+ }
+ }
+ }
+
+ jacocoTestCoverageVerification.dependsOn jacocoTestReport
+ check.dependsOn jacocoTestCoverageVerification
+}

Apply minimum code coverage assertions

Improved the test coverage slightly in the process.