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:
@@ -462,7 +462,7 @@ public class AuthMe extends JavaPlugin {
|
||||
private void setupConsoleFilter() {
|
||||
if (Settings.removePassword) {
|
||||
ConsoleFilter filter = new ConsoleFilter();
|
||||
ConsoleLogger.getLogger().setFilter(filter);
|
||||
getLogger().setFilter(filter);
|
||||
Bukkit.getLogger().setFilter(filter);
|
||||
Logger.getLogger("Minecraft").setFilter(filter);
|
||||
// Set Log4J Filter
|
||||
@@ -955,20 +955,10 @@ public class AuthMe extends JavaPlugin {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the management instance.
|
||||
* Return the management instance.
|
||||
*/
|
||||
public Management getManagement() {
|
||||
return management;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the server instance running this plugin. Use this method in favor of {@link
|
||||
* AuthMe#getServer()} for testability purposes.
|
||||
*
|
||||
* @return the server instance
|
||||
*/
|
||||
public Server getGameServer() {
|
||||
return super.getServer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package fr.xephi.authme;
|
||||
import com.google.common.base.Throwables;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import fr.xephi.authme.util.Wrapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@@ -10,23 +11,17 @@ import java.nio.file.StandardOpenOption;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* The plugin's static logger.
|
||||
*/
|
||||
public class ConsoleLogger {
|
||||
public final class ConsoleLogger {
|
||||
|
||||
private static final Logger log = AuthMe.getInstance().getLogger();
|
||||
private static Wrapper wrapper = new Wrapper(AuthMe.getInstance());
|
||||
private static final DateFormat df = new SimpleDateFormat("[MM-dd HH:mm:ss]");
|
||||
|
||||
/**
|
||||
* Returns the plugin's logger.
|
||||
*
|
||||
* @return Logger
|
||||
*/
|
||||
public static Logger getLogger() {
|
||||
return log;
|
||||
private ConsoleLogger() {
|
||||
// Service class
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,7 +30,7 @@ public class ConsoleLogger {
|
||||
* @param message String
|
||||
*/
|
||||
public static void info(String message) {
|
||||
log.info(message);
|
||||
wrapper.getLogger().info(message);
|
||||
if (!Settings.useLogging) {
|
||||
return;
|
||||
}
|
||||
@@ -48,7 +43,7 @@ public class ConsoleLogger {
|
||||
* @param message String
|
||||
*/
|
||||
public static void showError(String message) {
|
||||
log.warning(message);
|
||||
wrapper.getLogger().warning(message);
|
||||
if (!Settings.useLogging) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user