Skip to content

Commit b319475

Browse files
sbrannenmarcphilipp
authored andcommitted
Make version parsing in JUnit4VersionCheck more lenient
Prior to this commit, JUnit4VersionCheck failed to parse custom JUnit 4 version IDs if they did not match the pattern #.# (e.g., 4.12, 4.13, etc.). This commit relaxes the regular expression used to parse version IDs in order to support custom version ID formats such as `4.12.0`, `4.12-patch_1`, etc. Closes #2198
1 parent 3ffde1a commit b319475

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.6.3.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ No changes.
2727

2828
==== Bug Fixes
2929

30-
* ❓
30+
* The internal `JUnit4VersionCheck` class -- which verifies that a supported version of
31+
JUnit 4 is on the classpath -- now implements a lenient version ID parsing algorithm in
32+
order to support custom version ID formats such as `4.13.1`, `4.13-patch_1`, etc.

junit-vintage-engine/src/main/java/org/junit/vintage/engine/JUnit4VersionCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
class JUnit4VersionCheck {
2727

28-
private static final Pattern versionPattern = Pattern.compile("^(\\d+\\.\\d+)(?:-.+)?");
28+
private static final Pattern versionPattern = Pattern.compile("^(\\d+\\.\\d+).*");
2929
private static final BigDecimal minVersion = new BigDecimal("4.12");
3030

3131
static void checkSupported() {

junit-vintage-engine/src/test/java/org/junit/vintage/engine/JUnit4VersionCheckTests.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.vintage.engine;
1212

13+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1314
import static org.junit.jupiter.api.Assertions.assertEquals;
1415
import static org.junit.jupiter.api.Assertions.assertSame;
1516
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -22,6 +23,35 @@
2223
*/
2324
class JUnit4VersionCheckTests {
2425

26+
/**
27+
* @since 5.7
28+
*/
29+
@Test
30+
void handlesParsingSupportedVersionIdWithStandardVersionFormat() {
31+
assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.12"));
32+
assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.13"));
33+
}
34+
35+
/**
36+
* @since 5.7
37+
*/
38+
@Test
39+
void handlesParsingSupportedVersionIdWithCustomizedVersionFormat() {
40+
assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.12-patch_1"));
41+
assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.12.0"));
42+
assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.12.0.1"));
43+
assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.12.0.patch-042"));
44+
}
45+
46+
@Test
47+
void throwsExceptionForUnsupportedVersion() {
48+
JUnitException exception = assertThrows(JUnitException.class,
49+
() -> JUnit4VersionCheck.checkSupported(() -> "4.11"));
50+
51+
assertEquals("Unsupported version of junit:junit: 4.11. Please upgrade to version 4.12 or later.",
52+
exception.getMessage());
53+
}
54+
2555
@Test
2656
void handlesErrorsReadingVersion() {
2757
Error error = new NoClassDefFoundError();
@@ -42,13 +72,4 @@ void handlesErrorsParsingVersion() {
4272
assertEquals("Failed to parse version of junit:junit: not a version", exception.getMessage());
4373
}
4474

45-
@Test
46-
void throwsExceptionOnUnsupportedVersion() {
47-
JUnitException exception = assertThrows(JUnitException.class,
48-
() -> JUnit4VersionCheck.checkSupported(() -> "4.11"));
49-
50-
assertEquals("Unsupported version of junit:junit: 4.11. Please upgrade to version 4.12 or later.",
51-
exception.getMessage());
52-
}
53-
5475
}

0 commit comments

Comments
 (0)