Improves version checking to prevent and update notice when running an unreleased version
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2021-08-03 11:50:32 +02:00
parent 6ca49d2ccd
commit 85febdf41b
2 changed files with 47 additions and 1 deletions

View File

@ -51,7 +51,7 @@ public final class Updater {
JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject();
String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString();
if (!oldType.equals(updateChannel) || !oldVer.equals(latest)) {
if (!oldType.equals(updateChannel) || isVersionHigher(oldVer, latest)) {
JsonArray versionList = jsonObject.getAsJsonArray("versions");
String url = "";
for (JsonElement elem : versionList) {
@ -77,6 +77,31 @@ public final class Updater {
}
}
/**
* Decides whether one version number is higher than another
*
* @param oldVersion <p>The old version to check</p>
* @param newVersion <p>The new version to check</p>
* @return <p>True if the new version is higher than the old one</p>
*/
public static boolean isVersionHigher(String oldVersion, String newVersion) {
String[] oldVersionParts = oldVersion.split("\\.");
String[] newVersionParts = newVersion.split("\\.");
int[] oldVersionInts = new int[]{Integer.parseInt(oldVersionParts[0]), Integer.parseInt(oldVersionParts[1]),
Integer.parseInt(oldVersionParts[2])};
int[] newVersionInts = new int[]{Integer.parseInt(newVersionParts[0]), Integer.parseInt(newVersionParts[1]),
Integer.parseInt(newVersionParts[2])};
if (newVersionInts[0] > oldVersionInts[0]) {
return true;
} else if (newVersionInts[0] == oldVersionInts[0]) {
if (newVersionInts[1] > oldVersionInts[1]) {
return true;
}
return newVersionInts[1] == oldVersionInts[1] && newVersionInts[2] > oldVersionInts[2];
}
return false;
}
/**
* Updates the software
*

View File

@ -0,0 +1,21 @@
package net.knarcraft.minecraftserverlauncher.utility;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class UpdaterTest {
@Test
public void isVersionHigherTest() {
assertTrue(Updater.isVersionHigher("1.1.1", "1.1.2"));
assertFalse(Updater.isVersionHigher("1.1.1", "1.1.1"));
assertFalse(Updater.isVersionHigher("1.1.1", "1.1.0"));
assertTrue(Updater.isVersionHigher("1.1.1", "1.2.1"));
assertFalse(Updater.isVersionHigher("1.2.1", "1.1.6"));
assertTrue(Updater.isVersionHigher("1.2.1", "2.1.6"));
assertTrue(Updater.isVersionHigher("1.2.1", "2.0.0"));
}
}