|
15 | 15 | import java.io.IOException; |
16 | 16 | import java.net.URL; |
17 | 17 | import java.net.URLClassLoader; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.NoSuchFileException; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.Paths; |
| 22 | +import java.nio.file.attribute.PosixFilePermissions; |
| 23 | + |
18 | 24 | import org.junit.jupiter.api.Test; |
19 | 25 |
|
20 | 26 | public class NativeLoaderTest { |
@@ -208,49 +214,105 @@ public void fromDirBackedClassLoader_with_subResource_and_comp() |
208 | 214 | public void fromJarBackedClassLoader() throws IOException, LibraryLoadException { |
209 | 215 | URL[] urls = {new File("test-data/libdummy.jar").toURL()}; |
210 | 216 |
|
211 | | - URLClassLoader classLoader = new URLClassLoader(urls); |
212 | | - |
213 | | - NativeLoader loader = NativeLoader.builder().fromClassLoader(classLoader).build(); |
214 | | - try (LibFile lib = loader.resolveDynamic("dummy")) { |
215 | | - // loaded from a jar, so copied to temp file |
216 | | - assertTempFile(lib); |
| 217 | + try (URLClassLoader classLoader = new URLClassLoader(urls)) { |
| 218 | + NativeLoader loader = NativeLoader.builder().fromClassLoader(classLoader).build(); |
| 219 | + try (LibFile lib = loader.resolveDynamic("dummy")) { |
| 220 | + // loaded from a jar, so copied to temp file |
| 221 | + assertTempFile(lib); |
| 222 | + } |
217 | 223 | } |
218 | 224 | } |
219 | 225 |
|
220 | 226 | @Test |
221 | 227 | public void fromJarBackedClassLoader_with_tempDir() throws IOException, LibraryLoadException { |
222 | 228 | URL[] urls = {new File("test-data/libdummy.jar").toURL()}; |
| 229 | + |
| 230 | + Path tempDir = Paths.get("temp"); |
| 231 | + deleteHelper(tempDir); |
223 | 232 |
|
224 | | - URLClassLoader classLoader = new URLClassLoader(urls); |
| 233 | + try (URLClassLoader classLoader = new URLClassLoader(urls)) { |
| 234 | + NativeLoader loader = |
| 235 | + NativeLoader.builder().fromClassLoader(classLoader).tempDir(tempDir).build(); |
| 236 | + try (LibFile lib = loader.resolveDynamic("dummy")) { |
| 237 | + // loaded from a jar, so copied to temp file |
| 238 | + assertTempFile(lib); |
| 239 | + } |
| 240 | + } finally { |
| 241 | + deleteHelper(tempDir); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + @Test |
| 246 | + public void fromJarBackedClassLoader_with_unwritable_tempDir() throws IOException, LibraryLoadException { |
| 247 | + URL[] urls = {new File("test-data/libdummy.jar").toURL()}; |
| 248 | + |
| 249 | + Path noWriteDir = Paths.get("no-write-temp"); |
| 250 | + deleteHelper(noWriteDir); |
| 251 | + Files.createDirectories(noWriteDir, PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r-x------"))); |
225 | 252 |
|
226 | | - NativeLoader loader = |
227 | | - NativeLoader.builder().fromClassLoader(classLoader).tempDir("temp").build(); |
228 | | - try (LibFile lib = loader.resolveDynamic("dummy")) { |
229 | | - // loaded from a jar, so copied to temp file |
230 | | - assertTempFile(lib); |
| 253 | + try (URLClassLoader classLoader = new URLClassLoader(urls)) { |
| 254 | + NativeLoader loader = NativeLoader.builder().fromClassLoader(classLoader).tempDir(noWriteDir).build(); |
| 255 | + |
| 256 | + // unable to resolve to a File because tempDir isn't writable |
| 257 | + assertThrows(LibraryLoadException.class, () -> loader.resolveDynamic("dummy")); |
| 258 | + } finally { |
| 259 | + deleteHelper(noWriteDir); |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + @Test |
| 264 | + public void fromJarBackedClassLoader_with_locked_file() throws IOException, LibraryLoadException { |
| 265 | + URL[] urls = {new File("test-data/libdummy.jar").toURL()}; |
| 266 | + |
| 267 | + Path tempDir = Paths.get("temp"); |
| 268 | + deleteHelper(tempDir); |
| 269 | + |
| 270 | + try (URLClassLoader classLoader = new URLClassLoader(urls)) { |
| 271 | + NativeLoader loader = NativeLoader.builder().fromClassLoader(classLoader).tempDir(tempDir).build(); |
| 272 | + try (LibFile lib = loader.resolveDynamic("dummy")) { |
| 273 | + // loaded from a jar, so copied to temp file |
| 274 | + assertTempFile(lib); |
| 275 | + |
| 276 | + // simulating lock - by blocking ability to delete from parent dir |
| 277 | + // forces fallback to deleteOnExit |
| 278 | + Files.setPosixFilePermissions(lib.toPath().getParent(), PosixFilePermissions.fromString("r-x------")); |
| 279 | + } |
| 280 | + } finally { |
| 281 | + deleteHelper(tempDir); |
231 | 282 | } |
232 | 283 | } |
| 284 | + |
| 285 | + void deleteHelper(Path dir) { |
| 286 | + try { |
| 287 | + Files.setPosixFilePermissions(dir, PosixFilePermissions.fromString("rwx------")); |
| 288 | + Files.delete(dir); |
| 289 | + } catch (IOException e) { |
| 290 | + } |
| 291 | + } |
233 | 292 |
|
234 | 293 | void assertPreloaded(LibFile lib) { |
235 | 294 | assertTrue(lib.isPreloaded()); |
236 | | - assertNull(lib.file); |
| 295 | + assertNull(lib.toFile()); |
| 296 | + assertNull(lib.toPath()); |
237 | 297 | assertNull(lib.getAbsolutePath()); |
238 | 298 | assertFalse(lib.needsCleanup); |
239 | 299 | } |
240 | 300 |
|
241 | 301 | void assertRegularFile(LibFile lib) { |
242 | 302 | assertFalse(lib.isPreloaded()); |
243 | | - assertNotNull(lib.file); |
244 | | - assertTrue(lib.file.exists()); |
245 | | - assertEquals(lib.file.getAbsolutePath(), lib.getAbsolutePath()); |
| 303 | + assertNotNull(lib.toFile()); |
| 304 | + assertNotNull(lib.toPath()); |
| 305 | + assertTrue(lib.toFile().exists()); |
| 306 | + assertNotNull(lib.getAbsolutePath()); |
246 | 307 | assertFalse(lib.needsCleanup); |
247 | 308 | } |
248 | 309 |
|
249 | 310 | void assertTempFile(LibFile lib) { |
250 | 311 | assertFalse(lib.isPreloaded()); |
251 | | - assertNotNull(lib.file); |
252 | | - assertTrue(lib.file.exists()); |
253 | | - assertEquals(lib.file.getAbsolutePath(), lib.getAbsolutePath()); |
| 312 | + assertNotNull(lib.toFile()); |
| 313 | + assertNotNull(lib.toPath()); |
| 314 | + assertTrue(lib.toFile().exists()); |
| 315 | + assertNotNull(lib.getAbsolutePath()); |
254 | 316 | assertTrue(lib.needsCleanup); |
255 | 317 | } |
256 | 318 | } |
0 commit comments