ProfileDiskAction { public void saveProfile(final Path baseDir, final Path destination, final ProgressObserver callback) throws IOException { log.info(String.format("Saving profile [%s] to [%s]", baseDir, destination)); final Path output = destination.resolveSibling(destination.getFileName().toString() + ".tmp~"); if (!FileUtil.deleteQuietly(output)) { String message = String.format("Could not delete original profile file: %s. " + "Will try to delete on exit.", output.toAbsolutePath().toString()); log.warn(message); output.toFile().deleteOnExit(); } if (!Files.exists(output)) { try { Files.createFile(output); } catch (FileAlreadyExistsException e) { throw new IOException(String.format("Error creating tmp file [%s]", output)); } } final ProfileWalker profileWalker = new ProfileWalker(baseDir, output, callback); try { profileWalker.save(); callback.onProgress(UNITY_PERCENT); if (Files.exists(destination)) { if (!FileUtil.deleteQuietly(destination)) { throw new IOException(String.format("Error removing old file [%s]", destination)); } } if (!Files.exists(Files.move(output, destination))) { throw new IOException(String.format("Error creating saved file [%s]", destination)); } } finally { profileWalker.close(); } }  void saveProfile(final Path baseDir, final Path destination,
            final ProgressObserver callback); void load(final Path source, final Path destination, ProgressObserver observer);  }
@Test public void testSaveProfileToFile() throws Exception { ProgressObserver callback = mock(ProgressObserver.class); ProfileDiskAction action = new ProfileDiskAction(); action.saveProfile(Paths.get(profilesDir.getAbsolutePath(), "profileToSave"), destination, callback); try(final ZipFile savedFile = new ZipFile(destination.toFile())) { final Enumeration<? extends ZipEntry> entries = savedFile.entries(); final List<String> entryNames = new ArrayList<>(); while (entries.hasMoreElements()) { entryNames.add(FilenameUtils.separatorsToUnix(entries.nextElement().getName())); } assertEquals(true, entryNames.contains("db/file2")); assertEquals(true, entryNames.contains("db/file3")); assertEquals(true, entryNames.contains("db/file4")); assertEquals(true, entryNames.contains("db/service.properties")); assertEquals(true, entryNames.contains("profile.xml")); try(final ZipFile saved = new ZipFile(destination.toFile()); final InputStream in = saved.getInputStream(new ZipEntry("profile.xml"))) { final StringBuilder sb = new StringBuilder(); int bytesIn = 0; byte[] readBuffer = new byte[20]; while ((bytesIn = in.read(readBuffer)) != -1) { sb.append(new String(readBuffer, 0, bytesIn)); } assertEquals(getXmlString(), sb.toString()); verify(callback, atLeastOnce()).onProgress(AdditionalMatchers.leq(100)); } } }