Merge pull request #9608 from SmartyAnsh/BAEL-4175_Comparing_version_Strings_in_Java

BAEL-4175 - comparing version strings in java
This commit is contained in:
Eric Martin 2020-07-13 10:54:11 -05:00 committed by GitHub
commit db13e0ec44
4 changed files with 223 additions and 0 deletions

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>core-java-string-operations-3</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-string-operations-3</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${maven-artifact.version}</version>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-core</artifactId>
<version>${gradle-core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-core.version}</version>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
<version>${semver4j.version}</version>
</dependency>
</dependencies>
<properties>
<assertj.version>3.6.1</assertj.version>
<maven-artifact.version>3.6.3</maven-artifact.version>
<gradle-core.version>6.1.1</gradle-core.version>
<jackson-core.version>2.11.1</jackson-core.version>
<semver4j.version>3.1.0</semver4j.version>
</properties>
<repositories>
<repository>
<id>gradle-repo</id>
<url>https://repo.gradle.org/gradle/libs-releases-local/</url>
</repository>
</repositories>
</project>

View File

@ -0,0 +1,25 @@
package com.baeldung.versioncomparison;
public class VersionCompare {
public static int compareVersions(String version1, String version2) {
int comparisonResult = 0;
String[] version1Splits = version1.split("\\.");
String[] version2Splits = version2.split("\\.");
int maxLengthOfVersionSplits = Math.max(version1Splits.length, version2Splits.length);
for (int i = 0; i < maxLengthOfVersionSplits; i++){
Integer v1 = i < version1Splits.length ? Integer.parseInt(version1Splits[i]) : 0;
Integer v2 = i < version2Splits.length ? Integer.parseInt(version2Splits[i]) : 0;
int compare = v1.compareTo(v2);
if (compare != 0) {
comparisonResult = compare;
break;
}
}
return comparisonResult;
}
}

View File

