Merge branch 'master' of https://github.com/AuthMe/AuthMeReloaded into 1141-optional-additional-2fa-auth
# Conflicts: # src/main/java/fr/xephi/authme/datasource/MySQL.java
This commit is contained in:
@@ -11,6 +11,7 @@ import java.util.Objects;
|
||||
/**
|
||||
* Custom matchers for AuthMe entities.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod") // Justification: Javadoc would be huge because of the many parameters
|
||||
public final class AuthMeMatchers {
|
||||
|
||||
private AuthMeMatchers() {
|
||||
|
||||
@@ -67,7 +67,7 @@ public class ClassCollector {
|
||||
public List<Class<?>> collectClasses(Predicate<Class<?>> filter) {
|
||||
File rootFolder = new File(root);
|
||||
List<Class<?>> collection = new ArrayList<>();
|
||||
collectClasses(rootFolder, filter, collection);
|
||||
gatherClassesFromFile(rootFolder, filter, collection);
|
||||
return collection;
|
||||
}
|
||||
|
||||
@@ -124,14 +124,14 @@ public class ClassCollector {
|
||||
* @param filter the class predicate
|
||||
* @param collection collection to add classes to
|
||||
*/
|
||||
private void collectClasses(File folder, Predicate<Class<?>> filter, List<Class<?>> collection) {
|
||||
private void gatherClassesFromFile(File folder, Predicate<Class<?>> filter, List<Class<?>> collection) {
|
||||
File[] files = folder.listFiles();
|
||||
if (files == null) {
|
||||
throw new IllegalStateException("Could not read files from '" + folder + "'");
|
||||
}
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
collectClasses(file, filter, collection);
|
||||
gatherClassesFromFile(file, filter, collection);
|
||||
} else if (file.isFile()) {
|
||||
Class<?> clazz = loadTaskClassFromFile(file);
|
||||
if (clazz != null && filter.test(clazz)) {
|
||||
|
||||
@@ -82,7 +82,6 @@ public final class ReflectionTestUtils {
|
||||
* @param clazz the class to retrieve a method from
|
||||
* @param methodName the name of the method
|
||||
* @param parameterTypes the parameter types the method to retrieve has
|
||||
*
|
||||
* @return the method of the class, set to be accessible
|
||||
*/
|
||||
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
|
||||
@@ -96,6 +95,15 @@ public final class ReflectionTestUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the given method on the provided instance with the given parameters.
|
||||
*
|
||||
* @param method the method to invoke
|
||||
* @param instance the instance to invoke the method on (null for static methods)
|
||||
* @param parameters the parameters to pass to the method
|
||||
* @param <V> return value of the method
|
||||
* @return method return value
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <V> V invokeMethod(Method method, Object instance, Object... parameters) {
|
||||
method.setAccessible(true);
|
||||
|
||||
+1
-1
@@ -160,7 +160,7 @@ public class RegisterAdminCommandTest {
|
||||
Player player = mock(Player.class);
|
||||
given(bukkitService.getPlayerExact(user)).willReturn(player);
|
||||
String kickForAdminRegister = "Admin registered you -- log in again";
|
||||
given(commandService.retrieveSingleMessage(MessageKey.KICK_FOR_ADMIN_REGISTER)).willReturn(kickForAdminRegister);
|
||||
given(commandService.retrieveSingleMessage(player, MessageKey.KICK_FOR_ADMIN_REGISTER)).willReturn(kickForAdminRegister);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
@@ -149,7 +149,7 @@ public class TempbanManagerTest {
|
||||
String ip = "123.45.67.89";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
String banReason = "IP ban too many logins";
|
||||
given(messages.retrieveSingle(MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
|
||||
given(messages.retrieveSingle(player, MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
|
||||
Settings settings = mockSettings(2, 100, "");
|
||||
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
|
||||
setBukkitServiceToScheduleSyncDelayedTask(bukkitService);
|
||||
@@ -195,7 +195,7 @@ public class TempbanManagerTest {
|
||||
String ip = "22.44.66.88";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
String banReason = "kick msg";
|
||||
given(messages.retrieveSingle(MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
|
||||
given(messages.retrieveSingle(player, MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
|
||||
Settings settings = mockSettings(10, 60, "");
|
||||
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
|
||||
manager.increaseCount(ip, "user");
|
||||
|
||||
@@ -14,6 +14,7 @@ import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
/**
|
||||
* Contains matchers for LimboPlayer.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod") // Justification: Javadoc would be huge because of the many parameters
|
||||
public final class LimboPlayerMatchers {
|
||||
|
||||
private LimboPlayerMatchers() {
|
||||
@@ -45,7 +46,8 @@ public final class LimboPlayerMatchers {
|
||||
@Override
|
||||
public void describeMismatchSafely(LimboPlayer item, Description description) {
|
||||
description.appendText(format("Limbo with isOp=%s, groups={%s}, canFly=%s, walkSpeed=%f, flySpeed=%f",
|
||||
item.isOperator(), String.join(" ,", item.getGroups()), item.isCanFly(), item.getWalkSpeed(), item.getFlySpeed()));
|
||||
item.isOperator(), String.join(" ,", item.getGroups()), item.isCanFly(),
|
||||
item.getWalkSpeed(), item.getFlySpeed()));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
Player player = mock(Player.class);
|
||||
LimboPlayer limboPlayer = mock(LimboPlayer.class);
|
||||
MessageKey key = MessageKey.REGISTER_MESSAGE;
|
||||
given(messages.retrieveSingle(key)).willReturn("Please register!");
|
||||
given(messages.retrieveSingle(player, key)).willReturn("Please register!");
|
||||
int interval = 12;
|
||||
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(interval);
|
||||
|
||||
@@ -80,7 +80,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
|
||||
// then
|
||||
verify(limboPlayer).setMessageTask(any(MessageTask.class));
|
||||
verify(messages).retrieveSingle(key);
|
||||
verify(messages).retrieveSingle(player, key);
|
||||
verify(bukkitService).runTaskTimer(
|
||||
any(MessageTask.class), eq(2L * TICKS_PER_SECOND), eq((long) interval * TICKS_PER_SECOND));
|
||||
}
|
||||
@@ -110,7 +110,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
MessageTask existingMessageTask = mock(MessageTask.class);
|
||||
limboPlayer.setMessageTask(existingMessageTask);
|
||||
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(8);
|
||||
given(messages.retrieveSingle(MessageKey.REGISTER_MESSAGE)).willReturn("Please register!");
|
||||
given(messages.retrieveSingle(player, MessageKey.REGISTER_MESSAGE)).willReturn("Please register!");
|
||||
|
||||
// when
|
||||
limboPlayerTaskManager.registerMessageTask(player, limboPlayer, false);
|
||||
@@ -119,7 +119,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
assertThat(limboPlayer.getMessageTask(), not(nullValue()));
|
||||
assertThat(limboPlayer.getMessageTask(), not(sameInstance(existingMessageTask)));
|
||||
verify(registrationCaptchaManager).isCaptchaRequired(name);
|
||||
verify(messages).retrieveSingle(MessageKey.REGISTER_MESSAGE);
|
||||
verify(messages).retrieveSingle(player, MessageKey.REGISTER_MESSAGE);
|
||||
verify(existingMessageTask).cancel();
|
||||
}
|
||||
|
||||
@@ -134,14 +134,14 @@ public class LimboPlayerTaskManagerTest {
|
||||
given(registrationCaptchaManager.isCaptchaRequired(name)).willReturn(true);
|
||||
String captcha = "M032";
|
||||
given(registrationCaptchaManager.getCaptchaCodeOrGenerateNew(name)).willReturn(captcha);
|
||||
given(messages.retrieveSingle(MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, captcha)).willReturn("Need to use captcha");
|
||||
given(messages.retrieveSingle(player, MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, captcha)).willReturn("Need to use captcha");
|
||||
|
||||
// when
|
||||
limboPlayerTaskManager.registerMessageTask(player, limboPlayer, false);
|
||||
|
||||
// then
|
||||
assertThat(limboPlayer.getMessageTask(), not(nullValue()));
|
||||
verify(messages).retrieveSingle(MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, captcha);
|
||||
verify(messages).retrieveSingle(player, MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, captcha);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -159,7 +159,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
// then
|
||||
verify(limboPlayer).setTimeoutTask(bukkitTask);
|
||||
verify(bukkitService).runTaskLater(any(TimeoutTask.class), eq(600L)); // 30 * TICKS_PER_SECOND
|
||||
verify(messages).retrieveSingle(MessageKey.LOGIN_TIMEOUT_ERROR);
|
||||
verify(messages).retrieveSingle(player, MessageKey.LOGIN_TIMEOUT_ERROR);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -194,7 +194,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
verify(existingTask).cancel();
|
||||
assertThat(limboPlayer.getTimeoutTask(), equalTo(bukkitTask));
|
||||
verify(bukkitService).runTaskLater(any(TimeoutTask.class), eq(360L)); // 18 * TICKS_PER_SECOND
|
||||
verify(messages).retrieveSingle(MessageKey.LOGIN_TIMEOUT_ERROR);
|
||||
verify(messages).retrieveSingle(player, MessageKey.LOGIN_TIMEOUT_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,15 @@ public final class SqlDataSourceTestUtil {
|
||||
return new MySQL(settings, hikariDataSource, extensionsFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a SQLite implementation for testing purposes. Methods are overridden so the
|
||||
* provided connection is never overridden.
|
||||
*
|
||||
* @param settings settings instance
|
||||
* @param dataFolder data folder
|
||||
* @param connection connection to use
|
||||
* @return the created SQLite instance
|
||||
*/
|
||||
public static SQLite createSqlite(Settings settings, File dataFolder, Connection connection) {
|
||||
return new SQLite(settings, dataFolder, connection) {
|
||||
// Override reload() so it doesn't run SQLite#connect, since we're given a specific Connection to use
|
||||
|
||||
@@ -101,7 +101,7 @@ public class OnJoinVerifierTest {
|
||||
event.setResult(PlayerLoginEvent.Result.KICK_FULL);
|
||||
given(permissionsManager.hasPermission(player, PlayerStatePermission.IS_VIP)).willReturn(false);
|
||||
String serverFullMessage = "server is full";
|
||||
given(messages.retrieveSingle(MessageKey.KICK_FULL_SERVER)).willReturn(serverFullMessage);
|
||||
given(messages.retrieveSingle(player, MessageKey.KICK_FULL_SERVER)).willReturn(serverFullMessage);
|
||||
|
||||
// when
|
||||
boolean result = onJoinVerifier.refusePlayerForFullServer(event);
|
||||
@@ -125,7 +125,7 @@ public class OnJoinVerifierTest {
|
||||
given(permissionsManager.hasPermission(onlinePlayers.get(1), PlayerStatePermission.IS_VIP)).willReturn(false);
|
||||
returnOnlineListFromBukkitServer(onlinePlayers);
|
||||
given(server.getMaxPlayers()).willReturn(onlinePlayers.size());
|
||||
given(messages.retrieveSingle(MessageKey.KICK_FOR_VIP)).willReturn("kick for vip");
|
||||
given(messages.retrieveSingle(player, MessageKey.KICK_FOR_VIP)).willReturn("kick for vip");
|
||||
|
||||
// when
|
||||
boolean result = onJoinVerifier.refusePlayerForFullServer(event);
|
||||
@@ -149,7 +149,7 @@ public class OnJoinVerifierTest {
|
||||
given(permissionsManager.hasPermission(onlinePlayers.get(0), PlayerStatePermission.IS_VIP)).willReturn(true);
|
||||
returnOnlineListFromBukkitServer(onlinePlayers);
|
||||
given(server.getMaxPlayers()).willReturn(onlinePlayers.size());
|
||||
given(messages.retrieveSingle(MessageKey.KICK_FULL_SERVER)).willReturn("kick full server");
|
||||
given(messages.retrieveSingle(player, MessageKey.KICK_FULL_SERVER)).willReturn("kick full server");
|
||||
|
||||
// when
|
||||
boolean result = onJoinVerifier.refusePlayerForFullServer(event);
|
||||
|
||||
@@ -596,7 +596,7 @@ public class PlayerListenerTest {
|
||||
MessageKey.INVALID_NAME_CHARACTERS, "[a-z]");
|
||||
doThrow(exception).when(onJoinVerifier).checkIsValidName(name);
|
||||
String message = "Invalid characters!";
|
||||
given(messages.retrieveSingle(exception.getReason(), exception.getArgs())).willReturn(message);
|
||||
given(messages.retrieveSingle(player, exception.getReason(), exception.getArgs())).willReturn(message);
|
||||
|
||||
// when
|
||||
listener.onPlayerLogin(event);
|
||||
|
||||
@@ -81,9 +81,11 @@ public class MessagesIntegrationTest {
|
||||
public void shouldLoadMessageAndSplitAtNewLines() {
|
||||
// given
|
||||
MessageKey key = MessageKey.UNKNOWN_USER;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
String[] message = messages.retrieve(key);
|
||||
String[] message = messages.retrieve(key, sender);
|
||||
|
||||
// then
|
||||
String[] lines = new String[]{"We've got", "new lines", "and ' apostrophes"};
|
||||
@@ -94,9 +96,11 @@ public class MessagesIntegrationTest {
|
||||
public void shouldLoadMessageAsStringWithNewLines() {
|
||||
// given
|
||||
MessageKey key = MessageKey.UNKNOWN_USER;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
String message = messages.retrieveSingle(key);
|
||||
String message = messages.retrieveSingle(sender, key);
|
||||
|
||||
// then
|
||||
assertThat(message, equalTo("We've got\nnew lines\nand ' apostrophes"));
|
||||
@@ -106,9 +110,11 @@ public class MessagesIntegrationTest {
|
||||
public void shouldFormatColorCodes() {
|
||||
// given
|
||||
MessageKey key = MessageKey.LOGIN_SUCCESS;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
String[] message = messages.retrieve(key);
|
||||
String[] message = messages.retrieve(key, sender);
|
||||
|
||||
// then
|
||||
assertThat(message, arrayWithSize(1));
|
||||
@@ -120,6 +126,7 @@ public class MessagesIntegrationTest {
|
||||
// given
|
||||
MessageKey key = MessageKey.EMAIL_ALREADY_USED_ERROR;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
messages.send(sender, key);
|
||||
@@ -133,6 +140,8 @@ public class MessagesIntegrationTest {
|
||||
// given
|
||||
MessageKey key = MessageKey.LOGIN_SUCCESS;
|
||||
Player player = Mockito.mock(Player.class);
|
||||
given(player.getName()).willReturn("Tester");
|
||||
given(player.getDisplayName()).willReturn("§cTesty");
|
||||
|
||||
// when
|
||||
messages.send(player, key);
|
||||
@@ -146,6 +155,8 @@ public class MessagesIntegrationTest {
|
||||
// given
|
||||
MessageKey key = MessageKey.UNKNOWN_USER;
|
||||
Player player = Mockito.mock(Player.class);
|
||||
given(player.getName()).willReturn("Tester");
|
||||
given(player.getDisplayName()).willReturn("§cTesty");
|
||||
|
||||
// when
|
||||
messages.send(player, key);
|
||||
@@ -157,11 +168,27 @@ public class MessagesIntegrationTest {
|
||||
assertThat(captor.getAllValues(), contains(lines));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSendMessageToPlayerWithNameReplacement() {
|
||||
// given
|
||||
MessageKey key = MessageKey.REGISTER_MESSAGE;
|
||||
Player player = Mockito.mock(Player.class);
|
||||
given(player.getName()).willReturn("Tester");
|
||||
given(player.getDisplayName()).willReturn("§cTesty");
|
||||
|
||||
// when
|
||||
messages.send(player, key);
|
||||
|
||||
// then
|
||||
verify(player).sendMessage("§3Please Tester, register to the §cTesty§3.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSendMessageToPlayerWithTagReplacement() {
|
||||
// given
|
||||
MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR;
|
||||
CommandSender sender = Mockito.mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
messages.send(sender, key, "1234");
|
||||
@@ -175,6 +202,7 @@ public class MessagesIntegrationTest {
|
||||
// given
|
||||
MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
messages.send(sender, key);
|
||||
@@ -189,9 +217,11 @@ public class MessagesIntegrationTest {
|
||||
Logger logger = mock(Logger.class);
|
||||
ConsoleLogger.setLogger(logger);
|
||||
MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
messages.send(mock(CommandSender.class), key, "rep", "rep2");
|
||||
messages.send(sender, key, "rep", "rep2");
|
||||
|
||||
// then
|
||||
verify(logger).warning(argThat(containsString("Invalid number of replacements")));
|
||||
@@ -203,9 +233,11 @@ public class MessagesIntegrationTest {
|
||||
Logger logger = mock(Logger.class);
|
||||
ConsoleLogger.setLogger(logger);
|
||||
MessageKey key = MessageKey.UNKNOWN_USER;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
messages.send(mock(CommandSender.class), key, "Replacement");
|
||||
messages.send(sender, key, "Replacement");
|
||||
|
||||
// then
|
||||
verify(logger).warning(argThat(containsString("Invalid number of replacements")));
|
||||
@@ -216,9 +248,11 @@ public class MessagesIntegrationTest {
|
||||
// given
|
||||
// Key is present in both files
|
||||
MessageKey key = MessageKey.WRONG_PASSWORD;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
String message = messages.retrieveSingle(key);
|
||||
String message = messages.retrieveSingle(sender, key);
|
||||
|
||||
// then
|
||||
assertThat(message, equalTo("§cWrong password!"));
|
||||
@@ -228,9 +262,11 @@ public class MessagesIntegrationTest {
|
||||
public void shouldRetrieveMessageWithReplacements() {
|
||||
// given
|
||||
MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
String result = messages.retrieveSingle(key, "24680");
|
||||
String result = messages.retrieveSingle(sender.getName(), key, "24680");
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("Use /captcha 24680 to solve the captcha"));
|
||||
|
||||
@@ -83,15 +83,16 @@ public class CommonServiceTest {
|
||||
public void shouldRetrieveSingleMessage() {
|
||||
// given
|
||||
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
|
||||
Player player = mock(Player.class);
|
||||
String text = "Test text";
|
||||
given(messages.retrieveSingle(key)).willReturn(text);
|
||||
given(messages.retrieveSingle(player, key)).willReturn(text);
|
||||
|
||||
// when
|
||||
String result = commonService.retrieveSingleMessage(key);
|
||||
String result = commonService.retrieveSingleMessage(player, key);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(text));
|
||||
verify(messages).retrieveSingle(key);
|
||||
verify(messages).retrieveSingle(player, key);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package fr.xephi.authme.service;
|
||||
|
||||
import com.maxmind.geoip.Country;
|
||||
import com.maxmind.geoip.LookupService;
|
||||
import com.maxmind.db.GeoIp2Provider;
|
||||
import com.maxmind.db.model.Country;
|
||||
import com.maxmind.db.model.CountryResponse;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -10,13 +16,11 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -29,8 +33,12 @@ public class GeoIpServiceTest {
|
||||
|
||||
private GeoIpService geoIpService;
|
||||
private File dataFolder;
|
||||
|
||||
@Mock
|
||||
private LookupService lookupService;
|
||||
private GeoIp2Provider lookupService;
|
||||
|
||||
@Mock
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
@@ -38,20 +46,24 @@ public class GeoIpServiceTest {
|
||||
@Before
|
||||
public void initializeGeoLiteApi() throws IOException {
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
geoIpService = new GeoIpService(dataFolder, lookupService);
|
||||
geoIpService = new GeoIpService(dataFolder, bukkitService, lookupService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetCountry() {
|
||||
public void shouldGetCountry() throws Exception {
|
||||
// given
|
||||
String ip = "123.45.67.89";
|
||||
InetAddress ip = InetAddress.getByName("123.45.67.89");
|
||||
String countryCode = "XX";
|
||||
|
||||
Country country = mock(Country.class);
|
||||
given(country.getCode()).willReturn(countryCode);
|
||||
given(lookupService.getCountry(ip)).willReturn(country);
|
||||
given(country.getIsoCode()).willReturn(countryCode);
|
||||
|
||||
CountryResponse response = mock(CountryResponse.class);
|
||||
given(response.getCountry()).willReturn(country);
|
||||
given(lookupService.getCountry(ip)).willReturn(response);
|
||||
|
||||
// when
|
||||
String result = geoIpService.getCountryCode(ip);
|
||||
String result = geoIpService.getCountryCode(ip.getHostAddress());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(countryCode));
|
||||
@@ -59,7 +71,7 @@ public class GeoIpServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotLookUpCountryForLocalhostIp() {
|
||||
public void shouldNotLookUpCountryForLocalhostIp() throws Exception {
|
||||
// given
|
||||
String ip = "127.0.0.1";
|
||||
|
||||
@@ -68,20 +80,24 @@ public class GeoIpServiceTest {
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("--"));
|
||||
verify(lookupService, never()).getCountry(anyString());
|
||||
verify(lookupService, never()).getCountry(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLookUpCountryName() {
|
||||
public void shouldLookUpCountryName() throws Exception {
|
||||
// given
|
||||
String ip = "24.45.167.89";
|
||||
InetAddress ip = InetAddress.getByName("24.45.167.89");
|
||||
String countryName = "Ecuador";
|
||||
|
||||
Country country = mock(Country.class);
|
||||
given(country.getName()).willReturn(countryName);
|
||||
given(lookupService.getCountry(ip)).willReturn(country);
|
||||
|
||||
CountryResponse response = mock(CountryResponse.class);
|
||||
given(response.getCountry()).willReturn(country);
|
||||
given(lookupService.getCountry(ip)).willReturn(response);
|
||||
|
||||
// when
|
||||
String result = geoIpService.getCountryName(ip);
|
||||
String result = geoIpService.getCountryName(ip.getHostAddress());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(countryName));
|
||||
@@ -89,16 +105,15 @@ public class GeoIpServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotLookUpCountryNameForLocalhostIp() {
|
||||
public void shouldNotLookUpCountryNameForLocalhostIp() throws Exception {
|
||||
// given
|
||||
String ip = "127.0.0.1";
|
||||
InetAddress ip = InetAddress.getByName("127.0.0.1");
|
||||
|
||||
// when
|
||||
String result = geoIpService.getCountryName(ip);
|
||||
String result = geoIpService.getCountryName(ip.getHostAddress());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("N/A"));
|
||||
verify(lookupService, never()).getCountry(ip);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -106,7 +106,6 @@ public class SessionServiceTest {
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED);
|
||||
verify(commonService).send(player, MessageKey.SESSION_EXPIRED);
|
||||
verify(dataSource).hasSession(name);
|
||||
verify(dataSource).setUnlogged(name);
|
||||
verify(dataSource).revokeSession(name);
|
||||
@@ -132,7 +131,6 @@ public class SessionServiceTest {
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED);
|
||||
verify(commonService).send(player, MessageKey.SESSION_EXPIRED);
|
||||
verify(dataSource).hasSession(name);
|
||||
verify(dataSource).setUnlogged(name);
|
||||
verify(dataSource).revokeSession(name);
|
||||
@@ -145,9 +143,10 @@ public class SessionServiceTest {
|
||||
String ip = "127.3.12.15";
|
||||
Player player = mockPlayerWithNameAndIp(name, ip);
|
||||
given(dataSource.hasSession(name)).willReturn(true);
|
||||
given(commonService.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(8);
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name(name)
|
||||
.lastLogin(System.currentTimeMillis())
|
||||
.lastLogin(System.currentTimeMillis() - 7 * 60 * 1000)
|
||||
.lastIp("8.8.8.8").build();
|
||||
given(dataSource.getAuth(name)).willReturn(auth);
|
||||
|
||||
@@ -219,6 +218,7 @@ public class SessionServiceTest {
|
||||
String name = "Charles";
|
||||
Player player = mockPlayerWithNameAndIp(name, "144.117.118.145");
|
||||
given(dataSource.hasSession(name)).willReturn(true);
|
||||
given(commonService.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(8);
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name(name)
|
||||
.lastIp(null)
|
||||
|
||||
@@ -131,7 +131,8 @@ public class DrawDependency implements ToolTask {
|
||||
private Class<?> unwrapGenericClass(Type genericType) {
|
||||
if (genericType == Factory.class || genericType == SingletonStore.class) {
|
||||
Class<?> parameterType = ReflectionUtils.getGenericType(genericType);
|
||||
Objects.requireNonNull(parameterType, "Parameter type for '" + genericType + "' should be a concrete class");
|
||||
Objects.requireNonNull(parameterType,
|
||||
"Parameter type for '" + genericType + "' should be a concrete class");
|
||||
return parameterType;
|
||||
}
|
||||
return InjectorUtils.convertToClass(genericType);
|
||||
|
||||
@@ -56,6 +56,12 @@ public class EncryptionMethodInfoGatherer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a description of the given hash algorithm based on its annotations.
|
||||
*
|
||||
* @param algorithm the algorithm to describe
|
||||
* @return description of the hash algorithm
|
||||
*/
|
||||
private static MethodDescription createDescription(HashAlgorithm algorithm) {
|
||||
Class<? extends EncryptionMethod> clazz = algorithm.getClazz();
|
||||
EncryptionMethod method = createEncryptionMethod(clazz);
|
||||
|
||||
@@ -37,6 +37,7 @@ public class PermissionNodesGatherer {
|
||||
/**
|
||||
* Return a sorted collection of all permission nodes, including its JavaDoc description.
|
||||
*
|
||||
* @param <T> permission node enum type
|
||||
* @return Ordered map whose keys are the permission nodes and the values the associated JavaDoc
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -4,18 +4,20 @@ import tools.utils.AutoToolTask;
|
||||
import tools.utils.FileIoUtils;
|
||||
import tools.utils.TagValue.NestedTagValue;
|
||||
import tools.utils.TagValueHolder;
|
||||
import tools.utils.ToolsConstants;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static tools.utils.ToolsConstants.DOCS_FOLDER;
|
||||
import static tools.utils.ToolsConstants.TOOLS_SOURCE_ROOT;
|
||||
|
||||
/**
|
||||
* Task responsible for formatting a permissions node list and
|
||||
* for writing it to a file if desired.
|
||||
*/
|
||||
public class PermissionsListWriter implements AutoToolTask {
|
||||
|
||||
private static final String TEMPLATE_FILE = ToolsConstants.TOOLS_SOURCE_ROOT + "docs/permissions/permission_nodes.tpl.md";
|
||||
private static final String PERMISSIONS_OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "permission_nodes.md";
|
||||
private static final String TEMPLATE_FILE = TOOLS_SOURCE_ROOT + "docs/permissions/permission_nodes.tpl.md";
|
||||
private static final String PERMISSIONS_OUTPUT_FILE = DOCS_FOLDER + "permission_nodes.md";
|
||||
|
||||
@Override
|
||||
public String getTaskName() {
|
||||
|
||||
@@ -45,10 +45,10 @@ public class TranslationPageGenerator implements AutoToolTask {
|
||||
NestedTagValue translationValuesHolder = new NestedTagValue();
|
||||
|
||||
for (TranslationInfo translation : gatherer.getTranslationInfo()) {
|
||||
int percentage = (int) Math.round(translation.percentTranslated * 100);
|
||||
String name = firstNonNull(LANGUAGE_NAMES.get(translation.code), "?");
|
||||
int percentage = (int) Math.round(translation.getPercentTranslated() * 100);
|
||||
String name = firstNonNull(LANGUAGE_NAMES.get(translation.getCode()), "?");
|
||||
TagValueHolder valueHolder = TagValueHolder.create()
|
||||
.put("code", translation.code)
|
||||
.put("code", translation.getCode())
|
||||
.put("name", name)
|
||||
.put("percentage", Integer.toString(percentage))
|
||||
.put("color", computeColor(percentage));
|
||||
|
||||
@@ -25,7 +25,7 @@ public class TranslationsGatherer {
|
||||
|
||||
public TranslationsGatherer() {
|
||||
gatherTranslations();
|
||||
translationInfo.sort((e1, e2) -> getCode(e1).compareTo(getCode(e2)));
|
||||
translationInfo.sort((e1, e2) -> getSortCode(e1).compareTo(getSortCode(e2)));
|
||||
}
|
||||
|
||||
public List<TranslationInfo> getTranslationInfo() {
|
||||
@@ -61,16 +61,6 @@ public class TranslationsGatherer {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static final class TranslationInfo {
|
||||
public final String code;
|
||||
public final double percentTranslated;
|
||||
|
||||
TranslationInfo(String code, double percentTranslated) {
|
||||
this.code = code;
|
||||
this.percentTranslated = percentTranslated;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the language code from the translation info for sorting purposes.
|
||||
* Returns "a" for "en" language code to sort English on top.
|
||||
@@ -78,8 +68,26 @@ public class TranslationsGatherer {
|
||||
* @param info the translation info
|
||||
* @return the language code for sorting
|
||||
*/
|
||||
private static String getCode(TranslationInfo info) {
|
||||
private static String getSortCode(TranslationInfo info) {
|
||||
return "en".equals(info.code) ? "a" : info.code;
|
||||
}
|
||||
|
||||
public static final class TranslationInfo {
|
||||
private final String code;
|
||||
private final double percentTranslated;
|
||||
|
||||
TranslationInfo(String code, double percentTranslated) {
|
||||
this.code = code;
|
||||
this.percentTranslated = percentTranslated;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public double getPercentTranslated() {
|
||||
return percentTranslated;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ public class GeneratePluginYml implements AutoToolTask {
|
||||
List<String> pluginYmlLines = FileIoUtils.readLinesFromFile(Paths.get(PLUGIN_YML_FILE));
|
||||
int lineNr = 0;
|
||||
for (String line : pluginYmlLines) {
|
||||
if (line.equals("commands:")) {
|
||||
if ("commands:".equals(line)) {
|
||||
break;
|
||||
}
|
||||
++lineNr;
|
||||
|
||||
@@ -33,8 +33,8 @@ public class CheckMessageKeyUsages implements AutoToolTask {
|
||||
if (unusedKeys.isEmpty()) {
|
||||
System.out.println("No unused MessageKey entries found :)");
|
||||
} else {
|
||||
System.out.println("Did not find usages for keys:\n- " +
|
||||
String.join("\n- ", Lists.transform(unusedKeys, MessageKey::name)));
|
||||
System.out.println("Did not find usages for keys:\n- "
|
||||
+ String.join("\n- ", Lists.transform(unusedKeys, MessageKey::name)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,21 +51,6 @@ public class CheckMessageKeyUsages implements AutoToolTask {
|
||||
return keys;
|
||||
}
|
||||
|
||||
private List<File> findUsagesOfKey(MessageKey key) {
|
||||
List<File> filesUsingKey = new ArrayList<>();
|
||||
File sourceFolder = new File(ToolsConstants.MAIN_SOURCE_ROOT);
|
||||
|
||||
Consumer<File> usagesCollector = file -> {
|
||||
String source = FileIoUtils.readFromFile(file.toPath());
|
||||
if (source.contains(key.name())) {
|
||||
filesUsingKey.add(file);
|
||||
}
|
||||
};
|
||||
|
||||
walkJavaFileTree(sourceFolder, usagesCollector);
|
||||
return filesUsingKey;
|
||||
}
|
||||
|
||||
private static void walkJavaFileTree(File folder, Consumer<File> javaFileConsumer) {
|
||||
for (File file : FileIoUtils.listFilesOrThrow(folder)) {
|
||||
if (file.isDirectory()) {
|
||||
|
||||
@@ -27,6 +27,12 @@ public final class FileIoUtils {
|
||||
writeToFile(Paths.get(outputFile), contents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the given contents to the file, overriding any existing content.
|
||||
*
|
||||
* @param path the file to write to
|
||||
* @param contents the contents to write
|
||||
*/
|
||||
public static void writeToFile(Path path, String contents) {
|
||||
try {
|
||||
Files.write(path, contents.getBytes());
|
||||
@@ -35,6 +41,12 @@ public final class FileIoUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given contents to the file while keeping any existing content.
|
||||
*
|
||||
* @param outputFile the file to write to
|
||||
* @param contents the contents to append
|
||||
*/
|
||||
public static void appendToFile(String outputFile, String contents) {
|
||||
try {
|
||||
Files.write(Paths.get(outputFile), contents.getBytes(), StandardOpenOption.APPEND);
|
||||
@@ -47,6 +59,12 @@ public final class FileIoUtils {
|
||||
return readFromFile(Paths.get(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the given file's contents as string.
|
||||
*
|
||||
* @param file the file to read
|
||||
* @return the file's contents
|
||||
*/
|
||||
public static String readFromFile(Path file) {
|
||||
try {
|
||||
return new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
|
||||
@@ -55,6 +73,12 @@ public final class FileIoUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the lines of the given file.
|
||||
*
|
||||
* @param path the path of the file to read
|
||||
* @return the lines of the file
|
||||
*/
|
||||
public static List<String> readLinesFromFile(Path path) {
|
||||
try {
|
||||
return Files.readAllLines(path, StandardCharsets.UTF_8);
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
error:
|
||||
unregistered_user: 'We''ve got%nl%new lines%nl%and '' apostrophes'
|
||||
registration:
|
||||
register_request: '&3Please %username%, register to the %displayname%&3.'
|
||||
login:
|
||||
success: '&cHere we have&bdefined some colors &dand some other <hings'
|
||||
wrong_password: '&cWrong password!'
|
||||
|
||||
Reference in New Issue
Block a user