SourcesExporter { public void removeUnreferencedFiles(Collection<ReleaseWithSources> releases) { RemoveUnreferencedFilesVisitor visitor = new RemoveUnreferencedFilesVisitor(releases); try { Files.walkFileTree(sourcePath, visitor); } catch (IOException e) { LOG.error("Could not traverse sources directory {}", sourcePath, e); } } SourcesExporter(Path sourcePath); Collection<ReleaseWithSources> downloadSources(SW360ReleaseClientAdapterAsync releaseAdapter,
                                                          Collection<SW360Release> releases); void removeUnreferencedFiles(Collection<ReleaseWithSources> releases);  }
@Test public void testUnreferencedFilesCanBeRemoved() { Set<SW360SparseAttachment> attachments1 = createAttachments(1, 2); Set<SW360SparseAttachment> attachments2 = createAttachments(2, 3); Set<SW360SparseAttachment> attachments3 = createAttachments(1, 3); ReleaseWithSources release1 = createReleaseWithSources(createReleaseWithAttachments(1, attachments1), attachments1); ReleaseWithSources release2 = createReleaseWithSources(createReleaseWithAttachments(2, attachments2), attachments2); SW360SparseAttachment unreferencedAttachment = createAttachment(17, 47); SW360Release unreferencedRelease1 = createReleaseWithAttachments(17, Collections.singleton(unreferencedAttachment)); SW360Release unreferencedRelease2 = createReleaseWithAttachments(1, attachments3); unreferencedRelease2.setVersion("2.0"); ReleaseWithSources unreferencedRelease2WithSources = createReleaseWithSources(unreferencedRelease2, attachments3); Path unreferencedPath = attachmentPath(unreferencedRelease1, unreferencedAttachment); createTestFile(unreferencedPath); createAttachmentFiles(release1); createAttachmentFiles(release2); createAttachmentFiles(unreferencedRelease2WithSources); Path furtherSubPath1 = createTestFile(releasePath(release1.getRelease()) .resolve("sub").resolve("subFile.txt")).getParent(); Path furtherSubPath2 = createTestFile(releasePath(release1.getRelease()) .resolve("aSubDir").resolve("otherSubFile.txt")).getParent(); Path topPath1 = createTestFile(sourcePath.resolve("a.txt")); Path topPath2 = createTestFile(sourcePath.resolve("z.txt")); sourcesExporter.removeUnreferencedFiles(Arrays.asList(release1, release2)); checkAllAttachmentsExist(release1); checkAllAttachmentsExist(release2); assertThat(checkFileExists(unreferencedPath)).isFalse(); assertThat(checkFileExists(releasePath(unreferencedRelease1))).isFalse(); assertThat(checkFileExists(releasePath(unreferencedRelease2))).isFalse(); assertThat(checkFileExists(furtherSubPath1)).isFalse(); assertThat(checkFileExists(furtherSubPath2)).isFalse(); assertThat(checkFileExists(topPath1)).isFalse(); assertThat(checkFileExists(topPath2)).isFalse(); }
@Test public void testPathsAreSanitizedWhenRemovingUnreferencedFiles() { SW360Release release = new SW360Release(); release.setName("org.apache.commons/commons-lang3"); release.setVersion("3.*"); Path delPath1 = sourcePath.resolve("org.apache.commons"); Path delPath2 = delPath1.resolve("commons-lang3"); Path delPath3 = createTestFile(delPath2.resolve("lang-source.zip")); Path validPath1 = sourcePath.resolve("org.apache.commons_commons-lang3").resolve("3._"); Path validPath2 = createTestFile(validPath1.resolve("lang-source.zip")); Set<SW360SparseAttachment> attachments = createAttachments(1, 1); initAttachments(release, attachments); ReleaseWithSources releaseWithSources = createReleaseWithSources(release, attachments); sourcesExporter.removeUnreferencedFiles(Collections.singleton(releaseWithSources)); assertThat(checkFileExists(validPath1)).isTrue(); assertThat(checkFileExists(validPath2)).isTrue(); assertThat(checkFileExists(delPath3)).isFalse(); assertThat(checkFileExists(delPath2)).isFalse(); assertThat(checkFileExists(delPath1)).isFalse(); }
@Test public void testIOExceptionIsHandledWhenRemovingUnreferencedFiles() { Path nonExisting = sourcePath.resolve("non/existing/sub/folder"); sourcesExporter = new SourcesExporter(nonExisting); sourcesExporter.removeUnreferencedFiles(Collections.emptyList()); }