Code householding

- Move console initialization for tests into TestHelper
- Remove unused properties in legacy Settings
- Add issue number to TODO comments where applicable
This commit is contained in:
ljacqu 2016-04-14 12:28:19 +02:00
parent 3bb7ff2b85
commit ca0cbe6caf
23 changed files with 82 additions and 101 deletions

View File

@ -106,23 +106,25 @@ public class AuthMe extends JavaPlugin {
private static Server server;
/*
* Maps and stuff
* TODO: Clean up and Move into a manager
*/
// TODO #601: Integrate CaptchaManager
public final ConcurrentHashMap<String, BukkitTask> sessions = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String, Integer> captcha = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String, String> cap = new ConcurrentHashMap<>();
/*
* Public Instances
* TODO #432: Encapsulation
*/
public NewAPI api;
// TODO #655: Encapsulate mail
public SendMailSSL mail;
// TODO #656: Encapsulate data manager
public DataManager dataManager;
/*
* Plugin Hooks
* TODO: Move into modules
* Private instances
* TODO #432: Move instantiation and management of these services
*/
// TODO #604: Encapsulate ProtocolLib members
public AuthMeInventoryPacketAdapter inventoryProtector;
public AuthMeTabCompletePacketAdapter tabComplete;
public AuthMeTablistPacketAdapter tablistHider;

View File

@ -122,69 +122,85 @@ public interface DataSource {
void close();
/**
* Method purgeBanned.
* Purge all given players, i.e. delete all players whose name is in the list.
*
* @param banned List of String
* @param banned the list of players to delete
*/
void purgeBanned(List<String> banned);
/**
* Method getType.
* Return the data source type.
*
* @return DataSourceType
* @return the data source type
*/
DataSourceType getType();
/**
* Method isLogged.
* Query the datasource whether the player is logged in or not.
*
* @param user String
*
* @return boolean
* @param user The name of the player to verify
* @return True if logged in, false otherwise
*/
boolean isLogged(String user);
/**
* Method setLogged.
* Set a player as logged in.
*
* @param user String
* @param user The name of the player to change
*/
void setLogged(String user);
/**
* Method setUnlogged.
* Set a player as unlogged (not logged in).
*
* @param user String
* @param user The name of the player to change
*/
void setUnlogged(String user);
/**
* Set all players who are marked as logged in as NOT logged in.
*/
void purgeLogged();
/**
* Method getAccountsRegistered.
* Return all players which are logged in.
*
* @return int
* @return All logged in players
*/
List<PlayerAuth> getLoggedPlayers();
/**
* Return the number of registered accounts.
*
* @return Total number of accounts
*/
int getAccountsRegistered();
/**
* Update a player's real name (capitalization).
*
* @param user The name of the user (lowercase)
* @param realName The real name of the user (proper casing)
* @return True upon success, false upon failure
*/
boolean updateRealName(String user, String realName);
/**
* Update a player's IP address.
*
* @param user The name of the user (lowercase)
* @param ip The IP address to save
* @return True upon success, false upon failure
*/
boolean updateIp(String user, String ip);
/**
* Method getAllAuths.
* Return all players of the database.
*
* @return List of PlayerAuth
* @return List of all players
*/
List<PlayerAuth> getAllAuths();
/**
* Method getLoggedPlayers.
*
* @return List of PlayerAuth
*/
List<PlayerAuth> getLoggedPlayers();
/**
* Reload the data source.
*/

View File

@ -33,7 +33,7 @@ public class AuthMeEntityListener implements Listener {
}
}
// TODO #360: npc status can be used to bypass security!!!
// Note #360: npc status can be used to bypass security!!!
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onEntityDamage(EntityDamageEvent event) {
if (shouldCancelEvent(event)) {

View File

@ -24,7 +24,6 @@ import java.util.regex.Pattern;
public final class Settings {
public static final File PLUGIN_FOLDER = Wrapper.getInstance().getDataFolder();
public static final File MODULE_FOLDER = new File(PLUGIN_FOLDER, "modules");
public static final File CACHE_FOLDER = new File(PLUGIN_FOLDER, "cache");
public static List<String> allowCommands;
public static List<String> getJoinPermissions;
@ -62,7 +61,7 @@ public final class Settings {
spawnPriority, crazyloginFileName, sendPlayerTo;
public static int getWarnMessageInterval, getSessionTimeout,
getRegistrationTimeout, getMaxNickLength, getMinNickLength,
getPasswordMinLen, getMovementRadius, getNonActivatedGroup, passwordMaxLength,
getMovementRadius, getNonActivatedGroup,
maxLoginTry, captchaLength, saltLength,
bCryptLog2Rounds, getMaxLoginPerIp, getMaxJoinPerIp;
protected static FileConfiguration configFile;
@ -88,7 +87,6 @@ public final class Settings {
isChatAllowed = load(RestrictionSettings.ALLOW_CHAT);
getMaxNickLength = configFile.getInt("settings.restrictions.maxNicknameLength", 20);
getMinNickLength = configFile.getInt("settings.restrictions.minNicknameLength", 3);
getPasswordMinLen = configFile.getInt("settings.security.minPasswordLength", 4);
getNickRegex = configFile.getString("settings.restrictions.allowedNicknameCharacters", "[a-zA-Z0-9_?]*");
nickPattern = Pattern.compile(getNickRegex);
isAllowRestrictedIp = load(RestrictionSettings.ENABLE_RESTRICTED_USERS);
@ -120,7 +118,6 @@ public final class Settings {
denyTabcompleteBeforeLogin = load(RestrictionSettings.DENY_TABCOMPLETE_BEFORE_LOGIN);
hideTablistBeforeLogin = load(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN);
passwordMaxLength = load(SecuritySettings.MAX_PASSWORD_LENGTH);
backupWindowsPath = configFile.getString("BackupSystem.MysqlWindowsPath", "C:\\Program Files\\MySQL\\MySQL Server 5.1\\");
isStopEnabled = configFile.getBoolean("Security.SQLProblem.stopServer", true);
reloadSupport = configFile.getBoolean("Security.ReloadCommand.useReloadCommandSupport", true);

View File

@ -1,20 +0,0 @@
package fr.xephi.authme;
import org.mockito.Mockito;
import java.util.logging.Logger;
/**
* Test initializer for {@link ConsoleLogger}.
*/
public class ConsoleLoggerTestInitializer {
private ConsoleLoggerTestInitializer() {
}
public static Logger setupLogger() {
Logger logger = Mockito.mock(Logger.class);
ConsoleLogger.setLogger(logger);
return logger;
}
}

View File

@ -3,11 +3,13 @@ package fr.xephi.authme;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.util.BukkitService;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.io.File;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Logger;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.verify;
@ -68,20 +70,6 @@ public final class TestHelper {
runnable.run();
}
/**
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#scheduleSyncDelayedTask(Runnable)} method.
* Note that calling this method expects that there be a runnable sent to the method and will fail
* otherwise.
*
* @param service The mock service
*/
public static void runSyncDelayedTask(BukkitService service) {
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(service).scheduleSyncDelayedTask(captor.capture());
Runnable runnable = captor.getValue();
runnable.run();
}
/**
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#scheduleSyncDelayedTask(Runnable, long)}
* method. Note that calling this method expects that there be a runnable sent to the method and will fail
@ -96,4 +84,9 @@ public final class TestHelper {
runnable.run();
}
public static Logger setupLogger() {
Logger logger = Mockito.mock(Logger.class);
ConsoleLogger.setLogger(logger);
return logger;
}
}

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
@ -36,7 +36,7 @@ public class ChangePasswordAdminCommandTest {
@BeforeClass
public static void setUpLogger() {
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
@Test

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
@ -43,7 +42,7 @@ public class RegisterAdminCommandTest {
@BeforeClass
public static void setUpLogger() {
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
@Test

View File

@ -1,7 +1,7 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.output.MessageKey;
@ -24,7 +24,7 @@ public class ReloadCommandTest {
@BeforeClass
public static void setUpLogger() {
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
@Test

View File

@ -1,7 +1,6 @@
package fr.xephi.authme.converter;
import com.google.common.io.Files;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
@ -37,7 +36,7 @@ public class ForceFlatToSqliteTest {
@BeforeClass
public static void setup() {
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
@Before

View File

@ -4,7 +4,7 @@ import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.HashedPassword;
@ -107,7 +107,7 @@ public abstract class AbstractResourceClosingTest {
return ((Property) invocation.getArguments()[0]).getDefaultValue();
}
});
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
/** Initialize the dataSource implementation to test based on a mock connection. */

View File

@ -2,7 +2,6 @@ package fr.xephi.authme.datasource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.domain.Property;
@ -53,7 +52,7 @@ public class MySqlIntegrationTest extends AbstractDataSourceIntegrationTest {
});
set(DatabaseSettings.MYSQL_DATABASE, "h2_test");
set(DatabaseSettings.MYSQL_TABLE, "authme");
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
Path sqlInitFile = TestHelper.getJarPath("/datasource-integration/sql-initialize.sql");
sqlInitialize = new String(Files.readAllBytes(sqlInitFile));

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.datasource;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.domain.Property;
@ -52,8 +51,7 @@ public class SQLiteIntegrationTest extends AbstractDataSourceIntegrationTest {
});
set(DatabaseSettings.MYSQL_DATABASE, "sqlite-test");
set(DatabaseSettings.MYSQL_TABLE, "authme");
set(DatabaseSettings.MYSQL_COL_SALT, "salt");
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
Path sqlInitFile = TestHelper.getJarPath("/datasource-integration/sql-initialize.sql");
// Note ljacqu 20160221: It appears that we can only run one statement per Statement.execute() so we split

View File

@ -2,8 +2,8 @@ package fr.xephi.authme.hooks;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
@ -29,7 +29,7 @@ public class PluginHooksTest {
@BeforeClass
public static void setLogger() {
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
@Test

View File

@ -1,7 +1,6 @@
package fr.xephi.authme.output;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.CommandSender;
@ -36,7 +35,7 @@ public class MessagesIntegrationTest {
@BeforeClass
public static void setup() {
WrapperMock.createInstance();
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
/**

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.process.email;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
@ -37,7 +37,7 @@ public class AsyncAddEmailTest {
@BeforeClass
public static void setUp() {
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
@Test

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.WrapperMock;
import org.junit.BeforeClass;
@ -14,7 +14,7 @@ public class BcryptTest extends AbstractEncryptionMethodTest {
public static void setUpSettings() {
WrapperMock.createInstance();
Settings.bCryptLog2Rounds = 8;
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
public BcryptTest() {

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import org.junit.BeforeClass;
/**
@ -10,7 +10,7 @@ public class CryptPBKDF2DjangoTest extends AbstractEncryptionMethodTest {
@BeforeClass
public static void setupLogger() {
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
public CryptPBKDF2DjangoTest() {

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.util.WrapperMock;
import org.junit.BeforeClass;
@ -12,7 +12,7 @@ public class IPB4Test extends AbstractEncryptionMethodTest {
@BeforeClass
public static void setUpSettings() {
WrapperMock.createInstance();
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
public IPB4Test() {

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.security.crypts;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.util.WrapperMock;
import org.junit.BeforeClass;
@ -12,7 +12,7 @@ public class XFBCRYPTTest extends AbstractEncryptionMethodTest {
@BeforeClass
public static void setup() {
WrapperMock.createInstance();
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
public XFBCRYPTTest() {

View File

@ -2,7 +2,6 @@ package fr.xephi.authme.settings;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.properties.TestConfiguration;
@ -48,7 +47,7 @@ public class NewSettingIntegrationTest {
@BeforeClass
public static void setUpLogger() {
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
@Before

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.settings;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.TestConfiguration;
@ -48,7 +48,7 @@ public class NewSettingTest {
@BeforeClass
public static void setUpLogger() {
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
@Before

View File

@ -1,8 +1,8 @@
package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLoggerTestInitializer;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.Settings;
import org.bukkit.entity.Player;
import org.junit.Before;
@ -37,7 +37,7 @@ public class UtilsTest {
public static void setUpMocks() {
WrapperMock wrapperMock = WrapperMock.createInstance();
authMeMock = wrapperMock.getAuthMe();
ConsoleLoggerTestInitializer.setupLogger();
TestHelper.setupLogger();
}
@Before