@Test public void download_from_artifactory_and_add_to_cache() throws Exception { when(artifactory.downloadToFile(any(), any())).thenAnswer((Answer<Boolean>) invocationOnMock -> { File file = (File) invocationOnMock.getArguments()[1]; FileUtils.write(file, "content of file"); return true; }); File file = underTest.locateResolvedVersion(MavenLocation.of("foo", "bar", "1.1")); assertThat(file) .exists() .isFile() .hasContent("content of file"); assertThat(file.getCanonicalPath()).startsWith(fileSystem.getCacheDir().getCanonicalPath()); }
@Nullable File locateResolvedVersion(MavenLocation resolvedLocation) { String cacheKey = cacheKeyOf(resolvedLocation); File cachedDir = new File(fileSystem.getCacheDir(), cacheKey); if (cachedDir.exists()) { Collection<File> files = FileUtils.listFiles(cachedDir, null, false); if (files.size() == 1) { File file = files.iterator().next(); LOG.info("Found {} at {}", resolvedLocation, file); return file; } } File file = locateInLocalRepository(resolvedLocation); if (file != null) { LOG.info("Found {} in Maven local repository at {}", resolvedLocation, file); return file; } if (resolvedLocation.getVersion().endsWith("-SNAPSHOT")) { return null; } File cachedFile = new File(cachedDir, resolvedLocation.getFilename()); boolean found = artifactory.downloadToFile(resolvedLocation, cachedFile); return found ? cachedFile : null; }
MavenLocator implements Locator<MavenLocation> { @Nullable File locateResolvedVersion(MavenLocation resolvedLocation) { String cacheKey = cacheKeyOf(resolvedLocation); File cachedDir = new File(fileSystem.getCacheDir(), cacheKey); if (cachedDir.exists()) { Collection<File> files = FileUtils.listFiles(cachedDir, null, false); if (files.size() == 1) { File file = files.iterator().next(); LOG.info("Found {} at {}", resolvedLocation, file); return file; } } File file = locateInLocalRepository(resolvedLocation); if (file != null) { LOG.info("Found {} in Maven local repository at {}", resolvedLocation, file); return file; } if (resolvedLocation.getVersion().endsWith("-SNAPSHOT")) { return null; } File cachedFile = new File(cachedDir, resolvedLocation.getFilename()); boolean found = artifactory.downloadToFile(resolvedLocation, cachedFile); return found ? cachedFile : null; } }
MavenLocator implements Locator<MavenLocation> { @Nullable File locateResolvedVersion(MavenLocation resolvedLocation) { String cacheKey = cacheKeyOf(resolvedLocation); File cachedDir = new File(fileSystem.getCacheDir(), cacheKey); if (cachedDir.exists()) { Collection<File> files = FileUtils.listFiles(cachedDir, null, false); if (files.size() == 1) { File file = files.iterator().next(); LOG.info("Found {} at {}", resolvedLocation, file); return file; } } File file = locateInLocalRepository(resolvedLocation); if (file != null) { LOG.info("Found {} in Maven local repository at {}", resolvedLocation, file); return file; } if (resolvedLocation.getVersion().endsWith("-SNAPSHOT")) { return null; } File cachedFile = new File(cachedDir, resolvedLocation.getFilename()); boolean found = artifactory.downloadToFile(resolvedLocation, cachedFile); return found ? cachedFile : null; } MavenLocator(FileSystem fileSystem, Artifactory artifactory); }
MavenLocator implements Locator<MavenLocation> { @Nullable File locateResolvedVersion(MavenLocation resolvedLocation) { String cacheKey = cacheKeyOf(resolvedLocation); File cachedDir = new File(fileSystem.getCacheDir(), cacheKey); if (cachedDir.exists()) { Collection<File> files = FileUtils.listFiles(cachedDir, null, false); if (files.size() == 1) { File file = files.iterator().next(); LOG.info("Found {} at {}", resolvedLocation, file); return file; } } File file = locateInLocalRepository(resolvedLocation); if (file != null) { LOG.info("Found {} in Maven local repository at {}", resolvedLocation, file); return file; } if (resolvedLocation.getVersion().endsWith("-SNAPSHOT")) { return null; } File cachedFile = new File(cachedDir, resolvedLocation.getFilename()); boolean found = artifactory.downloadToFile(resolvedLocation, cachedFile); return found ? cachedFile : null; } MavenLocator(FileSystem fileSystem, Artifactory artifactory); @Override File locate(MavenLocation location); Optional<String> resolveVersion(MavenLocation location); @Override File copyToDirectory(MavenLocation location, File toDir); @Override File copyToFile(MavenLocation location, File toFile); @Override @CheckForNull InputStream openInputStream(MavenLocation location); }
MavenLocator implements Locator<MavenLocation> { @Nullable File locateResolvedVersion(MavenLocation resolvedLocation) { String cacheKey = cacheKeyOf(resolvedLocation); File cachedDir = new File(fileSystem.getCacheDir(), cacheKey); if (cachedDir.exists()) { Collection<File> files = FileUtils.listFiles(cachedDir, null, false); if (files.size() == 1) { File file = files.iterator().next(); LOG.info("Found {} at {}", resolvedLocation, file); return file; } } File file = locateInLocalRepository(resolvedLocation); if (file != null) { LOG.info("Found {} in Maven local repository at {}", resolvedLocation, file); return file; } if (resolvedLocation.getVersion().endsWith("-SNAPSHOT")) { return null; } File cachedFile = new File(cachedDir, resolvedLocation.getFilename()); boolean found = artifactory.downloadToFile(resolvedLocation, cachedFile); return found ? cachedFile : null; } MavenLocator(FileSystem fileSystem, Artifactory artifactory); @Override File locate(MavenLocation location); Optional<String> resolveVersion(MavenLocation location); @Override File copyToDirectory(MavenLocation location, File toDir); @Override File copyToFile(MavenLocation location, File toFile); @Override @CheckForNull InputStream openInputStream(MavenLocation location);  }