Skip to content

Commit 52c3c44

Browse files
authored
Add coverage for ForeignMemoryWriterFactory (#11772)
* Add coverage for ForeignMemoryWriterFactoryTest * test on 22+
1 parent 4b5b0cf commit 52c3c44

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package datadog.trace.core.servicediscovery;
2+
3+
import static org.junit.jupiter.api.Assertions.assertNotNull;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.mockito.Mockito.mockStatic;
7+
8+
import datadog.environment.JavaVirtualMachine;
9+
import datadog.environment.OperatingSystem;
10+
import org.junit.jupiter.api.Assumptions;
11+
import org.mockito.MockedStatic;
12+
import org.tabletest.junit.TableTest;
13+
14+
class ForeignMemoryWriterFactoryTest {
15+
16+
@TableTest({
17+
"scenario | osType | architecture | javaAtLeast22 | expectedClassFragment",
18+
"macOS | MACOS | X64 | false | ",
19+
"Windows | WINDOWS | X64 | false | ",
20+
"Linux unknown arch | LINUX | UNKNOWN | false | ",
21+
"Linux pre-Java22 | LINUX | X64 | false | JNA ",
22+
"Linux Java22+ | LINUX | X64 | true | FFM "
23+
})
24+
void get(
25+
String scenario,
26+
String osType,
27+
String architecture,
28+
boolean javaAtLeast22,
29+
String expectedClassFragment) {
30+
// MemFDUnixWriterFFM uses java.lang.foreign and will fail to load on pre-22 JVMs
31+
boolean realJavaAtLeast22 = JavaVirtualMachine.isJavaVersionAtLeast(22);
32+
Assumptions.assumeTrue(!javaAtLeast22 || realJavaAtLeast22, "FFM writer requires Java 22+");
33+
try (MockedStatic<OperatingSystem> osMock = mockStatic(OperatingSystem.class);
34+
MockedStatic<JavaVirtualMachine> jvmMock = mockStatic(JavaVirtualMachine.class)) {
35+
osMock.when(OperatingSystem::type).thenReturn(OperatingSystem.Type.valueOf(osType));
36+
osMock
37+
.when(OperatingSystem::architecture)
38+
.thenReturn(OperatingSystem.Architecture.valueOf(architecture));
39+
jvmMock.when(() -> JavaVirtualMachine.isJavaVersionAtLeast(22)).thenReturn(javaAtLeast22);
40+
41+
ForeignMemoryWriter writer = new ForeignMemoryWriterFactory().get();
42+
43+
if (expectedClassFragment == null) {
44+
assertNull(writer, scenario);
45+
} else {
46+
assertNotNull(writer, scenario);
47+
assertTrue(writer.getClass().getName().contains(expectedClassFragment), scenario);
48+
}
49+
}
50+
}
51+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mock-maker-inline

0 commit comments

Comments
 (0)