diff --git a/src/main/java/com/github/zafarkhaja/semver/Version.java b/src/main/java/com/github/zafarkhaja/semver/Version.java index 020109e..6cd3477 100644 --- a/src/main/java/com/github/zafarkhaja/semver/Version.java +++ b/src/main/java/com/github/zafarkhaja/semver/Version.java @@ -85,19 +85,29 @@ public class Version implements Comparable { */ private String build; + /** + * Constructs a {@code Builder} instance. + */ + public Builder() { + + } + /** * Constructs a {@code Builder} instance with the * string representation of the normal version. * * @param normal the string representation of the normal version - * @throws NullPointerException if the specified normal version is null */ public Builder(String normal) { - if (normal == null) { - throw new NullPointerException( - "Normal version MUST NOT be NULL" - ); - } + this.normal = normal; + } + + /** + * Sets the normal version. + * + * @param normal the string representation of the normal version + */ + public void setNormalVersion(String normal) { this.normal = normal; } @@ -125,11 +135,27 @@ public class Version implements Comparable { * @return a newly built {@code Version} instance */ public Version build() { - return new Version( - VersionParser.parseVersionCore(normal), - VersionParser.parsePreRelease(preRelease), - VersionParser.parseBuild(build) - ); + StringBuilder sb = new StringBuilder(); + if (isFilled(normal)) { + sb.append(normal); + } + if (isFilled(preRelease)) { + sb.append(PRE_RELEASE_PREFIX).append(preRelease); + } + if (isFilled(build)) { + sb.append(BUILD_PREFIX).append(build); + } + return VersionParser.parseValidSemVer(sb.toString()); + } + + /** + * Checks if a string has a usable value. + * + * @param str the string to check + * @return {@code true} if the string is filled or {@code false} otherwise + */ + private boolean isFilled(String str) { + return str != null && !str.isEmpty(); } } @@ -221,6 +247,7 @@ public class Version implements Comparable { * * @param version the version string to parse * @return a new instance of the {@code Version} class + * @throws IllegalArgumentException if the input string is {@code NULL} or empty */ public static Version valueOf(String version) { return VersionParser.parseValidSemVer(version); @@ -294,6 +321,7 @@ public class Version implements Comparable { * * @param preRelease the pre-release version to append * @return a new instance of the {@code Version} class + * @throws IllegalArgumentException if the input string is {@code NULL} or empty */ public Version incrementMajorVersion(String preRelease) { return new Version( @@ -316,6 +344,7 @@ public class Version implements Comparable { * * @param preRelease the pre-release version to append * @return a new instance of the {@code Version} class + * @throws IllegalArgumentException if the input string is {@code NULL} or empty */ public Version incrementMinorVersion(String preRelease) { return new Version( @@ -338,6 +367,7 @@ public class Version implements Comparable { * * @param preRelease the pre-release version to append * @return a new instance of the {@code Version} class + * @throws IllegalArgumentException if the input string is {@code NULL} or empty */ public Version incrementPatchVersion(String preRelease) { return new Version( @@ -369,6 +399,7 @@ public class Version implements Comparable { * * @param preRelease the pre-release version to set * @return a new instance of the {@code Version} class + * @throws IllegalArgumentException if the input string is {@code NULL} or empty */ public Version setPreReleaseVersion(String preRelease) { return new Version(normal, VersionParser.parsePreRelease(preRelease)); @@ -379,6 +410,7 @@ public class Version implements Comparable { * * @param build the build metadata to set * @return a new instance of the {@code Version} class + * @throws IllegalArgumentException if the input string is {@code NULL} or empty */ public Version setBuildMetadata(String build) { return new Version(normal, preRelease, VersionParser.parseBuild(build)); diff --git a/src/main/java/com/github/zafarkhaja/semver/VersionParser.java b/src/main/java/com/github/zafarkhaja/semver/VersionParser.java index 7f14bfd..bbeb0d1 100644 --- a/src/main/java/com/github/zafarkhaja/semver/VersionParser.java +++ b/src/main/java/com/github/zafarkhaja/semver/VersionParser.java @@ -124,8 +124,12 @@ class VersionParser implements Parser { * with the input string to parse. * * @param input the input string to parse + * @throws IllegalArgumentException if the input string is {@code NULL} or empty */ VersionParser(String input) { + if (input == null || input.isEmpty()) { + throw new IllegalArgumentException("Input string is NULL or empty"); + } Character[] elements = new Character[input.length()]; for (int i = 0; i < input.length(); i++) { elements[i] = Character.valueOf(input.charAt(i)); @@ -152,6 +156,7 @@ class VersionParser implements Parser { * * @param version the version string to parse * @return a valid version object + * @throws IllegalArgumentException if the input string is {@code NULL} or empty * @throws ParseException when there is an error defined in * the SemVer or the formal grammar * @throws UnexpectedElementException when encounters an unexpected character type @@ -166,6 +171,7 @@ class VersionParser implements Parser { * * @param versionCore the version core string to parse * @return a valid normal version object + * @throws IllegalArgumentException if the input string is {@code NULL} or empty * @throws ParseException when there is an error defined in * the SemVer or the formal grammar * @throws UnexpectedElementException when encounters an unexpected character type @@ -180,13 +186,11 @@ class VersionParser implements Parser { * * @param preRelease the pre-release version string to parse * @return a valid pre-release version object + * @throws IllegalArgumentException if the input string is {@code NULL} or empty * @throws ParseException when there is an error defined in * the SemVer or the formal grammar */ static MetadataVersion parsePreRelease(String preRelease) { - if (preRelease == null) { - return MetadataVersion.NULL; - } VersionParser parser = new VersionParser(preRelease); return parser.parsePreRelease(); } @@ -196,13 +200,11 @@ class VersionParser implements Parser { * * @param build the build metadata string to parse * @return a valid build metadata object + * @throws IllegalArgumentException if the input string is {@code NULL} or empty * @throws ParseException when there is an error defined in * the SemVer or the formal grammar */ static MetadataVersion parseBuild(String build) { - if (build == null) { - return MetadataVersion.NULL; - } VersionParser parser = new VersionParser(build); return parser.parseBuild(); } diff --git a/src/test/java/com/github/zafarkhaja/semver/VersionParserTest.java b/src/test/java/com/github/zafarkhaja/semver/VersionParserTest.java index a2cfc39..36b8cea 100644 --- a/src/test/java/com/github/zafarkhaja/semver/VersionParserTest.java +++ b/src/test/java/com/github/zafarkhaja/semver/VersionParserTest.java @@ -54,12 +54,6 @@ public class VersionParserTest { assertEquals(new MetadataVersion(new String[] {"beta-1", "1"}), preRelease); } - @Test - public void shouldReturnNullMetadataVersionIfPreReleaseIsNull() { - MetadataVersion preRelease = VersionParser.parsePreRelease(null); - assertEquals(MetadataVersion.NULL, preRelease); - } - @Test public void shouldNotAllowDigitsInPreReleaseVersion() { try { @@ -86,12 +80,6 @@ public class VersionParserTest { assertEquals(new MetadataVersion(new String[] {"build", "1"}), build); } - @Test - public void shouldReturnNullMetadataVersionIfBuildIsNull() { - MetadataVersion build = VersionParser.parseBuild(null); - assertEquals(MetadataVersion.NULL, build); - } - @Test public void shouldAllowDigitsInBuildMetadata() { try { @@ -124,4 +112,16 @@ public class VersionParserTest { version ); } + + @Test + public void shouldRaiseErrorForIllegalInputString() { + for (String illegal : new String[] { "", null }) { + try { + new VersionParser(illegal); + } catch (IllegalArgumentException e) { + continue; + } + fail("Should raise error for illegal input string"); + } + } } diff --git a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java index 53b1a6f..edae560 100644 --- a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java +++ b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java @@ -394,13 +394,12 @@ public class VersionTest { public static class BuilderTest { @Test - public void shouldThrowNullPointerExceptionIfNormalVersionIsNull() { - try { - new Version.Builder(null); - } catch (NullPointerException e) { - return; - } - fail("Builder was expected to throw NullPointerException"); + public void shouldBuildVersionInSteps() { + Version.Builder builder = new Version.Builder(); + builder.setNormalVersion("1.0.0"); + builder.setPreReleaseVersion("alpha"); + builder.setBuildMetadata("build"); + assertEquals(Version.valueOf("1.0.0-alpha+build"), builder.build()); } @Test