Minor - fix incorrect javadoc and add unit test for Messages#reload

This commit is contained in:
ljacqu 2016-02-12 23:38:00 +01:00
parent 57da572b23
commit fcfe26f34d
5 changed files with 54 additions and 39 deletions

View File

@ -16,7 +16,7 @@ public class Messages {
private FileConfiguration configuration; private FileConfiguration configuration;
private String fileName; private String fileName;
private File defaultFile; private final File defaultFile;
private FileConfiguration defaultConfiguration; private FileConfiguration defaultConfiguration;
/** /**
@ -91,7 +91,6 @@ public class Messages {
* Retrieve the message from the text file. * Retrieve the message from the text file.
* *
* @param key The message key to retrieve * @param key The message key to retrieve
*
* @return The message from the file * @return The message from the file
*/ */
public String retrieveSingle(MessageKey key) { public String retrieveSingle(MessageKey key) {
@ -100,6 +99,8 @@ public class Messages {
/** /**
* Reload the messages manager. * Reload the messages manager.
*
* @param messagesFile The new file to load messages from
*/ */
public void reload(File messagesFile) { public void reload(File messagesFile) {
initializeFile(messagesFile); initializeFile(messagesFile);
@ -119,9 +120,7 @@ public class Messages {
defaultConfiguration = YamlConfiguration.loadConfiguration(defaultFile); defaultConfiguration = YamlConfiguration.loadConfiguration(defaultFile);
} }
String message = defaultConfiguration.getString(code); String message = defaultConfiguration.getString(code);
return (message == null) return message == null ? getDefaultErrorMessage(code) : message;
? "Error retrieving message '" + code + "'"
: message;
} }
private static String getDefaultErrorMessage(String code) { private static String getDefaultErrorMessage(String code) {

View File

@ -80,18 +80,17 @@ public class AsynchronousJoin {
if (Settings.getMaxJoinPerIp > 0 if (Settings.getMaxJoinPerIp > 0
&& !plugin.getPermissionsManager().hasPermission(player, PlayerPermission.ALLOW_MULTIPLE_ACCOUNTS) && !plugin.getPermissionsManager().hasPermission(player, PlayerPermission.ALLOW_MULTIPLE_ACCOUNTS)
&& !ip.equalsIgnoreCase("127.0.0.1") && !ip.equalsIgnoreCase("127.0.0.1")
&& !ip.equalsIgnoreCase("localhost")) { && !ip.equalsIgnoreCase("localhost")
if (plugin.hasJoinedIp(player.getName(), ip)) { && plugin.hasJoinedIp(player.getName(), ip)) {
sched.scheduleSyncDelayedTask(plugin, new Runnable() { sched.scheduleSyncDelayedTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
player.kickPlayer("A player with the same IP is already in game!"); player.kickPlayer("A player with the same IP is already in game!");
} }
}); });
return; return;
}
} }
final Location spawnLoc = plugin.getSpawnLocation(player); final Location spawnLoc = plugin.getSpawnLocation(player);
final boolean isAuthAvailable = database.isAuthAvailable(name); final boolean isAuthAvailable = database.isAuthAvailable(name);
@ -104,12 +103,9 @@ public class AsynchronousJoin {
public void run() { public void run() {
SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawnLoc, PlayerCache.getInstance().isAuthenticated(name)); SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawnLoc, PlayerCache.getInstance().isAuthenticated(name));
plugin.getServer().getPluginManager().callEvent(tpEvent); plugin.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) { if (!tpEvent.isCancelled() && player.isOnline() && tpEvent.getTo() != null
if (player.isOnline() && tpEvent.getTo() != null) { && tpEvent.getTo().getWorld() != null) {
if (tpEvent.getTo().getWorld() != null) { player.teleport(tpEvent.getTo());
player.teleport(tpEvent.getTo());
}
}
} }
} }
@ -155,25 +151,21 @@ public class AsynchronousJoin {
return; return;
} }
if (!Settings.noTeleport) { if (!Settings.noTeleport && !needFirstSpawn() && Settings.isTeleportToSpawnEnabled
if (!needFirstSpawn() && Settings.isTeleportToSpawnEnabled || (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName()))) { || (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName()))) {
sched.scheduleSyncDelayedTask(plugin, new Runnable() { sched.scheduleSyncDelayedTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawnLoc, PlayerCache.getInstance().isAuthenticated(name)); SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawnLoc, PlayerCache.getInstance().isAuthenticated(name));
plugin.getServer().getPluginManager().callEvent(tpEvent); plugin.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) { if (!tpEvent.isCancelled() && player.isOnline() && tpEvent.getTo() != null
if (player.isOnline() && tpEvent.getTo() != null) { && tpEvent.getTo().getWorld() != null) {
if (tpEvent.getTo().getWorld() != null) { player.teleport(tpEvent.getTo());
player.teleport(tpEvent.getTo()); }
} }
}
}
}
}); });
}
} }
} }

View File

@ -12,7 +12,7 @@ public final class TestHelper {
} }
/** /**
* Return a {@link File} to an existing file from the main (non-test) resources folder. * Return a {@link File} to a file in the JAR's resources (main or test).
* *
* @param path The absolute path to the file * @param path The absolute path to the file
* @return The project file * @return The project file

View File

@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
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.junit.Assume.assumeThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -249,4 +250,21 @@ public class MessagesIntegrationTest {
// then // then
assertThat(message, containsString("Error retrieving message")); assertThat(message, containsString("Error retrieving message"));
} }
@Test
public void shouldLoadOtherFile() {
// given
MessageKey key = MessageKey.WRONG_PASSWORD;
// assumption: message comes back as defined in messages_test.yml
assumeThat(messages.retrieveSingle(key), equalTo("§cWrong password!"));
// when
messages.reload(TestHelper.getJarFile("/messages_test2.yml"));
// then
assertThat(messages.retrieveSingle(key), equalTo("test2 - wrong password"));
// check that default message handling still works
assertThat(messages.retrieveSingle(MessageKey.MUST_REGISTER_MESSAGE),
equalTo("Message from default file"));
}
} }

View File

@ -0,0 +1,6 @@
# Sample messages file
unknown_user: 'Message from test2'
unsafe_spawn: 'test2 - unsafe spawn'
not_logged_in: 'test2 - not logged in'
wrong_pwd: 'test2 - wrong password'