@ -0,0 +1,136 @@
package com.baeldung.versioncomparison;
import org.junit.Test;
import com.fasterxml.jackson.core.Version;
import com.vdurmont.semver4j.Semver;
import com.vdurmont.semver4j.Semver.VersionDiff;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.gradle.util.VersionNumber;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class VersionComparisonUnitTest {
@Test
public void givenVersionStrings_whenUsingMavenArtifact_thenCompareVersions() {
ComparableVersion version1_1 = new ComparableVersion("1.1");
ComparableVersion version1_2 = new ComparableVersion("1.2");
ComparableVersion version1_3 = new ComparableVersion("1.3");
assertTrue(version1_1.compareTo(version1_2) < 0);
assertTrue(version1_3.compareTo(version1_2) > 0);
ComparableVersion version1_1_0 = new ComparableVersion("1.1.0");
assertEquals(0, version1_1.compareTo(version1_1_0));
ComparableVersion version1_1_alpha = new ComparableVersion("1.1-alpha");
assertTrue(version1_1.compareTo(version1_1_alpha) > 0);
ComparableVersion version1_1_beta = new ComparableVersion("1.1-beta");
ComparableVersion version1_1_milestone = new ComparableVersion("1.1-milestone");
ComparableVersion version1_1_rc = new ComparableVersion("1.1-rc");
ComparableVersion version1_1_snapshot = new ComparableVersion("1.1-snapshot");
assertTrue(version1_1_alpha.compareTo(version1_1_beta) < 0);
assertTrue(version1_1_beta.compareTo(version1_1_milestone) < 0);
assertTrue(version1_1_rc.compareTo(version1_1_snapshot) < 0);
assertTrue(version1_1_snapshot.compareTo(version1_1) < 0);
ComparableVersion version1_1_c = new ComparableVersion("1.1-c");
ComparableVersion version1_1_z = new ComparableVersion("1.1-z");
ComparableVersion version1_1_1 = new ComparableVersion("1.1.1");
assertTrue(version1_1_c.compareTo(version1_1_z) < 0);
assertTrue(version1_1_z.compareTo(version1_1_1) < 0);
}
@Test
public void givenVersionStrings_whenUsingGradle_thenCompareVersions() {
VersionNumber version1_1 = VersionNumber.parse("1.1");
VersionNumber version1_2 = VersionNumber.parse("1.2");
VersionNumber version1_3 = VersionNumber.parse("1.3");
assertTrue(version1_1.compareTo(version1_2) < 0);
assertTrue(version1_3.compareTo(version1_2) > 0);
VersionNumber version1_1_0 = VersionNumber.parse("1.1.0");
assertEquals(0, version1_1.compareTo(version1_1_0));
VersionNumber version1_1_1_1_alpha = VersionNumber.parse("1.1.1.1-alpha");
assertTrue(version1_1.compareTo(version1_1_1_1_alpha) < 0);
VersionNumber version1_1_beta = VersionNumber.parse("1.1.0.0-beta");
assertTrue(version1_1_beta.compareTo(version1_1_1_1_alpha) < 0);
VersionNumber version1_1_1_snapshot = VersionNumber.parse("1.1.1-snapshot");
assertTrue(version1_1_1_1_alpha.compareTo(version1_1_1_snapshot) < 0);
}
@Test
public void givenVersionStrings_whenUsingJackson_thenCompareVersions() {
Version version1_1 = new Version(1, 1, 0, null, null, null);
Version version1_2 = new Version(1, 2, 0, null, null, null);
Version version1_3 = new Version(1, 3, 0, null, null, null);
assertTrue(version1_1.compareTo(version1_2) < 0);
assertTrue(version1_3.compareTo(version1_2) > 0);
Version version1_1_1 = new Version(1, 1, 1, null, null, null);
assertTrue(version1_1.compareTo(version1_1_1) < 0);
Version version1_1_maven = new Version(1, 1, 0, null, "org.apache.maven", null);
Version version1_1_gradle = new Version(1, 1, 0, null, "org.gradle", null);
assertTrue(version1_1_maven.compareTo(version1_1_gradle) < 0);
Version version1_1_snapshot = new Version(1, 1, 0, "snapshot", null, null);
assertEquals(0, version1_1.compareTo(version1_1_snapshot));
assertTrue(version1_1_snapshot.isSnapshot());
}
@Test
public void givenVersionStrings_whenUsingSemver_thenCompareVersions() {
Semver version1_1 = new Semver("1.1.0");
Semver version1_2 = new Semver("1.2.0");
Semver version1_3 = new Semver("1.3.0");
assertTrue(version1_1.compareTo(version1_2) < 0);
assertTrue(version1_3.compareTo(version1_2) > 0);
Semver version1_1_alpha = new Semver("1.1.0-alpha");
assertTrue(version1_1.isGreaterThan(version1_1_alpha));
Semver version1_1_beta = new Semver("1.1.0-beta");
Semver version1_1_milestone = new Semver("1.1.0-milestone");
Semver version1_1_rc = new Semver("1.1.0-rc");
Semver version1_1_snapshot = new Semver("1.1.0-snapshot");
assertTrue(version1_1_alpha.isLowerThan(version1_1_beta));
assertTrue(version1_1_beta.compareTo(version1_1_milestone) < 0);
assertTrue(version1_1_rc.compareTo(version1_1_snapshot) < 0);
assertTrue(version1_1_snapshot.compareTo(version1_1) < 0);
assertTrue(version1_1.isEqualTo("1.1.0"));
assertEquals(VersionDiff.MAJOR, version1_1.diff("2.1.0"));
assertEquals(VersionDiff.MINOR, version1_1.diff("1.2.3"));
assertEquals(VersionDiff.PATCH, version1_1.diff("1.1.1"));
assertTrue(version1_1.isStable());
assertFalse(version1_1_alpha.isStable());
}
@Test
public void givenVersionStrings_whenUsingCustomVersionCompare_thenCompareVersions() {
assertTrue(VersionCompare.compareVersions("1.0.1", "1.1.2") < 0);
assertTrue(VersionCompare.compareVersions("1.0.1", "1.10") < 0);
assertTrue(VersionCompare.compareVersions("1.1.2", "1.0.1") > 0);
assertTrue(VersionCompare.compareVersions("1.1.2", "1.2") < 0);
assertEquals(0, VersionCompare.compareVersions("1.3.0", "1.3"));
}
}

View File

@ -126,6 +126,7 @@
<module>core-java-string-conversions-2</module> <module>core-java-string-conversions-2</module>
<module>core-java-string-operations</module> <module>core-java-string-operations</module>
<module>core-java-string-operations-2</module> <module>core-java-string-operations-2</module>
<module>core-java-string-operations-3</module>
<module>core-java-strings</module> <module>core-java-strings</module>
<module>core-java-sun</module> <module>core-java-sun</module>