diff --git a/enforcer-rules/pom.xml b/enforcer-rules/pom.xml index a974cd42..1116ce06 100644 --- a/enforcer-rules/pom.xml +++ b/enforcer-rules/pom.xml @@ -102,6 +102,11 @@ 2.2 test + + org.junit.jupiter + junit-jupiter-params + test + org.junit.jupiter junit-jupiter-engine diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractVersionEnforcer.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractVersionEnforcer.java index 5883ad1c..609de91d 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractVersionEnforcer.java +++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractVersionEnforcer.java @@ -159,7 +159,7 @@ public final String getVersion() { * * @param theVersion the required version to set */ - public final void setVersion(String theVersion) { + public void setVersion(String theVersion) { this.version = theVersion; } } diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVersion.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVersion.java index 624f1128..c682cc67 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVersion.java +++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVersion.java @@ -21,6 +21,8 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.lang3.SystemUtils; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; @@ -35,6 +37,28 @@ * @author Brian Fox */ public class RequireJavaVersion extends AbstractVersionEnforcer { + + private static final Pattern JDK8_VERSION_PATTERN = Pattern.compile("([\\[(,]?)(1\\.8|8)([]),]?)"); + + @Override + public void setVersion(String theVersion) { + + if ("8".equals(theVersion)) { + super.setVersion("1.8"); + return; + } + + Matcher matcher = JDK8_VERSION_PATTERN.matcher(theVersion); + + StringBuffer result = new StringBuffer(); + while (matcher.find()) { + matcher.appendReplacement(result, "$11.8$3"); + } + matcher.appendTail(result); + + super.setVersion(result.toString()); + } + @Override public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { String javaVersion = SystemUtils.JAVA_VERSION; diff --git a/enforcer-rules/src/site/apt/requireJavaVersion.apt.vm b/enforcer-rules/src/site/apt/requireJavaVersion.apt.vm index 1c0921f1..9b32b513 100644 --- a/enforcer-rules/src/site/apt/requireJavaVersion.apt.vm +++ b/enforcer-rules/src/site/apt/requireJavaVersion.apt.vm @@ -32,6 +32,8 @@ Require Java Version * message - an optional message to the user if the rule fails. * {{{../apidocs/org/apache/maven/plugins/enforcer/AbstractVersionEnforcer.html#version}version}} - {{{./versionRanges.html}range}} of allowed JDKs. + + For JDK 1.8 you can also use simple <<<8>>> as - it will be internally changed to <<<1.8>>> [] diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireJavaVersion.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireJavaVersion.java index 48bf29b6..d32441ef 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireJavaVersion.java +++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireJavaVersion.java @@ -21,24 +21,28 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.util.stream.Stream; import org.apache.commons.lang3.SystemUtils; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; /** * The Class TestRequireJavaVersion. * * @author Brian Fox */ -public class TestRequireJavaVersion { +class TestRequireJavaVersion { /** * Test fix jdk version. */ @Test - public void testFixJDKVersion() { + void testFixJDKVersion() { // test that we only take the first 3 versions for // comparison assertThat(RequireJavaVersion.normalizeJDKVersion("1.5.0_11")).isEqualTo("1.5.0-11"); @@ -69,7 +73,7 @@ public void testFixJDKVersion() { * @throws EnforcerRuleException the enforcer rule exception */ @Test - public void settingsTheJavaVersionAsNormalizedVersionShouldNotFail() throws EnforcerRuleException { + void settingsTheJavaVersionAsNormalizedVersionShouldNotFail() throws EnforcerRuleException { String normalizedJDKVersion = RequireJavaVersion.normalizeJDKVersion(SystemUtils.JAVA_VERSION); RequireJavaVersion rule = new RequireJavaVersion(); @@ -83,7 +87,7 @@ public void settingsTheJavaVersionAsNormalizedVersionShouldNotFail() throws Enfo } @Test - public void excludingTheCurrentJavaVersionViaRangeThisShouldFailWithException() { + void excludingTheCurrentJavaVersionViaRangeThisShouldFailWithException() { assertThrows(EnforcerRuleException.class, () -> { String thisVersion = RequireJavaVersion.normalizeJDKVersion(SystemUtils.JAVA_VERSION); @@ -101,7 +105,7 @@ public void excludingTheCurrentJavaVersionViaRangeThisShouldFailWithException() @Test @Disabled // TODO: Think about the intention of this test? What should it prove? - public void thisShouldNotCrash() throws EnforcerRuleException { + void thisShouldNotCrash() throws EnforcerRuleException { RequireJavaVersion rule = new RequireJavaVersion(); rule.setVersion(SystemUtils.JAVA_VERSION); @@ -114,8 +118,30 @@ public void thisShouldNotCrash() throws EnforcerRuleException { * Test id. */ @Test - public void testId() { + void testId() { RequireJavaVersion rule = new RequireJavaVersion(); assertThat(rule.getCacheId()).isEqualTo("0"); } + + static Stream fixJava8ShortVersion() { + return Stream.of( + Arguments.of("1.8", "1.8"), + Arguments.of("8", "1.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,)", "(1.8,)"), + Arguments.of("[8]", "[1.8]"), + Arguments.of("(9,11],[8]", "(9,11],[1.8]")); + } + + @ParameterizedTest + @MethodSource + void fixJava8ShortVersion(String input, String expected) { + RequireJavaVersion requireJavaVersion = new RequireJavaVersion(); + requireJavaVersion.setVersion(input); + assertThat(requireJavaVersion.getVersion()).isEqualTo(expected); + } }