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 820dde96..e9092087 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,19 @@ */ 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.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 +115,78 @@ void checkRequireVersionMatrix() throws EnforcerRuleException { } } + @ParameterizedTest(name = "{0} IS in range \"{1}\", because {2}") + @MethodSource("provideIsInVersionsrange") + void shouldBeInVersionsRange(String runtimeVersion, String rulesVersionRange, String reason) 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"), + 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} IS NOT in range \"{1}\", because {2}") + @MethodSource("provideIsNotInVersionsrange") + void shouldNotBeInVersionsRange(String runtimeVersion, String rulesVersionRange, String reason) { + 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"), + 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") + ); + } + /** * Test id. */