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

View File

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

View File

@ -3,6 +3,7 @@ package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -22,7 +23,7 @@ public class ChangeEmailCommand extends ExecutableCommand {
String playerMailNew = commandArguments.get(1); String playerMailNew = commandArguments.get(1);
// Get the player instance and execute action // Get the player instance and execute action
final AuthMe plugin = AuthMe.getInstance(); final AuthMe plugin = Wrapper.getInstance().getAuthMe();
final Player player = (Player) sender; final Player player = (Player) sender;
plugin.getManagement().performChangeEmail(player, playerMailOld, playerMailNew); plugin.getManagement().performChangeEmail(player, playerMailOld, playerMailNew);
return true; 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.MessageKey;
import fr.xephi.authme.settings.Messages; import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -35,8 +36,9 @@ public class RecoverEmailCommand extends ExecutableCommand {
final String playerName = player.getName(); final String playerName = player.getName();
// Command logic // Command logic
final AuthMe plugin = AuthMe.getInstance(); final Wrapper wrapper = Wrapper.getInstance();
final Messages m = plugin.getMessages(); final AuthMe plugin = wrapper.getAuthMe();
final Messages m = wrapper.getMessages();
if (plugin.mail == null) { if (plugin.mail == null) {
m.send(player, MessageKey.ERROR); 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.AuthMe;
import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -18,7 +19,7 @@ public class LoginCommand extends ExecutableCommand {
} }
// Get the necessary objects // Get the necessary objects
final AuthMe plugin = AuthMe.getInstance(); final AuthMe plugin = Wrapper.getInstance().getAuthMe();
final Player player = (Player) sender; final Player player = (Player) sender;
final String password = commandArguments.get(0); 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.MessageKey;
import fr.xephi.authme.settings.Messages; import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -24,8 +25,9 @@ public class RegisterCommand extends ExecutableCommand {
return true; return true;
} }
final AuthMe plugin = AuthMe.getInstance(); final Wrapper wrapper = Wrapper.getInstance();
final Messages m = plugin.getMessages(); final AuthMe plugin = wrapper.getAuthMe();
final Messages m = wrapper.getMessages();
// Make sure the command arguments are valid // Make sure the command arguments are valid
final Player player = (Player) sender; final Player player = (Player) sender;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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