#1015 Implement lazy replace of tags for welcome message

- Check which tags are used when loading the welcome message and only apply their replacements afterwards
- Moves AuthMe#replaceAllInfo to a more appropriate place
- Avoid fetching data again for each line
This commit is contained in:
ljacqu
2017-01-21 13:53:11 +01:00
parent 666e242212
commit 811bdee128
7 changed files with 311 additions and 67 deletions
@@ -4,7 +4,6 @@ import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.configurationdata.ConfigurationDataBuilder;
import ch.jalu.configme.resource.PropertyResource;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.TestConfiguration;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -16,11 +15,8 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
@@ -45,26 +41,6 @@ public class SettingsTest {
testPluginFolder = temporaryFolder.newFolder();
}
@Test
public void shouldLoadWelcomeMessage() throws IOException {
// given
String welcomeMessage = "This is my welcome message for testing\nBye!";
File welcomeFile = new File(testPluginFolder, "welcome.txt");
createFile(welcomeFile);
Files.write(welcomeFile.toPath(), welcomeMessage.getBytes());
PropertyResource resource = mock(PropertyResource.class);
given(resource.getBoolean(RegistrationSettings.USE_WELCOME_MESSAGE.getPath())).willReturn(true);
Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA);
// when
String[] result = settings.getWelcomeMessage();
// then
assertThat(result, arrayWithSize(2));
assertThat(result, arrayContaining(welcomeMessage.split("\\n")));
}
@Test
public void shouldLoadEmailMessage() throws IOException {
// given
@@ -0,0 +1,106 @@
package fr.xephi.authme.settings;
import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.GeoIpService;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Test for {@link WelcomeMessageConfiguration}.
*/
@RunWith(DelayedInjectionRunner.class)
public class WelcomeMessageConfigurationTest {
@InjectDelayed
private WelcomeMessageConfiguration welcomeMessageConfiguration;
@Mock
private Server server;
@Mock
private BukkitService bukkitService;
@Mock
private GeoIpService geoIpService;
@Mock
private PlayerCache playerCache;
@DataFolder
private File testPluginFolder;
private File welcomeFile;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@BeforeInjecting
public void createPluginFolder() throws IOException {
testPluginFolder = temporaryFolder.newFolder();
welcomeFile = new File(testPluginFolder, "welcome.txt");
welcomeFile.createNewFile();
}
@Test
public void shouldLoadWelcomeMessage() throws IOException {
// given
String welcomeMessage = "This is my welcome message for testing\nBye!";
Files.write(welcomeFile.toPath(), welcomeMessage.getBytes());
Player player = mock(Player.class);
welcomeMessageConfiguration.reload();
// when
List<String> result = welcomeMessageConfiguration.getWelcomeMessage(player);
// then
assertThat(result, hasSize(2));
assertThat(result, contains(welcomeMessage.split("\\n")));
verifyZeroInteractions(player, playerCache, geoIpService, bukkitService, server);
}
@Test
public void shouldReplaceNameAndIpAndCountry() throws IOException {
// given
String welcomeMessage = "Hello {PLAYER}, your IP is {IP}\nYour country is {COUNTRY}.\nWelcome to {SERVER}!";
Files.write(welcomeFile.toPath(), welcomeMessage.getBytes());
welcomeMessageConfiguration.reload();
Player player = mock(Player.class);
given(player.getName()).willReturn("Bobby");
TestHelper.mockPlayerIp(player, "123.45.66.77");
given(geoIpService.getCountryName("123.45.66.77")).willReturn("Syldavia");
given(server.getServerName()).willReturn("CrazyServer");
// when
List<String> result = welcomeMessageConfiguration.getWelcomeMessage(player);
// then
assertThat(result, hasSize(3));
assertThat(result.get(0), equalTo("Hello Bobby, your IP is 123.45.66.77"));
assertThat(result.get(1), equalTo("Your country is Syldavia."));
assertThat(result.get(2), equalTo("Welcome to CrazyServer!"));
verify(server, only()).getServerName();
verifyZeroInteractions(playerCache);
}
}