From f30c96a9cad89a62a28750c5fe4dd83ad4333e99 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 27 Feb 2020 13:24:59 +0100 Subject: [PATCH] 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 --- .../release-notes/release-notes-5.7.0-M1.adoc | 4 +- .../vintage/engine/JUnit4VersionCheck.java | 2 +- .../engine/JUnit4VersionCheckTests.java | 39 ++++++++++++++----- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/documentation/src/docs/asciidoc/release-notes/release-notes-5.7.0-M1.adoc b/documentation/src/docs/asciidoc/release-notes/release-notes-5.7.0-M1.adoc index 8abcb332b315..cf4db517ad99 100644 --- a/documentation/src/docs/asciidoc/release-notes/release-notes-5.7.0-M1.adoc +++ b/documentation/src/docs/asciidoc/release-notes/release-notes-5.7.0-M1.adoc @@ -64,4 +64,6 @@ on GitHub. ==== New Features and Improvements -* ❓ +* The internal `JUnit4VersionCheck` class -- which verifies that a supported version of + JUnit 4 is on the classpath -- now implements a lenient version ID parsing algorithm in + order to support custom version ID formats such as `4.12.0`, `4.12-patch_1`, etc. diff --git a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/JUnit4VersionCheck.java b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/JUnit4VersionCheck.java index edb66fc1c1d2..5af186c794dc 100644 --- a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/JUnit4VersionCheck.java +++ b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/JUnit4VersionCheck.java @@ -25,7 +25,7 @@ */ class JUnit4VersionCheck { - private static final Pattern versionPattern = Pattern.compile("^(\\d+\\.\\d+)(?:-.+)?"); + private static final Pattern versionPattern = Pattern.compile("^(\\d+\\.\\d+).*"); private static final BigDecimal minVersion = new BigDecimal("4.12"); static void checkSupported() { diff --git a/junit-vintage-engine/src/test/java/org/junit/vintage/engine/JUnit4VersionCheckTests.java b/junit-vintage-engine/src/test/java/org/junit/vintage/engine/JUnit4VersionCheckTests.java index 3da9ce8c02a5..13937da6f6e3 100644 --- a/junit-vintage-engine/src/test/java/org/junit/vintage/engine/JUnit4VersionCheckTests.java +++ b/junit-vintage-engine/src/test/java/org/junit/vintage/engine/JUnit4VersionCheckTests.java @@ -10,6 +10,7 @@ package org.junit.vintage.engine; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -22,6 +23,35 @@ */ class JUnit4VersionCheckTests { + /** + * @since 5.7 + */ + @Test + void handlesParsingSupportedVersionIdWithStandardVersionFormat() { + assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.12")); + assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.13")); + } + + /** + * @since 5.7 + */ + @Test + void handlesParsingSupportedVersionIdWithCustomizedVersionFormat() { + assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.12-patch_1")); + assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.12.0")); + assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.12.0.1")); + assertDoesNotThrow(() -> JUnit4VersionCheck.checkSupported(() -> "4.12.0.patch-042")); + } + + @Test + void throwsExceptionForUnsupportedVersion() { + JUnitException exception = assertThrows(JUnitException.class, + () -> JUnit4VersionCheck.checkSupported(() -> "4.11")); + + assertEquals("Unsupported version of junit:junit: 4.11. Please upgrade to version 4.12 or later.", + exception.getMessage()); + } + @Test void handlesErrorsReadingVersion() { Error error = new NoClassDefFoundError(); @@ -42,13 +72,4 @@ void handlesErrorsParsingVersion() { assertEquals("Failed to parse version of junit:junit: not a version", exception.getMessage()); } - @Test - void throwsExceptionOnUnsupportedVersion() { - JUnitException exception = assertThrows(JUnitException.class, - () -> JUnit4VersionCheck.checkSupported(() -> "4.11")); - - assertEquals("Unsupported version of junit:junit: 4.11. Please upgrade to version 4.12 or later.", - exception.getMessage()); - } - }