Implemented User class JUnit tests.

Fixed some bugs in User, Notifier.
This commit is contained in:
tastybento 2018-04-27 09:20:51 +09:00
parent 135f35131c
commit ca5fdbd71d
5 changed files with 468 additions and 8 deletions

View File

@ -21,6 +21,7 @@ public class BSBLocale {
private YamlConfiguration config;
private ItemStack banner;
@SuppressWarnings("deprecation")
public BSBLocale(Locale locale, File file) {
this.locale = locale;
config = YamlConfiguration.loadConfiguration(file);

View File

@ -30,6 +30,12 @@ public class Notifier {
}
);
/**
* Sends message to a user only if the message hasn't been sent recently
* @param user - user
* @param message - message to send (already translated)
* @return true if message sent successfully, false it it has been throttled
*/
public synchronized boolean notify(User user, String message) {
try {
Notification lastNotification = notificationCache.get(user);
@ -37,8 +43,9 @@ public class Notifier {
if (now >= lastNotification.getTime() + NOTIFICATION_DELAY * 1000 || !message.equals(lastNotification.getMessage())) {
notificationCache.put(user, new Notification(message, now));
user.sendRawMessage(message);
}
return true;
}
return false;
} catch (ExecutionException e) {
return false;
}

View File

@ -27,6 +27,14 @@ import us.tastybento.bskyblock.api.placeholders.PlaceholderHandler;
public class User {
private static Map<UUID, User> users = new HashMap<>();
/**
* Clears all users from the user list
*/
public static void clearUsers() {
users.clear();
}
/**
* Get an instance of User from a CommandSender
* @param sender
@ -131,6 +139,9 @@ public class User {
return player;
}
/**
* @return true if this user is a player, false if not, e.g., console
*/
public boolean isPlayer() {
return player != null;
}
@ -237,8 +248,8 @@ public class User {
*/
public void notify(String reference, String... variables) {
String message = getTranslation(reference, variables);
if (!ChatColor.stripColor(message).trim().isEmpty() && (sender != null || !plugin.getNotifier().notify(this, message))) {
sendRawMessage(message);
if (!ChatColor.stripColor(message).trim().isEmpty() && sender != null) {
plugin.getNotifier().notify(this, message);
}
}
@ -302,7 +313,6 @@ public class User {
*/
public boolean performCommand(String cmd) {
return player.performCommand(cmd);
}
/* (non-Javadoc)

View File

@ -0,0 +1,436 @@
package us.tastybento.bskyblock.api.user;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.PluginManager;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.modules.junit4.PowerMockRunner;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.managers.LocalesManager;
import us.tastybento.bskyblock.managers.PlayersManager;
@RunWith(PowerMockRunner.class)
public class UserTest {
private static World world;
private static Player player;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
Server server = mock(Server.class);
world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
player = mock(Player.class);
when(server.getPlayer(Mockito.any(UUID.class))).thenReturn(player);
PluginManager pluginManager = mock(PluginManager.class);
when(server.getPluginManager()).thenReturn(pluginManager);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
Bukkit.setServer(server);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
}
private CommandSender sender;
@Before
public void setUp() throws Exception {
sender = mock(CommandSender.class);
User.clearUsers();
User.setPlugin(null);
}
@Test
public void testGetInstanceCommandSender() {
User user = User.getInstance(sender);
assertNotNull(user);
assertEquals(sender,user.getSender());
}
@Test
public void testGetInstancePlayer() {
Player player = mock(Player.class);
User user = User.getInstance(player);
assertNotNull(user);
assertEquals(player,user.getPlayer());
}
@Test
public void testGetInstanceUUID() {
UUID uuid = UUID.randomUUID();
User user = User.getInstance(uuid);
assertNotNull(user);
assertEquals(uuid,user.getUniqueId());
}
@Test
public void testRemovePlayer() {
User.removePlayer(player);
}
@Test
public void testSetPlugin() {
BSkyBlock plugin = mock(BSkyBlock.class);
User.setPlugin(plugin);
}
@Test
public void testGetEffectivePermissions() {
Set<PermissionAttachmentInfo> value = new HashSet<>();
PermissionAttachmentInfo perm = new PermissionAttachmentInfo(sender, "perm", null, false);
value.add(perm);
when(sender.getEffectivePermissions()).thenReturn(value );
User user = User.getInstance(sender);
assertEquals(value, user.getEffectivePermissions());
}
@Test
public void testGetInventory() {
PlayerInventory value = mock(PlayerInventory.class);
when(player.getInventory()).thenReturn(value);
assertEquals(value, player.getInventory());
User user = User.getInstance(player);
assertNotNull(user.getInventory());
assertEquals(value, user.getInventory());
}
@Test
public void testGetLocation() {
Location loc = mock(Location.class);
when(player.getLocation()).thenReturn(loc);
User user = User.getInstance(player);
assertNotNull(user.getLocation());
assertEquals(loc, user.getLocation());
}
@Test
public void testGetName() {
String name = "tastybento";
when(player.getName()).thenReturn(name);
User user = User.getInstance(player);
assertNotNull(user.getName());
assertEquals(name, user.getName());
}
@Test
public void testGetPlayer() {
User user = User.getInstance(player);
assertEquals(player, user.getPlayer());
}
@Test
public void testIsPlayer() {
User user = User.getInstance(sender);
assertFalse(user.isPlayer());
user = User.getInstance(player);
assertTrue(user.isPlayer());
}
@Test
public void testGetSender() {
User user = User.getInstance(sender);
assertEquals(sender, user.getSender());
}
@Test
public void testGetUniqueId() {
UUID uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid);
User user = User.getInstance(player);
assertEquals(uuid, user.getUniqueId());
}
@Test
public void testHasPermission() {
when(player.hasPermission(Mockito.anyString())).thenReturn(true);
User user = User.getInstance(player);
assertTrue(user.hasPermission(""));
assertTrue(user.hasPermission("perm"));
}
@Test
public void testIsOnline() {
when(player.isOnline()).thenReturn(true);
User user = User.getInstance(player);
assertTrue(user.isOnline());
}
@Test
public void testIsOp() {
when(player.isOp()).thenReturn(true);
User user = User.getInstance(player);
assertTrue(user.isOp());
}
@Test
public void testGetTranslation() {
BSkyBlock plugin = mock(BSkyBlock.class);
User.setPlugin(plugin);
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
when(lm.get(any(), any())).thenReturn("mock translation [test]");
User user = User.getInstance(player);
assertEquals("mock translation [test]", user.getTranslation("a.reference"));
assertEquals("mock translation variable", user.getTranslation("a.reference", "[test]", "variable"));
}
@Test
public void testGetTranslationOrNothing() {
BSkyBlock plugin = mock(BSkyBlock.class);
User.setPlugin(plugin);
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
// Return the original string to pretend that a translation could not be found
when(lm.get(any(), any())).thenReturn("fake.reference");
User user = User.getInstance(player);
assertEquals("", user.getTranslationOrNothing("fake.reference"));
assertEquals("", user.getTranslationOrNothing("fake.reference", "[test]", "variable"));
}
@Test
public void testSendMessage() {
BSkyBlock plugin = mock(BSkyBlock.class);
User.setPlugin(plugin);
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
String translation = ChatColor.RED + "" + ChatColor.BOLD + "test translation";
when(lm.get(any(), any())).thenReturn(translation);
Player pl = mock(Player.class);
User user = User.getInstance(pl);
user.sendMessage("a.reference");
Mockito.verify(pl).sendMessage(translation);
}
@Test
public void testSendMessageBlankTranslation() {
BSkyBlock plugin = mock(BSkyBlock.class);
User.setPlugin(plugin);
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
// Nothing - blank translation
when(lm.get(any(), any())).thenReturn("");
when(plugin.getLocalesManager()).thenReturn(lm);
Player pl = mock(Player.class);
User user = User.getInstance(pl);
user.sendMessage("a.reference");
Mockito.verify(pl, Mockito.never()).sendMessage(Mockito.anyString());
}
@Test
public void testSendMessageOnlyColors() {
BSkyBlock plugin = mock(BSkyBlock.class);
User.setPlugin(plugin);
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Player pl = mock(Player.class);
User user = User.getInstance(pl);
// Nothing - just color codes
StringBuilder allColors = new StringBuilder();
for (ChatColor cc : ChatColor.values()) {
allColors.append(cc);
}
when(lm.get(any(), any())).thenReturn(allColors.toString());
user.sendMessage("a.reference");
Mockito.verify(pl, Mockito.never()).sendMessage(Mockito.anyString());
}
@Test
public void testSendRawMessage() {
BSkyBlock plugin = mock(BSkyBlock.class);
User.setPlugin(plugin);
String raw = ChatColor.RED + "" + ChatColor.BOLD + "test message";
Player pl = mock(Player.class);
User user = User.getInstance(pl);
user.sendRawMessage(raw);
Mockito.verify(pl).sendMessage(raw);
}
@Test
public void testNotifyStringStringArrayNotifyOK() {
BSkyBlock plugin = mock(BSkyBlock.class);
Notifier notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
User.setPlugin(plugin);
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
String translation = ChatColor.RED + "" + ChatColor.BOLD + "test translation";
when(lm.get(any(), any())).thenReturn(translation);
Player pl = mock(Player.class);
User user = User.getInstance(pl);
// Set notify
when(notifier.notify(Mockito.any(), Mockito.eq(translation))).thenReturn(true);
user.notify("a.reference");
Mockito.verify(notifier).notify(user, translation);
}
@Test
public void testSetGameMode() {
Player pl = mock(Player.class);
User user = User.getInstance(pl);
for (GameMode gm: GameMode.values()) {
user.setGameMode(gm);
}
Mockito.verify(pl, Mockito.times(GameMode.values().length)).setGameMode(Mockito.any());
}
@Test
public void testTeleport() {
Player pl = mock(Player.class);
User user = User.getInstance(pl);
when(pl.teleport(Mockito.any(Location.class))).thenReturn(true);
Location loc = mock(Location.class);
user.teleport(loc);
Mockito.verify(pl).teleport(loc);
}
@Test
public void testGetWorld() {
Player pl = mock(Player.class);
World world = mock(World.class);
when(pl.getWorld()).thenReturn(world);
User user = User.getInstance(pl);
assertEquals(world, user.getWorld());
}
@Test
public void testCloseInventory() {
Player pl = mock(Player.class);
User user = User.getInstance(pl);
user.closeInventory();
Mockito.verify(pl).closeInventory();
}
@Test
public void testGetLocalePlayer() {
BSkyBlock plugin = mock(BSkyBlock.class);
User.setPlugin(plugin);
PlayersManager pm = mock(PlayersManager.class);
when(plugin.getPlayers()).thenReturn(pm);
when(pm.getLocale(Mockito.any())).thenReturn("en-US");
// Confirm that Locale object is correctly obtained
Locale locale = Locale.US;
Player pl = mock(Player.class);
User user = User.getInstance(pl);
assertEquals(locale, user.getLocale());
}
@Test
public void testGetLocaleConsole() {
BSkyBlock plugin = mock(BSkyBlock.class);
User.setPlugin(plugin);
PlayersManager pm = mock(PlayersManager.class);
when(plugin.getPlayers()).thenReturn(pm);
when(pm.getLocale(Mockito.any())).thenReturn("en-US");
// Confirm that Locale object is correctly obtained
Locale locale = Locale.US;
// Try for console
User console = User.getInstance(mock(CommandSender.class));
Settings settings = mock(Settings.class);
when(plugin.getSettings()).thenReturn(settings);
when(settings.getDefaultLanguage()).thenReturn("en-US");
assertEquals(locale, console.getLocale());
}
@SuppressWarnings("deprecation")
@Test
public void testUpdateInventory() {
Player pl = mock(Player.class);
User user = User.getInstance(pl);
user.updateInventory();
Mockito.verify(pl).updateInventory();
}
@Test
public void testPerformCommand() {
Player pl = mock(Player.class);
User user = User.getInstance(pl);
user.performCommand("test");
Mockito.verify(pl).performCommand("test");
}
@Test
public void testEqualsObject() {
User user1 = User.getInstance(UUID.randomUUID());
User user2 = User.getInstance(UUID.randomUUID());
assertTrue(user1.equals(user1));
assertFalse(user1.equals(user2));
assertFalse(user2.equals(user1));
}
@Test
public void testhashCode() {
UUID uuid = UUID.randomUUID();
User user1 = User.getInstance(uuid);
User user2 = User.getInstance(uuid);
assertEquals(user1, user2);
assertTrue(user1.hashCode() == user2.hashCode());
}
}

View File

@ -41,6 +41,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.user.Notifier;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.generators.IslandWorld;
@ -382,6 +383,10 @@ public class FireListenerTest {
@Test
public void testOnTNTDamage() {
// Notifier
Notifier notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
// Island
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
@ -441,6 +446,7 @@ public class FireListenerTest {
// Fire arrow
when(arrow.getFireTicks()).thenReturn(10);
// Break blocks not allowed, general flag should have no effect
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
Flags.BREAK_BLOCKS.setDefaultSetting(false);