From 0dd20f8d081e55f8f368d70a2cafaf626c32dfc1 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sat, 30 Jan 2016 12:33:16 -0500 Subject: [PATCH] Add tests for the updaters, improve the check command --- .../comphenix/protocol/CommandProtocol.java | 23 +++-- .../comphenix/protocol/ProtocolLibrary.java | 4 +- .../protocol/updater/BukkitUpdater.java | 97 ++++++++++--------- .../protocol/updater/SpigotUpdater.java | 7 ++ .../comphenix/protocol/updater/Updater.java | 8 +- .../protocol/updater/UpdaterTest.java | 62 ++++++++++++ 6 files changed, 142 insertions(+), 59 deletions(-) create mode 100644 ProtocolLib/src/test/java/com/comphenix/protocol/updater/UpdaterTest.java diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java index a5352f1b..af02889b 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java @@ -69,9 +69,9 @@ class CommandProtocol extends CommandBase { if (subCommand.equalsIgnoreCase("config") || subCommand.equalsIgnoreCase("reload")) { reloadConfiguration(sender); } else if (subCommand.equalsIgnoreCase("check")) { - checkVersion(sender); + checkVersion(sender, true); } else if (subCommand.equalsIgnoreCase("update")) { - updateVersion(sender); + updateVersion(sender, true); } else if (subCommand.equalsIgnoreCase("timings")) { toggleTimings(sender, args); } else if (subCommand.equalsIgnoreCase("listeners")) { @@ -87,12 +87,12 @@ class CommandProtocol extends CommandBase { return true; } - public void checkVersion(final CommandSender sender) { - performUpdate(sender, UpdateType.NO_DOWNLOAD); + public void checkVersion(final CommandSender sender, boolean command) { + performUpdate(sender, UpdateType.NO_DOWNLOAD, command); } - public void updateVersion(final CommandSender sender) { - performUpdate(sender, UpdateType.DEFAULT); + public void updateVersion(final CommandSender sender, boolean command) { + performUpdate(sender, UpdateType.DEFAULT, command); } // Display every listener on the server @@ -111,7 +111,7 @@ class CommandProtocol extends CommandBase { } } - private void performUpdate(final CommandSender sender, UpdateType type) { + private void performUpdate(final CommandSender sender, UpdateType type, final boolean command) { if (updater.isChecking()) { sender.sendMessage(ChatColor.RED + "Already checking for an update."); return; @@ -121,7 +121,14 @@ class CommandProtocol extends CommandBase { Runnable notify = new Runnable() { @Override public void run() { - if (updater.shouldNotify() || config.isDebug()) { + if (command) { + sender.sendMessage(ChatColor.YELLOW + "[ProtocolLib] " + updater.getResult()); + String remoteVersion = updater.getRemoteVersion(); + if (remoteVersion != null) { + sender.sendMessage(ChatColor.YELLOW + "Remote version: " + remoteVersion); + sender.sendMessage(ChatColor.YELLOW + "Current version: " + plugin.getDescription().getVersion()); + } + } else if (updater.shouldNotify() || config.isDebug()) { sender.sendMessage(ChatColor.YELLOW + "[ProtocolLib] " + updater.getResult()); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java index c09a80f4..1582de8b 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java @@ -615,9 +615,9 @@ public class ProtocolLibrary extends JavaPlugin { if (currentTime > updateTime && !updater.isChecking()) { // Initiate the update as if it came from the console if (config.isAutoDownload()) - commandProtocol.updateVersion(getServer().getConsoleSender()); + commandProtocol.updateVersion(getServer().getConsoleSender(), false); else if (config.isAutoNotify()) - commandProtocol.checkVersion(getServer().getConsoleSender()); + commandProtocol.checkVersion(getServer().getConsoleSender(), false); else commandProtocol.updateFinished(); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/BukkitUpdater.java b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/BukkitUpdater.java index 72342403..4a835906 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/BukkitUpdater.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/BukkitUpdater.java @@ -22,6 +22,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; @@ -78,57 +79,60 @@ public class BukkitUpdater extends Updater { * @param type Specify the type of update this will be. See {@link UpdateType} * @param announce True if the program should announce the progress of new updates in console */ - public BukkitUpdater(ProtocolLibrary plugin, int id, File file, UpdateType type, boolean announce) { + public BukkitUpdater(Plugin plugin, int id, File file, UpdateType type, boolean announce) { super(plugin, type, announce); this.file = file; this.id = id; this.updateFolder = plugin.getServer().getUpdateFolder(); - final File pluginFile = plugin.getDataFolder().getParentFile(); - final File updaterFile = new File(pluginFile, "Updater"); - final File updaterConfigFile = new File(updaterFile, "config.yml"); + File dataFolder = plugin.getDataFolder(); + if (dataFolder != null) { + final File pluginFile = plugin.getDataFolder().getParentFile(); + final File updaterFile = new File(pluginFile, "Updater"); + final File updaterConfigFile = new File(updaterFile, "config.yml"); - if (!updaterFile.exists()) { - updaterFile.mkdir(); + if (!updaterFile.exists()) { + updaterFile.mkdir(); + } + if (!updaterConfigFile.exists()) { + try { + updaterConfigFile.createNewFile(); + } catch (final IOException e) { + plugin.getLogger().severe("The updater could not create a configuration in " + updaterFile.getAbsolutePath()); + e.printStackTrace(); + } + } + this.config = YamlConfiguration.loadConfiguration(updaterConfigFile); + + this.config.options().header("This configuration file affects all plugins using the Updater system (version 2+ - http://forums.bukkit.org/threads/96681/ )" + '\n' + + "If you wish to use your API key, read http://wiki.bukkit.org/ServerMods_API and place it below." + '\n' + + "Some updating systems will not adhere to the disabled value, but these may be turned off in their plugin's configuration."); + this.config.addDefault("api-key", "PUT_API_KEY_HERE"); + this.config.addDefault("disable", false); + + if (this.config.get("api-key", null) == null) { + this.config.options().copyDefaults(true); + try { + this.config.save(updaterConfigFile); + } catch (final IOException e) { + plugin.getLogger().severe("The updater could not save the configuration in " + updaterFile.getAbsolutePath()); + e.printStackTrace(); + } + } + + if (this.config.getBoolean("disable")) { + this.result = UpdateResult.DISABLED; + return; + } + + String key = this.config.getString("api-key"); + if (key.equalsIgnoreCase("PUT_API_KEY_HERE") || key.equals("")) { + key = null; + } + + this.apiKey = key; } - if (!updaterConfigFile.exists()) { - try { - updaterConfigFile.createNewFile(); - } catch (final IOException e) { - plugin.getLogger().severe("The updater could not create a configuration in " + updaterFile.getAbsolutePath()); - e.printStackTrace(); - } - } - this.config = YamlConfiguration.loadConfiguration(updaterConfigFile); - - this.config.options().header("This configuration file affects all plugins using the Updater system (version 2+ - http://forums.bukkit.org/threads/96681/ )" + '\n' - + "If you wish to use your API key, read http://wiki.bukkit.org/ServerMods_API and place it below." + '\n' - + "Some updating systems will not adhere to the disabled value, but these may be turned off in their plugin's configuration."); - this.config.addDefault("api-key", "PUT_API_KEY_HERE"); - this.config.addDefault("disable", false); - - if (this.config.get("api-key", null) == null) { - this.config.options().copyDefaults(true); - try { - this.config.save(updaterConfigFile); - } catch (final IOException e) { - plugin.getLogger().severe("The updater could not save the configuration in " + updaterFile.getAbsolutePath()); - e.printStackTrace(); - } - } - - if (this.config.getBoolean("disable")) { - this.result = UpdateResult.DISABLED; - return; - } - - String key = this.config.getString("api-key"); - if (key.equalsIgnoreCase("PUT_API_KEY_HERE") || key.equals("")) { - key = null; - } - - this.apiKey = key; try { this.url = new URL(BukkitUpdater.HOST + BukkitUpdater.QUERY + id); @@ -314,7 +318,7 @@ public class BukkitUpdater extends Updater { return false; } */ - private boolean read() { + public boolean read() { try { final URLConnection conn = this.url.openConnection(); conn.setConnectTimeout(5000); @@ -407,8 +411,7 @@ public class BukkitUpdater extends Updater { } @Override - public boolean shouldNotify() { - // TODO Auto-generated method stub - return false; + public String getRemoteVersion() { + return getLatestName(); } } \ No newline at end of file diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java index 252adafd..17b4604a 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java @@ -34,10 +34,12 @@ import com.google.common.base.Charsets; */ public final class SpigotUpdater extends Updater { + private ProtocolLibrary plugin; private String remoteVersion; public SpigotUpdater(ProtocolLibrary plugin, UpdateType type, boolean announce) { super(plugin, type, announce); + this.plugin = plugin; } @Override @@ -110,4 +112,9 @@ public final class SpigotUpdater extends Updater { closer.close(); } } + + @Override + public String getRemoteVersion() { + return remoteVersion; + } } \ No newline at end of file diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/Updater.java b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/Updater.java index fa47c935..bf91a441 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/Updater.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/Updater.java @@ -20,6 +20,8 @@ import java.io.File; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import org.bukkit.plugin.Plugin; + import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.error.ReportType; import com.comphenix.protocol.utility.MinecraftVersion; @@ -31,7 +33,7 @@ import com.google.common.base.Preconditions; */ public abstract class Updater { - protected ProtocolLibrary plugin; + protected Plugin plugin; protected String versionName; protected String versionLink; @@ -48,7 +50,7 @@ public abstract class Updater { public static final ReportType REPORT_CANNOT_UPDATE_PLUGIN = new ReportType("Cannot update ProtocolLib."); - protected Updater(ProtocolLibrary plugin, UpdateType type, boolean announce) { + protected Updater(Plugin plugin, UpdateType type, boolean announce) { this.plugin = plugin; this.type = type; this.announce = announce; @@ -285,4 +287,6 @@ public abstract class Updater { return false; } } + + public abstract String getRemoteVersion(); } diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/updater/UpdaterTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/updater/UpdaterTest.java new file mode 100644 index 00000000..4ac0eef8 --- /dev/null +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/updater/UpdaterTest.java @@ -0,0 +1,62 @@ +/** + * (c) 2016 dmulloy2 + */ +package com.comphenix.protocol.updater; + +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.logging.Logger; + +import org.bukkit.Server; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginDescriptionFile; +import org.junit.Test; + +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.updater.Updater.UpdateType; + +/** + * @author dmulloy2 + */ + +public class UpdaterTest { + private static final int BUKKIT_DEV_ID = 45564; + + @Test + public void testSpigotUpdater() { + SpigotUpdater updater = new SpigotUpdater(null, UpdateType.NO_DOWNLOAD, true); + + String remote = null; + + try { + remote = updater.getSpigotVersion(); + } catch (Throwable ex) { + ex.printStackTrace(); + fail("Failed to check for updates"); + } + + System.out.println("Determined remote Spigot version: " + remote); + } + + @Test + public void testBukkitUpdater() { + Server server = mock(Server.class); + when(server.getUpdateFolder()).thenReturn(null); + + Plugin plugin = mock(Plugin.class); + when(plugin.getDescription()).thenReturn(new PluginDescriptionFile("ProtocolLib", ProtocolLibrary.class.getPackage().getImplementationVersion(), null)); + when(plugin.getLogger()).thenReturn(Logger.getLogger("ProtocolLib")); + when(plugin.getDataFolder()).thenReturn(null); + when(plugin.getServer()).thenReturn(server); + + BukkitUpdater updater = new BukkitUpdater(plugin, BUKKIT_DEV_ID, null, UpdateType.NO_DOWNLOAD, true); + if (! updater.read()) { + fail("Failed to check for updates"); + } + + String remote = updater.getLatestName(); + System.out.println("Determined remote Bukkit Dev version: " + remote); + } +} \ No newline at end of file