Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package datadog.trace.core.servicediscovery;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mockStatic;

import datadog.environment.JavaVirtualMachine;
import datadog.environment.OperatingSystem;
import org.junit.jupiter.api.Assumptions;
import org.mockito.MockedStatic;
import org.tabletest.junit.TableTest;

class ForeignMemoryWriterFactoryTest {

@TableTest({
"scenario | osType | architecture | javaAtLeast22 | expectedClassFragment",
"macOS | MACOS | X64 | false | ",
"Windows | WINDOWS | X64 | false | ",
"Linux unknown arch | LINUX | UNKNOWN | false | ",
"Linux pre-Java22 | LINUX | X64 | false | JNA ",
"Linux Java22+ | LINUX | X64 | true | FFM "
Comment thread
amarziali marked this conversation as resolved.
})
void get(
String scenario,
String osType,
String architecture,
boolean javaAtLeast22,
String expectedClassFragment) {
// MemFDUnixWriterFFM uses java.lang.foreign and will fail to load on pre-22 JVMs
boolean realJavaAtLeast22 = JavaVirtualMachine.isJavaVersionAtLeast(22);
Assumptions.assumeTrue(!javaAtLeast22 || realJavaAtLeast22, "FFM writer requires Java 22+");
try (MockedStatic<OperatingSystem> osMock = mockStatic(OperatingSystem.class);
MockedStatic<JavaVirtualMachine> jvmMock = mockStatic(JavaVirtualMachine.class)) {
osMock.when(OperatingSystem::type).thenReturn(OperatingSystem.Type.valueOf(osType));
osMock
.when(OperatingSystem::architecture)
.thenReturn(OperatingSystem.Architecture.valueOf(architecture));
jvmMock.when(() -> JavaVirtualMachine.isJavaVersionAtLeast(22)).thenReturn(javaAtLeast22);

ForeignMemoryWriter writer = new ForeignMemoryWriterFactory().get();

if (expectedClassFragment == null) {
assertNull(writer, scenario);
} else {
assertNotNull(writer, scenario);
assertTrue(writer.getClass().getName().contains(expectedClassFragment), scenario);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❔ question: ‏Will it impact other mocks from the module too?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

As an additional note this mockmaker is the default since mockito 5.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that for OperatingSystem/SystemProperties and CO, we should work for a better testability interop (or at least design the class that use it to receive a mock) if we want to avoid mocking statically

Loading