Skip to content

Commit

Permalink
Refine findConfigurationFor invocations
Browse files Browse the repository at this point in the history
Perform 2 distinct findConfigurationFor invocations
in order to not be dependent of the order.
  • Loading branch information
sdeleuze committed May 2, 2023
1 parent 1feb0d4 commit 6a052e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ private Map<String, List<Artifact>> parseIndexFile(Path rootPath) {
*/
@Override
public Optional<DirectoryConfiguration> findConfiguration(String groupId, String artifactId, String version) {
return findConfigurationFor(groupId, artifactId, version,
artifact -> artifact.getVersions().contains(version) || artifact.isDefaultFor(version));
Optional<DirectoryConfiguration> exactMatch = findConfigurationFor(groupId, artifactId, version, artifact -> artifact.getVersions().contains(version));
return exactMatch.isPresent() ? exactMatch :
findConfigurationFor(groupId, artifactId, version, artifact -> artifact.isDefaultFor(version));
}

@Override
Expand All @@ -111,7 +112,9 @@ public Optional<DirectoryConfiguration> findLatestConfigurationFor(String groupI
*/
@Override
public Optional<DirectoryConfiguration> findLatestConfigurationFor(String groupId, String artifactId, String version) {
return findConfigurationFor(groupId, artifactId, version, artifact -> artifact.isDefaultFor(version) || artifact.isLatest());
Optional<DirectoryConfiguration> defaultMatch = findConfigurationFor(groupId, artifactId, version, artifact -> artifact.isDefaultFor(version));
return defaultMatch.isPresent() ? defaultMatch :
findConfigurationFor(groupId, artifactId, version, Artifact::isLatest);
}

private Optional<DirectoryConfiguration> findConfigurationFor(String groupId, String artifactId, String version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,25 @@ void checkIndexWithDefaultFor() throws URISyntaxException {
assertTrue(config.isPresent());
assertEquals(repoPath.resolve("2.0"), config.get().getDirectory());

config = index.findConfiguration("com.foo", "baz", "1.1");
assertTrue(config.isPresent());
assertEquals(repoPath.resolve("1.0"), config.get().getDirectory());

Optional<DirectoryConfiguration> latest = index.findLatestConfigurationFor("com.foo", "bar", "123");
assertTrue(latest.isPresent());
assertEquals(repoPath.resolve("2.0"), latest.get().getDirectory());

config = index.findConfiguration("com.foo", "baz", "1.1");
assertTrue(config.isPresent());
assertEquals(repoPath.resolve("1.0"), config.get().getDirectory());
latest = index.findLatestConfigurationFor("com.foo", "bar", "123");
assertTrue(latest.isPresent());
assertEquals(repoPath.resolve("2.0"), latest.get().getDirectory());

latest = index.findLatestConfigurationFor("com.foo", "bar", "1.0");
assertTrue(latest.isPresent());
assertEquals(repoPath.resolve("1.0"), latest.get().getDirectory());

latest = index.findLatestConfigurationFor("com.foo", "bar", "2.0");
assertTrue(latest.isPresent());
assertEquals(repoPath.resolve("2.0"), latest.get().getDirectory());
}

private void withIndex(String json) throws URISyntaxException {
Expand Down

0 comments on commit 6a052e7

Please sign in to comment.