Archiviert
13
0

Add tests for the updaters, improve the check command

Dieser Commit ist enthalten in:
Dan Mulloy 2016-01-30 12:33:16 -05:00
Ursprung 7630007833
Commit 0dd20f8d08
6 geänderte Dateien mit 142 neuen und 59 gelöschten Zeilen

Datei anzeigen

@ -69,9 +69,9 @@ class CommandProtocol extends CommandBase {
if (subCommand.equalsIgnoreCase("config") || subCommand.equalsIgnoreCase("reload")) { if (subCommand.equalsIgnoreCase("config") || subCommand.equalsIgnoreCase("reload")) {
reloadConfiguration(sender); reloadConfiguration(sender);
} else if (subCommand.equalsIgnoreCase("check")) { } else if (subCommand.equalsIgnoreCase("check")) {
checkVersion(sender); checkVersion(sender, true);
} else if (subCommand.equalsIgnoreCase("update")) { } else if (subCommand.equalsIgnoreCase("update")) {
updateVersion(sender); updateVersion(sender, true);
} else if (subCommand.equalsIgnoreCase("timings")) { } else if (subCommand.equalsIgnoreCase("timings")) {
toggleTimings(sender, args); toggleTimings(sender, args);
} else if (subCommand.equalsIgnoreCase("listeners")) { } else if (subCommand.equalsIgnoreCase("listeners")) {
@ -87,12 +87,12 @@ class CommandProtocol extends CommandBase {
return true; return true;
} }
public void checkVersion(final CommandSender sender) { public void checkVersion(final CommandSender sender, boolean command) {
performUpdate(sender, UpdateType.NO_DOWNLOAD); performUpdate(sender, UpdateType.NO_DOWNLOAD, command);
} }
public void updateVersion(final CommandSender sender) { public void updateVersion(final CommandSender sender, boolean command) {
performUpdate(sender, UpdateType.DEFAULT); performUpdate(sender, UpdateType.DEFAULT, command);
} }
// Display every listener on the server // 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()) { if (updater.isChecking()) {
sender.sendMessage(ChatColor.RED + "Already checking for an update."); sender.sendMessage(ChatColor.RED + "Already checking for an update.");
return; return;
@ -121,7 +121,14 @@ class CommandProtocol extends CommandBase {
Runnable notify = new Runnable() { Runnable notify = new Runnable() {
@Override @Override
public void run() { 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()); sender.sendMessage(ChatColor.YELLOW + "[ProtocolLib] " + updater.getResult());
} }

Datei anzeigen

@ -615,9 +615,9 @@ public class ProtocolLibrary extends JavaPlugin {
if (currentTime > updateTime && !updater.isChecking()) { if (currentTime > updateTime && !updater.isChecking()) {
// Initiate the update as if it came from the console // Initiate the update as if it came from the console
if (config.isAutoDownload()) if (config.isAutoDownload())
commandProtocol.updateVersion(getServer().getConsoleSender()); commandProtocol.updateVersion(getServer().getConsoleSender(), false);
else if (config.isAutoNotify()) else if (config.isAutoNotify())
commandProtocol.checkVersion(getServer().getConsoleSender()); commandProtocol.checkVersion(getServer().getConsoleSender(), false);
else else
commandProtocol.updateFinished(); commandProtocol.updateFinished();
} }

Datei anzeigen

@ -22,6 +22,7 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.JSONValue; 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 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 * @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); super(plugin, type, announce);
this.file = file; this.file = file;
this.id = id; this.id = id;
this.updateFolder = plugin.getServer().getUpdateFolder(); this.updateFolder = plugin.getServer().getUpdateFolder();
final File pluginFile = plugin.getDataFolder().getParentFile(); File dataFolder = plugin.getDataFolder();
final File updaterFile = new File(pluginFile, "Updater"); if (dataFolder != null) {
final File updaterConfigFile = new File(updaterFile, "config.yml"); final File pluginFile = plugin.getDataFolder().getParentFile();
final File updaterFile = new File(pluginFile, "Updater");
final File updaterConfigFile = new File(updaterFile, "config.yml");
if (!updaterFile.exists()) { if (!updaterFile.exists()) {
updaterFile.mkdir(); 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 { try {
this.url = new URL(BukkitUpdater.HOST + BukkitUpdater.QUERY + id); this.url = new URL(BukkitUpdater.HOST + BukkitUpdater.QUERY + id);
@ -314,7 +318,7 @@ public class BukkitUpdater extends Updater {
return false; return false;
} */ } */
private boolean read() { public boolean read() {
try { try {
final URLConnection conn = this.url.openConnection(); final URLConnection conn = this.url.openConnection();
conn.setConnectTimeout(5000); conn.setConnectTimeout(5000);
@ -407,8 +411,7 @@ public class BukkitUpdater extends Updater {
} }
@Override @Override
public boolean shouldNotify() { public String getRemoteVersion() {
// TODO Auto-generated method stub return getLatestName();
return false;
} }
} }

Datei anzeigen

@ -34,10 +34,12 @@ import com.google.common.base.Charsets;
*/ */
public final class SpigotUpdater extends Updater { public final class SpigotUpdater extends Updater {
private ProtocolLibrary plugin;
private String remoteVersion; private String remoteVersion;
public SpigotUpdater(ProtocolLibrary plugin, UpdateType type, boolean announce) { public SpigotUpdater(ProtocolLibrary plugin, UpdateType type, boolean announce) {
super(plugin, type, announce); super(plugin, type, announce);
this.plugin = plugin;
} }
@Override @Override
@ -110,4 +112,9 @@ public final class SpigotUpdater extends Updater {
closer.close(); closer.close();
} }
} }
@Override
public String getRemoteVersion() {
return remoteVersion;
}
} }

Datei anzeigen

@ -20,6 +20,8 @@ import java.io.File;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import org.bukkit.plugin.Plugin;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.error.ReportType; import com.comphenix.protocol.error.ReportType;
import com.comphenix.protocol.utility.MinecraftVersion; import com.comphenix.protocol.utility.MinecraftVersion;
@ -31,7 +33,7 @@ import com.google.common.base.Preconditions;
*/ */
public abstract class Updater { public abstract class Updater {
protected ProtocolLibrary plugin; protected Plugin plugin;
protected String versionName; protected String versionName;
protected String versionLink; protected String versionLink;
@ -48,7 +50,7 @@ public abstract class Updater {
public static final ReportType REPORT_CANNOT_UPDATE_PLUGIN = new ReportType("Cannot update ProtocolLib."); 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.plugin = plugin;
this.type = type; this.type = type;
this.announce = announce; this.announce = announce;
@ -285,4 +287,6 @@ public abstract class Updater {
return false; return false;
} }
} }
public abstract String getRemoteVersion();
} }

Datei anzeigen

@ -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);
}
}