From 632cc690ac07130a51a947ee0f0b2bfae10b6595 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Thu, 6 Jul 2017 08:25:24 +0200 Subject: [PATCH] 1.6.3: fix Version.toString() --- Makefile.base | 5 ++ VERSION | 2 +- changelog | 6 ++ export.sh | 25 ++++++++ src/be/nikiroo/utils/Version.java | 66 ++++++++++++++++------ src/be/nikiroo/utils/test/VersionTest.java | 31 ++++++++++ 6 files changed, 116 insertions(+), 19 deletions(-) create mode 100755 export.sh diff --git a/Makefile.base b/Makefile.base index 3304c83..3143778 100644 --- a/Makefile.base +++ b/Makefile.base @@ -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 fdd3be6..266146b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.6.2 +1.6.3 diff --git a/changelog b/changelog index da05339..2069eac 100644 --- 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 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/ + diff --git a/src/be/nikiroo/utils/Version.java b/src/be/nikiroo/utils/Version.java index 2f10c5e..2c6f3d0 100644 --- a/src/be/nikiroo/utils/Version.java +++ b/src/be/nikiroo/utils/Version.java @@ -80,14 +80,7 @@ public class Version implements Comparable { 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 { 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 { 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 { } /** - * 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 { /** * Check if this {@link Version} is "empty" (i.e., the version was not * parse-able or not given). - *

- * An empty {@link Version} is always 0.0.0. * * @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 { *

* Note that a tagged version is considered newer than a non-tagged version, * but two tagged versions with different tags are not comparable. + *

+ * 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 { /** * Check if we are older than the given {@link Version}. + *

+ * Note that a tagged version is considered newer than a non-tagged version, + * but two tagged versions with different tags are not comparable. + *

+ * 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 { 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 { 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); + } } diff --git a/src/be/nikiroo/utils/test/VersionTest.java b/src/be/nikiroo/utils/test/VersionTest.java index f6f7132..75e1a1c 100644 --- a/src/be/nikiroo/utils/test/VersionTest.java +++ b/src/be/nikiroo/utils/test/VersionTest.java @@ -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()); + } + } + }); } } -- 2.27.0