-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0cafa40
commit 7b86752
Showing
15 changed files
with
278 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
versions-common/src/main/java/org/codehaus/mojo/versions/api/IgnoreVersionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.codehaus.mojo.versions.api; | ||
|
||
/* | ||
* Copyright MojoHaus and Contributors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.BiFunction; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.maven.artifact.versioning.ArtifactVersion; | ||
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; | ||
import org.apache.maven.artifact.versioning.VersionRange; | ||
import org.codehaus.mojo.versions.model.IgnoreVersion; | ||
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; | ||
import org.eclipse.aether.version.Version; | ||
|
||
/** | ||
* Helper class for {@link IgnoreVersion} | ||
*/ | ||
public class IgnoreVersionHelper { | ||
|
||
static class IgnoreVersionException extends RuntimeException { | ||
IgnoreVersionException(Throwable cause) { | ||
super(cause); | ||
} | ||
} | ||
|
||
public static final List<String> VALID_TYPES = Collections.unmodifiableList( | ||
Arrays.asList(IgnoreVersion.TYPE_EXACT, IgnoreVersion.TYPE_REGEX, IgnoreVersion.TYPE_RANGE)); | ||
|
||
private static final Map<String, BiFunction<Version, IgnoreVersion, Boolean>> VERSION_MATCHERS; | ||
|
||
static { | ||
VERSION_MATCHERS = new HashMap<>(); | ||
VERSION_MATCHERS.put(IgnoreVersion.TYPE_EXACT, IgnoreVersionHelper::isVersionIgnoredExact); | ||
VERSION_MATCHERS.put(IgnoreVersion.TYPE_REGEX, IgnoreVersionHelper::isVersionIgnoredRegex); | ||
VERSION_MATCHERS.put(IgnoreVersion.TYPE_RANGE, IgnoreVersionHelper::isVersionIgnoredRange); | ||
} | ||
|
||
private IgnoreVersionHelper() {} | ||
|
||
/** | ||
* Check if type for given ignoredVersion is valid. | ||
* | ||
* @param ignoreVersion an ignored version to check | ||
* @return true if type is valid | ||
*/ | ||
public static boolean isValidType(IgnoreVersion ignoreVersion) { | ||
return VALID_TYPES.contains(ignoreVersion.getType()); | ||
} | ||
|
||
public static boolean isVersionIgnored(Version version, IgnoreVersion ignoreVersion) { | ||
return VERSION_MATCHERS.get(ignoreVersion.getType()).apply(version, ignoreVersion); | ||
} | ||
|
||
private static boolean isVersionIgnoredExact(Version version, IgnoreVersion ignoreVersion) { | ||
return ignoreVersion.getVersion().equals(version.toString()); | ||
} | ||
|
||
private static boolean isVersionIgnoredRegex(Version version, IgnoreVersion ignoreVersion) { | ||
return Pattern.compile(ignoreVersion.getVersion()) | ||
.matcher(version.toString()) | ||
.matches(); | ||
} | ||
|
||
private static boolean isVersionIgnoredRange(Version version, IgnoreVersion ignoreVersion) { | ||
try { | ||
ArtifactVersion aVersion = DefaultArtifactVersionCache.of(version.toString()); | ||
VersionRange versionRange = VersionRange.createFromVersionSpec(ignoreVersion.getVersion()); | ||
if (versionRange.hasRestrictions()) { | ||
return versionRange.containsVersion(aVersion); | ||
} else { | ||
return versionRange.getRecommendedVersion().equals(aVersion); | ||
} | ||
} catch (InvalidVersionSpecificationException e) { | ||
throw new IgnoreVersionException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
versions-common/src/test/java/org/codehaus/mojo/versions/api/IgnoreVersionHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package org.codehaus.mojo.versions.api; | ||
|
||
/* | ||
* Copyright MojoHaus and Contributors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import java.util.stream.Stream; | ||
|
||
import org.codehaus.mojo.versions.model.IgnoreVersion; | ||
import org.eclipse.aether.util.version.GenericVersionScheme; | ||
import org.eclipse.aether.version.VersionScheme; | ||
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; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.params.provider.Arguments.of; | ||
|
||
class IgnoreVersionHelperTest { | ||
|
||
private static final VersionScheme VERSION_SCHEME = new GenericVersionScheme(); | ||
|
||
static Stream<Arguments> ignoredTypeExact() { | ||
return Stream.of(of("1.0.0", "1.0.0", true), of("2.0.0", "1.0.0", false)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
void ignoredTypeExact(String version, String ignore, boolean expected) throws Exception { | ||
|
||
IgnoreVersion ignoreVersion = aIgnoreVersion(ignore, IgnoreVersion.TYPE_EXACT); | ||
|
||
assertEquals( | ||
expected, IgnoreVersionHelper.isVersionIgnored(VERSION_SCHEME.parseVersion(version), ignoreVersion)); | ||
} | ||
|
||
public static Stream<Arguments> ignoredTypeRegex() { | ||
return Stream.of( | ||
of("1.0.0", "1\\.0\\..*", true), | ||
of("1.0.0-SNAPSHOT", ".*-SNAPSHOT", true), | ||
of("1.0.0", "2\\.0\\..*", false)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
void ignoredTypeRegex(String version, String ignore, boolean expected) throws Exception { | ||
|
||
IgnoreVersion ignoreVersion = aIgnoreVersion(ignore, IgnoreVersion.TYPE_REGEX); | ||
|
||
assertEquals( | ||
expected, IgnoreVersionHelper.isVersionIgnored(VERSION_SCHEME.parseVersion(version), ignoreVersion)); | ||
} | ||
|
||
public static Stream<Arguments> ignoredTypeRange() { | ||
return Stream.of( | ||
of("1.0.0", "[1.0,)", true), | ||
of("1.0.0", "(,2.0.0]", true), | ||
of("2.2.0", "[1.0,3)", true), | ||
of("2.2.0", "[1.0,2.0)", false), | ||
of("2.2.0", "(,2.0.0]", false), | ||
of("1.0.0", "1.0.0", true), | ||
of("1.0.0", "1.0", true), | ||
of("1.0.1", "1.0", false), | ||
of("1.0.0", "2.0.0", false)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
void ignoredTypeRange(String version, String ignore, boolean expected) throws Exception { | ||
|
||
IgnoreVersion ignoreVersion = aIgnoreVersion(ignore, IgnoreVersion.TYPE_RANGE); | ||
|
||
assertEquals( | ||
expected, IgnoreVersionHelper.isVersionIgnored(VERSION_SCHEME.parseVersion(version), ignoreVersion)); | ||
} | ||
|
||
@Test | ||
void invalidRangeShouldThrowException() throws Exception { | ||
IgnoreVersion ignoreVersion = aIgnoreVersion("[1,,", IgnoreVersion.TYPE_RANGE); | ||
|
||
assertThrows( | ||
IgnoreVersionHelper.IgnoreVersionException.class, | ||
() -> IgnoreVersionHelper.isVersionIgnored(VERSION_SCHEME.parseVersion("1.0.0"), ignoreVersion)); | ||
} | ||
|
||
private IgnoreVersion aIgnoreVersion(String version, String type) { | ||
IgnoreVersion ignoreVersion = new IgnoreVersion(); | ||
ignoreVersion.setVersion(version); | ||
ignoreVersion.setType(type); | ||
return ignoreVersion; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...maven-plugin/src/it/it-display-dependency-updates-issue-684-pom-based-rules/verify.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
output = new File(basedir, "output1.txt").text | ||
assert ! ( output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*1\.1\s+->\s+3\.0\b/ ) | ||
assert output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*1\.1\s+->\s+2\.1\b/ | ||
assert output =~ /\Qlocalhost:dummy-impl\E\s*\.*\s*1\.0\s+->\s+1\.4\b/ | ||
|
||
output = new File(basedir, "output2.txt").text | ||
assert ! ( output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*1\.1\s+->\s+2\.1\b/ ) | ||
assert output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*1\.1\s+->\s+2\.0\b/ | ||
assert output =~ /\Qlocalhost:dummy-impl\E\s*\.*\s*1\.0\s+->\s+1\.4\b/ |
Oops, something went wrong.