Testing - change to non-reflection WrapperMock test setup

Replaced many classes to use Wrapper to get singletons and replaced the test setups to use the WrapperMock instead of setting fields through reflection
This commit is contained in:
ljacqu 2015-11-26 22:53:12 +01:00
parent 77f2f80eaf
commit 210b691353
14 changed files with 69 additions and 57 deletions

View File

@ -8,6 +8,7 @@ import fr.xephi.authme.settings.MessageKey;
import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.task.ChangePasswordTask;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -22,8 +23,8 @@ public class ChangePasswordCommand extends ExecutableCommand {
return true;
}
final AuthMe plugin = AuthMe.getInstance();
final Messages m = plugin.getMessages();
final Wrapper wrapper = Wrapper.getInstance();
final Messages m = wrapper.getMessages();
// Get the passwords
String playerPass = commandArguments.get(0);
@ -32,7 +33,8 @@ public class ChangePasswordCommand extends ExecutableCommand {
// Get the player instance and make sure it's authenticated
Player player = (Player) sender;
String name = player.getName().toLowerCase();
if (!PlayerCache.getInstance().isAuthenticated(name)) {
final PlayerCache playerCache = wrapper.getPlayerCache();
if (!playerCache.isAuthenticated(name)) {
m.send(player, MessageKey.NOT_LOGGED_IN);
return true;
}
@ -45,7 +47,7 @@ public class ChangePasswordCommand extends ExecutableCommand {
|| playerPassLowerCase.contains("from") || playerPassLowerCase.contains("select")
|| playerPassLowerCase.contains(";") || playerPassLowerCase.contains("null")
|| !playerPassLowerCase.matches(Settings.getPassRegex)) {
m.send(player, MessageKey.PASSWORD_IS_USERNAME_ERROR);
m.send(player, MessageKey.PASSWORD_MATCH_ERROR);
return true;
}
if (playerPassLowerCase.equalsIgnoreCase(name)) {
@ -63,6 +65,7 @@ public class ChangePasswordCommand extends ExecutableCommand {
}
// Set the password
final AuthMe plugin = wrapper.getAuthMe();
plugin.getServer().getScheduler().runTaskAsynchronously(plugin,
new ChangePasswordTask(plugin, player, playerPass, playerPassVerify));
return true;

View File

@ -3,6 +3,7 @@ package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -22,7 +23,7 @@ public class AddEmailCommand extends ExecutableCommand {
String playerMailVerify = commandArguments.get(1);
// Get the player and perform email addition
final AuthMe plugin = AuthMe.getInstance();
final AuthMe plugin = Wrapper.getInstance().getAuthMe();
final Player player = (Player) sender;
plugin.getManagement().performAddEmail(player, playerMail, playerMailVerify);
return true;

View File

@ -3,6 +3,7 @@ package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -22,7 +23,7 @@ public class ChangeEmailCommand extends ExecutableCommand {
String playerMailNew = commandArguments.get(1);
// Get the player instance and execute action
final AuthMe plugin = AuthMe.getInstance();
final AuthMe plugin = Wrapper.getInstance().getAuthMe();
final Player player = (Player) sender;
plugin.getManagement().performChangeEmail(player, playerMailOld, playerMailNew);
return true;

View File

@ -11,6 +11,7 @@ import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.MessageKey;
import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -35,8 +36,9 @@ public class RecoverEmailCommand extends ExecutableCommand {
final String playerName = player.getName();
// Command logic
final AuthMe plugin = AuthMe.getInstance();
final Messages m = plugin.getMessages();
final Wrapper wrapper = Wrapper.getInstance();
final AuthMe plugin = wrapper.getAuthMe();
final Messages m = wrapper.getMessages();
if (plugin.mail == null) {
m.send(player, MessageKey.ERROR);

View File

@ -3,6 +3,7 @@ package fr.xephi.authme.command.executable.login;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -18,7 +19,7 @@ public class LoginCommand extends ExecutableCommand {
}
// Get the necessary objects
final AuthMe plugin = AuthMe.getInstance();
final AuthMe plugin = Wrapper.getInstance().getAuthMe();
final Player player = (Player) sender;
final String password = commandArguments.get(0);

View File

@ -8,6 +8,7 @@ import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.MessageKey;
import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -24,8 +25,9 @@ public class RegisterCommand extends ExecutableCommand {
return true;
}
final AuthMe plugin = AuthMe.getInstance();
final Messages m = plugin.getMessages();
final Wrapper wrapper = Wrapper.getInstance();
final AuthMe plugin = wrapper.getAuthMe();
final Messages m = wrapper.getMessages();
// Make sure the command arguments are valid
final Player player = (Player) sender;

View File

@ -1,6 +1,7 @@
package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.settings.Messages;
import org.bukkit.Bukkit;
import org.bukkit.Server;
@ -55,6 +56,10 @@ public class Wrapper {
return getAuthMe().getMessages();
}
public PlayerCache getPlayerCache() {
return PlayerCache.getInstance();
}
/**
* Return the folder containing plugin data via the AuthMe instance.
*

View File

@ -1,19 +1,20 @@
package fr.xephi.authme.command.executable.changepassword;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.settings.MessageKey;
import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.util.Arrays;
import static org.mockito.Matchers.anyInt;
@ -25,7 +26,6 @@ import static org.mockito.Mockito.when;
/**
* Test for {@link ChangePasswordCommand}.
*/
@Ignore
public class ChangePasswordCommandTest {
private Messages messagesMock;
@ -33,26 +33,16 @@ public class ChangePasswordCommandTest {
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
AuthMeMockUtil.mockPlayerCacheInstance();
cacheMock = PlayerCache.getInstance();
AuthMeMockUtil.mockMessagesInstance();
messagesMock = Messages.getInstance();
WrapperMock wrapper = WrapperMock.createInstance();
wrapper.setDataFolder(new File("/"));
messagesMock = wrapper.getMessages();
cacheMock = wrapper.getPlayerCache();
// Only allow passwords with alphanumerical characters for the test
Settings.getPassRegex = "[a-zA-Z0-9]+";
Settings.getPasswordMinLen = 2;
Settings.passwordMaxLength = 50;
// TODO ljacqu 20151121: Cannot mock getServer() as it's final
// Probably the Command class should delegate as the others do
/*
Server server = Mockito.mock(Server.class);
schedulerMock = Mockito.mock(BukkitScheduler.class);
Mockito.when(server.getScheduler()).thenReturn(schedulerMock);
Mockito.when(pluginMock.getServer()).thenReturn(server);
*/
// TODO ljacqu 20151126: Verify the calls to getServer() (see commented code)
}
@Test
@ -80,7 +70,7 @@ public class ChangePasswordCommandTest {
command.executeCommand(sender, new CommandParts(), new CommandParts("pass"));
// then
verify(messagesMock).send(sender, "not_logged_in");
verify(messagesMock).send(sender, MessageKey.NOT_LOGGED_IN);
//verify(pluginMock, never()).getServer();
}
@ -94,7 +84,7 @@ public class ChangePasswordCommandTest {
command.executeCommand(sender, new CommandParts(), new CommandParts("!pass"));
// then
verify(messagesMock).send(sender, "password_error");
verify(messagesMock).send(sender, MessageKey.PASSWORD_MATCH_ERROR);
//verify(pluginMock, never()).getServer();
}
@ -109,7 +99,7 @@ public class ChangePasswordCommandTest {
command.executeCommand(sender, new CommandParts(), new CommandParts("Tester"));
// then
verify(messagesMock).send(sender, "password_error_nick");
verify(messagesMock).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR);
//verify(pluginMock, never()).getServer();
}
@ -124,7 +114,7 @@ public class ChangePasswordCommandTest {
command.executeCommand(sender, new CommandParts(), new CommandParts("test"));
// then
verify(messagesMock).send(sender, "pass_len");
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
//verify(pluginMock, never()).getServer();
}
@ -139,7 +129,7 @@ public class ChangePasswordCommandTest {
command.executeCommand(sender, new CommandParts(), new CommandParts("tester"));
// then
verify(messagesMock).send(sender, "pass_len");
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
//verify(pluginMock, never()).getServer();
}
@ -154,7 +144,7 @@ public class ChangePasswordCommandTest {
command.executeCommand(sender, new CommandParts(), new CommandParts("abc123"));
// then
verify(messagesMock).send(sender, "password_error_unsafe");
verify(messagesMock).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR);
//verify(pluginMock, never()).getServer();
}

View File

@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -27,8 +27,8 @@ public class AddEmailCommandTest {
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
authMeMock = AuthMe.getInstance();
WrapperMock wrapper = WrapperMock.createInstance();
authMeMock = wrapper.getAuthMe();
managementMock = Mockito.mock(Management.class);
when(authMeMock.getManagement()).thenReturn(managementMock);
}

View File

@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -27,8 +27,8 @@ public class ChangeEmailCommandTest {
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
authMeMock = AuthMe.getInstance();
WrapperMock wrapper = WrapperMock.createInstance();
authMeMock = wrapper.getAuthMe();
managementMock = Mockito.mock(Management.class);
when(authMeMock.getManagement()).thenReturn(managementMock);
}

View File

@ -1,11 +1,11 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
@ -16,10 +16,11 @@ public class RecoverEmailCommandTest {
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
WrapperMock wrapper = WrapperMock.createInstance();
}
@Test
@Ignore
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = Mockito.mock(BlockCommandSender.class);

View File

@ -1,10 +1,9 @@
package fr.xephi.authme.command.executable.login;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -12,6 +11,8 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import java.io.File;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString;
@ -28,12 +29,12 @@ public class LoginCommandTest {
@Before
public void initializeAuthMeMock() {
AuthMeMockUtil.mockAuthMeInstance();
AuthMe pluginMock = AuthMe.getInstance();
WrapperMock wrapper = WrapperMock.createInstance();
wrapper.setDataFolder(new File("/"));
Settings.captchaLength = 10;
managementMock = mock(Management.class);
Mockito.when(pluginMock.getManagement()).thenReturn(managementMock);
Mockito.when(wrapper.getAuthMe().getManagement()).thenReturn(managementMock);
}
@Test

View File

@ -1,12 +1,11 @@
package fr.xephi.authme.command.executable.register;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.settings.MessageKey;
import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -15,6 +14,8 @@ import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.io.File;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
@ -33,15 +34,13 @@ public class RegisterCommandTest {
@Before
public void initializeAuthMeMock() {
AuthMeMockUtil.mockAuthMeInstance();
AuthMe pluginMock = AuthMe.getInstance();
messagesMock = mock(Messages.class);
Mockito.when(pluginMock.getMessages()).thenReturn(messagesMock);
WrapperMock wrapper = WrapperMock.createInstance();
wrapper.setDataFolder(new File("/"));
messagesMock = wrapper.getMessages();
Settings.captchaLength = 10;
managementMock = mock(Management.class);
Mockito.when(pluginMock.getManagement()).thenReturn(managementMock);
Mockito.when(wrapper.getAuthMe().getManagement()).thenReturn(managementMock);
}
@Test

View File

@ -1,6 +1,7 @@
package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.settings.Messages;
import org.bukkit.Server;
import org.bukkit.scheduler.BukkitScheduler;
@ -72,6 +73,11 @@ public class WrapperMock extends Wrapper {
return getMock(Messages.class);
}
@Override
public PlayerCache getPlayerCache() {
return getMock(PlayerCache.class);
}
@Override
public File getDataFolder() {
if (singleton.getDataFolderValue != null) {