[jsemver] 71/95: Refactor VersionParser to handle illegal inputs

Alexandre Viau reazem-guest at moszumanska.debian.org
Mon Feb 16 14:58:31 UTC 2015


This is an automated email from the git hooks/post-receive script.

reazem-guest pushed a commit to branch master
in repository jsemver.

commit 988059b4440b89029e309cc607cda8aef0caebb7
Author: Zafar Khaja <zafarkhaja at gmail.com>
Date:   Thu Jan 23 18:43:11 2014 +0400

    Refactor VersionParser to handle illegal inputs
---
 .../java/com/github/zafarkhaja/semver/Version.java | 54 +++++++++++++++++-----
 .../github/zafarkhaja/semver/VersionParser.java    | 14 +++---
 .../zafarkhaja/semver/VersionParserTest.java       | 24 +++++-----
 .../com/github/zafarkhaja/semver/VersionTest.java  | 13 +++---
 4 files changed, 69 insertions(+), 36 deletions(-)

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
@@ -86,18 +86,28 @@ public class Version implements Comparable<Version> {
         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<Version> {
          * @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<Version> {
      *
      * @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<Version> {
      *
      * @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<Version> {
      *
      * @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<Version> {
      *
      * @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<Version> {
      *
      * @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<Version> {
      *
      * @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<Version> {
      * 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<Version> {
      *
      * @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<Version> {
      *
      * @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<Version> {
      *
      * @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<Version> {
      *
      * @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
@@ -55,12 +55,6 @@ public class VersionParserTest {
     }
 
     @Test
-    public void shouldReturnNullMetadataVersionIfPreReleaseIsNull() {
-        MetadataVersion preRelease = VersionParser.parsePreRelease(null);
-        assertEquals(MetadataVersion.NULL, preRelease);
-    }
-
-    @Test
     public void shouldNotAllowDigitsInPreReleaseVersion() {
         try {
             VersionParser.parsePreRelease("alpha.01");
@@ -87,12 +81,6 @@ public class VersionParserTest {
     }
 
     @Test
-    public void shouldReturnNullMetadataVersionIfBuildIsNull() {
-        MetadataVersion build = VersionParser.parseBuild(null);
-        assertEquals(MetadataVersion.NULL, build);
-    }
-
-    @Test
     public void shouldAllowDigitsInBuildMetadata() {
         try {
             VersionParser.parseBuild("build.01");
@@ -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

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jsemver.git



More information about the pkg-java-commits mailing list