Convert InstrumentationCheckerTest to Spock
MODIFY
dd-java-agent/src/main/java/com/datadoghq/trace/agent/InstrumentationChecker.java
dd-java-agent/src/main/java/com/datadoghq/trace/agent/InstrumentationChecker.java
package com.datadoghq.trace.agent;
import com.datadoghq.trace.resolver.FactoryUtils;
+import com.fasterxml.jackson.core.type.TypeReference;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
@@ -32,7 +33,9 @@ public class InstrumentationChecker {
}
private InstrumentationChecker() {
- rules = FactoryUtils.loadConfigFromResource(CONFIG_FILE, Map.class);
+ rules =
+ FactoryUtils.loadConfigFromResource(
+ CONFIG_FILE, new TypeReference<Map<String, List<Map<String, String>>>>() {});
frameworks = scanLoadedLibraries();
}

ADD
dd-java-agent/src/test/groovy/com/datadoghq/trace/agent/InstrumentationCheckerTest.groovy
+package com.datadoghq.trace.agent
+
+import com.datadoghq.trace.resolver.FactoryUtils
+import com.fasterxml.jackson.core.type.TypeReference
+import spock.lang.Specification
+
+class InstrumentationCheckerTest extends Specification {
+ Map<String, List<Map<String, String>>> rules =
+ FactoryUtils.loadConfigFromResource("supported-version-test", new TypeReference<Map<String, List<Map<String, String>>>>() {
+ });
+ Map<String, String> frameworks = [
+ "artifact-1": "1.2.3.1232",
+ "artifact-2": "4.y.z",
+ "artifact-3": "5.123-1"
+ ]
+
+ def checker = new InstrumentationChecker(rules, frameworks)
+
+ def "test rules"() {
+ setup:
+ def rules = InstrumentationChecker.getUnsupportedRules();
+
+ expect:
+ rules.size() == 3
+ rules.sort() == ["unsupportedRuleOne", "unsupportedRuleThree", "unsupportedRuleTwo"]
+ }
+}

DELETE
dd-java-agent/src/test/java/com/datadoghq/trace/agent/InstrumentationCheckerTest.java
-package com.datadoghq.trace.agent;
-
-import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
-
-import com.datadoghq.trace.resolver.FactoryUtils;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import org.junit.Before;
-import org.junit.Test;
-
-public class InstrumentationCheckerTest {
-
- @Before
- public void setup() {
- final Map<String, List<Map<String, String>>> rules =
- FactoryUtils.loadConfigFromResource("supported-version-test", Map.class);
- final Map<String, String> frameworks =
- new HashMap<String, String>() {
- {
- put("artifact-1", "1.2.3.1232");
- put("artifact-2", "4.y.z");
- put("artifact-3", "5.123-1");
- }
- };
-
- new InstrumentationChecker(rules, frameworks);
- }
-
- @Test
- public void testRules() throws Exception {
-
- final List<String> rules = InstrumentationChecker.getUnsupportedRules();
- assertThat(rules.size()).isEqualTo(3);
- assertThat(rules)
- .containsExactlyInAnyOrder(
- "unsupportedRuleOne", "unsupportedRuleTwo", "unsupportedRuleThree");
- }
-}

MODIFY
dd-trace/dd-trace.gradle
dd-trace/dd-trace.gradle
plugins {
- id 'groovy'
id "me.champeau.gradle.jmh" version "0.4.4"
}
@@ -32,13 +31,6 @@ dependencies {
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
- testCompile group: 'junit', name: 'junit', version: '4.12'
- testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.2'
- testCompile group: 'org.mockito', name: 'mockito-core', version: '2.7.22'
-
- testCompile group: 'org.spockframework', name: 'spock-core', version: '1.0-groovy-2.4'
- testCompile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.4'
- testCompile group: 'io.ratpack', name: 'ratpack-groovy-test', version: '1.4.6'
testCompile group: 'org.objenesis', name: 'objenesis', version: '2.6'
testCompile group: 'cglib', name: 'cglib-nodep', version: '3.2.5'

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/resolver/FactoryUtils.java
dd-trace/src/main/java/com/datadoghq/trace/resolver/FactoryUtils.java
package com.datadoghq.trace.resolver;
+import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.File;
@@ -14,29 +15,39 @@ public class FactoryUtils {
public static <A> A loadConfigFromFilePropertyOrResource(
final String systemProperty, final String resourceName, final Class<A> targetClass) {
+ return loadConfigFromFilePropertyOrResource(
+ systemProperty, resourceName, new TypeReference<A>() {});
+ }
+
+ public static <A> A loadConfigFromFilePropertyOrResource(
+ final String systemProperty, final String resourceName, final TypeReference type) {
final String filePath = System.getProperty(systemProperty);
if (filePath != null) {
try {
log.info("Loading config from file " + filePath);
- return objectMapper.readValue(new File(filePath), targetClass);
+ return objectMapper.readValue(new File(filePath), type);
} catch (final Exception e) {
log.error(
"Cannot read provided configuration file " + filePath + ". Using the default one.", e);
}
}
- return loadConfigFromResource(resourceName, targetClass);
+ return loadConfigFromResource(resourceName, type);
}
public static <A> A loadConfigFromResource(
final String resourceName, final Class<A> targetClass) {
+ return loadConfigFromResource(resourceName, new TypeReference<A>() {});
+ }
+
+ public static <A> A loadConfigFromResource(final String resourceName, final TypeReference type) {
A config = null;
// Try loading both suffixes
if (!resourceName.endsWith(".yaml") && !resourceName.endsWith(".yml")) {
- config = loadConfigFromResource(resourceName + ".yaml", targetClass);
+ config = loadConfigFromResource(resourceName + ".yaml", type);
if (config == null) {
- config = loadConfigFromResource(resourceName + ".yml", targetClass);
+ config = loadConfigFromResource(resourceName + ".yml", type);
}
if (config != null) {
return config;
@@ -48,7 +59,7 @@ public class FactoryUtils {
final URL resource = classLoader.getResource(resourceName);
if (resource != null) {
log.info("Loading config from resource " + resource);
- config = objectMapper.readValue(resource.openStream(), targetClass);
+ config = objectMapper.readValue(resource.openStream(), type);
}
} catch (final IOException e) {
log.warn("Could not load configuration file {}.", resourceName);

MODIFY
gradle/java.gradle
gradle/java.gradle
apply plugin: 'java'
+apply plugin: 'groovy'
sourceCompatibility = 1.7
targetCompatibility = 1.7
@@ -21,6 +22,16 @@ repositories {
maven { url "http://repo.maven.apache.org/maven2" }
}
+dependencies {
+ testCompile group: 'junit', name: 'junit', version: '4.12'
+ testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.2'
+ testCompile group: 'org.mockito', name: 'mockito-core', version: '2.7.22'
+
+ testCompile group: 'org.spockframework', name: 'spock-core', version: '1.0-groovy-2.4'
+ testCompile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.4'
+ testCompile group: 'io.ratpack', name: 'ratpack-groovy-test', version: '1.4.6'
+}
+
tasks.withType(Javadoc) {
options.encoding = "utf-8"
options.docEncoding = "utf-8"

Convert InstrumentationCheckerTest to Spock