Skip to content

Commit

Permalink
[MNG-7700] test some edge cases with leading zeroes (#1014)
Browse files Browse the repository at this point in the history
* add tests for MNG-7700
  • Loading branch information
elharo authored Mar 2, 2023
1 parent da64ee9 commit b9b6d85
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
* over {@code 1.0.0.X1}.</li>
* </ul>
*
* @see <a href="https://cwiki.apache.org/confluence/display/MAVENOLD/Versioning">"Versioning" on Maven Wiki</a>
* @see <a href="https://maven.apache.org/pom.html#version-order-specification">"Versioning" in the POM reference</a>
* @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
* @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
*/
@SuppressWarnings("unchecked")
public class ComparableVersionTest {
private Comparable newComparable(String version) {
private ComparableVersion newComparable(String version) {
ComparableVersion ret = new ComparableVersion(version);
String canonical = ret.getCanonical();
String parsedCanonical = new ComparableVersion(canonical).getCanonical();

System.out.println("canonical( " + version + " ) = " + canonical);
assertEquals(
canonical,
parsedCanonical,
Expand Down Expand Up @@ -95,11 +94,11 @@ private void checkVersionsOrder(String[] versions) {
private void checkVersionsEqual(String v1, String v2) {
Comparable c1 = newComparable(v1);
Comparable c2 = newComparable(v2);
assertTrue(c1.compareTo(c2) == 0, "expected " + v1 + " == " + v2);
assertTrue(c2.compareTo(c1) == 0, "expected " + v2 + " == " + v1);
assertTrue(c1.hashCode() == c2.hashCode(), "expected same hashcode for " + v1 + " and " + v2);
assertTrue(c1.equals(c2), "expected " + v1 + ".equals( " + v2 + " )");
assertTrue(c2.equals(c1), "expected " + v2 + ".equals( " + v1 + " )");
assertEquals(0, c1.compareTo(c2), "expected " + v1 + " == " + v2);
assertEquals(0, c2.compareTo(c1), "expected " + v2 + " == " + v1);
assertEquals(c1.hashCode(), c2.hashCode(), "expected same hashcode for " + v1 + " and " + v2);
assertEquals(c1, c2, "expected " + v1 + ".equals( " + v2 + " )");
assertEquals(c2, c1, "expected " + v2 + ".equals( " + v1 + " )");
}

private void checkVersionsArrayEqual(String[] array) {
Expand Down Expand Up @@ -209,6 +208,26 @@ public void testVersionComparing() {
checkVersionsOrder("2.0.1-xyz", "2.0.1-123");
}

@Test
public void testLeadingZeroes() {
checkVersionsOrder("0.7", "2");
checkVersionsOrder("0.2", "1.0.7");
}

@Test
public void testGetCanonical() {
// MNG-7700
newComparable("0.x");
newComparable("0-x");
newComparable("0.rc");
newComparable("0-1");

ComparableVersion version = new ComparableVersion("0.x");
assertEquals("x", version.getCanonical());
ComparableVersion version2 = new ComparableVersion("0.2");
assertEquals("0.2", version2.getCanonical());
}

/**
* Test <a href="https://issues.apache.org/jira/browse/MNG-5568">MNG-5568</a> edge case
* which was showing transitive inconsistency: since A &gt; B and B &gt; C then we should have A &gt; C
Expand Down Expand Up @@ -343,7 +362,7 @@ public void testReuse() {
ComparableVersion c1 = new ComparableVersion("1");
c1.parseVersion("2");

Comparable c2 = newComparable("2");
Comparable<?> c2 = newComparable("2");

assertEquals(c1, c2, "reused instance should be equivalent to new instance");
}
Expand Down

0 comments on commit b9b6d85

Please sign in to comment.