Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Arguments> 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<Arguments> 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.
*/
Expand Down
Loading