FilterServiceFileUtils { static void atomicMoveWithFallback(Path source, Path target) throws IOException { try { Files.move(source, target, StandardCopyOption.ATOMIC_MOVE); } catch (IOException outer) { try { Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); logger.debug("Non-atomic move of {} to {} succeeded after atomic move failed due to {}", source, target, outer.getMessage()); } catch (IOException inner) { inner.addSuppressed(outer); throw inner; } } }    }
@Test public void atomicMoveWithFallbackFailed() throws Exception { final String tempDir = System.getProperty("java.io.tmpdir", "/tmp") + File.separator + "filter_service_" + System.nanoTime(); FileUtils.forceMkdir(new File(tempDir)); Path a = Paths.get(tempDir).resolve("path_a"); Path b = Paths.get(tempDir).resolve("path_b"); assertThatThrownBy(() -> FilterServiceFileUtils.atomicMoveWithFallback(a, b)) .isInstanceOf(NoSuchFileException.class); FileUtils.forceDelete(new File(tempDir)); }