Skip to content

Commit

Permalink
Fixed new version check
Browse files Browse the repository at this point in the history
  • Loading branch information
scavchik committed Sep 11, 2023
1 parent 3a84c9c commit 386e9bf
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@
import java.util.function.BiPredicate;

import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;

import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.substringAfter;
import static org.apache.commons.lang3.StringUtils.substringBefore;

public class NewVersionPredicate implements BiPredicate<String, String> {

private static final String VERSION_SEPARATOR = ".";

@Override
public boolean test(String newVersion, String currentVersion) {
newVersion = RegExUtils.replaceAll(newVersion, "[^\\d]", "");
currentVersion = RegExUtils.replaceAll(currentVersion, "[^\\d]", "");
int length = Math.max(newVersion.length(), currentVersion.length());
newVersion = StringUtils.rightPad(newVersion, length, '0');
currentVersion = StringUtils.rightPad(currentVersion, length, '0');
return Integer.parseInt(newVersion) > Integer.parseInt(currentVersion);
return verifyVersion(RegExUtils.replaceAll(newVersion, "[^\\d]", VERSION_SEPARATOR), RegExUtils.replaceAll(currentVersion, "[^\\d]", VERSION_SEPARATOR));
}

private boolean verifyVersion(String newVersion, String currentVersion) {
if (isBlank(newVersion) && isBlank(currentVersion)) {
return false;
}

int newVer = Integer.parseInt(defaultIfBlank(substringBefore(newVersion, VERSION_SEPARATOR), "0"));
int curVer = Integer.parseInt(defaultIfBlank(substringBefore(currentVersion, VERSION_SEPARATOR), "0"));
return newVer == curVer ? verifyVersion(substringAfter(newVersion, VERSION_SEPARATOR), substringAfter(currentVersion, VERSION_SEPARATOR)) : newVer > curVer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.engatec.vdl.service.newversion;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.assertj.core.api.Assertions.assertThat;

class NewVersionPredicateTest {

@ParameterizedTest
@CsvSource({
"1.0, 0.9",
"1.1, 1.0",
"1.1.3, 1.1.1",
"1.1.3.1, 1.1.3",
"1.1.10, 1.1.9",
"1.5.3, 1.4.9",
"1.10.1, 1.9.29",
"1.10.1, 1.10.0",
"12, 1.3.5"
})
void givenNewAndCurrentVersions_whenNewVersionIsHigher_thenReturnTrue(String newVersion, String currentVersion) {
boolean result = new NewVersionPredicate().test(newVersion, currentVersion);
assertThat(result).isTrue();
}

@ParameterizedTest
@CsvSource({
"0.9, 0.9",
"1.0, 1.0",
"1.1.3, 1.1.3",
"1.1.3.1, 1.1.3.1",
"1.10, 1.10",
"12, 12"
})
void givenNewAndCurrentVersions_whenVersionsEqual_thenReturnFalse(String newVersion, String currentVersion) {
boolean result = new NewVersionPredicate().test(newVersion, currentVersion);
assertThat(result).isFalse();
}

@ParameterizedTest
@CsvSource({
"0.9, 1.0",
"1.0, 1.1",
"1.1.1, 1.1.3",
"1.1.3, 1.1.3.1",
"1.1.9, 1.1.10",
"1.4.9, 1.5.3",
"1.9.29, 1.10.1",
"1.10.0, 1.10.1",
"1.3.5, 12"
})
void givenNewAndCurrentVersions_whenCurrentVersionIsHigher_thenReturnFalse(String newVersion, String currentVersion) {
boolean result = new NewVersionPredicate().test(newVersion, currentVersion);
assertThat(result).isFalse();
}
}

0 comments on commit 386e9bf

Please sign in to comment.