Skip to content

Commit 00fa98a

Browse files
committed
Testing tempDir functionality
Fixed problem with tempDir support when tempDir doesn't already exist
1 parent a76f79d commit 00fa98a

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

components/native-loader/src/main/java/datadog/nativeloader/NativeLoader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ static final Path createTempFile(Path tempDir, String libname, String libExt)
289289
if (tempDir == null) {
290290
return Files.createTempFile(libname, "." + libExt, permAttrs);
291291
} else {
292+
Files.createDirectories(
293+
tempDir,
294+
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")));
295+
292296
return Files.createTempFile(tempDir, libname, "." + libExt, permAttrs);
293297
}
294298
}

components/native-loader/src/test/java/datadog/nativeloader/NativeLoaderTest.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
import static datadog.nativeloader.TestPlatformSpec.LINUX;
55
import static datadog.nativeloader.TestPlatformSpec.UNSUPPORTED_ARCH;
66
import static datadog.nativeloader.TestPlatformSpec.UNSUPPORTED_OS;
7-
8-
97
import static org.junit.jupiter.api.Assertions.assertEquals;
108
import static org.junit.jupiter.api.Assertions.assertFalse;
11-
import static org.junit.jupiter.api.Assertions.assertNotEquals;
129
import static org.junit.jupiter.api.Assertions.assertNotNull;
1310
import static org.junit.jupiter.api.Assertions.assertNull;
1411
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -77,7 +74,7 @@ public void fromDir() throws LibraryLoadException {
7774
try (LibFile lib = loader.resolveDynamic("dummy")) {
7875
// loaded directly from directory, so no clean-up required
7976
assertRegularFile(lib);
80-
77+
8178
// file isn't actually a dynamic library
8279
assertThrows(LibraryLoadException.class, () -> lib.load());
8380
}
@@ -186,27 +183,41 @@ public void fromJarBackedClassLoader() throws IOException, LibraryLoadException
186183
assertTempFile(lib);
187184
}
188185
}
189-
186+
187+
@Test
188+
public void fromJarBackedClassLoader_with_tempDir() throws IOException, LibraryLoadException {
189+
URL[] urls = {new File("test-data/libdummy.jar").toURL()};
190+
191+
URLClassLoader classLoader = new URLClassLoader(urls);
192+
193+
NativeLoader loader =
194+
NativeLoader.builder().fromClassLoader(classLoader).tempDir("temp").build();
195+
try (LibFile lib = loader.resolveDynamic("dummy")) {
196+
// loaded from a jar, so copied to temp file
197+
assertTempFile(lib);
198+
}
199+
}
200+
190201
void assertPreloaded(LibFile lib) {
191-
assertTrue(lib.isPreloaded());
192-
assertNull(lib.file);
193-
assertNull(lib.getAbsolutePath());
194-
assertFalse(lib.needsCleanup);
202+
assertTrue(lib.isPreloaded());
203+
assertNull(lib.file);
204+
assertNull(lib.getAbsolutePath());
205+
assertFalse(lib.needsCleanup);
195206
}
196207

197208
void assertRegularFile(LibFile lib) {
198-
assertFalse(lib.isPreloaded());
199-
assertNotNull(lib.file);
200-
assertTrue(lib.file.exists());
201-
assertEquals(lib.file.getAbsolutePath(), lib.getAbsolutePath());
209+
assertFalse(lib.isPreloaded());
210+
assertNotNull(lib.file);
211+
assertTrue(lib.file.exists());
212+
assertEquals(lib.file.getAbsolutePath(), lib.getAbsolutePath());
202213
assertFalse(lib.needsCleanup);
203214
}
204215

205216
void assertTempFile(LibFile lib) {
206-
assertFalse(lib.isPreloaded());
207-
assertNotNull(lib.file);
208-
assertTrue(lib.file.exists());
209-
assertEquals(lib.file.getAbsolutePath(), lib.getAbsolutePath());
210-
assertTrue(lib.needsCleanup);
217+
assertFalse(lib.isPreloaded());
218+
assertNotNull(lib.file);
219+
assertTrue(lib.file.exists());
220+
assertEquals(lib.file.getAbsolutePath(), lib.getAbsolutePath());
221+
assertTrue(lib.needsCleanup);
211222
}
212223
}

0 commit comments

Comments
 (0)