diff --git a/dd-java-agent/src/main/java/com/datadoghq/trace/agent/InstrumentationChecker.java b/dd-java-agent/src/main/java/com/datadoghq/trace/agent/InstrumentationChecker.java index e39371645e..1c428d20cd 100644 --- a/dd-java-agent/src/main/java/com/datadoghq/trace/agent/InstrumentationChecker.java +++ b/dd-java-agent/src/main/java/com/datadoghq/trace/agent/InstrumentationChecker.java @@ -1,6 +1,7 @@ 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>>>() {}); frameworks = scanLoadedLibraries(); } diff --git a/dd-java-agent/src/test/groovy/com/datadoghq/trace/agent/InstrumentationCheckerTest.groovy b/dd-java-agent/src/test/groovy/com/datadoghq/trace/agent/InstrumentationCheckerTest.groovy new file mode 100644 index 0000000000..7a13ab2e60 --- /dev/null +++ b/dd-java-agent/src/test/groovy/com/datadoghq/trace/agent/InstrumentationCheckerTest.groovy @@ -0,0 +1,27 @@ +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>> rules = + FactoryUtils.loadConfigFromResource("supported-version-test", new TypeReference>>>() { + }); + Map 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"] + } +} diff --git a/dd-java-agent/src/test/java/com/datadoghq/trace/agent/InstrumentationCheckerTest.java b/dd-java-agent/src/test/java/com/datadoghq/trace/agent/InstrumentationCheckerTest.java deleted file mode 100644 index 213184107c..0000000000 --- a/dd-java-agent/src/test/java/com/datadoghq/trace/agent/InstrumentationCheckerTest.java +++ /dev/null @@ -1,39 +0,0 @@ -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>> rules = - FactoryUtils.loadConfigFromResource("supported-version-test", Map.class); - final Map frameworks = - new HashMap() { - { - 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 rules = InstrumentationChecker.getUnsupportedRules(); - assertThat(rules.size()).isEqualTo(3); - assertThat(rules) - .containsExactlyInAnyOrder( - "unsupportedRuleOne", "unsupportedRuleTwo", "unsupportedRuleThree"); - } -} diff --git a/dd-trace/dd-trace.gradle b/dd-trace/dd-trace.gradle index 9b39533509..2ac83084f4 100644 --- a/dd-trace/dd-trace.gradle +++ b/dd-trace/dd-trace.gradle @@ -1,5 +1,4 @@ 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' diff --git a/dd-trace/src/main/java/com/datadoghq/trace/resolver/FactoryUtils.java b/dd-trace/src/main/java/com/datadoghq/trace/resolver/FactoryUtils.java index 7b90459d48..85e8fed89a 100644 --- a/dd-trace/src/main/java/com/datadoghq/trace/resolver/FactoryUtils.java +++ b/dd-trace/src/main/java/com/datadoghq/trace/resolver/FactoryUtils.java @@ -1,5 +1,6 @@ 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 loadConfigFromFilePropertyOrResource( final String systemProperty, final String resourceName, final Class targetClass) { + return loadConfigFromFilePropertyOrResource( + systemProperty, resourceName, new TypeReference() {}); + } + + public static 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 loadConfigFromResource( final String resourceName, final Class targetClass) { + return loadConfigFromResource(resourceName, new TypeReference() {}); + } + + public static 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); diff --git a/gradle/java.gradle b/gradle/java.gradle index 351ff4a36c..b058fb6e89 100644 --- a/gradle/java.gradle +++ b/gradle/java.gradle @@ -1,4 +1,5 @@ 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"