diff options
-rwxr-xr-x | api/src/main/java/org/semver/Version.java | 8 | ||||
-rwxr-xr-x | api/src/test/java/org/semver/VersionTest.java | 14 | ||||
-rwxr-xr-x | enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java | 3 |
3 files changed, 24 insertions, 1 deletions
diff --git a/api/src/main/java/org/semver/Version.java b/api/src/main/java/org/semver/Version.java index 4cb54db..83765da 100755 --- a/api/src/main/java/org/semver/Version.java +++ b/api/src/main/java/org/semver/Version.java @@ -168,6 +168,14 @@ public final class Version implements Comparable<Version> { return this.special != null && this.special.isSnapshot(); } + /** + * @param version version to check with + * @return {@code true}, if supplied version is compatible with this version, {@code false} - otherwise + */ + public boolean isCompatible(Version version) { + return version != null && this.major == version.major; + } + @Override public int hashCode() { int hash = 5; diff --git a/api/src/test/java/org/semver/VersionTest.java b/api/src/test/java/org/semver/VersionTest.java index 6fbcf47..625c3ac 100755 --- a/api/src/test/java/org/semver/VersionTest.java +++ b/api/src/test/java/org/semver/VersionTest.java @@ -102,6 +102,20 @@ public class VersionTest { } @Test + public void shouldBeCompatible() { + Assert.assertTrue(Version.parse("1.0.0").isCompatible(Version.parse("1.2.3-SNAPSHOT"))); + Assert.assertTrue(Version.parse("1.0.0").isCompatible(Version.parse("1.0.1"))); + Assert.assertTrue(Version.parse("1.0.0").isCompatible(Version.parse("1.1.0"))); + } + + @Test + public void shouldBeIncompatible() { + Assert.assertFalse(Version.parse("0.0.1-SNAPSHOT").isCompatible(null)); + Assert.assertFalse(Version.parse("1.0.1").isCompatible(Version.parse("2.0.0"))); + Assert.assertFalse(Version.parse("1.1.0-rc3").isCompatible(Version.parse("3.1.0-SNAPSHOT"))); + } + + @Test public void isNewer() { Assert.assertTrue(Version.parse("3.2.3").compareTo(Version.parse("3.2-M1-SNAPSHOT")) > 0); Assert.assertTrue(Version.parse("1.0.0").compareTo(Version.parse("0.0.0")) > 0); diff --git a/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java b/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java index 098a0e7..f0ce4cf 100755 --- a/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java +++ b/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java @@ -178,7 +178,8 @@ public abstract class AbstractEnforcerRule implements EnforcerRule { protected final List<ArtifactVersion> filterNonPreviousVersions(final List<ArtifactVersion> availableVersions, final Version version) { final List<ArtifactVersion> versions = new ArrayList<ArtifactVersion>(); for (final ArtifactVersion artifactVersion : availableVersions) { - if (version.compareTo(Version.parse(artifactVersion.toString())) > 0) { + Version parsedVersion = Version.parse(artifactVersion.toString()); + if (version.isCompatible(parsedVersion) && version.compareTo(parsedVersion) > 0) { versions.add(artifactVersion); } } |