Skip to content

Commit 864ef84

Browse files
authored
Migrate dd-trace-ot tests to JUnit 5 (#11483)
Migrate dd-trace-ot tests to JUnit 5 suggestions and compare resource name More tabletest and converters suggestions remove spurious efit suggestions Co-authored-by: andrea.marziali <andrea.marziali@datadoghq.com>
1 parent f464101 commit 864ef84

32 files changed

Lines changed: 1975 additions & 1976 deletions

dd-trace-ot/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ dependencies {
6666
implementation(project(":dd-trace-ot:correlation-id-injection"))
6767

6868
testImplementation(project(":dd-java-agent:testing"))
69+
testImplementation(project(":utils:junit-utils"))
70+
testImplementation(libs.bundles.mockito)
71+
72+
add("ot31CompatibilityTestImplementation", project(":utils:junit-utils"))
73+
add("ot33CompatibilityTestImplementation", project(":utils:junit-utils"))
6974

7075
// Kotlin accessors not generated if not coming from plugin
7176
add("ot33CompatibilityTestImplementation", "io.opentracing:opentracing-api") {

dd-trace-ot/correlation-id-injection/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies {
2828
testImplementation(libs.guava)
2929
testImplementation(project(":dd-trace-ot"))
3030
testImplementation(project(":dd-java-agent:testing"))
31+
testImplementation(libs.bundles.mockito)
3132
testImplementation("org.apache.logging.log4j:log4j-core:$log4j2")
3233
testImplementation("ch.qos.logback:logback-core:$logback")
3334
}

dd-trace-ot/correlation-id-injection/src/test/groovy/CorrelationIdInjectorTest.groovy

Lines changed: 0 additions & 74 deletions
This file was deleted.

dd-trace-ot/correlation-id-injection/src/test/groovy/Log4j2CorrelationIdInjectorTest.groovy

Lines changed: 0 additions & 64 deletions
This file was deleted.

dd-trace-ot/correlation-id-injection/src/test/groovy/Slf4jCorrelationIdInjectorTest.groovy

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import datadog.opentracing.DDTracer;
4+
import datadog.trace.api.CorrelationIdentifier;
5+
import datadog.trace.api.GlobalTracer;
6+
import datadog.trace.test.util.DDJavaSpecification;
7+
import io.opentracing.Scope;
8+
import io.opentracing.Span;
9+
import org.junit.jupiter.api.Test;
10+
11+
abstract class CorrelationIdInjectorTest extends DDJavaSpecification {
12+
13+
protected static final String LOG_PATTERN =
14+
"TRACE_ID=%X{"
15+
+ CorrelationIdentifier.getTraceIdKey()
16+
+ "} SPAN_ID=%X{"
17+
+ CorrelationIdentifier.getSpanIdKey()
18+
+ "} %m";
19+
20+
@Test
21+
void testCorrelationIdInjection() throws Exception {
22+
DDTracer tracer = buildTracer();
23+
LogJournal journal = buildJournal();
24+
TestLogger logger = buildLogger();
25+
26+
logger.log("Event without context");
27+
28+
assertEquals("TRACE_ID= SPAN_ID= Event without context", journal.nextLog());
29+
30+
Span rootSpan = tracer.buildSpan("operation1").start();
31+
Scope rootScope = tracer.activateSpan(rootSpan);
32+
logger.log("Event with root span context");
33+
34+
assertEquals(expectedLog("Event with root span context"), journal.nextLog());
35+
36+
Span childSpan = tracer.buildSpan("operation1").asChildOf(rootSpan).start();
37+
Scope childScope = tracer.activateSpan(childSpan);
38+
logger.log("Event with child span context");
39+
40+
assertEquals(expectedLog("Event with child span context"), journal.nextLog());
41+
42+
childScope.close();
43+
childSpan.finish();
44+
logger.log("Event with root span context");
45+
46+
assertEquals(expectedLog("Event with root span context"), journal.nextLog());
47+
48+
rootScope.close();
49+
rootSpan.finish();
50+
logger.log("Event without context");
51+
52+
assertEquals("TRACE_ID= SPAN_ID= Event without context", journal.nextLog());
53+
54+
tracer.close();
55+
}
56+
57+
private static String expectedLog(String message) {
58+
return String.format(
59+
"TRACE_ID=%s SPAN_ID=%s %s",
60+
CorrelationIdentifier.getTraceId(), CorrelationIdentifier.getSpanId(), message);
61+
}
62+
63+
DDTracer buildTracer() {
64+
DDTracer tracer = new DDTracer.DDTracerBuilder().build();
65+
GlobalTracer.registerIfAbsent(tracer);
66+
return tracer;
67+
}
68+
69+
abstract LogJournal buildJournal();
70+
71+
abstract TestLogger buildLogger();
72+
73+
interface LogJournal {
74+
String nextLog();
75+
}
76+
77+
interface TestLogger {
78+
void log(String message);
79+
}
80+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import org.apache.logging.log4j.Level;
4+
import org.apache.logging.log4j.LogManager;
5+
import org.apache.logging.log4j.Logger;
6+
import org.apache.logging.log4j.core.Appender;
7+
import org.apache.logging.log4j.core.Filter;
8+
import org.apache.logging.log4j.core.LogEvent;
9+
import org.apache.logging.log4j.core.LoggerContext;
10+
import org.apache.logging.log4j.core.appender.AbstractAppender;
11+
import org.apache.logging.log4j.core.config.Configuration;
12+
import org.apache.logging.log4j.core.config.LoggerConfig;
13+
import org.apache.logging.log4j.core.layout.PatternLayout;
14+
15+
class Log4j2CorrelationIdInjectorTest extends CorrelationIdInjectorTest {
16+
17+
@Override
18+
LogJournal buildJournal() {
19+
LoggerContext context = LoggerContext.getContext(false);
20+
Configuration config = context.getConfiguration();
21+
22+
TestAppender appender =
23+
new TestAppender(PatternLayout.newBuilder().withPattern(LOG_PATTERN).build());
24+
appender.start();
25+
config.addAppender(appender);
26+
updateLoggers(appender, config);
27+
return appender;
28+
}
29+
30+
@Override
31+
TestLogger buildLogger() {
32+
Logger logger = LogManager.getLogger("TestLogger");
33+
return message -> logger.error(message);
34+
}
35+
36+
private static void updateLoggers(Appender appender, Configuration config) {
37+
Level level = null;
38+
Filter filter = null;
39+
for (LoggerConfig loggerConfig : config.getLoggers().values()) {
40+
loggerConfig.addAppender(appender, level, filter);
41+
}
42+
config.getRootLogger().addAppender(appender, level, filter);
43+
}
44+
45+
static class TestAppender extends AbstractAppender
46+
implements CorrelationIdInjectorTest.LogJournal {
47+
List<String> events;
48+
int read;
49+
50+
protected TestAppender(PatternLayout patternLayout) {
51+
super("TestAppender", null, patternLayout, false, null);
52+
events = new ArrayList<>();
53+
read = 0;
54+
}
55+
56+
@Override
57+
public void append(LogEvent event) {
58+
String log = ((PatternLayout) getLayout()).toSerializable(event);
59+
events.add(log);
60+
}
61+
62+
@Override
63+
public String nextLog() {
64+
if (events.size() <= read) {
65+
return null;
66+
}
67+
return events.get(read++);
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)