Skip to content

Commit

Permalink
Make version parsing in JUnit4VersionCheck more lenient
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbrannen committed Feb 27, 2020
1 parent 4359d97 commit f30c96a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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());
}

}

1 comment on commit f30c96a

@marcphilipp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.