Skip to content

Commit

Permalink
[MENFORCER-440] Fix pattern for Java 8 version
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Jan 26, 2023
1 parent 4bbf7cd commit 84e0363
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,24 @@ static Stream<Arguments> 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
Expand Down

0 comments on commit 84e0363

Please sign in to comment.