Skip to content

Commit

Permalink
Resolves #929: "numeric" version comparator requires all versions to …
Browse files Browse the repository at this point in the history
…produce the String representation of the version in their #toString method

Otherwise numeric comparison will not work properly. Amended BoundArtifactVersion
  • Loading branch information
andrzejj0 authored and slawekjaranowski committed Mar 14, 2023
1 parent 841d24f commit 7ed5bd4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,15 @@ public String getQualifier() {
public void parseVersion(String version) {
throw new UnsupportedOperationException();
}

/**
* {@inheritDoc}
*
* Quasi-contract: {@link #toString()} must produce the textual representation of the version, without any
* additional items, this is required by the implementation of {@link NumericVersionComparator}.
*/
@Override
public String toString() {
return comparable.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
*/

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;

/**
Expand Down Expand Up @@ -87,4 +89,10 @@ public void testVersionComparatorRow7() {
assertThat(instance.compare(version("1-alpha-1"), version("2-alpha-1")), lessThan(0));
assertThat(instance.compare(version("1-alpha-1"), version("1-alpha-2")), lessThan(0));
}

@Test
public void testBoundArtifactVersion() {
assertThat(
instance.compare(new BoundArtifactVersion("1.0.0", Segment.MAJOR), version("1.9.9")), greaterThan(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;

import org.codehaus.mojo.versions.change.DefaultDependencyVersionChange;
import org.codehaus.mojo.versions.model.RuleSet;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
import org.codehaus.mojo.versions.utils.TestUtils;
import org.junit.Test;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItem;
Expand Down Expand Up @@ -88,4 +91,24 @@ public void testChangesNotRegisteredIfNoUpdatesInPom() throws Exception {
mojo.execute();
assertThat(changeRecorder.getChanges(), is(empty()));
}

@Test
public void testIssue929() throws Exception {
TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/update-properties/issue-929"), pomDir);
UpdatePropertiesMojo mojo = setUpMojo("update-properties");
mojo.allowMajorUpdates = false;
mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
put("artifactA", new String[] {"6.2.5.Final", "8.0.0.Final"});
}
});
mojo.ruleSet = new RuleSet() {
{
setComparisonMethod("numeric");
}
};

mojo.execute();
assertThat(changeRecorder.getChanges(), is(empty()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>default-group</groupId>
<artifactId>default-artifact</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<properties>
<artifact-version>6.2.5.Final</artifact-version>
</properties>

<dependencies>
<dependency>
<groupId>default-group</groupId>
<artifactId>artifactA</artifactId>
<version>${artifact-version}</version>
</dependency>
</dependencies>

</project>

0 comments on commit 7ed5bd4

Please sign in to comment.