diff options
Diffstat (limited to 'api')
-rwxr-xr-x | api/src/main/java/org/semver/Delta.java | 49 | ||||
-rwxr-xr-x | api/src/main/java/org/semver/Version.java | 9 | ||||
-rwxr-xr-x | api/src/test/java/org/semver/DeltaTest.java | 79 |
3 files changed, 85 insertions, 52 deletions
diff --git a/api/src/main/java/org/semver/Delta.java b/api/src/main/java/org/semver/Delta.java index 20d963c..d38777d 100755 --- a/api/src/main/java/org/semver/Delta.java +++ b/api/src/main/java/org/semver/Delta.java @@ -28,7 +28,7 @@ import org.osjava.jardiff.AbstractInfo; * Encapsulates differences between two sets of classes. * <br /> * Provides convenient methods to validate that chosen {@link Version} are correct. - * + * */ @Immutable public final class Delta { @@ -37,9 +37,9 @@ public final class Delta { * Library compatibility type. From most compatible to less compatible. */ public enum CompatibilityType { - + BACKWARD_COMPATIBLE_IMPLEMENTER, - + BACKWARD_COMPATIBLE_USER, NON_BACKWARD_COMPATIBLE @@ -47,10 +47,10 @@ public final class Delta { @Immutable public static class Difference implements Comparable<Difference> { - + private final String className; private final AbstractInfo info; - + public Difference(@Nonnull final String className, @Nonnull final AbstractInfo info) { if (className == null) { throw new IllegalArgumentException("null className"); @@ -58,7 +58,7 @@ public final class Delta { if (info == null) { throw new IllegalArgumentException("null info"); } - + this.className = className; this.info = info; } @@ -77,33 +77,33 @@ public final class Delta { public int compareTo(final Difference other) { return getClassName().compareTo(other.getClassName()); } - + } @Immutable public static class Add extends Difference { - + public Add(@Nonnull final String className, @Nonnull final AbstractInfo info) { super(className, info); } - + } @Immutable public static class Change extends Difference { - + private final AbstractInfo modifiedInfo; - + public Change(@Nonnull final String className, @Nonnull final AbstractInfo info, @Nonnull final AbstractInfo modifiedInfo) { super(className, info); - + this.modifiedInfo = modifiedInfo; } public AbstractInfo getModifiedInfo() { return this.modifiedInfo; } - + } @Immutable @@ -126,15 +126,15 @@ public final class Delta { @Immutable public static class Remove extends Difference { - + public Remove(@Nonnull final String className, @Nonnull final AbstractInfo info) { super(className, info); } - + } private final Set<Difference> differences; - + public Delta(@Nonnull final Set<? extends Difference> differences) { this.differences = Collections.unmodifiableSet(differences); } @@ -154,7 +154,7 @@ public final class Delta { if (contains(this.differences, Change.class) || contains(this.differences, Remove.class)) { return CompatibilityType.NON_BACKWARD_COMPATIBLE; - } else if (contains(this.differences, Add.class) || + } else if (contains(this.differences, Add.class) || contains(this.differences, Deprecate.class)) { return CompatibilityType.BACKWARD_COMPATIBLE_USER; } else { @@ -170,7 +170,7 @@ public final class Delta { } return false; } - + /** * * Infers next {@link Version} depending on provided {@link CompatibilityType}. @@ -187,7 +187,7 @@ public final class Delta { if (compatibilityType == null) { throw new IllegalArgumentException("null compatibilityType"); } - + switch (compatibilityType) { case BACKWARD_COMPATIBLE_IMPLEMENTER: return version.next(Version.Element.PATCH); @@ -198,8 +198,8 @@ public final class Delta { default: throw new IllegalArgumentException("Unknown type <"+compatibilityType+">"); } - } - + } + /** * @param previous * @return an inferred {@link Version} for current JAR based on previous JAR content/version. @@ -213,7 +213,7 @@ public final class Delta { if (previous.isInDevelopment()) { throw new IllegalArgumentException("Cannot infer for in development version <"+previous+">"); } - + final CompatibilityType compatibilityType = computeCompatibilityType(); return inferNextVersion(previous, compatibilityType); } @@ -241,7 +241,8 @@ public final class Delta { //Current version must be superior or equals to inferred version final Version inferredVersion = infer(previous); - return current.compareTo(inferredVersion) >= 0; + // if the current version is a pre-release then the corresponding release need to be superior or equal + return current.toReleaseVersion().compareTo(inferredVersion) >= 0; } - + } diff --git a/api/src/main/java/org/semver/Version.java b/api/src/main/java/org/semver/Version.java index 09046b8..4f73e4f 100755 --- a/api/src/main/java/org/semver/Version.java +++ b/api/src/main/java/org/semver/Version.java @@ -147,6 +147,15 @@ public final class Version implements Comparable<Version> { } } + /** + * if this is a pre-release version, returns the corresponding release + * return the same version if already a release + * @return a release version + */ + public Version toReleaseVersion() { + return new Version(major, minor, patch); + } + public boolean isInDevelopment() { return this.major == 0; } diff --git a/api/src/test/java/org/semver/DeltaTest.java b/api/src/test/java/org/semver/DeltaTest.java index 3562592..0c99636 100755 --- a/api/src/test/java/org/semver/DeltaTest.java +++ b/api/src/test/java/org/semver/DeltaTest.java @@ -16,10 +16,21 @@ */ package org.semver; +import static java.util.Collections.singleton; +import static org.junit.Assert.assertEquals; +import static org.semver.Delta.inferNextVersion; +import static org.semver.Delta.CompatibilityType.BACKWARD_COMPATIBLE_IMPLEMENTER; +import static org.semver.Delta.CompatibilityType.BACKWARD_COMPATIBLE_USER; +import static org.semver.Delta.CompatibilityType.NON_BACKWARD_COMPATIBLE; +import static org.semver.Version.Element.MAJOR; +import static org.semver.Version.Element.MINOR; +import static org.semver.Version.Element.PATCH; + +import org.semver.Delta.Difference; + import java.util.Collections; import java.util.Set; -import org.junit.Assert; import org.junit.Test; import org.osjava.jardiff.ClassInfo; import org.osjava.jardiff.FieldInfo; @@ -27,86 +38,86 @@ import org.osjava.jardiff.MethodInfo; public class DeltaTest { - private static final Set<Delta.Difference> EMPTY_DIFFERENCES = Collections.<Delta.Difference>emptySet(); - + private static final Set<Difference> EMPTY_DIFFERENCES = Collections.<Difference>emptySet(); + @Test public void inferVersion() { final int major = 1; final int minor = 2; final int patch = 3; final Version version = new Version(major, minor, patch); - - Assert.assertEquals(version.next(Version.Element.MAJOR), Delta.inferNextVersion(version, Delta.CompatibilityType.NON_BACKWARD_COMPATIBLE)); - Assert.assertEquals(version.next(Version.Element.MINOR), Delta.inferNextVersion(version, Delta.CompatibilityType.BACKWARD_COMPATIBLE_USER)); - Assert.assertEquals(version.next(Version.Element.PATCH), Delta.inferNextVersion(version, Delta.CompatibilityType.BACKWARD_COMPATIBLE_IMPLEMENTER)); + + assertEquals(version.next(MAJOR), inferNextVersion(version, NON_BACKWARD_COMPATIBLE)); + assertEquals(version.next(MINOR), inferNextVersion(version, BACKWARD_COMPATIBLE_USER)); + assertEquals(version.next(PATCH), inferNextVersion(version, BACKWARD_COMPATIBLE_IMPLEMENTER)); } @Test(expected=IllegalArgumentException.class) public void shouldInferWithNullVersionFail() { - Delta.inferNextVersion(null, Delta.CompatibilityType.BACKWARD_COMPATIBLE_IMPLEMENTER); + inferNextVersion(null, BACKWARD_COMPATIBLE_IMPLEMENTER); } @Test(expected=IllegalArgumentException.class) public void shouldInferWithNullCompatibilityTypeFail() { - Delta.inferNextVersion(new Version(1, 0, 0), null); + inferNextVersion(new Version(1, 0, 0), null); } @Test(expected=IllegalArgumentException.class) public void shouldNullVersionNotBeInferable() { new Delta(EMPTY_DIFFERENCES).infer(null); } - + @Test(expected=IllegalArgumentException.class) public void shouldDevelopmentVersionNotBeInferable() { new Delta(EMPTY_DIFFERENCES).infer(new Version(0, 0, 0)); } @Test - public void shouldEmptyDeltaBeImplementerBackwareCompatible() { + public void shouldEmptyDeltaBeImplementerBackwardCompatible() { final int major = 1; final int minor = 2; final int patch = 3; final Version version = new Version(major, minor, patch); final Version inferedVersion = new Delta(EMPTY_DIFFERENCES).infer(version); - - Assert.assertEquals(new Version(major, minor, patch+1), inferedVersion); + + assertEquals(new Version(major, minor, patch+1), inferedVersion); } @Test - public void shouldDeltaWithAddsBeUserBackwareCompatible() { + public void shouldDeltaWithAddsBeUserBackwardCompatible() { final int major = 1; final int minor = 2; final int patch = 3; final Version version = new Version(major, minor, patch); final Version inferedVersion = new Delta(Collections.singleton(new Delta.Add("class", new FieldInfo(0, "", "", "", null)))).infer(version); - - Assert.assertEquals(new Version(major, minor+1, 0), inferedVersion); + + assertEquals(new Version(major, minor+1, 0), inferedVersion); } @Test - public void shouldDeltaWithChangesBeNonBackwareCompatible() { + public void shouldDeltaWithChangesBeNonBackwardCompatible() { final int major = 1; final int minor = 2; final int patch = 3; final Version version = new Version(major, minor, patch); final Version inferedVersion = new Delta(Collections.singleton(new Delta.Change("class", new FieldInfo(0, "", "", "", null), new FieldInfo(0, "", "", "", null)))).infer(version); - - Assert.assertEquals(new Version(major+1, 0, 0), inferedVersion); + + assertEquals(new Version(major+1, 0, 0), inferedVersion); } @Test - public void shouldDeltaWithRemovesBeNonBackwareCompatible() { + public void shouldDeltaWithRemovesBeNonBackwardCompatible() { final int major = 1; final int minor = 2; final int patch = 3; final Version version = new Version(major, minor, patch); final Version inferedVersion = new Delta(Collections.singleton(new Delta.Remove("class", new FieldInfo(0, "", "", "", null)))).infer(version); - - Assert.assertEquals(new Version(major+1, 0, 0), inferedVersion); + + assertEquals(new Version(major+1, 0, 0), inferedVersion); } @Test(expected=IllegalArgumentException.class) @@ -121,7 +132,7 @@ public class DeltaTest { @Test public void shouldValidateWithCurrentVersionInDevelopmentSucceed() { - Assert.assertTrue(new Delta(EMPTY_DIFFERENCES).validate(new Version(0, 0, 0), new Version(0, 0, 1))); + validate(EMPTY_DIFFERENCES, new Version(0, 0, 0), new Version(0, 0, 1), true); } @Test(expected=IllegalArgumentException.class) @@ -136,26 +147,38 @@ public class DeltaTest { @Test public void shouldValidateWithCorrectVersionsSucceed() { - Assert.assertTrue(new Delta(EMPTY_DIFFERENCES).validate(new Version(1, 1, 0), new Version(1, 1, 1))); + validate(EMPTY_DIFFERENCES, new Version(1, 1, 0), new Version(1, 1, 1), true); + } + + @Test + public void shouldValidateWithCorrectPreVersionsSucceed() { + validate(EMPTY_DIFFERENCES, new Version(1, 1, 0, "-", "rc1"), new Version(1, 1, 0, "-", "rc2"), true); } @Test public void shouldValidateWithIncorrectVersionFail() { - Assert.assertFalse(new Delta(Collections.singleton(new Delta.Remove("class", new FieldInfo(0, "", "", "", null)))).validate(new Version(1, 1, 0), new Version(1, 1, 1))); + validate(Collections.singleton(new Delta.Remove("class", new FieldInfo(0, "", "", "", null))), new Version(1, 1, 0), new Version(1, 1, 1), false); } @Test public void upgradeMinorVersionOnClassDeprecated() { - Assert.assertTrue(new Delta(Collections.singleton(new Delta.Deprecate("class", new ClassInfo(1, 0, "", "", "", null, null, null), new ClassInfo(1, 0, "", "", "", null, null, null)))).validate(new Version(1, 1, 0), new Version(1, 2, 0))); + validate(singleton(new Delta.Deprecate("class", new ClassInfo(1, 0, "", "", "", null, null, null), new ClassInfo(1, 0, "", "", "", null, null, null))), new Version(1, 1, 0), new Version(1, 2, 0), true); } @Test public void upgradeMinorVersionOnFieldDeprecated() { - Assert.assertTrue(new Delta(Collections.singleton(new Delta.Deprecate("class", new FieldInfo(0, "", "", "", null), new FieldInfo(0, "", "", "", null)))).validate(new Version(1, 1, 0), new Version(1, 2, 0))); + validate(singleton(new Delta.Deprecate("class", new FieldInfo(0, "", "", "", null), new FieldInfo(0, "", "", "", null))), new Version(1, 1, 0), new Version(1, 2, 0), true); } @Test public void upgradeMinorVersionOnMethodDeprecated() { - Assert.assertTrue(new Delta(Collections.singleton(new Delta.Deprecate("class", new MethodInfo(0, "", "", "", null), new MethodInfo(0, "", "", "", null)))).validate(new Version(1, 1, 0), new Version(1, 2, 0))); + validate(singleton(new Delta.Deprecate("class", new MethodInfo(0, "", "", "", null), new MethodInfo(0, "", "", "", null))), new Version(1, 1, 0), new Version(1, 2, 0), true); + } + + private void validate(Set<? extends Delta.Difference> differences, Version previous, Version current, boolean valid) { + assertEquals( + "accept differences " + differences + " when changing version from " + previous + " to " + current, + valid, + new Delta(differences).validate(previous, current)); } }
\ No newline at end of file |