diff options
author | Julien Eluard <[email protected]> | 2010-11-21 13:03:58 +0100 |
---|---|---|
committer | Julien Eluard <[email protected]> | 2010-11-21 13:03:58 +0100 |
commit | 86bcc9a108a5ba4454f40fe7df7745fb01627341 (patch) | |
tree | bdf11e61afad4fb9c6893092b5e5550fe7bca107 | |
parent | b61abf614ac91f0efe8416b8066009f7796928ab (diff) |
Added tests.
-rw-r--r-- | api/src/main/java/org/semver/Comparer.java | 1 | ||||
-rw-r--r-- | api/src/main/java/org/semver/Delta.java | 68 | ||||
-rw-r--r-- | api/src/main/java/org/semver/Version.java | 37 | ||||
-rw-r--r-- | api/src/test/java/org/semver/VersionTest.java | 30 |
4 files changed, 85 insertions, 51 deletions
diff --git a/api/src/main/java/org/semver/Comparer.java b/api/src/main/java/org/semver/Comparer.java index 1164779..57fce7f 100644 --- a/api/src/main/java/org/semver/Comparer.java +++ b/api/src/main/java/org/semver/Comparer.java @@ -27,7 +27,6 @@ import org.osjava.jardiff.DiffException; import org.osjava.jardiff.JarDiff; import org.osjava.jardiff.SimpleDiffCriteria; import org.semver.jardiff.DifferenceAccumulatingHandler; -import org.semver.Delta.Difference; /** * diff --git a/api/src/main/java/org/semver/Delta.java b/api/src/main/java/org/semver/Delta.java index 0fea247..e856bb2 100644 --- a/api/src/main/java/org/semver/Delta.java +++ b/api/src/main/java/org/semver/Delta.java @@ -33,7 +33,7 @@ import org.osjava.jardiff.AbstractInfo; * */ @Immutable -public class Delta { +public final class Delta { /** * Library compatibility type. From most compatible to less compatible. @@ -46,7 +46,8 @@ public class Delta { NON_BACKWARD_COMPATIBLE } - + + @Immutable public static class Difference implements Comparable<Difference> { private final String className; @@ -64,10 +65,12 @@ public class Delta { this.info = info; } + @Nonnull public String getClassName() { return this.className; } + @Nonnull public AbstractInfo getInfo() { return info; } @@ -78,7 +81,8 @@ public class Delta { } } - + + @Immutable public static class Add extends Difference { public Add(@Nonnull final String className, @Nonnull final AbstractInfo info) { @@ -86,7 +90,8 @@ public class Delta { } } - + + @Immutable public static class Change extends Difference { private final AbstractInfo modifiedInfo; @@ -102,7 +107,8 @@ public class Delta { } } - + + @Immutable public static class Remove extends Difference { public Remove(@Nonnull final String className, @Nonnull final AbstractInfo info) { @@ -113,23 +119,25 @@ public class Delta { private final Set<Difference> differences; - public Delta(@Nonnull final Set<Difference> differences) { + public Delta(@Nonnull final Set<? extends Difference> differences) { this.differences = Collections.unmodifiableSet(differences); } - public Set<Difference> getDifferences() { + @Nonnull + public final Set<Difference> getDifferences() { return this.differences; } - + /** * @param differences * @return {@link CompatibilityType} based on specified {@link Difference} */ + @Nonnull public final CompatibilityType computeCompatibilityType() { - if (!contains(this.differences, Change.class) && - !contains(this.differences, Remove.class)) { + 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)) { return CompatibilityType.BACKWARD_COMPATIBLE_USER; } else { return CompatibilityType.BACKWARD_COMPATIBLE_IMPLEMENTER; @@ -139,10 +147,10 @@ public class Delta { protected final boolean contains(final Set<Difference> differences, final Class<? extends Difference> type) { for (final Difference difference : differences) { if (type.isInstance(difference)) { - return false; + return true; } } - return true; + return false; } /** @@ -153,7 +161,15 @@ public class Delta { * @param compatibilityType * @return */ + @Nonnull public static Version inferNextVersion(@Nonnull final Version version, @Nonnull final CompatibilityType compatibilityType) { + if (version == null) { + throw new IllegalArgumentException("null version"); + } + if (compatibilityType == null) { + throw new IllegalArgumentException("null compatibilityType"); + } + switch (compatibilityType) { case BACKWARD_COMPATIBLE_IMPLEMENTER: return version.next(Version.Element.PATCH); @@ -168,14 +184,14 @@ public class Delta { /** * @param previous - * @param previousJAR - * @param currentJAR - * @param includes - * @param excludes * @return an inferred {@link Version} for current JAR based on previous JAR content/version. * @throws IOException */ - public final Version infer(final Version previous) { + @Nonnull + public final Version infer(@Nonnull final Version previous) { + if (previous == null) { + throw new IllegalArgumentException("null previous"); + } if (previous.isInDevelopment()) { throw new IllegalArgumentException("Cannot infer for in development version <"+previous+">"); } @@ -186,17 +202,19 @@ public class Delta { /** * @param previous - * @param previousJAR * @param current - * @param currentJAR - * @param includes - * @param excludes * @return true if {@link Version} provided for current JAR is compatible with previous JAR content/version. * @throws IOException */ - public final boolean validate(final Version previous, final Version current) { - if (previous.compareTo(current) < 0) { - throw new IllegalArgumentException("Previous version <"+previous+"> must not be more recent than current version <"+current+">."); + public final boolean validate(@Nonnull final Version previous, @Nonnull final Version current) { + if (previous == null) { + throw new IllegalArgumentException("null previous"); + } + if (current == null) { + throw new IllegalArgumentException("null current"); + } + if (previous.compareTo(current) <= 0) { + throw new IllegalArgumentException("Current version <"+previous+"> must be more recent than previous version <"+current+">."); } //When in development public API is not considered stable if (current.isInDevelopment()) { diff --git a/api/src/main/java/org/semver/Version.java b/api/src/main/java/org/semver/Version.java index 62e5da5..d7b8dbd 100644 --- a/api/src/main/java/org/semver/Version.java +++ b/api/src/main/java/org/semver/Version.java @@ -52,7 +52,7 @@ public final class Version implements Comparable<Version> { this(major, minor, patch, null); } - public Version(@Nonnegative final int major, @Nonnegative final int minor, @Nonnegative final int patch, @Nonnull final String special) { + public Version(@Nonnegative final int major, @Nonnegative final int minor, @Nonnegative final int patch, @Nullable final String special) { if (major < 0) { throw new IllegalArgumentException(Element.MAJOR+" must be positive"); } @@ -82,36 +82,23 @@ public final class Version implements Comparable<Version> { throw new IllegalArgumentException("<"+version+"> does not match format "+Version.FORMAT); } - final int major = Version.parseElement(matcher.group(1), Element.MAJOR); - final int minor = Version.parseElement(matcher.group(2), Element.MINOR); - final int patch = Version.parseElement(matcher.group(3), Element.PATCH); + final int major = Integer.valueOf(matcher.group(1)); + final int minor = Integer.valueOf(matcher.group(2)); + final int patch = Integer.valueOf(matcher.group(3)); - if (matcher.groupCount() == 4) { - return new Version(major, minor, patch, matcher.group(4)); - } else { - return new Version(major, minor, patch); - } + return new Version(major, minor, patch, matcher.group(4)); } - + /** - * @param number * @param type - * @return int representation of provided number + * @return next {@link Version} regarding specified {@link Version.Element} */ - private @Nonnegative static int parseElement(@Nonnull final String number, @Nonnull final Version.Element type) { - try { - return Integer.valueOf(number); - } catch (NumberFormatException e) { - throw new IllegalArgumentException(type+" must be an integer", e); + public Version next(@Nonnull final Version.Element element) { + if (element == null) { + throw new IllegalArgumentException("null element"); } - } - /** - * @param type - * @return next {@link Version} regarding specified {@link Version.Element} - */ - public Version next(@Nonnull final Version.Element type) { - switch (type) { + switch (element) { case MAJOR: return new Version(this.major+1, 0, 0); case MINOR: @@ -119,7 +106,7 @@ public final class Version implements Comparable<Version> { case PATCH: return new Version(this.major, this.minor, this.patch+1); default: - throw new IllegalArgumentException("Unknown type <"+type+">"); + throw new IllegalArgumentException("Unknown element <"+element+">"); } } diff --git a/api/src/test/java/org/semver/VersionTest.java b/api/src/test/java/org/semver/VersionTest.java index 73f3b76..0923c5f 100644 --- a/api/src/test/java/org/semver/VersionTest.java +++ b/api/src/test/java/org/semver/VersionTest.java @@ -68,6 +68,11 @@ public class VersionTest { Version.parse("1.2"); } + @Test(expected=IllegalArgumentException.class) + public void shouldInvalidVersion5NotBeParsed() { + Version.parse("a.2.3"); + } + @Test public void shouldDevelopmentBeInDevelopment() { Assert.assertTrue(Version.parse("0.1.1").isInDevelopment()); @@ -83,11 +88,36 @@ public class VersionTest { @Test public void isNewer() { Assert.assertTrue(Version.parse("1.0.0").compareTo(Version.parse("0.0.0")) < 0); + Assert.assertTrue(Version.parse("0.0.0").compareTo(Version.parse("1.0.0")) > 0); Assert.assertTrue(Version.parse("1.1.0").compareTo(Version.parse("1.0.0")) < 0); + Assert.assertTrue(Version.parse("1.0.0").compareTo(Version.parse("1.1.0")) > 0); Assert.assertTrue(Version.parse("1.0.1").compareTo(Version.parse("1.0.0")) < 0); + Assert.assertTrue(Version.parse("1.0.0").compareTo(Version.parse("1.0.1")) > 0); Assert.assertTrue(Version.parse("1.0.0Beta").compareTo(Version.parse("1.0.0Alpha")) < 0); Assert.assertFalse(Version.parse("0.0.0").compareTo(Version.parse("0.0.0")) < 0); Assert.assertFalse(Version.parse("0.0.0").compareTo(Version.parse("0.0.1")) < 0); } + @Test + public void next() { + 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), new Version(major+1, 0, 0)); + Assert.assertEquals(version.next(Version.Element.MINOR), new Version(major, minor+1, 0)); + Assert.assertEquals(version.next(Version.Element.PATCH), new Version(major, minor, patch+1)); + } + + @Test(expected=IllegalArgumentException.class) + public void shouldNextWithNullComparisonTypeFail() { + final int major = 1; + final int minor = 2; + final int patch = 3; + final Version version = new Version(major, minor, patch); + + version.next(null); + } + } |