From 4a02b6228202c84a2368424a677f2c6347550aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20B=C3=BCnger?= Date: Thu, 18 Dec 2025 18:24:37 +0100 Subject: [PATCH 1/3] Add tests for Maven version range checking --- .../rules/version/TestMavenVersion.java | 78 ++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java index 820dde966..604e64551 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java @@ -18,16 +18,20 @@ */ package org.apache.maven.enforcer.rules.version; +import java.util.stream.Stream; import org.apache.maven.enforcer.rule.api.EnforcerLogger; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.rtinfo.RuntimeInformation; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.when; @@ -112,6 +116,78 @@ void checkRequireVersionMatrix() throws EnforcerRuleException { } } + @ParameterizedTest(name = "{0} should be in version range \"{1}\", because {2}") + @MethodSource("provideIsInVersionsrange") + @DisplayName("The provided version") + void shouldBeInVersionsRange(String runtimeVersion, String rulesVersionRange) throws EnforcerRuleException { + when(runtimeInformation.getMavenVersion()).thenReturn(runtimeVersion); + rule.setVersion(rulesVersionRange); + rule.execute(); + } + + + private static Stream provideIsInVersionsrange() { + // Based on from https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html + // in combination with the there linked https://maven.apache.org/pom.html#Dependency_Version_Requirement_Specification + return Stream.of( + Arguments.of("3.9.12", "3.9.11", "3.9.12 >= 3.9.11 (\"Minimum in enforcer\")"), + Arguments.of("4.0.0-rc-5", "3.6.3", "4.0.0-rc-5 >= 3.9.12 (\"Minimum in enforcer\")"), + Arguments.of("3.9.12", "(,3.9.12]", "3.9.12 <= 3.9.12"), + Arguments.of("3.9.12", "(,3.9.13]", "3.9.12 < 3.9.13"), + Arguments.of("3.9.12", "[3.9.12]", "3.9.12 == 3.9.12"), + Arguments.of("3.9.12", "[3.9.12,)", "3.9.12 >= 3.9.12"), + Arguments.of("3.9.12", "[3.9.11,)", "3.9.12 >= 3.9.11"), + Arguments.of("3.9.12", "(3.9.11,)", "3.9.12 > 3.9.11"), + Arguments.of("3.9.12", "(3.9.12-alpha-1,)", "3.9.12 > 3.9.12-alpha-1"), + Arguments.of("3.9.12", "(3.6.3,3.9.13)", "3.6.3 < 3.9.12 < 3.9.13"), + Arguments.of("3.9.12", "(3.6.3,3.9.12]", "3.6.3 < 3.9.12 <= 3.9.12"), + Arguments.of("3.9.12", "(,3.9.11],[3.9.12,)", "3.9.12 <= 3.9.11 (false) OR 3.9.12 >= 3.9.12 (true)"), + Arguments.of("3.9.12", "(,3.9.12],[3.9.11,)", "3.9.12 <= 3.9.12 (true) OR 3.9.11 >= 3.9.12 (false)"), + Arguments.of("3.9.12", "(,3.9.11],(,3.9.10,[3.9.12,)", "3.9.12 <= 3.9.11 (false) OR 3.9.12 <= 3.9.10 (false) OR 3.9.12 >= 3.9.12 (true)"), + Arguments.of("3.9.11", "(,3.9.12),(3.9.12,)", "3.9.12 != 3.9.12"), + Arguments.of("4.0.0-rc-5", "(3.6.3,4.0.0)", "3.6.3 < 4.0.0-rc-5 < 4.0.0") + ); + } + + @ParameterizedTest(name = "{0} should NOT be in version range \"{1}\", because {2}") + @MethodSource("provideIsNotInVersionsrange") + @DisplayName("The provided version") + void shouldNotBeInVersionsRange(String runtimeVersion, String rulesVersionRange) { + when(runtimeInformation.getMavenVersion()).thenReturn(runtimeVersion); + rule.setVersion(rulesVersionRange); + try { + rule.execute(); + fail("Expected an exception."); + } catch (EnforcerRuleException e) { + // expected that this fails + } + } + + + private static Stream provideIsNotInVersionsrange() { + // Based on from https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html + // in combination with the there linked https://maven.apache.org/pom.html#Dependency_Version_Requirement_Specification + return Stream.of( + Arguments.of("3.9.11", "3.9.12", "3.9.11 is not >= 3.9.12 (\"Minimum in enforcer\")"), + Arguments.of("3.9.13", "(,3.9.12]", "3.9.13 is not <= 3.9.12"), + Arguments.of("3.9.13", "(,3.9.13]", "3.9.13 is not < 3.9.13"), + Arguments.of("3.9.13", "[3.9.12]", "3.9.13 is not == 3.9.12"), + Arguments.of("3.9.11", "[3.9.12,)", "3.9.11 is not >= 3.9.12"), + Arguments.of("3.9.11", "[3.9.12,)", "3.9.11 is not >= 3.9.12"), + Arguments.of("3.9.11", "(3.9.11,)", "3.9.11 is not > 3.9.11"), + Arguments.of("3.9.12-alpha-1", "(3.9.12-alpha-2,)", "3.9.12-alpha 1 is not > 3.9.12-alpha-2"), + Arguments.of("3.9.11", "(3.6.12,3.9.13)", "3.6.12 is not < 3.9.11 but 3.9.11 is < 3.9.13"), + Arguments.of("3.9.13", "(3.6.11,3.9.12)", "3.6.12 is < 3.9.13 but 3.9.13 is not < 3.9.12"), + Arguments.of("3.9.12", "(3.6.3,3.9.11]", "3.6.3 is < 3.9.12 but 3.9.12 is not <= 3.9.11"), + Arguments.of("3.9.12", "(,3.9.11],[3.9.13,)", "3.9.12 is not <= 3.9.11 and 3.9.12 is not >= 3.9.13"), + Arguments.of("3.9.12", "(,3.9.11],[3.9.12,)", "3.9.12 is not <= 3.9.11 but 3.9.12 is >= 3.9.13"), + Arguments.of("3.9.12", "(,3.9.12],[3.9.13,)", "3.9.12 is <= 3.9.12 and 3.9.12 is not >= 3.9.13"), + Arguments.of("3.9.12", "(,3.9.12],(,3.9.12,[3.9.13,)", "3.9.12 is <= 3.9.11 and 3.9.12 is <= 3.9.10 but 3.9.12 is not >= 3.9.13"), + Arguments.of("3.9.12", "(,3.9.12),(3.9.12,)", "3.9.11 is not != 3.9.12"), + Arguments.of("4.0.0-rc-5", "(3.6.12,3.99.99)", "3.6.12 is < 4.0.0-rc-5 but 4.0.0-rc-5 is not < 3.99.99") + ); + } + /** * Test id. */ From 878b6d1125633b62d1655f8be4fffabaab21473f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20B=C3=BCnger?= Date: Thu, 18 Dec 2025 18:34:55 +0100 Subject: [PATCH 2/3] Add tests for Maven version range checking --- .../rules/version/TestMavenVersion.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java index 604e64551..cfb1e2cf5 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java @@ -23,7 +23,6 @@ import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.rtinfo.RuntimeInformation; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; @@ -116,10 +115,9 @@ void checkRequireVersionMatrix() throws EnforcerRuleException { } } - @ParameterizedTest(name = "{0} should be in version range \"{1}\", because {2}") + @ParameterizedTest(name = "{0} IS in range \"{1}\", because {2}") @MethodSource("provideIsInVersionsrange") - @DisplayName("The provided version") - void shouldBeInVersionsRange(String runtimeVersion, String rulesVersionRange) throws EnforcerRuleException { + void shouldBeInVersionsRange(String runtimeVersion, String rulesVersionRange, String reason) throws EnforcerRuleException { when(runtimeInformation.getMavenVersion()).thenReturn(runtimeVersion); rule.setVersion(rulesVersionRange); rule.execute(); @@ -128,7 +126,6 @@ void shouldBeInVersionsRange(String runtimeVersion, String rulesVersionRange) th private static Stream provideIsInVersionsrange() { // Based on from https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html - // in combination with the there linked https://maven.apache.org/pom.html#Dependency_Version_Requirement_Specification return Stream.of( Arguments.of("3.9.12", "3.9.11", "3.9.12 >= 3.9.11 (\"Minimum in enforcer\")"), Arguments.of("4.0.0-rc-5", "3.6.3", "4.0.0-rc-5 >= 3.9.12 (\"Minimum in enforcer\")"), @@ -145,14 +142,14 @@ private static Stream provideIsInVersionsrange() { Arguments.of("3.9.12", "(,3.9.12],[3.9.11,)", "3.9.12 <= 3.9.12 (true) OR 3.9.11 >= 3.9.12 (false)"), Arguments.of("3.9.12", "(,3.9.11],(,3.9.10,[3.9.12,)", "3.9.12 <= 3.9.11 (false) OR 3.9.12 <= 3.9.10 (false) OR 3.9.12 >= 3.9.12 (true)"), Arguments.of("3.9.11", "(,3.9.12),(3.9.12,)", "3.9.12 != 3.9.12"), - Arguments.of("4.0.0-rc-5", "(3.6.3,4.0.0)", "3.6.3 < 4.0.0-rc-5 < 4.0.0") + Arguments.of("4.0.0-rc-5", "(3.6.3,4.0.0)", "3.6.3 < 4.0.0-rc-5 < 4.0.0"), + Arguments.of("4.0.0-alpha-2", "(4.0.0-alpha-1,4.0.0-beta-1)", "4.0.0-alpha-1 < 4.0.0-alpha-2 < 4.0.0-beta-1") ); } - @ParameterizedTest(name = "{0} should NOT be in version range \"{1}\", because {2}") + @ParameterizedTest(name = "{0} IS NOT in range \"{1}\", because {2}") @MethodSource("provideIsNotInVersionsrange") - @DisplayName("The provided version") - void shouldNotBeInVersionsRange(String runtimeVersion, String rulesVersionRange) { + void shouldNotBeInVersionsRange(String runtimeVersion, String rulesVersionRange, String reason) { when(runtimeInformation.getMavenVersion()).thenReturn(runtimeVersion); rule.setVersion(rulesVersionRange); try { @@ -184,7 +181,8 @@ private static Stream provideIsNotInVersionsrange() { Arguments.of("3.9.12", "(,3.9.12],[3.9.13,)", "3.9.12 is <= 3.9.12 and 3.9.12 is not >= 3.9.13"), Arguments.of("3.9.12", "(,3.9.12],(,3.9.12,[3.9.13,)", "3.9.12 is <= 3.9.11 and 3.9.12 is <= 3.9.10 but 3.9.12 is not >= 3.9.13"), Arguments.of("3.9.12", "(,3.9.12),(3.9.12,)", "3.9.11 is not != 3.9.12"), - Arguments.of("4.0.0-rc-5", "(3.6.12,3.99.99)", "3.6.12 is < 4.0.0-rc-5 but 4.0.0-rc-5 is not < 3.99.99") + Arguments.of("4.0.0-rc-5", "(3.6.12,3.99.99)", "3.6.12 is < 4.0.0-rc-5 but 4.0.0-rc-5 is not < 3.99.99"), + Arguments.of("4.0.0-rc-5", "[4.0.0-alpha-1,4.0.0-beta-1]", "4.0.0-alpha-1 is < 4.0.0-rc-5 but 4.0.0-rc-5 is not < 4.0.0-beta-1") ); } From ba22862c6f7026f1737620033a1ca12f841178af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20B=C3=BCnger?= Date: Thu, 18 Dec 2025 18:37:00 +0100 Subject: [PATCH 3/3] Add tests for Maven version range checking --- .../apache/maven/enforcer/rules/version/TestMavenVersion.java | 1 + 1 file changed, 1 insertion(+) diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java index cfb1e2cf5..e9092087f 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/version/TestMavenVersion.java @@ -126,6 +126,7 @@ void shouldBeInVersionsRange(String runtimeVersion, String rulesVersionRange, St private static Stream provideIsInVersionsrange() { // Based on from https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html + // in combination with the there linked https://maven.apache.org/pom.html#Dependency_Version_Requirement_Specification return Stream.of( Arguments.of("3.9.12", "3.9.11", "3.9.12 >= 3.9.11 (\"Minimum in enforcer\")"), Arguments.of("4.0.0-rc-5", "3.6.3", "4.0.0-rc-5 >= 3.9.12 (\"Minimum in enforcer\")"),