Merge enum branch into master

This commit is contained in:
ljacqu
2015-11-26 21:45:06 +01:00
21 changed files with 537 additions and 155 deletions
@@ -1,7 +1,9 @@
package fr.xephi.authme;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.util.Wrapper;
import fr.xephi.authme.util.WrapperMock;
import org.mockito.Mockito;
import java.lang.reflect.Field;
@@ -34,6 +36,19 @@ public final class AuthMeMockUtil {
mockSingletonForClass(Messages.class, "singleton", mock);
}
/**
* Creates a mock singleton for the player cache, retrievable from {@link PlayerCache#getInstance()}.
*/
public static void mockPlayerCacheInstance() {
PlayerCache mock = Mockito.mock(PlayerCache.class);
mockSingletonForClass(PlayerCache.class, "singleton", mock);
}
public static void initializeWrapperMock() {
WrapperMock wrapper = new WrapperMock();
mockSingletonForClass(Wrapper.class, "singleton", wrapper);
}
/**
* Set the given class' {@link Wrapper} field to a mock implementation.
*
@@ -50,7 +65,7 @@ public final class AuthMeMockUtil {
}
public static Wrapper insertMockWrapperInstance(Class<?> clazz, String fieldName, AuthMe authMe) {
Wrapper wrapperMock = new WrapperMock(authMe);
Wrapper wrapperMock = new WrapperMock();
mockSingletonForClass(clazz, fieldName, wrapperMock);
return wrapperMock;
}
@@ -69,7 +84,7 @@ public final class AuthMeMockUtil {
* @param fieldName The field name
* @param mock The mock to set for the given field
*/
private static void mockSingletonForClass(Class<?> clazz, String fieldName, Object mock) {
public static void mockSingletonForClass(Class<?> clazz, String fieldName, Object mock) {
try {
Field instance = clazz.getDeclaredField(fieldName);
instance.setAccessible(true);
@@ -0,0 +1,41 @@
package fr.xephi.authme.command;
import org.junit.Test;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test for {@link CommandManager}.
*/
public class CommandManagerTest {
@Test
public void shouldInitializeCommands() {
// given/when
CommandManager manager = new CommandManager(true);
int commandCount = manager.getCommandDescriptionCount();
List<CommandDescription> commands = manager.getCommandDescriptions();
// then
// It obviously doesn't make sense to test much of the concrete data
// that is being initialized; we just want to guarantee with this test
// that data is indeed being initialized and we take a few "probes"
assertThat(commandCount, equalTo(9));
assertThat(commandsIncludeLabel(commands, "authme"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "register"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "help"), equalTo(false));
}
private static boolean commandsIncludeLabel(Iterable<CommandDescription> commands, String label) {
for (CommandDescription command : commands) {
if (command.getLabels().contains(label)) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,166 @@
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.Messages;
import fr.xephi.authme.settings.Settings;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Test for {@link ChangePasswordCommand}.
*/
public class ChangePasswordCommandTest {
private Messages messagesMock;
private PlayerCache cacheMock;
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
AuthMeMockUtil.mockPlayerCacheInstance();
cacheMock = PlayerCache.getInstance();
AuthMeMockUtil.mockMessagesInstance();
messagesMock = Messages.getInstance();
// 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);
*/
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = mock(BlockCommandSender.class);
ChangePasswordCommand command = new ChangePasswordCommand();
CommandParts arguments = mock(CommandParts.class);
// when
command.executeCommand(sender, new CommandParts(), arguments);
// then
verify(arguments, never()).get(anyInt());
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldRejectNotLoggedInPlayer() {
// given
CommandSender sender = initPlayerWithName("name", false);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("pass"));
// then
verify(messagesMock).send(sender, "not_logged_in");
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldDenyInvalidPassword() {
// given
CommandSender sender = initPlayerWithName("name", true);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("!pass"));
// then
verify(messagesMock).send(sender, "password_error");
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldRejectPasswordEqualToNick() {
// given
CommandSender sender = initPlayerWithName("tester", true);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("Tester"));
// then
verify(messagesMock).send(sender, "password_error_nick");
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldRejectTooLongPassword() {
// given
CommandSender sender = initPlayerWithName("abc12", true);
ChangePasswordCommand command = new ChangePasswordCommand();
Settings.passwordMaxLength = 3;
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("test"));
// then
verify(messagesMock).send(sender, "pass_len");
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldRejectTooShortPassword() {
// given
CommandSender sender = initPlayerWithName("abc12", true);
ChangePasswordCommand command = new ChangePasswordCommand();
Settings.getPasswordMinLen = 7;
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("tester"));
// then
verify(messagesMock).send(sender, "pass_len");
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldRejectUnsafeCustomPassword() {
// given
CommandSender sender = initPlayerWithName("player", true);
ChangePasswordCommand command = new ChangePasswordCommand();
Settings.unsafePasswords = Arrays.asList("test", "abc123");
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("abc123"));
// then
verify(messagesMock).send(sender, "password_error_unsafe");
//verify(pluginMock, never()).getServer();
}
private Player initPlayerWithName(String name, boolean loggedIn) {
Player player = mock(Player.class);
when(player.getName()).thenReturn(name);
when(cacheMock.isAuthenticated(name)).thenReturn(loggedIn);
return player;
}
}
@@ -0,0 +1,63 @@
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 org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.Arrays;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Test for {@link AddEmailCommand}.
*/
public class AddEmailCommandTest {
private AuthMe authMeMock;
private Management managementMock;
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
authMeMock = AuthMe.getInstance();
managementMock = Mockito.mock(Management.class);
when(authMeMock.getManagement()).thenReturn(managementMock);
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = Mockito.mock(BlockCommandSender.class);
AddEmailCommand command = new AddEmailCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts());
// then
verify(authMeMock, never()).getManagement();
}
@Test
public void shouldForwardData() {
// given
Player sender = Mockito.mock(Player.class);
AddEmailCommand command = new AddEmailCommand();
// when
command.executeCommand(sender, new CommandParts(),
new CommandParts(Arrays.asList("mail@example", "other_example")));
// then
verify(authMeMock).getManagement();
verify(managementMock).performAddEmail(sender, "mail@example", "other_example");
}
}
@@ -0,0 +1,63 @@
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 org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.Arrays;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Test for {@link ChangeEmailCommand}.
*/
public class ChangeEmailCommandTest {
private AuthMe authMeMock;
private Management managementMock;
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
authMeMock = AuthMe.getInstance();
managementMock = Mockito.mock(Management.class);
when(authMeMock.getManagement()).thenReturn(managementMock);
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = Mockito.mock(BlockCommandSender.class);
ChangeEmailCommand command = new ChangeEmailCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts());
// then
verify(authMeMock, never()).getManagement();
}
@Test
public void shouldForwardData() {
// given
Player sender = Mockito.mock(Player.class);
ChangeEmailCommand command = new ChangeEmailCommand();
// when
command.executeCommand(sender, new CommandParts(),
new CommandParts(Arrays.asList("new.mail@example.org", "old_mail@example.org")));
// then
verify(authMeMock).getManagement();
verify(managementMock).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org");
}
}
@@ -0,0 +1,36 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.command.CommandParts;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
/**
* Test for {@link RecoverEmailCommand}.
*/
public class RecoverEmailCommandTest {
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = Mockito.mock(BlockCommandSender.class);
RecoverEmailCommand command = new RecoverEmailCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts());
// then
}
// TODO ljacqu 20151121: Expand tests. This command doesn't use a scheduler and has all of its
// logic inside here.
}
@@ -6,7 +6,9 @@ import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@@ -97,4 +99,32 @@ public class StringUtilsTest {
// then
assertThat(result, equalTo("[MalformedURLException]: Unrecognized URL format"));
}
@Test
public void shouldPrintStackTrace() {
// given
MalformedURLException ex = new MalformedURLException("Unrecognized URL format");
// when
String result = StringUtils.getStackTrace(ex);
// then
assertThat(result, stringContainsInOrder(getClass().getName()));
}
@Test
public void shouldGetDifferenceWithNullString() {
// given/when/then
assertThat(StringUtils.getDifference(null, "test"), equalTo(1.0));
assertThat(StringUtils.getDifference("test", null), equalTo(1.0));
assertThat(StringUtils.getDifference(null, null), equalTo(1.0));
}
@Test
public void shouldGetDifferenceBetweenTwoString() {
// given/when/then
assertThat(StringUtils.getDifference("test", "taste"), equalTo(0.4));
assertThat(StringUtils.getDifference("test", "bear"), equalTo(0.75));
assertThat(StringUtils.getDifference("test", "something"), greaterThan(0.88));
}
}
@@ -2,63 +2,44 @@ package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.WrapperMock;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.Server;
import fr.xephi.authme.settings.Settings;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.logging.Logger;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;
/**
* Test for the {@link Utils} class.
*/
@Ignore
// TODO ljacqu 20151123: Fix test setup
public class UtilsTest {
private AuthMe authMeMock;
private PermissionsManager permissionsManagerMock;
private Wrapper wrapperMock;
@Before
public void setUpMocks() {
authMeMock = AuthMeMockUtil.mockAuthMeInstance();
// We need to create the Wrapper mock before injecting it into Utils because it runs a lot of code in
// a static block which needs the proper mocks to be set up.
wrapperMock = new WrapperMock(authMeMock);
Server serverMock = wrapperMock.getServer();
BukkitScheduler schedulerMock = mock(BukkitScheduler.class);
when(serverMock.getScheduler()).thenReturn(schedulerMock);
when(schedulerMock.runTaskAsynchronously(any(Plugin.class), any(Runnable.class)))
.thenReturn(mock(BukkitTask.class));
System.out.println("Initialized scheduler mock for server mock");
AuthMeMockUtil.insertMockWrapperInstance(Utils.class, "wrapper", (WrapperMock) wrapperMock);
System.out.println("Iniadfk");
permissionsManagerMock = mock(PermissionsManager.class);
AuthMeMockUtil.mockSingletonForClass(Utils.class, "plugin", authMeMock);
permissionsManagerMock = Mockito.mock(PermissionsManager.class);
when(authMeMock.getPermissionsManager()).thenReturn(permissionsManagerMock);
AuthMeMockUtil.initializeWrapperMock();
}
// TODO ljacques 20151122: The tests for Utils.forceGM somehow can't be set up with the mocks correctly
/*@Test
@Test
public void shouldForceSurvivalGameMode() {
// given
Player player = mock(Player.class);
@@ -68,6 +49,7 @@ public class UtilsTest {
Utils.forceGM(player);
// then
verify(authMeMock).getPermissionsManager();
verify(player).setGameMode(GameMode.SURVIVAL);
}
@@ -84,10 +66,40 @@ public class UtilsTest {
verify(authMeMock).getPermissionsManager();
verify(permissionsManagerMock).hasPermission(player, "authme.bypassforcesurvival");
verify(player, never()).setGameMode(any(GameMode.class));
}*/
}
@Test
// Note ljacqu 20151122: This is a heavy test setup with Reflections... If it causes trouble, skip it with @Ignore
public void shouldNotAddToNormalGroupIfPermissionsAreDisabled() {
// given
Settings.isPermissionCheckEnabled = false;
Player player = mock(Player.class);
// when
boolean result = Utils.addNormal(player, "test_group");
// then
assertThat(result, equalTo(false));
verify(authMeMock, never()).getPermissionsManager();
}
@Test
public void shouldNotAddToNormalGroupIfPermManagerIsNull() {
// given
Settings.isPermissionCheckEnabled = true;
given(authMeMock.getPermissionsManager()).willReturn(null);
Player player = mock(Player.class);
AuthMeMockUtil.mockSingletonForClass(ConsoleLogger.class, "wrapper", Wrapper.getInstance());
// when
boolean result = Utils.addNormal(player, "test_group");
// then
assertThat(result, equalTo(false));
verify(authMeMock).getPermissionsManager();
}
@Test
// Note ljacqu 20151122: This is a heavy test setup with reflections... If it causes trouble, skip it with @Ignore
public void shouldRetrieveListOfOnlinePlayersFromReflectedMethod() {
// given
setField("getOnlinePlayersIsCollection", false);
@@ -114,6 +126,7 @@ public class UtilsTest {
}
}
// Note: This method is used through reflections
public static Player[] onlinePlayersImpl() {
return new Player[]{
mock(Player.class), mock(Player.class)
@@ -1,6 +1,6 @@
package fr.xephi.authme;
package fr.xephi.authme.util;
import fr.xephi.authme.util.Wrapper;
import fr.xephi.authme.AuthMe;
import org.bukkit.Server;
import org.bukkit.scheduler.BukkitScheduler;
import org.mockito.Mockito;
@@ -19,11 +19,7 @@ public class WrapperMock extends Wrapper {
private static Map<Class<?>, Object> mocks = new HashMap<>();
public WrapperMock() {
this((AuthMe) getMock(AuthMe.class));
}
public WrapperMock(AuthMe authMe) {
super(authMe);
super();
}
@Override