NewestVersionSelector extends ConflictResolver.VersionSelector { @Override public void selectVersion(ConflictResolver.ConflictContext context) throws RepositoryException { ConflictGroup group = new ConflictGroup(); for (ConflictResolver.ConflictItem item : context.getItems()) { DependencyNode node = item.getNode(); VersionConstraint constraint = node.getVersionConstraint(); boolean backtrack = false; boolean hardConstraint = constraint.getRange() != null; if (hardConstraint) { if (group.constraints.add(constraint)) { if (group.winner != null && !constraint.containsVersion(group.winner.getNode().getVersion())) { backtrack = true; } } } if (isAcceptable(group, node.getVersion())) { group.candidates.add(item); if (backtrack) { backtrack(group, context); } else if (group.winner == null || isNewer(item, group.winner)) { group.winner = item; } } else if (backtrack) { backtrack(group, context); } } context.setWinner(group.winner); }  @Override void selectVersion(ConflictResolver.ConflictContext context);  }
@Test public void betaVersionComparison() throws Exception { NewestVersionSelector selector = new NewestVersionSelector(); List<ConflictResolver.ConflictItem> items = new ArrayList<>(); items.add(new ConflictResolver.ConflictItem(null, createNode("1.0.0.Beta3"), 0, 0)); items.add(new ConflictResolver.ConflictItem(null, createNode("1.0.0.Beta5-SNAPSHOT"), 0, 0)); ConflictResolver.ConflictContext context = new ConflictResolver.ConflictContext(null, null, null, items); selector.selectVersion(context); assertThat(context.getWinner().getNode().getVersion().toString()).isEqualTo("1.0.0.Beta5-SNAPSHOT"); }
@Test public void snapshotVersionComparison() throws Exception { NewestVersionSelector selector = new NewestVersionSelector(); List<ConflictResolver.ConflictItem> items = new ArrayList<>(); items.add(new ConflictResolver.ConflictItem(null, createNode("1.0.0.Beta3"), 0, 0)); items.add(new ConflictResolver.ConflictItem(null, createNode("1.0.0.Beta3-SNAPSHOT"), 0, 0)); ConflictResolver.ConflictContext context = new ConflictResolver.ConflictContext(null, null, null, items); selector.selectVersion(context); assertThat(context.getWinner().getNode().getVersion().toString()).isEqualTo("1.0.0.Beta3"); }
@Test public void alphaToBetaVersionComparison() throws Exception { NewestVersionSelector selector = new NewestVersionSelector(); List<ConflictResolver.ConflictItem> items = new ArrayList<>(); items.add(new ConflictResolver.ConflictItem(null, createNode("1.0.0.Alpha8"), 0, 0)); items.add(new ConflictResolver.ConflictItem(null, createNode("1.0.0.Beta3"), 0, 0)); ConflictResolver.ConflictContext context = new ConflictResolver.ConflictContext(null, null, null, items); selector.selectVersion(context); assertThat(context.getWinner().getNode().getVersion().toString()).isEqualTo("1.0.0.Beta3"); }
@Test public void finalVersionComparison() throws Exception { NewestVersionSelector selector = new NewestVersionSelector(); List<ConflictResolver.ConflictItem> items = new ArrayList<>(); items.add(new ConflictResolver.ConflictItem(null, createNode("1.0.0.CR1"), 0, 0)); items.add(new ConflictResolver.ConflictItem(null, createNode("1.0.0.Final"), 0, 0)); ConflictResolver.ConflictContext context = new ConflictResolver.ConflictContext(null, null, null, items); selector.selectVersion(context); assertThat(context.getWinner().getNode().getVersion().toString()).isEqualTo("1.0.0.Final"); }
@Test public void differentVersionComparison() throws Exception { NewestVersionSelector selector = new NewestVersionSelector(); List<ConflictResolver.ConflictItem> items = new ArrayList<>(); items.add(new ConflictResolver.ConflictItem(null, createNode("1.0.1.Alpha4"), 0, 0)); items.add(new ConflictResolver.ConflictItem(null, createNode("1.0.0.Final"), 0, 0)); ConflictResolver.ConflictContext context = new ConflictResolver.ConflictContext(null, null, null, items); selector.selectVersion(context); assertThat(context.getWinner().getNode().getVersion().toString()).isEqualTo("1.0.1.Alpha4"); }