From 84e0363287b01e1817db72fc5c3f3ce8f77a0e5c Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Thu, 26 Jan 2023 00:47:55 +0100 Subject: [PATCH] [MENFORCER-440] Fix pattern for Java 8 version --- .../enforcer/rules/version/RequireJavaVersion.java | 13 +++++++++++-- .../rules/version/TestRequireJavaVersion.java | 12 +++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/RequireJavaVersion.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/RequireJavaVersion.java index dcaeb686..1a37c497 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/RequireJavaVersion.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/version/RequireJavaVersion.java @@ -42,7 +42,7 @@ @Named("requireJavaVersion") public final class RequireJavaVersion extends AbstractVersionEnforcer { - private static final Pattern JDK8_VERSION_PATTERN = Pattern.compile("([\\[(,]?)(1\\.8|8)([]),]?)"); + private static final Pattern JDK8_VERSION_PATTERN = Pattern.compile("([\\d.]+)"); /** * Display the normalized JDK version. @@ -57,11 +57,20 @@ public void setVersion(String theVersion) { return; } + if (!theVersion.contains("8")) { + super.setVersion(theVersion); + return; + } + Matcher matcher = JDK8_VERSION_PATTERN.matcher(theVersion); StringBuffer result = new StringBuffer(); while (matcher.find()) { - matcher.appendReplacement(result, "$11.8$3"); + if ("8".equals(matcher.group(1))) { + matcher.appendReplacement(result, "1.8"); + } else { + matcher.appendReplacement(result, "$1"); + } } matcher.appendTail(result); diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestRequireJavaVersion.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestRequireJavaVersion.java index 38bb3433..419dfac1 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestRequireJavaVersion.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestRequireJavaVersion.java @@ -140,14 +140,24 @@ static Stream fixJava8ShortVersion() { return Stream.of( Arguments.of("1.8", "1.8"), Arguments.of("8", "1.8"), + Arguments.of(".8", ".8"), + Arguments.of("8.", "8."), Arguments.of("8,)", "1.8,)"), Arguments.of("[8,)", "[1.8,)"), Arguments.of("(1.7,8]", "(1.7,1.8]"), Arguments.of("[1.8,)", "[1.8,)"), Arguments.of("(1.8,8]", "(1.8,1.8]"), + Arguments.of("(8,8]", "(1.8,1.8]"), Arguments.of("(8,)", "(1.8,)"), Arguments.of("[8]", "[1.8]"), - Arguments.of("(9,11],[8]", "(9,11],[1.8]")); + Arguments.of("(9,11],[8]", "(9,11],[1.8]"), + Arguments.of("(11.0.18", "(11.0.18"), + Arguments.of("(15.1.8", "(15.1.8"), + Arguments.of("(15.1.2", "(15.1.2"), + Arguments.of("18", "18"), + Arguments.of("18.", "18."), + Arguments.of(".18", ".18"), + Arguments.of("38", "38")); } @ParameterizedTest