Skip to content

Commit d79a88d

Browse files
authored
fix: fixes comparison of prerelease versions FE deps (#11353)
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
1 parent 0f62620 commit d79a88d

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/frontend/FrontendVersion.java‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.io.Serializable;
1919
import java.util.Objects;
20+
import java.util.regex.Matcher;
21+
import java.util.regex.Pattern;
2022

2123
/**
2224
* Version object for frontend versions comparison and handling.
@@ -28,6 +30,13 @@
2830
public class FrontendVersion
2931
implements Serializable, Comparable<FrontendVersion> {
3032

33+
/**
34+
* Parses the buildIdentifier to String + Integer. For instance beta1
35+
* returns 'beta' and '1'
36+
*/
37+
private final Pattern buildIdentifierParser = Pattern
38+
.compile("(\\D*)(\\d*)");
39+
3140
/**
3241
* The version number of this release. For example "6.2.0". Always in the
3342
* format "major.minor.revision[.build]". The build part is optional. All of
@@ -306,11 +315,35 @@ public int compareTo(FrontendVersion other) {
306315
&& other.buildIdentifier.isEmpty()) {
307316
return -1;
308317
}
309-
return buildIdentifier.compareToIgnoreCase(other.buildIdentifier);
318+
return compareBuildIdentifier(other);
310319
}
311320
return 0;
312321
}
313322

323+
private int compareBuildIdentifier(FrontendVersion other) {
324+
final Matcher thisMatcher = buildIdentifierParser
325+
.matcher(buildIdentifier);
326+
final Matcher otherMatcher = buildIdentifierParser
327+
.matcher(other.buildIdentifier);
328+
if (thisMatcher.find() && otherMatcher.find()) {
329+
if (thisMatcher.group(1)
330+
.compareToIgnoreCase(otherMatcher.group(1)) != 0) {
331+
// If we do not have a text identifier assume newer
332+
// If other doesn't have text identifier assume older
333+
if (thisMatcher.group(1).isEmpty()) {
334+
return 1;
335+
} else if (otherMatcher.group(1).isEmpty()) {
336+
return -1;
337+
}
338+
return thisMatcher.group(1)
339+
.compareToIgnoreCase(otherMatcher.group(1));
340+
}
341+
return Integer.parseInt(thisMatcher.group(2))
342+
- Integer.parseInt(otherMatcher.group(2));
343+
}
344+
return buildIdentifier.compareToIgnoreCase(other.buildIdentifier);
345+
}
346+
314347
private String getInvalidVersionMessage(String name, String version) {
315348
if (name != null) {
316349
return String.format("'%s' is not a valid version for '%s'!",

‎flow-server/src/test/java/com/vaadin/flow/server/frontend/FrontendVersionTest.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,56 @@ public void newerVersionByBuildIdentifier() {
199199
test.isNewerThan(new FrontendVersion("2.0.0-RC1")));
200200
}
201201

202+
@Test
203+
public void buildIdentifierNumbers_returnsAsExpected() {
204+
205+
FrontendVersion test = new FrontendVersion("2.0.0-alpha6");
206+
assertTrue("2.0.0-alpha6 should be older than 2.0.0-alpha13",
207+
test.isOlderThan(new FrontendVersion("2.0.0-alpha13")));
208+
209+
test = new FrontendVersion("2.0.0-alpha20");
210+
assertTrue("2.0.0-alpha20 should be newer than 2.0.0-alpha13",
211+
test.isNewerThan(new FrontendVersion("2.0.0-alpha13")));
212+
assertFalse("2.0.0-alpha20 should be newer than 2.0.0-alpha13",
213+
test.isOlderThan(new FrontendVersion("2.0.0-alpha13")));
214+
215+
assertTrue("2.0.0-alpha13 should not be older than 2.0.0-alpha20",
216+
new FrontendVersion("2.0.0-alpha13").isOlderThan(test));
217+
assertFalse("2.0.0-alpha13 should not be older than 2.0.0-alpha20",
218+
new FrontendVersion("2.0.0-alpha13").isNewerThan(test));
219+
220+
assertTrue("same versions should equal",
221+
test.isEqualTo(new FrontendVersion("2.0.0.alpha20")));
222+
}
223+
224+
@Test
225+
public void testAgainstVersionWithValueInBuildInfo() {
226+
FrontendVersion alpha3 = new FrontendVersion("2.0.0-alpha3");
227+
FrontendVersion five = new FrontendVersion("2.0.0.5");
228+
FrontendVersion fifteen = new FrontendVersion("2.0.0.15");
229+
230+
assertTrue("2.0.0-alpha3 should be older than 2.0.0.5",
231+
alpha3.isOlderThan(five));
232+
assertFalse("2.0.0-alpha3 should be older than 2.0.0.5",
233+
alpha3.isNewerThan(five));
234+
235+
assertTrue("2.0.0.5 should be newer than 2.0.0-alpha3",
236+
five.isNewerThan(alpha3));
237+
assertFalse("2.0.0.5 should be newer than 2.0.0-alpha3",
238+
five.isOlderThan(alpha3));
239+
240+
assertTrue("2.0.0.5 should be older than 2.0.0.15",
241+
five.isOlderThan(fifteen));
242+
assertFalse("2.0.0.5 should be older than 2.0.0.15",
243+
five.isNewerThan(fifteen));
244+
245+
assertTrue("2.0.0.15 should be newer than 2.0.0.5",
246+
fifteen.isNewerThan(five));
247+
assertFalse("2.0.0.15 should be newer than 2.0.0.5",
248+
fifteen.isOlderThan(five));
249+
250+
}
251+
202252
private void assertVersion(FrontendVersion version, int major, int minor,
203253
int revision, String build) {
204254
Assert.assertEquals(

0 commit comments

Comments
 (0)