TempCleaning { void clean() { logger.debug("Start temp cleaning..."); long cutoff = System.currentTimeMillis() - ONE_DAY_IN_MILLISECONDS; try (Stream<Path> files = Files.list(tempDir)) { files .filter(p -> p.getFileName().toString().startsWith("sonar-scanner-api-batch")) .filter(p -> lastModifiedTime(p) < cutoff) .forEach(Utils::deleteQuietly); logger.debug("Temp cleaning done"); } catch (IOException e) { logger.warn("Failed to clean files in " + tempDir.toString() + ": " + e.getMessage()); } } TempCleaning(Logger logger);   TempCleaning(Path tempDir, Logger logger);   }
@Test public void should_clean() throws Exception { Path dir = temp.newFolder().toPath(); Path oldBatch = dir.resolve("sonar-scanner-api-batch656.jar"); Files.write(oldBatch, "foo".getBytes(StandardCharsets.UTF_8)); FileTime fTime = FileTime.fromMillis(System.currentTimeMillis() - 3 * TempCleaning.ONE_DAY_IN_MILLISECONDS); Files.setLastModifiedTime(oldBatch, fTime); Path youngBatch = dir.resolve("sonar-scanner-api-batch123.jar"); Files.write(youngBatch, "foo".getBytes(StandardCharsets.UTF_8)); Path doNotDelete = dir.resolve("jacoco.txt"); Files.write(doNotDelete, "foo".getBytes(StandardCharsets.UTF_8)); assertThat(oldBatch).exists(); assertThat(youngBatch).exists(); assertThat(doNotDelete).exists(); new TempCleaning(dir, mock(Logger.class)).clean(); assertThat(oldBatch).doesNotExist(); assertThat(youngBatch).exists(); assertThat(doNotDelete).exists(); }