From 11b5dc66e5b1aebe419cc73c036f98b03920f24e Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Fri, 2 Sep 2016 16:38:00 +0200 Subject: [PATCH] Version check now sees 1.0.4-pre.1 as older than 1.0.4, but 1.0.4-abd332 as newer. --- .../fabianonline/telegram_backup/Utils.java | 33 +++++++++++++++---- .../utils/CompareVersionsTest.java | 1 + 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/fabianonline/telegram_backup/Utils.java b/src/main/java/de/fabianonline/telegram_backup/Utils.java index 04ac672..b5c3117 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Utils.java +++ b/src/main/java/de/fabianonline/telegram_backup/Utils.java @@ -102,8 +102,8 @@ public class Utils { logger.debug("Comparing versions {} and {}.", v1, v2); if (v1.equals(v2)) return VERSIONS_EQUAL; - String[] v1_p = v1.split("-"); - String[] v2_p = v2.split("-"); + String[] v1_p = v1.split("-", 2); + String[] v2_p = v2.split("-", 2); logger.trace("Parts to compare without suffixes: {} and {}.", v1_p[0], v2_p[0]); @@ -134,12 +134,33 @@ public class Utils { return VERSION_2_NEWER; } + // startsWith if (v1_p.length>1 && v2_p.length==1) { - logger.debug("v1 has a suffix, v2 not - so v2 is newer."); - return VERSION_2_NEWER; + logger.debug("v1 has a suffix, v2 not."); + if (v1_p[1].startsWith("pre")) { + logger.debug("v1 is a pre version, so v1 is newer"); + return VERSION_2_NEWER; + } else { + return VERSION_1_NEWER; + } } else if (v1_p.length==1 && v2_p.length>1) { - logger.debug("v1 has no suffix, but v2 has - so v1 is newer."); - return VERSION_1_NEWER; + logger.debug("v1 has no suffix, but v2 has"); + if (v2_p[1].startsWith("pre")) { + logger.debug("v2 is a pre version, so v1 is better"); + return VERSION_1_NEWER; + } else { + return VERSION_2_NEWER; + } + } else if (v1_p.length>1 && v2_p.length>1) { + logger.debug("Both have a suffix"); + if (v1_p[1].startsWith("pre") && !v2_p[1].startsWith("pre")) { + logger.debug("v1 is a 'pre' version, v2 not."); + return VERSION_2_NEWER; + } else if (!v1_p[1].startsWith("pre") && v2_p[1].startsWith("pre")) { + logger.debug("v2 is a 'pre' version, v2 not."); + return VERSION_1_NEWER; + } + return VERSIONS_EQUAL; } logger.debug("We couldn't find a real difference, so we're assuming the versions are equal-ish."); return VERSIONS_EQUAL; diff --git a/src/test/java/de/fabianonline/telegram_backup/utils/CompareVersionsTest.java b/src/test/java/de/fabianonline/telegram_backup/utils/CompareVersionsTest.java index edb4fd8..b8ac085 100644 --- a/src/test/java/de/fabianonline/telegram_backup/utils/CompareVersionsTest.java +++ b/src/test/java/de/fabianonline/telegram_backup/utils/CompareVersionsTest.java @@ -11,5 +11,6 @@ public class CompareVersionsTest { assertEquals(Utils.compareVersions("1.0.4", "1.0.4-pre.1"), Utils.VERSION_1_NEWER); assertEquals(Utils.compareVersions("1.0.4", "1.0.4"), Utils.VERSIONS_EQUAL); assertEquals(Utils.compareVersions("1.0.4-pre.2", "1.0.4-pre.1"), Utils.VERSIONS_EQUAL); + assertEquals(Utils.compareVersions("1.0.4", "1.0.4-abcdef-dirty"), Utils.VERSION_2_NEWER); } }