DATACMNS-384 - Polished toString() representation of Version.
Trailing zeros now get removed up until the minor version number.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package org.springframework.data.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Value object to represent a Version consisting of major, minor and bugfix part.
|
||||
@@ -176,6 +180,19 @@ public class Version implements Comparable<Version> {
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%d.%d.%d.%d", major, minor, bugfix, build);
|
||||
|
||||
List<Integer> digits = new ArrayList<Integer>();
|
||||
digits.add(major);
|
||||
digits.add(minor);
|
||||
|
||||
if (build != 0 || bugfix != 0) {
|
||||
digits.add(bugfix);
|
||||
}
|
||||
|
||||
if (build != 0) {
|
||||
digits.add(build);
|
||||
}
|
||||
|
||||
return StringUtils.collectionToDelimitedString(digits, ".");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,4 +111,16 @@ public class VersionUnitTests {
|
||||
assertThat(nextBugfix.compareTo(version), is(greaterThan(0)));
|
||||
assertThat(nextBuild.compareTo(version), is(greaterThan(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removesTrailingZerosAfterSecondValueForToString() {
|
||||
|
||||
assertThat(new Version(2).toString(), is("2.0"));
|
||||
assertThat(new Version(2, 0).toString(), is("2.0"));
|
||||
assertThat(new Version(2, 0, 0).toString(), is("2.0"));
|
||||
assertThat(new Version(2, 0, 0, 0).toString(), is("2.0"));
|
||||
assertThat(new Version(2, 0, 1).toString(), is("2.0.1"));
|
||||
assertThat(new Version(2, 0, 1, 0).toString(), is("2.0.1"));
|
||||
assertThat(new Version(2, 0, 0, 1).toString(), is("2.0.0.1"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user