Create Wrapper for instances / revise MockUtils

- Add test resources folder
- Create basic test for Messages (todo: add concrete tests)
- Create WrapperMock
- Change UtilsTest (todo: make it work)
This commit is contained in:
ljacqu
2015-11-23 23:25:03 +01:00
parent 8719cce334
commit 6422f90114
11 changed files with 261 additions and 51 deletions
@@ -12,15 +12,15 @@ import java.util.zip.GZIPInputStream;
public class GeoLiteAPI {
private static final String GEOIP_URL = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry" +
"/GeoIP.dat.gz";
private static final AuthMe plugin = AuthMe.getInstance();
private static final String GEOIP_URL = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry"
+ "/GeoIP.dat.gz";
private static final Wrapper wrapper = new Wrapper(AuthMe.getInstance());
private static LookupService lookupService;
/**
* Download (if absent) the GeoIpLite data file and then try to load it.
*
* @return Boolean True if the data is available, false if not.
* @return True if the data is available, false otherwise.
*/
public static boolean isDataAvailable() {
if (lookupService != null) {
@@ -30,15 +30,17 @@ public class GeoLiteAPI {
if (data.exists()) {
try {
lookupService = new LookupService(data);
plugin.getLogger().info("[LICENSE] This product uses data from the GeoLite API created by MaxMind, " +
// TODO ljacqu 20151123: Should this not be output over the ConsoleLogger service?
wrapper.getLogger().info("[LICENSE] This product uses data from the GeoLite API created by MaxMind, " +
"available at http://www.maxmind.com");
return true;
} catch (IOException e) {
// TODO ljacqu 20151123: Log the exception instead of just swallowing it
return false;
}
}
// Ok, let's try to download the data file!
plugin.getGameServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
wrapper.getServer().getScheduler().runTaskAsynchronously(wrapper.getAuthMe(), new Runnable() {
@Override
public void run() {
try {
+10 -6
View File
@@ -31,7 +31,8 @@ import java.util.zip.GZIPInputStream;
*/
public final class Utils {
public static AuthMe plugin;
private static AuthMe plugin;
private static Wrapper wrapper;
private static boolean getOnlinePlayersIsCollection = false;
private static Method getOnlinePlayers;
@@ -39,6 +40,7 @@ public final class Utils {
static {
plugin = AuthMe.getInstance();
wrapper = new Wrapper(plugin);
checkGeoIP();
initializeOnlinePlayersIsCollectionField();
}
@@ -65,7 +67,7 @@ public final class Utils {
}
}
}
plugin.getGameServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
wrapper.getServer().getScheduler().runTaskAsynchronously(wrapper.getAuthMe(), new Runnable() {
@Override
public void run() {
try {
@@ -190,6 +192,7 @@ public final class Utils {
}
if (!Settings.isForcedRegistrationEnabled) {
// TODO ljacqu 20151123: Use a setter to retrieve things from AuthMe
if (!plugin.database.isAuthAvailable(name)) {
return true;
}
@@ -225,12 +228,12 @@ public final class Utils {
final World world = theWorld;
final Location loc = new Location(world, x, y, z);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
Bukkit.getScheduler().scheduleSyncDelayedTask(wrapper.getAuthMe(), new Runnable() {
@Override
public void run() {
AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(pl, loc);
plugin.getServer().getPluginManager().callEvent(tpEvent);
wrapper.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) {
pl.teleport(tpEvent.getTo());
}
@@ -325,7 +328,7 @@ public final class Utils {
@SuppressWarnings("deprecation")
public static Player getPlayer(String name) {
name = name.toLowerCase();
return plugin.getServer().getPlayer(name);
return wrapper.getServer().getPlayer(name);
}
public static boolean isNPC(final Entity player) {
@@ -333,6 +336,7 @@ public final class Utils {
if (player.hasMetadata("NPC")) {
return true;
} else if (plugin.combatTagPlus != null
// TODO ljacqu 20151123: Use a getter for combatTagPlus in AuthMe instead of using direct field access
&& player instanceof Player
&& plugin.combatTagPlus.getNpcPlayerHelper().isNpc((Player) player)) {
return true;
@@ -347,7 +351,7 @@ public final class Utils {
if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) {
Location spawn = plugin.getSpawnLocation(player);
AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(player, spawn);
plugin.getServer().getPluginManager().callEvent(tpEvent);
wrapper.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) {
player.teleport(tpEvent.getTo());
}
@@ -0,0 +1,34 @@
package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe;
import org.bukkit.Server;
import java.util.logging.Logger;
/**
* Wrapper for the retrieval of common singletons used throughout the application.
* This class simply delegates the calls.
*/
public class Wrapper {
private AuthMe authMe;
public Wrapper(AuthMe authMe) {
this.authMe = authMe;
}
public AuthMe getAuthMe() {
return authMe;
}
public Server getServer() {
return authMe.getServer();
}
public Logger getLogger() {
return authMe.getLogger();
}
}