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:
ljacqu
2015-11-23 23:25:03 +01:00
parent 8719cce334
commit 6422f90114
11 changed files with 261 additions and 51 deletions
@@ -1,6 +1,7 @@
package fr.xephi.authme;
import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.util.Wrapper;
import org.mockito.Mockito;
import java.lang.reflect.Field;
@@ -15,15 +16,18 @@ public final class AuthMeMockUtil {
}
/**
* Sets the AuthMe plugin instance to a mock object. Use {@link AuthMe#getInstance()} to retrieve the mock.
* Set the AuthMe plugin instance to a mock object. Use {@link AuthMe#getInstance()} to retrieve the mock.
*
* @return The generated mock for the AuthMe instance
*/
public static void mockAuthMeInstance() {
public static AuthMe mockAuthMeInstance() {
AuthMe mock = Mockito.mock(AuthMe.class);
mockSingletonForClass(AuthMe.class, "plugin", mock);
return mock;
}
/**
* Creates a mock Messages object for the instance returned from {@link Messages#getInstance()}.
* Create a mock Messages object for the instance returned from {@link Messages#getInstance()}.
*/
public static void mockMessagesInstance() {
Messages mock = Mockito.mock(Messages.class);
@@ -31,11 +35,39 @@ public final class AuthMeMockUtil {
}
/**
* Sets a field of a class to the given mock.
* Set the given class' {@link Wrapper} field to a mock implementation.
*
* @param clazz the class to modify
* @param fieldName the field name
* @param mock the mock to set for the given field
* @param clazz The class to modify
* @param fieldName The name of the field containing the Wrapper in the class
*
* @return The generated Wrapper mock
* @see WrapperMock
*/
public static Wrapper insertMockWrapperInstance(Class<?> clazz, String fieldName) {
Wrapper wrapperMock = new WrapperMock();
mockSingletonForClass(clazz, fieldName, wrapperMock);
return wrapperMock;
}
public static Wrapper insertMockWrapperInstance(Class<?> clazz, String fieldName, AuthMe authMe) {
Wrapper wrapperMock = new WrapperMock(authMe);
mockSingletonForClass(clazz, fieldName, wrapperMock);
return wrapperMock;
}
// TODO ljacqu 20151123: Find the use cases for the WrapperMock and remove any of these
// methods that will end up unused
public static Wrapper insertMockWrapperInstance(Class<?> clazz, String fieldName, WrapperMock wrapperMock) {
mockSingletonForClass(clazz, fieldName, wrapperMock);
return wrapperMock;
}
/**
* Set a field of a class to the given mock.
*
* @param clazz The class to modify
* @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) {
try {
@@ -0,0 +1,54 @@
package fr.xephi.authme;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.Server;
import org.mockito.Mockito;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
/**
* Class returning mocks for all calls in {@link Wrapper}.
* This class keeps track of its mocks and will always return
* the same one for each type.
*/
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);
}
@Override
public Logger getLogger() {
return getMock(Logger.class);
}
@Override
public Server getServer() {
return getMock(Server.class);
}
@Override
public AuthMe getAuthMe() {
return getMock(AuthMe.class);
}
@SuppressWarnings("unchecked")
private static <T> T getMock(Class<?> clazz) {
Object o = mocks.get(clazz);
if (o == null) {
o = Mockito.mock(clazz);
mocks.put(clazz, o);
}
return (T) o;
}
}
@@ -0,0 +1,28 @@
package fr.xephi.authme.settings;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.ConsoleLogger;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
/**
* Test for {@link Messages}.
*/
public class MessagesTest {
@Before
public void setUpMessages() {
AuthMe authMe = AuthMeMockUtil.mockAuthMeInstance();
AuthMeMockUtil.insertMockWrapperInstance(ConsoleLogger.class, "wrapper", authMe);
File file = new File("messages_test.yml");
Messages messages = new Messages(file, "en");
}
@Test
public void shouldLoadMessages() {
}
}
@@ -2,6 +2,7 @@ package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.WrapperMock;
import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.Server;
import org.bukkit.entity.Player;
@@ -9,6 +10,7 @@ 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 java.lang.reflect.Field;
@@ -23,26 +25,32 @@ import static org.mockito.Mockito.when;
/**
* 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() {
AuthMeMockUtil.mockAuthMeInstance();
authMeMock = AuthMe.getInstance();
authMeMock = AuthMeMockUtil.mockAuthMeInstance();
permissionsManagerMock = mock(PermissionsManager.class);
when(authMeMock.getPermissionsManager()).thenReturn(permissionsManagerMock);
Server serverMock = mock(Server.class);
when(authMeMock.getGameServer()).thenReturn(serverMock);
// 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));
.thenReturn(mock(BukkitTask.class));
AuthMeMockUtil.insertMockWrapperInstance(Utils.class, "wrapper", (WrapperMock) wrapperMock);
permissionsManagerMock = mock(PermissionsManager.class);
when(authMeMock.getPermissionsManager()).thenReturn(permissionsManagerMock);
}
// TODO ljacques 20151122: The tests for Utils.forceGM somehow can't be set up with the mocks correctly
+58
View File
@@ -0,0 +1,58 @@
unknown_user: '&cCan''t find the requested user in the database!'
unsafe_spawn: '&cYour quit location was unsafe, you have been teleported to the world''s spawnpoint.'
not_logged_in: '&cYou''re not logged in!'
reg_voluntarily: 'You can register yourself to the server with the command "/register <password> <ConfirmPassword>"'
usage_log: '&cUsage: /login <password>'
wrong_pwd: '&cWrong password!'
unregistered: '&cSuccessfully unregistered!'
reg_disabled: '&cIn-game registration is disabled!'
valid_session: '&2Logged-in due to Session Reconnection.'
login: '&2Successful login!'
vb_nonActiv: '&cYour account isn''t activated yet, please check your emails!'
user_regged: '&cYou already have registered this username!'
usage_reg: '&cUsage: /register <password> <ConfirmPassword>'
max_reg: '&cYou have exceeded the maximum number of registrations for your connection!'
no_perm: '&4You don''t have the permission to perform this action!'
error: '&4An unexpected error occurred, please contact an Administrator!'
login_msg: '&cPlease, login with the command "/login <password>"'
reg_msg: '&3Please, register to the server with the command "/register <password> <ConfirmPassword>"'
reg_email_msg: '&3Please, register to the server with the command "/register <email> <confirmEmail>"'
usage_unreg: '&cUsage: /unregister <password>'
pwd_changed: '&2Password changed successfully!'
user_unknown: '&cThis user isn''t registered!'
password_error: '&cPasswords didn''t match, check them again!'
password_error_nick: '&cYou can''t use your name as password, please choose another one...'
password_error_unsafe: '&cThe chosen password isn''t safe, please choose another one...'
invalid_session: '&cYour IP has been changed and your session data has expired!'
reg_only: '&4Only registered users can join the server! Please visit http://example.com to register yourself!'
logged_in: '&cYou''re already logged in!'
logout: '&2Logged-out successfully!'
same_nick: '&4The same username is already playing on the server!'
registered: '&2Successfully registered!'
pass_len: '&cYour password is too short or too long! Please try with another one!'
reload: '&2Configuration and database have been reloaded correctly!'
timeout: '&4Login timeout exceeded, you have been kicked from the server, please try again!'
usage_changepassword: '&cUsage: /changepassword <oldPassword> <newPassword>'
name_len: '&4Your username is either too short or too long!'
regex: '&4Your username contains illegal characters. Allowed chars: REG_EX'
add_email: '&3Please add your email to your account with the command "/email add <yourEmail> <confirmEmail>"'
recovery_email: '&3Forgot your password? Please use the command "/email recovery <yourEmail>"'
usage_captcha: '&3To login you have to solve a captcha code, please use the command "/captcha <theCaptcha>"'
wrong_captcha: '&cWrong Captcha, please type "/captcha THE_CAPTCHA" into the chat!'
valid_captcha: '&2Captcha code solved correctly!'
kick_forvip: '&3A VIP Player has joined the server when it was full!'
kick_fullserver: '&4The server is full, try again later!'
usage_email_add: '&cUsage: /email add <email> <confirmEmail>'
usage_email_change: '&cUsage: /email change <oldEmail> <newEmail>'
usage_email_recovery: '&cUsage: /email recovery <Email>'
new_email_invalid: '&cInvalid New Email, try again!'
old_email_invalid: '&cInvalid Old Email, try again!'
email_invalid: '&cInvalid Email address, try again!'
email_added: '&2Email address successfully added to your account!'
email_confirm: '&cPlease confirm your email address!'
email_changed: '&2Email address changed correctly!'
email_send: '&2Recovery email sent correctly! Check your email inbox!'
email_exists: '&cA recovery email was already sent! You can discart it and send a new one using the command below:'
country_banned: '&4Your country is banned from this server!'
antibot_auto_enabled: '&4[AntiBotService] AntiBot enabled due to the huge number of connections!'
antibot_auto_disabled: '&2[AntiBotService] AntiBot disabled disabled after %m minutes!'