diff --git a/components/context/build.gradle.kts b/components/context/build.gradle.kts index c737d9fefdd..8e60dbe7e83 100644 --- a/components/context/build.gradle.kts +++ b/components/context/build.gradle.kts @@ -8,6 +8,16 @@ jmh { version = "1.28" } +dependencies { + testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.0") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.0") + implementation("org.junit.platform:junit-platform-launcher:1.9.0") +} + val excludedClassesInstructionCoverage by extra { listOf("datadog.context.ContextProviders") // covered by forked test } + +tasks.test { + systemProperty("junit.jupiter.extensions.autodetection.enabled", "true") +} diff --git a/components/context/src/main/resources/META-INF/services/org.junit.jupiter.api.extension.Extension b/components/context/src/main/resources/META-INF/services/org.junit.jupiter.api.extension.Extension new file mode 100644 index 00000000000..cdd486e07c7 --- /dev/null +++ b/components/context/src/main/resources/META-INF/services/org.junit.jupiter.api.extension.Extension @@ -0,0 +1 @@ +datadog.context.GatherSourceFileInfoExtension diff --git a/components/context/src/main/resources/META-INF/services/org.junit.platform.launcher.TestExecutionListener b/components/context/src/main/resources/META-INF/services/org.junit.platform.launcher.TestExecutionListener new file mode 100644 index 00000000000..5f76f95d8c2 --- /dev/null +++ b/components/context/src/main/resources/META-INF/services/org.junit.platform.launcher.TestExecutionListener @@ -0,0 +1 @@ +datadog.context.InsertSourceFileExtension diff --git a/components/context/src/test/java/datadog/context/ContextKeyTest.java b/components/context/src/test/java/datadog/context/ContextKeyTest.java index 3ff674145da..546ecbd0c77 100644 --- a/components/context/src/test/java/datadog/context/ContextKeyTest.java +++ b/components/context/src/test/java/datadog/context/ContextKeyTest.java @@ -19,7 +19,8 @@ class ContextKeyTest { void testConstructor(String name) { ContextKey key = ContextKey.named(name); assertNotNull(key, "created key should not be null"); - assertEquals(name, key.toString(), name + " label should be supported"); + // assertEquals fails. remove '+ "X"' to fix. + assertEquals(name, key.toString() + "X", name + " label should be supported"); } @Test diff --git a/components/context/src/test/java/datadog/context/GatherSourceFileInfoExtension.java b/components/context/src/test/java/datadog/context/GatherSourceFileInfoExtension.java new file mode 100644 index 00000000000..955113f4795 --- /dev/null +++ b/components/context/src/test/java/datadog/context/GatherSourceFileInfoExtension.java @@ -0,0 +1,41 @@ +package datadog.context; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.TestInstancePostProcessor; + +public class GatherSourceFileInfoExtension implements TestInstancePostProcessor { + private static Map sourceFiles = new HashMap(); + + @Override + public void postProcessTestInstance(Object testInstance, ExtensionContext context) + throws IOException { + getTestData(context); + } + + public static Map getSourceFiles() { + return sourceFiles; + } + + private static void getTestData(ExtensionContext context) throws IOException { + // get test classname and source file + String testClassName = context.getTestClass().get().getName(); + String testClassPath = testClassName.replace(".", "/") + ".java"; + String root = Paths.get("").toAbsolutePath().toString(); + String absolutePath = root + "/src/test/java/" + testClassPath; + String subPath = + absolutePath.substring(absolutePath.indexOf("dd-trace-java") + "dd-trace-java".length()); + // add info to sourceFiles map + sourceFiles.put(testClassName, subPath); + // print to sourceFiles.xml + BufferedWriter writer = + new BufferedWriter(new FileWriter(root + "/build/test-results/test/sourceFiles.xml")); + writer.write(sourceFiles.toString()); + writer.close(); + } +} diff --git a/components/context/src/test/java/datadog/context/InsertSourceFileExtension.java b/components/context/src/test/java/datadog/context/InsertSourceFileExtension.java new file mode 100644 index 00000000000..7541b2cc84a --- /dev/null +++ b/components/context/src/test/java/datadog/context/InsertSourceFileExtension.java @@ -0,0 +1,81 @@ +package datadog.context; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.junit.platform.engine.TestExecutionResult; +import org.junit.platform.launcher.TestExecutionListener; +import org.junit.platform.launcher.TestIdentifier; +import org.junit.platform.launcher.TestPlan; + +public class InsertSourceFileExtension implements TestExecutionListener { + @Override + public void executionStarted(TestIdentifier testIdentifier) { + System.out.println(testIdentifier.getDisplayName() + " test started."); + } + + @Override + public void testPlanExecutionStarted(TestPlan testPlan) { + // does not print when tested locally + System.out.println("---testPlanExecutionStarted---"); + } + + @Override + public void executionFinished( + TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { + // get mapping of test classname to source file + Map sourceFiles = GatherSourceFileInfoExtension.getSourceFiles(); + + String filePath = Paths.get("").toAbsolutePath() + "/build/test-results/test/"; + + // for each test... + for (String sourceFile : sourceFiles.keySet()) { + try { + // get xml report file + String fileContent = + new String(Files.readAllBytes(Paths.get(filePath + "TEST-" + sourceFile + ".xml"))); + + // add test source file info to report + Pattern pattern = Pattern.compile(""); + Matcher matcher = pattern.matcher(fileContent); + StringBuffer result = new StringBuffer(); + while (matcher.find()) { + String begAttributes = matcher.group(1); + String className = matcher.group(2); + String endAttributes = matcher.group(3); + + String fileAttribute = " file=\"" + sourceFiles.getOrDefault(className, "") + "\""; + String newTestCase = + ""; + matcher.appendReplacement(result, newTestCase); + } + matcher.appendTail(result); + + // set old filePath to new xml result + BufferedWriter writer = + new BufferedWriter(new FileWriter(filePath + "MOD-" + sourceFile + ".xml")); + writer.write(result.toString()); + writer.close(); + } catch (Exception e) { + System.out.println("Modifying XML files did not work."); + } + } + } + + @Override + public void testPlanExecutionFinished(TestPlan testPlan) { + // does not print when tested locally + System.out.println("---testPlanExecutionFinished---."); + } +}