Skip to content

Commit 0dd20f8

Browse files
committed
Add tests for the updaters, improve the check command
1 parent 7630007 commit 0dd20f8

File tree

6 files changed

+140
-57
lines changed

6 files changed

+140
-57
lines changed

ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ protected boolean handleCommand(CommandSender sender, String[] args) {
6969
if (subCommand.equalsIgnoreCase("config") || subCommand.equalsIgnoreCase("reload")) {
7070
reloadConfiguration(sender);
7171
} else if (subCommand.equalsIgnoreCase("check")) {
72-
checkVersion(sender);
72+
checkVersion(sender, true);
7373
} else if (subCommand.equalsIgnoreCase("update")) {
74-
updateVersion(sender);
74+
updateVersion(sender, true);
7575
} else if (subCommand.equalsIgnoreCase("timings")) {
7676
toggleTimings(sender, args);
7777
} else if (subCommand.equalsIgnoreCase("listeners")) {
@@ -87,12 +87,12 @@ protected boolean handleCommand(CommandSender sender, String[] args) {
8787
return true;
8888
}
8989

90-
public void checkVersion(final CommandSender sender) {
91-
performUpdate(sender, UpdateType.NO_DOWNLOAD);
90+
public void checkVersion(final CommandSender sender, boolean command) {
91+
performUpdate(sender, UpdateType.NO_DOWNLOAD, command);
9292
}
9393

94-
public void updateVersion(final CommandSender sender) {
95-
performUpdate(sender, UpdateType.DEFAULT);
94+
public void updateVersion(final CommandSender sender, boolean command) {
95+
performUpdate(sender, UpdateType.DEFAULT, command);
9696
}
9797

9898
// Display every listener on the server
@@ -111,7 +111,7 @@ private void printListeners(final CommandSender sender) {
111111
}
112112
}
113113

114-
private void performUpdate(final CommandSender sender, UpdateType type) {
114+
private void performUpdate(final CommandSender sender, UpdateType type, final boolean command) {
115115
if (updater.isChecking()) {
116116
sender.sendMessage(ChatColor.RED + "Already checking for an update.");
117117
return;
@@ -121,7 +121,14 @@ private void performUpdate(final CommandSender sender, UpdateType type) {
121121
Runnable notify = new Runnable() {
122122
@Override
123123
public void run() {
124-
if (updater.shouldNotify() || config.isDebug()) {
124+
if (command) {
125+
sender.sendMessage(ChatColor.YELLOW + "[ProtocolLib] " + updater.getResult());
126+
String remoteVersion = updater.getRemoteVersion();
127+
if (remoteVersion != null) {
128+
sender.sendMessage(ChatColor.YELLOW + "Remote version: " + remoteVersion);
129+
sender.sendMessage(ChatColor.YELLOW + "Current version: " + plugin.getDescription().getVersion());
130+
}
131+
} else if (updater.shouldNotify() || config.isDebug()) {
125132
sender.sendMessage(ChatColor.YELLOW + "[ProtocolLib] " + updater.getResult());
126133
}
127134

ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,9 @@ private void checkUpdates() {
615615
if (currentTime > updateTime && !updater.isChecking()) {
616616
// Initiate the update as if it came from the console
617617
if (config.isAutoDownload())
618-
commandProtocol.updateVersion(getServer().getConsoleSender());
618+
commandProtocol.updateVersion(getServer().getConsoleSender(), false);
619619
else if (config.isAutoNotify())
620-
commandProtocol.checkVersion(getServer().getConsoleSender());
620+
commandProtocol.checkVersion(getServer().getConsoleSender(), false);
621621
else
622622
commandProtocol.updateFinished();
623623
}

ProtocolLib/src/main/java/com/comphenix/protocol/updater/BukkitUpdater.java

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.zip.ZipFile;
2323

2424
import org.bukkit.configuration.file.YamlConfiguration;
25+
import org.bukkit.plugin.Plugin;
2526
import org.json.simple.JSONArray;
2627
import org.json.simple.JSONObject;
2728
import org.json.simple.JSONValue;
@@ -78,58 +79,61 @@ public class BukkitUpdater extends Updater {
7879
* @param type Specify the type of update this will be. See {@link UpdateType}
7980
* @param announce True if the program should announce the progress of new updates in console
8081
*/
81-
public BukkitUpdater(ProtocolLibrary plugin, int id, File file, UpdateType type, boolean announce) {
82+
public BukkitUpdater(Plugin plugin, int id, File file, UpdateType type, boolean announce) {
8283
super(plugin, type, announce);
8384

8485
this.file = file;
8586
this.id = id;
8687
this.updateFolder = plugin.getServer().getUpdateFolder();
8788

88-
final File pluginFile = plugin.getDataFolder().getParentFile();
89-
final File updaterFile = new File(pluginFile, "Updater");
90-
final File updaterConfigFile = new File(updaterFile, "config.yml");
91-
92-
if (!updaterFile.exists()) {
93-
updaterFile.mkdir();
94-
}
95-
if (!updaterConfigFile.exists()) {
96-
try {
97-
updaterConfigFile.createNewFile();
98-
} catch (final IOException e) {
99-
plugin.getLogger().severe("The updater could not create a configuration in " + updaterFile.getAbsolutePath());
100-
e.printStackTrace();
101-
}
102-
}
103-
this.config = YamlConfiguration.loadConfiguration(updaterConfigFile);
104-
105-
this.config.options().header("This configuration file affects all plugins using the Updater system (version 2+ - http://forums.bukkit.org/threads/96681/ )" + '\n'
106-
+ "If you wish to use your API key, read http://wiki.bukkit.org/ServerMods_API and place it below." + '\n'
107-
+ "Some updating systems will not adhere to the disabled value, but these may be turned off in their plugin's configuration.");
108-
this.config.addDefault("api-key", "PUT_API_KEY_HERE");
109-
this.config.addDefault("disable", false);
89+
File dataFolder = plugin.getDataFolder();
90+
if (dataFolder != null) {
91+
final File pluginFile = plugin.getDataFolder().getParentFile();
92+
final File updaterFile = new File(pluginFile, "Updater");
93+
final File updaterConfigFile = new File(updaterFile, "config.yml");
94+
95+
if (!updaterFile.exists()) {
96+
updaterFile.mkdir();
97+
}
98+
if (!updaterConfigFile.exists()) {
99+
try {
100+
updaterConfigFile.createNewFile();
101+
} catch (final IOException e) {
102+
plugin.getLogger().severe("The updater could not create a configuration in " + updaterFile.getAbsolutePath());
103+
e.printStackTrace();
104+
}
105+
}
106+
this.config = YamlConfiguration.loadConfiguration(updaterConfigFile);
107+
108+
this.config.options().header("This configuration file affects all plugins using the Updater system (version 2+ - http://forums.bukkit.org/threads/96681/ )" + '\n'
109+
+ "If you wish to use your API key, read http://wiki.bukkit.org/ServerMods_API and place it below." + '\n'
110+
+ "Some updating systems will not adhere to the disabled value, but these may be turned off in their plugin's configuration.");
111+
this.config.addDefault("api-key", "PUT_API_KEY_HERE");
112+
this.config.addDefault("disable", false);
113+
114+
if (this.config.get("api-key", null) == null) {
115+
this.config.options().copyDefaults(true);
116+
try {
117+
this.config.save(updaterConfigFile);
118+
} catch (final IOException e) {
119+
plugin.getLogger().severe("The updater could not save the configuration in " + updaterFile.getAbsolutePath());
120+
e.printStackTrace();
121+
}
122+
}
110123

111-
if (this.config.get("api-key", null) == null) {
112-
this.config.options().copyDefaults(true);
113-
try {
114-
this.config.save(updaterConfigFile);
115-
} catch (final IOException e) {
116-
plugin.getLogger().severe("The updater could not save the configuration in " + updaterFile.getAbsolutePath());
117-
e.printStackTrace();
118-
}
119-
}
124+
if (this.config.getBoolean("disable")) {
125+
this.result = UpdateResult.DISABLED;
126+
return;
127+
}
120128

121-
if (this.config.getBoolean("disable")) {
122-
this.result = UpdateResult.DISABLED;
123-
return;
124-
}
129+
String key = this.config.getString("api-key");
130+
if (key.equalsIgnoreCase("PUT_API_KEY_HERE") || key.equals("")) {
131+
key = null;
132+
}
125133

126-
String key = this.config.getString("api-key");
127-
if (key.equalsIgnoreCase("PUT_API_KEY_HERE") || key.equals("")) {
128-
key = null;
134+
this.apiKey = key;
129135
}
130136

131-
this.apiKey = key;
132-
133137
try {
134138
this.url = new URL(BukkitUpdater.HOST + BukkitUpdater.QUERY + id);
135139
} catch (final MalformedURLException e) {
@@ -314,7 +318,7 @@ private boolean pluginFile(String name) {
314318
return false;
315319
} */
316320

317-
private boolean read() {
321+
public boolean read() {
318322
try {
319323
final URLConnection conn = this.url.openConnection();
320324
conn.setConnectTimeout(5000);
@@ -407,8 +411,7 @@ private void performUpdate() {
407411
}
408412

409413
@Override
410-
public boolean shouldNotify() {
411-
// TODO Auto-generated method stub
412-
return false;
414+
public String getRemoteVersion() {
415+
return getLatestName();
413416
}
414417
}

ProtocolLib/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
*/
3535

3636
public final class SpigotUpdater extends Updater {
37+
private ProtocolLibrary plugin;
3738
private String remoteVersion;
3839

3940
public SpigotUpdater(ProtocolLibrary plugin, UpdateType type, boolean announce) {
4041
super(plugin, type, announce);
42+
this.plugin = plugin;
4143
}
4244

4345
@Override
@@ -110,4 +112,9 @@ public String getSpigotVersion() throws IOException {
110112
closer.close();
111113
}
112114
}
115+
116+
@Override
117+
public String getRemoteVersion() {
118+
return remoteVersion;
119+
}
113120
}

ProtocolLib/src/main/java/com/comphenix/protocol/updater/Updater.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.List;
2121
import java.util.concurrent.CopyOnWriteArrayList;
2222

23+
import org.bukkit.plugin.Plugin;
24+
2325
import com.comphenix.protocol.ProtocolLibrary;
2426
import com.comphenix.protocol.error.ReportType;
2527
import com.comphenix.protocol.utility.MinecraftVersion;
@@ -31,7 +33,7 @@
3133
*/
3234

3335
public abstract class Updater {
34-
protected ProtocolLibrary plugin;
36+
protected Plugin plugin;
3537

3638
protected String versionName;
3739
protected String versionLink;
@@ -48,7 +50,7 @@ public abstract class Updater {
4850

4951
public static final ReportType REPORT_CANNOT_UPDATE_PLUGIN = new ReportType("Cannot update ProtocolLib.");
5052

51-
protected Updater(ProtocolLibrary plugin, UpdateType type, boolean announce) {
53+
protected Updater(Plugin plugin, UpdateType type, boolean announce) {
5254
this.plugin = plugin;
5355
this.type = type;
5456
this.announce = announce;
@@ -285,4 +287,6 @@ public boolean shouldNotify() {
285287
return false;
286288
}
287289
}
290+
291+
public abstract String getRemoteVersion();
288292
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* (c) 2016 dmulloy2
3+
*/
4+
package com.comphenix.protocol.updater;
5+
6+
import static org.junit.Assert.fail;
7+
import static org.mockito.Mockito.mock;
8+
import static org.mockito.Mockito.when;
9+
10+
import java.util.logging.Logger;
11+
12+
import org.bukkit.Server;
13+
import org.bukkit.plugin.Plugin;
14+
import org.bukkit.plugin.PluginDescriptionFile;
15+
import org.junit.Test;
16+
17+
import com.comphenix.protocol.ProtocolLibrary;
18+
import com.comphenix.protocol.updater.Updater.UpdateType;
19+
20+
/**
21+
* @author dmulloy2
22+
*/
23+
24+
public class UpdaterTest {
25+
private static final int BUKKIT_DEV_ID = 45564;
26+
27+
@Test
28+
public void testSpigotUpdater() {
29+
SpigotUpdater updater = new SpigotUpdater(null, UpdateType.NO_DOWNLOAD, true);
30+
31+
String remote = null;
32+
33+
try {
34+
remote = updater.getSpigotVersion();
35+
} catch (Throwable ex) {
36+
ex.printStackTrace();
37+
fail("Failed to check for updates");
38+
}
39+
40+
System.out.println("Determined remote Spigot version: " + remote);
41+
}
42+
43+
@Test
44+
public void testBukkitUpdater() {
45+
Server server = mock(Server.class);
46+
when(server.getUpdateFolder()).thenReturn(null);
47+
48+
Plugin plugin = mock(Plugin.class);
49+
when(plugin.getDescription()).thenReturn(new PluginDescriptionFile("ProtocolLib", ProtocolLibrary.class.getPackage().getImplementationVersion(), null));
50+
when(plugin.getLogger()).thenReturn(Logger.getLogger("ProtocolLib"));
51+
when(plugin.getDataFolder()).thenReturn(null);
52+
when(plugin.getServer()).thenReturn(server);
53+
54+
BukkitUpdater updater = new BukkitUpdater(plugin, BUKKIT_DEV_ID, null, UpdateType.NO_DOWNLOAD, true);
55+
if (! updater.read()) {
56+
fail("Failed to check for updates");
57+
}
58+
59+
String remote = updater.getLatestName();
60+
System.out.println("Determined remote Bukkit Dev version: " + remote);
61+
}
62+
}

0 commit comments

Comments
 (0)