1.6.3: fix Version.toString() nikiroo-utils-1.6.3
authorNiki Roo <niki@nikiroo.be>
Thu, 6 Jul 2017 06:25:24 +0000 (08:25 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 6 Jul 2017 06:25:24 +0000 (08:25 +0200)
Makefile.base
VERSION
changelog
export.sh [new file with mode: 0755]
src/be/nikiroo/utils/Version.java
src/be/nikiroo/utils/test/VersionTest.java

index 3304c83c802f47d4d5ede0e5cb33c5a252bd0552..3143778872e7de4e4f0e25d5afb82443dd860bc4 100644 (file)
@@ -1,3 +1,8 @@
+# Makefile base template
+# 
+# Version:
+# - 1.0.0: add a version comment
+
 # Required parameters (the commented out ones are supposed to change per project):
 
 #MAIN = path to main java source to compile
diff --git a/VERSION b/VERSION
index fdd3be6df54a88720553509b7a545f73294a867e..266146b87cbc8e6d59fe83c426a8500f9549adfb 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.6.2
+1.6.3
index da05339e66cbbb50234a78448f49cb074dcc7e26..2069eacf70941216143e5083a457ac5c09ecc1e4 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,9 @@
+Version 1.6.3
+-------------
+
+Version.java
+       Fix toString issues + test + update scripts
+
 Version 1.6.2
 -------------
 
diff --git a/export.sh b/export.sh
new file mode 100755 (executable)
index 0000000..b0fdddf
--- /dev/null
+++ b/export.sh
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+# Export script
+# 
+# Version:
+# - 1.0.0: add a version comment
+
+cd "`dirname "$0"`"
+
+if [ "$1" = "" ]; then
+       echo "You need to specify where to export it" >&2
+       exit 1
+elif [ ! -d "$1/libs" ]; then
+       echo "The target export directory is not compatible" >&2
+       exit 2
+fi
+
+LIBNAME="`cat configure.sh | grep '^echo "NAME = ' | cut -d'"' -f2 | cut -d= -f2`"
+LIBNAME="`echo $LIBNAME`"
+
+make mrpropre
+./configure.sh && make \
+       && cp "$LIBNAME"-`cat VERSION`-sources.jar "$1"/libs/ \
+       && cp "$LIBNAME".jar "$1"/libs/
+
index 2f10c5eba8f982076737543002b9a3f0176596e2..2c6f3d0913578601c9ee65dcc0c3486fd8971648 100644 (file)
@@ -80,14 +80,7 @@ public class Version implements Comparable<Version> {
                this.tag = tag;
                this.tagVersion = tagVersion;
 
-               String tagSuffix = "";
-               if (tag != null) {
-                       tagSuffix = "-" + tag
-                                       + (tagVersion >= 0 ? Integer.toString(tagVersion) : "");
-               }
-
-               this.version = String.format("%d.%d.%d%s", major, minor, patch,
-                               tagSuffix);
+               this.version = generateVersion();
        }
 
        /**
@@ -102,18 +95,19 @@ public class Version implements Comparable<Version> {
        public Version(String version) {
                try {
                        String[] tab = version.split("\\.");
-                       this.major = Integer.parseInt(tab[0]);
-                       this.minor = Integer.parseInt(tab[1]);
+                       this.major = Integer.parseInt(tab[0].trim());
+                       this.minor = Integer.parseInt(tab[1].trim());
                        if (tab[2].contains("-")) {
                                int posInVersion = version.indexOf('.');
                                posInVersion = version.indexOf('.', posInVersion + 1);
                                String rest = version.substring(posInVersion + 1);
 
                                int posInRest = rest.indexOf('-');
-                               this.patch = Integer.parseInt(rest.substring(0, posInRest));
+                               this.patch = Integer.parseInt(rest.substring(0, posInRest)
+                                               .trim());
 
                                posInVersion = version.indexOf('-');
-                               this.tag = version.substring(posInVersion + 1);
+                               this.tag = version.substring(posInVersion + 1).trim();
                                this.tagVersion = -1;
 
                                StringBuilder str = new StringBuilder();
@@ -127,12 +121,12 @@ public class Version implements Comparable<Version> {
                                        this.tagVersion = Integer.parseInt(str.toString());
                                }
                        } else {
-                               this.patch = Integer.parseInt(tab[2]);
+                               this.patch = Integer.parseInt(tab[2].trim());
                                this.tag = null;
                                this.tagVersion = -1;
                        }
 
-                       this.version = toString();
+                       this.version = generateVersion();
                } catch (Exception e) {
                        this.major = 0;
                        this.minor = 0;
@@ -189,7 +183,7 @@ public class Version implements Comparable<Version> {
        }
 
        /**
-        * The version of the tag.
+        * The version of the tag, or -1 for no version.
         * 
         * @return the tag version
         */
@@ -200,13 +194,11 @@ public class Version implements Comparable<Version> {
        /**
         * Check if this {@link Version} is "empty" (i.e., the version was not
         * parse-able or not given).
-        * <p>
-        * An empty {@link Version} is always <tt>0.0.0</tt>.
         * 
         * @return TRUE if it is empty
         */
        public boolean isEmpty() {
-               return major == 0 && minor == 0 && patch == 0 && tag == null;
+               return version == null;
        }
 
        /**
@@ -214,12 +206,20 @@ public class Version implements Comparable<Version> {
         * <p>
         * Note that a tagged version is considered newer than a non-tagged version,
         * but two tagged versions with different tags are not comparable.
+        * <p>
+        * Also, an empty version is always considered older.
         * 
         * @param o
         *            the other {@link Version}
         * @return TRUE if this {@link Version} is more recent than the given one
         */
        public boolean isNewerThan(Version o) {
+               if (isEmpty()) {
+                       return false;
+               } else if (o.isEmpty()) {
+                       return true;
+               }
+
                if (major > o.major) {
                        return true;
                }
@@ -251,12 +251,23 @@ public class Version implements Comparable<Version> {
 
        /**
         * Check if we are older than the given {@link Version}.
+        * <p>
+        * Note that a tagged version is considered newer than a non-tagged version,
+        * but two tagged versions with different tags are not comparable.
+        * <p>
+        * Also, an empty version is always considered older.
         * 
         * @param o
         *            the other {@link Version}
         * @return TRUE if this {@link Version} is older than the given one
         */
        public boolean isOlderThan(Version o) {
+               if (o.isEmpty()) {
+                       return false;
+               } else if (isEmpty()) {
+                       return true;
+               }
+
                // 2 <> tagged versions are not comparable
                boolean sameTag = (tag == null && o.tag == null)
                                || (tag != null && tag.equals(o.tag));
@@ -311,6 +322,10 @@ public class Version implements Comparable<Version> {
        public boolean equals(Object obj) {
                if (obj instanceof Version) {
                        Version o = (Version) obj;
+                       if (isEmpty()) {
+                               return o.isEmpty();
+                       }
+
                        boolean sameTag = (tag == null && o.tag == null)
                                        || (tag != null && tag.equals(o.tag));
                        return o.major == major && o.minor == minor && o.patch == patch
@@ -332,4 +347,19 @@ public class Version implements Comparable<Version> {
        public String toString() {
                return version == null ? "[unknown]" : version;
        }
+
+       /**
+        * Generate the clean version {@link String} from the current values.
+        * 
+        * @return the clean version string
+        */
+       private String generateVersion() {
+               String tagSuffix = "";
+               if (tag != null) {
+                       tagSuffix = "-" + tag
+                                       + (tagVersion >= 0 ? Integer.toString(tagVersion) : "");
+               }
+
+               return String.format("%d.%d.%d%s", major, minor, patch, tagSuffix);
+       }
 }
index f6f71322f51cbb535cee0954112f964444f89cbd..75e1a1cf261b4f7eaebeb931045bdebd447be118 100644 (file)
@@ -67,6 +67,14 @@ class VersionTest extends TestLauncher {
                                version = new Version("1.0.0-debian-12");
                                assertEquals("debian-", version.getTag());
                                assertEquals(12, version.getTagVersion());
+
+                               // tag with no tag version
+                               version = new Version("1.0.0-dev");
+                               assertEquals(1, version.getMajor());
+                               assertEquals(0, version.getMinor());
+                               assertEquals(0, version.getPatch());
+                               assertEquals("dev", version.getTag());
+                               assertEquals(-1, version.getTagVersion());
                        }
                });
 
@@ -103,5 +111,28 @@ class VersionTest extends TestLauncher {
                                                                1, 0, 1, "not-my.tag.", 2)));
                        }
                });
+
+               addTest(new TestCase("toString") {
+                       @Override
+                       public void test() throws Exception {
+                               // Check leading 0s:
+                               Version version = new Version("01.002.4");
+                               assertEquals("Leading 0s not working", "1.2.4",
+                                               version.toString());
+
+                               // Check spacing
+                               version = new Version("1 . 2.4 ");
+                               assertEquals("Additional spaces not working", "1.2.4",
+                                               version.toString());
+
+                               String[] tests = new String[] { "1.0.0", "1.2.3", "1.0.0-dev",
+                                               "1.1.2-niki0" };
+                               for (String test : tests) {
+                                       version = new Version(test);
+                                       assertEquals("toString and back conversion failed", test,
+                                                       version.toString());
+                               }
+                       }
+               });
        }
 }