Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions components/context/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
datadog.context.GatherSourceFileInfoExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
datadog.context.InsertSourceFileExtension
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class ContextKeyTest {
void testConstructor(String name) {
ContextKey<String> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> sourceFiles = new HashMap<String, String>();

@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context)
throws IOException {
getTestData(context);
}

public static Map<String, String> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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("<testcase(.*?)classname=\"(.*?)\"(.*?)>");
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 =
"<testcase"
+ begAttributes
+ "classname=\""
+ className
+ "\""
+ fileAttribute
+ endAttributes
+ ">";
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---.");
}
}
Loading