From 422648aed33589f18f8918826344a42a33948bf0 Mon Sep 17 00:00:00 2001 From: Vaadin Bot Date: Wed, 30 Jun 2021 14:56:46 +0200 Subject: [PATCH] fix: fixes comparison of prerelease versions FE deps (#11353) (#11364) (#11366) Fix buildIdentifier comparison so values compare correctly. The problem caused e.g. alphas greater than 10 to be considered older than 2-9. fixes #11352 Co-authored-by: caalador --- .../flow/server/frontend/FrontendVersion.java | 35 ++++++++++++- .../server/frontend/FrontendVersionTest.java | 50 +++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/frontend/FrontendVersion.java b/flow-server/src/main/java/com/vaadin/flow/server/frontend/FrontendVersion.java index 796b7d60321..62cba6d64f5 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/frontend/FrontendVersion.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/frontend/FrontendVersion.java @@ -17,6 +17,8 @@ import java.io.Serializable; import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Version object for frontend versions comparison and handling. @@ -28,6 +30,13 @@ public class FrontendVersion implements Serializable, Comparable { + /** + * Parses the buildIdentifier to String + Integer. For instance beta1 + * returns 'beta' and '1' + */ + private final Pattern buildIdentifierParser = Pattern + .compile("(\\D*)(\\d*)"); + /** * The version number of this release. For example "6.2.0". Always in the * format "major.minor.revision[.build]". The build part is optional. All of @@ -288,11 +297,35 @@ public int compareTo(FrontendVersion other) { && other.buildIdentifier.isEmpty()) { return -1; } - return buildIdentifier.compareToIgnoreCase(other.buildIdentifier); + return compareBuildIdentifier(other); } return 0; } + private int compareBuildIdentifier(FrontendVersion other) { + final Matcher thisMatcher = buildIdentifierParser + .matcher(buildIdentifier); + final Matcher otherMatcher = buildIdentifierParser + .matcher(other.buildIdentifier); + if (thisMatcher.find() && otherMatcher.find()) { + if (thisMatcher.group(1) + .compareToIgnoreCase(otherMatcher.group(1)) != 0) { + // If we do not have a text identifier assume newer + // If other doesn't have text identifier assume older + if (thisMatcher.group(1).isEmpty()) { + return 1; + } else if (otherMatcher.group(1).isEmpty()) { + return -1; + } + return thisMatcher.group(1) + .compareToIgnoreCase(otherMatcher.group(1)); + } + return Integer.parseInt(thisMatcher.group(2)) + - Integer.parseInt(otherMatcher.group(2)); + } + return buildIdentifier.compareToIgnoreCase(other.buildIdentifier); + } + private String getInvalidVersionMessage(String version) { return String.format("'%s' is not a valid version!", version); } diff --git a/flow-server/src/test/java/com/vaadin/flow/server/frontend/FrontendVersionTest.java b/flow-server/src/test/java/com/vaadin/flow/server/frontend/FrontendVersionTest.java index 452704a91ff..90a745bc2ac 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/frontend/FrontendVersionTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/frontend/FrontendVersionTest.java @@ -199,6 +199,56 @@ public void newerVersionByBuildIdentifier() { test.isNewerThan(new FrontendVersion("2.0.0-RC1"))); } + @Test + public void buildIdentifierNumbers_returnsAsExpected() { + + FrontendVersion test = new FrontendVersion("2.0.0-alpha6"); + assertTrue("2.0.0-alpha6 should be older than 2.0.0-alpha13", + test.isOlderThan(new FrontendVersion("2.0.0-alpha13"))); + + test = new FrontendVersion("2.0.0-alpha20"); + assertTrue("2.0.0-alpha20 should be newer than 2.0.0-alpha13", + test.isNewerThan(new FrontendVersion("2.0.0-alpha13"))); + assertFalse("2.0.0-alpha20 should be newer than 2.0.0-alpha13", + test.isOlderThan(new FrontendVersion("2.0.0-alpha13"))); + + assertTrue("2.0.0-alpha13 should not be older than 2.0.0-alpha20", + new FrontendVersion("2.0.0-alpha13").isOlderThan(test)); + assertFalse("2.0.0-alpha13 should not be older than 2.0.0-alpha20", + new FrontendVersion("2.0.0-alpha13").isNewerThan(test)); + + assertTrue("same versions should equal", + test.isEqualTo(new FrontendVersion("2.0.0.alpha20"))); + } + + @Test + public void testAgainstVersionWithValueInBuildInfo() { + FrontendVersion alpha3 = new FrontendVersion("2.0.0-alpha3"); + FrontendVersion five = new FrontendVersion("2.0.0.5"); + FrontendVersion fifteen = new FrontendVersion("2.0.0.15"); + + assertTrue("2.0.0-alpha3 should be older than 2.0.0.5", + alpha3.isOlderThan(five)); + assertFalse("2.0.0-alpha3 should be older than 2.0.0.5", + alpha3.isNewerThan(five)); + + assertTrue("2.0.0.5 should be newer than 2.0.0-alpha3", + five.isNewerThan(alpha3)); + assertFalse("2.0.0.5 should be newer than 2.0.0-alpha3", + five.isOlderThan(alpha3)); + + assertTrue("2.0.0.5 should be older than 2.0.0.15", + five.isOlderThan(fifteen)); + assertFalse("2.0.0.5 should be older than 2.0.0.15", + five.isNewerThan(fifteen)); + + assertTrue("2.0.0.15 should be newer than 2.0.0.5", + fifteen.isNewerThan(five)); + assertFalse("2.0.0.15 should be newer than 2.0.0.5", + fifteen.isOlderThan(five)); + + } + private void assertVersion(FrontendVersion version, int major, int minor, int revision, String build) { Assert.assertEquals(