Added assertions to tests per SonarCloud

This commit is contained in:
tastybento 2019-06-08 19:14:25 -07:00
parent 272e99bc12
commit fc2de2501c
12 changed files with 177 additions and 135 deletions

View File

@ -296,14 +296,14 @@ public class Flag implements Comparable<Flag> {
} }
Island island = plugin.getIslands().getIslandAt(user.getLocation()).orElse(plugin.getIslands().getIsland(user.getWorld(), user.getUniqueId())); Island island = plugin.getIslands().getIslandAt(user.getLocation()).orElse(plugin.getIslands().getIsland(user.getWorld(), user.getUniqueId()));
switch(getType()) { switch(getType()) {
case PROTECTION: case PROTECTION:
return createProtectionFlag(plugin, user, island, pib).build(); return createProtectionFlag(plugin, user, island, pib).build();
case SETTING: case SETTING:
return createSettingFlag(user, island, pib).build(); return createSettingFlag(user, island, pib).build();
case WORLD_SETTING: case WORLD_SETTING:
return createWorldSettingFlag(user, pib).build(); return createWorldSettingFlag(user, pib).build();
default: default:
return pib.build(); return pib.build();
} }
} }
@ -483,18 +483,18 @@ public class Flag implements Comparable<Flag> {
// If no clickHandler has been set, then apply default ones // If no clickHandler has been set, then apply default ones
if (clickHandler == null) { if (clickHandler == null) {
switch (type){ switch (type){
case PROTECTION: case PROTECTION:
clickHandler = new CycleClick(id); clickHandler = new CycleClick(id);
break; break;
case SETTING: case SETTING:
clickHandler = new IslandToggleClick(id); clickHandler = new IslandToggleClick(id);
break; break;
case WORLD_SETTING: case WORLD_SETTING:
clickHandler = new WorldToggleClick(id); clickHandler = new WorldToggleClick(id);
break; break;
default: default:
clickHandler = new CycleClick(id); clickHandler = new CycleClick(id);
break; break;
} }
} }

View File

@ -1,9 +1,13 @@
package world.bentobox.bentobox.api.commands.admin; package world.bentobox.bentobox.api.commands.admin;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -90,15 +94,15 @@ public class AdminTeleportCommandTest {
// Player has island to begin with // Player has island to begin with
im = mock(IslandsManager.class); im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true); when(im.hasIsland(any(), any(User.class))).thenReturn(true);
when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true); when(im.isOwner(any(),any())).thenReturn(true);
when(im.getOwner(Mockito.any(),Mockito.any())).thenReturn(uuid); when(im.getOwner(any(),any())).thenReturn(uuid);
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
// Has team // Has team
pm = mock(PlayersManager.class); pm = mock(PlayersManager.class);
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); when(im.inTeam(any(), eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm); when(plugin.getPlayers()).thenReturn(pm);
@ -109,7 +113,7 @@ public class AdminTeleportCommandTest {
// Locales // Locales
LocalesManager lm = mock(LocalesManager.class); LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); when(lm.get(any(), any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
when(user.getTranslation(Mockito.anyString(),Mockito.anyString(), Mockito.anyString())).thenAnswer(new Answer<String>() { when(user.getTranslation(Mockito.anyString(),Mockito.anyString(), Mockito.anyString())).thenAnswer(new Answer<String>() {
@ -122,12 +126,12 @@ public class AdminTeleportCommandTest {
// Island location // Island location
Location location = mock(Location.class); Location location = mock(Location.class);
Vector vector = mock(Vector.class); Vector vector = mock(Vector.class);
when(vector.toLocation(Mockito.any())).thenReturn(location); when(vector.toLocation(any())).thenReturn(location);
when(location.toVector()).thenReturn(vector); when(location.toVector()).thenReturn(vector);
when(im.getIslandLocation(Mockito.any(), Mockito.any())).thenReturn(location); when(im.getIslandLocation(any(), any())).thenReturn(location);
// We do no actually want to teleport in this test, so return no island // We do no actually want to teleport in this test, so return no island
Optional<Island> nothing = Optional.empty(); Optional<Island> nothing = Optional.empty();
when(im.getIslandAt(Mockito.any())).thenReturn(nothing ); when(im.getIslandAt(any())).thenReturn(nothing );
} }
@ -136,9 +140,12 @@ public class AdminTeleportCommandTest {
*/ */
@Test @Test
public void testExecuteUserStringListOfString() { public void testExecuteUserStringListOfString() {
new AdminTeleportCommand(ac,"tp"); AdminTeleportCommand c = new AdminTeleportCommand(ac,"tp");
new AdminTeleportCommand(ac,"tpnether"); assertEquals("tp",c.getLabel());
new AdminTeleportCommand(ac,"tpend"); c = new AdminTeleportCommand(ac,"tpnether");
assertEquals("tpnether",c.getLabel());
c = new AdminTeleportCommand(ac,"tpend");
assertEquals("tpend",c.getLabel());
} }
/** /**
@ -148,66 +155,66 @@ public class AdminTeleportCommandTest {
public void testExecuteUserStringListOfStringEmptyArgs() { public void testExecuteUserStringListOfStringEmptyArgs() {
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertFalse(atc.execute(user, "tp", new ArrayList<>())); assertFalse(atc.execute(user, "tp", new ArrayList<>()));
Mockito.verify(user).sendMessage(Mockito.anyString()); verify(user).sendMessage(Mockito.anyString());
} }
@Test @Test
public void testExecuteUserStringListOfStringUnknownTarget() { public void testExecuteUserStringListOfStringUnknownTarget() {
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertFalse(atc.execute(user, "tp", Collections.singletonList("tastybento"))); assertFalse(atc.execute(user, "tp", Collections.singletonList("tastybento")));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.unknown-player"), Mockito.eq(TextVariables.NAME), Mockito.eq("tastybento")); verify(user).sendMessage(eq("general.errors.unknown-player"), eq(TextVariables.NAME), eq("tastybento"));
} }
@Test @Test
public void testExecuteUserStringListOfStringKnownTargetNoIsland() { public void testExecuteUserStringListOfStringKnownTargetNoIsland() {
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertFalse(atc.execute(user, "tp", Collections.singletonList("tastybento"))); assertFalse(atc.execute(user, "tp", Collections.singletonList("tastybento")));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.player-has-no-island")); verify(user).sendMessage(eq("general.errors.player-has-no-island"));
} }
@Test @Test
public void testExecuteUserStringListOfStringKnownTargetHasIsland() { public void testExecuteUserStringListOfStringKnownTargetHasIsland() {
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento")));
Mockito.verify(user).getTranslation(Mockito.eq("commands.admin.tp.manual"), Mockito.eq("[location]"), Mockito.eq("0 0 0")); verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0"));
} }
@Test @Test
public void testExecuteUserStringListOfStringKnownTargetIsTeamMember() { public void testExecuteUserStringListOfStringKnownTargetIsTeamMember() {
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.inTeam(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento")));
Mockito.verify(iwm, Mockito.never()).getNetherWorld(Mockito.any()); verify(iwm, Mockito.never()).getNetherWorld(any());
Mockito.verify(iwm, Mockito.never()).getEndWorld(Mockito.any()); verify(iwm, Mockito.never()).getEndWorld(any());
Mockito.verify(user).getTranslation(Mockito.eq("commands.admin.tp.manual"), Mockito.eq("[location]"), Mockito.eq("0 0 0")); verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0"));
} }
@Test @Test
public void testExecuteUserStringListOfStringKnownTargetHasIslandNether() { public void testExecuteUserStringListOfStringKnownTargetHasIslandNether() {
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpnether"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpnether");
assertTrue(atc.execute(user, "tpnether", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tpnether", Collections.singletonList("tastybento")));
Mockito.verify(iwm).getNetherWorld(Mockito.any()); verify(iwm).getNetherWorld(any());
Mockito.verify(iwm, Mockito.never()).getEndWorld(Mockito.any()); verify(iwm, Mockito.never()).getEndWorld(any());
Mockito.verify(user).getTranslation(Mockito.eq("commands.admin.tp.manual"), Mockito.eq("[location]"), Mockito.eq("0 0 0")); verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0"));
} }
@Test @Test
public void testExecuteUserStringListOfStringKnownTargetHasIslandEnd() { public void testExecuteUserStringListOfStringKnownTargetHasIslandEnd() {
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend");
assertTrue(atc.execute(user, "tpend", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tpend", Collections.singletonList("tastybento")));
Mockito.verify(iwm, Mockito.never()).getNetherWorld(Mockito.any()); verify(iwm, Mockito.never()).getNetherWorld(any());
Mockito.verify(iwm).getEndWorld(Mockito.any()); verify(iwm).getEndWorld(any());
Mockito.verify(user).getTranslation(Mockito.eq("commands.admin.tp.manual"), Mockito.eq("[location]"), Mockito.eq("0 0 0")); verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0"));
} }

View File

@ -227,7 +227,8 @@ public class IslandTeamInviteCommandTest {
when(s.getInviteCooldown()).thenReturn(10); when(s.getInviteCooldown()).thenReturn(10);
IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic); IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic);
String[] name = {"tastybento"}; String[] name = {"tastybento"};
itl.execute(user, itl.getLabel(), Arrays.asList(name)); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
} }
} }

View File

@ -212,7 +212,7 @@ public class IslandTeamTrustCommandTest {
when(s.getInviteCooldown()).thenReturn(10); when(s.getInviteCooldown()).thenReturn(10);
IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic);
String[] name = {"tastybento"}; String[] name = {"tastybento"};
itl.execute(user, itl.getLabel(), Arrays.asList(name)); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
} }

View File

@ -221,7 +221,7 @@ public class IslandTeamUncoopCommandTest {
when(s.getInviteCooldown()).thenReturn(10); when(s.getInviteCooldown()).thenReturn(10);
IslandTeamUncoopCommand itl = new IslandTeamUncoopCommand(ic); IslandTeamUncoopCommand itl = new IslandTeamUncoopCommand(ic);
String[] name = {"tastybento"}; String[] name = {"tastybento"};
itl.execute(user, itl.getLabel(), Arrays.asList(name)); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
} }
@Test @Test
@ -255,8 +255,8 @@ public class IslandTeamUncoopCommandTest {
public void testTabComplete() { public void testTabComplete() {
Builder<UUID> memberSet = new ImmutableSet.Builder<>(); Builder<UUID> memberSet = new ImmutableSet.Builder<>();
for (int j = 0; j < 11; j++) { for (int j = 0; j < 11; j++) {
memberSet.add(UUID.randomUUID()); memberSet.add(UUID.randomUUID());
} }
when(island.getMemberSet()).thenReturn(memberSet.build()); when(island.getMemberSet()).thenReturn(memberSet.build());

View File

@ -221,7 +221,7 @@ public class IslandTeamUntrustCommandTest {
when(s.getInviteCooldown()).thenReturn(10); when(s.getInviteCooldown()).thenReturn(10);
IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic);
String[] name = {"tastybento"}; String[] name = {"tastybento"};
itl.execute(user, itl.getLabel(), Arrays.asList(name)); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
} }
@Test @Test
@ -255,8 +255,8 @@ public class IslandTeamUntrustCommandTest {
public void testTabComplete() { public void testTabComplete() {
Builder<UUID> memberSet = new ImmutableSet.Builder<>(); Builder<UUID> memberSet = new ImmutableSet.Builder<>();
for (int j = 0; j < 11; j++) { for (int j = 0; j < 11; j++) {
memberSet.add(UUID.randomUUID()); memberSet.add(UUID.randomUUID());
} }
when(island.getMemberSet()).thenReturn(memberSet.build()); when(island.getMemberSet()).thenReturn(memberSet.build());

View File

@ -74,7 +74,8 @@ public class FlagTest {
Whitebox.setInternalState(BentoBox.class, "instance", plugin); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
PowerMockito.mockStatic(Util.class); PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class)); // Return world
when(Util.getWorld(Mockito.any())).thenAnswer((Answer<World>) invocation -> invocation.getArgumentAt(0, World.class));
// World Settings // World Settings
IslandWorldManager iwm = mock(IslandWorldManager.class); IslandWorldManager iwm = mock(IslandWorldManager.class);
@ -193,6 +194,9 @@ public class FlagTest {
@Test @Test
public void testSetDefaultSettingBoolean() { public void testSetDefaultSettingBoolean() {
f.setDefaultSetting(true); f.setDefaultSetting(true);
assertTrue(f.isSetForWorld(world));
f.setDefaultSetting(false);
assertFalse(f.isSetForWorld(world));
} }
/** /**
@ -201,6 +205,9 @@ public class FlagTest {
@Test @Test
public void testSetDefaultSettingWorldBoolean() { public void testSetDefaultSettingWorldBoolean() {
f.setDefaultSetting(world, true); f.setDefaultSetting(world, true);
assertTrue(f.isSetForWorld(world));
f.setDefaultSetting(world, false);
assertFalse(f.isSetForWorld(world));
} }
/** /**

View File

@ -63,13 +63,6 @@ public class BentoBoxLocaleTest {
localeObject = new BentoBoxLocale(locale, config); localeObject = new BentoBoxLocale(locale, config);
} }
/**
* Test method for {@link world.bentobox.bentobox.api.localization.BentoBoxLocale#BentoBoxLocale(java.util.Locale, org.bukkit.configuration.file.YamlConfiguration)}.
*/
@Test
public void testBentoBoxLocale() {
}
/** /**
* Test method for {@link world.bentobox.bentobox.api.localization.BentoBoxLocale#get(java.lang.String)}. * Test method for {@link world.bentobox.bentobox.api.localization.BentoBoxLocale#get(java.lang.String)}.
*/ */

View File

@ -19,6 +19,7 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.PanelListener; import world.bentobox.bentobox.api.panels.PanelListener;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
@ -28,7 +29,7 @@ import world.bentobox.bentobox.api.user.User;
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class }) @PrepareForTest({Bukkit.class})
public class PanelBuilderTest { public class PanelBuilderTest {
/** /**
@ -63,7 +64,8 @@ public class PanelBuilderTest {
pb = pb.item(pi); pb = pb.item(pi);
pb = pb.item(pi); pb = pb.item(pi);
pb = pb.item(pi); pb = pb.item(pi);
assertTrue(pb.slotOccupied(3));
assertFalse(pb.slotOccupied(4));
} }
/** /**
@ -78,6 +80,11 @@ public class PanelBuilderTest {
pb = pb.item(1, pi); pb = pb.item(1, pi);
pb = pb.item(10, pi); pb = pb.item(10, pi);
pb = pb.item(20, pi); pb = pb.item(20, pi);
assertTrue(pb.slotOccupied(0));
assertTrue(pb.slotOccupied(1));
assertTrue(pb.slotOccupied(10));
assertTrue(pb.slotOccupied(20));
assertFalse(pb.slotOccupied(4));
} }
/** /**
@ -86,7 +93,7 @@ public class PanelBuilderTest {
@Test @Test
public void testSize() { public void testSize() {
PanelBuilder pb = new PanelBuilder(); PanelBuilder pb = new PanelBuilder();
pb = pb.size(45); assertEquals(pb, pb.size(45));
} }
/** /**
@ -99,7 +106,7 @@ public class PanelBuilderTest {
pb = pb.user(user); pb = pb.user(user);
// Change user // Change user
User user2 = mock(User.class); User user2 = mock(User.class);
pb = pb.user(user2); assertEquals(pb, pb.user(user2));
} }
/** /**
@ -109,7 +116,7 @@ public class PanelBuilderTest {
public void testListener() { public void testListener() {
PanelBuilder pb = new PanelBuilder(); PanelBuilder pb = new PanelBuilder();
PanelListener listener = mock(PanelListener.class); PanelListener listener = mock(PanelListener.class);
pb = pb.listener(listener); assertEquals(pb, pb.listener(listener));
} }
/** /**
@ -158,7 +165,9 @@ public class PanelBuilderTest {
@Test @Test
public void testBuild() { public void testBuild() {
PanelBuilder pb = new PanelBuilder(); PanelBuilder pb = new PanelBuilder();
pb.build(); pb.name("test");
} Panel p = pb.build();
assertEquals("test", p.getName());
}
} }

View File

@ -8,6 +8,8 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.verify;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
@ -59,6 +61,8 @@ public class UserTest {
private LocalesManager lm; private LocalesManager lm;
private User user; private User user;
private IslandWorldManager iwm; private IslandWorldManager iwm;
private Server server;
private UUID uuid;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -67,13 +71,15 @@ public class UserTest {
Whitebox.setInternalState(BentoBox.class, "instance", plugin); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
User.setPlugin(plugin); User.setPlugin(plugin);
Server server = mock(Server.class); server = mock(Server.class);
World world = mock(World.class); World world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger()); when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world); when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking"); when(server.getVersion()).thenReturn("BSB_Mocking");
player = mock(Player.class); player = mock(Player.class);
uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid);
when(server.getPlayer(Mockito.any(UUID.class))).thenReturn(player); when(server.getPlayer(Mockito.any(UUID.class))).thenReturn(player);
PluginManager pluginManager = mock(PluginManager.class); PluginManager pluginManager = mock(PluginManager.class);
@ -141,13 +147,21 @@ public class UserTest {
@Test @Test
public void testRemovePlayer() { public void testRemovePlayer() {
assertNotNull(User.getInstance(uuid));
assertEquals(user, User.getInstance(uuid));
User.removePlayer(player); User.removePlayer(player);
// If the player has been removed from the cache, then code will ask server for player
// Return null and check if instance is null will show that the player is not in the cache
when(Bukkit.getPlayer(Mockito.any(UUID.class))).thenReturn(null);
assertNull(User.getInstance(uuid).getPlayer());
} }
@Test @Test
public void testSetPlugin() { public void testSetPlugin() {
BentoBox plugin = mock(BentoBox.class); BentoBox plugin = mock(BentoBox.class);
User.setPlugin(plugin); User.setPlugin(plugin);
user.addPerm("testing123");
verify(player).addAttachment(eq(plugin), eq("testing123"), eq(true));
} }
@Test @Test
@ -278,7 +292,7 @@ public class UserTest {
@Test @Test
public void testSendMessage() { public void testSendMessage() {
user.sendMessage("a.reference"); user.sendMessage("a.reference");
Mockito.verify(player).sendMessage(Mockito.eq(TEST_TRANSLATION)); verify(player).sendMessage(eq(TEST_TRANSLATION));
} }
@Test @Test
@ -290,10 +304,10 @@ public class UserTest {
user.setAddon(addon); user.setAddon(addon);
Optional<GameModeAddon> optionalAddon = Optional.of(addon); Optional<GameModeAddon> optionalAddon = Optional.of(addon);
when(iwm .getAddon(any())).thenReturn(optionalAddon); when(iwm .getAddon(any())).thenReturn(optionalAddon);
when(lm.get(any(), Mockito.eq("name.a.reference"))).thenReturn("mockmockmock"); when(lm.get(any(), eq("name.a.reference"))).thenReturn("mockmockmock");
user.sendMessage("a.reference"); user.sendMessage("a.reference");
Mockito.verify(player, Mockito.never()).sendMessage(Mockito.eq(TEST_TRANSLATION)); verify(player, Mockito.never()).sendMessage(eq(TEST_TRANSLATION));
Mockito.verify(player).sendMessage(Mockito.eq("mockmockmock")); verify(player).sendMessage(eq("mockmockmock"));
} }
@Test @Test
@ -301,7 +315,7 @@ public class UserTest {
// Nothing - blank translation // Nothing - blank translation
when(lm.get(any(), any())).thenReturn(""); when(lm.get(any(), any())).thenReturn("");
user.sendMessage("a.reference"); user.sendMessage("a.reference");
Mockito.verify(player, Mockito.never()).sendMessage(Mockito.anyString()); verify(player, Mockito.never()).sendMessage(Mockito.anyString());
} }
@Test @Test
@ -313,14 +327,14 @@ public class UserTest {
} }
when(lm.get(any(), any())).thenReturn(allColors.toString()); when(lm.get(any(), any())).thenReturn(allColors.toString());
user.sendMessage("a.reference"); user.sendMessage("a.reference");
Mockito.verify(player, Mockito.never()).sendMessage(Mockito.anyString()); verify(player, Mockito.never()).sendMessage(Mockito.anyString());
} }
@Test @Test
public void testSendRawMessage() { public void testSendRawMessage() {
String raw = ChatColor.RED + "" + ChatColor.BOLD + "test message"; String raw = ChatColor.RED + "" + ChatColor.BOLD + "test message";
user.sendRawMessage(raw); user.sendRawMessage(raw);
Mockito.verify(player).sendMessage(raw); verify(player).sendMessage(raw);
} }
@Test @Test
@ -328,7 +342,7 @@ public class UserTest {
String raw = ChatColor.RED + "" + ChatColor.BOLD + "test message"; String raw = ChatColor.RED + "" + ChatColor.BOLD + "test message";
user = User.getInstance((CommandSender)null); user = User.getInstance((CommandSender)null);
user.sendRawMessage(raw); user.sendRawMessage(raw);
Mockito.verify(player, Mockito.never()).sendMessage(Mockito.anyString()); verify(player, Mockito.never()).sendMessage(Mockito.anyString());
} }
@Test @Test
@ -340,10 +354,10 @@ public class UserTest {
when(lm.get(any(), any())).thenReturn(translation); when(lm.get(any(), any())).thenReturn(translation);
// Set notify // Set notify
when(notifier.notify(any(), Mockito.eq(translation))).thenReturn(true); when(notifier.notify(any(), eq(translation))).thenReturn(true);
user.notify("a.reference"); user.notify("a.reference");
Mockito.verify(notifier).notify(user, translation); verify(notifier).notify(user, translation);
} }
@ -352,7 +366,7 @@ public class UserTest {
for (GameMode gm: GameMode.values()) { for (GameMode gm: GameMode.values()) {
user.setGameMode(gm); user.setGameMode(gm);
} }
Mockito.verify(player, Mockito.times(GameMode.values().length)).setGameMode(Mockito.any()); verify(player, Mockito.times(GameMode.values().length)).setGameMode(Mockito.any());
} }
@Test @Test
@ -360,7 +374,7 @@ public class UserTest {
when(player.teleport(Mockito.any(Location.class))).thenReturn(true); when(player.teleport(Mockito.any(Location.class))).thenReturn(true);
Location loc = mock(Location.class); Location loc = mock(Location.class);
user.teleport(loc); user.teleport(loc);
Mockito.verify(player).teleport(loc); verify(player).teleport(loc);
} }
@Test @Test
@ -374,7 +388,7 @@ public class UserTest {
@Test @Test
public void testCloseInventory() { public void testCloseInventory() {
user.closeInventory(); user.closeInventory();
Mockito.verify(player).closeInventory(); verify(player).closeInventory();
} }
@Test @Test
@ -406,13 +420,13 @@ public class UserTest {
@Test @Test
public void testUpdateInventory() { public void testUpdateInventory() {
user.updateInventory(); user.updateInventory();
Mockito.verify(player).updateInventory(); verify(player).updateInventory();
} }
@Test @Test
public void testPerformCommand() { public void testPerformCommand() {
user.performCommand("test"); user.performCommand("test");
Mockito.verify(player).performCommand("test"); verify(player).performCommand("test");
} }
@SuppressWarnings("unlikely-arg-type") @SuppressWarnings("unlikely-arg-type")

View File

@ -65,11 +65,6 @@ public class FlagAdapterTest {
when(plugin.getFlagsManager()).thenReturn(flagsManager); when(plugin.getFlagsManager()).thenReturn(flagsManager);
} }
@Test
public void testFlagAdapter() {
new FlagTypeAdapter(plugin);
}
@Test @Test
public void testWriteJsonWriterFlag() throws IOException { public void testWriteJsonWriterFlag() throws IOException {
FlagTypeAdapter fa = new FlagTypeAdapter(plugin); FlagTypeAdapter fa = new FlagTypeAdapter(plugin);

View File

@ -1,6 +1,9 @@
package world.bentobox.bentobox.database.mysql; package world.bentobox.bentobox.database.mysql;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.sql.Connection; import java.sql.Connection;
@ -44,11 +47,13 @@ import world.bentobox.bentobox.util.Util;
@PrepareForTest( { Bukkit.class, BentoBox.class, Util.class }) @PrepareForTest( { Bukkit.class, BentoBox.class, Util.class })
public class MySQLDatabaseHandlerTest { public class MySQLDatabaseHandlerTest {
private static MySQLDatabaseHandler<Island> handler; private MySQLDatabaseHandler<Island> handler;
private static Island instance; private Island instance;
private static String UNIQUE_ID = "xyz"; private String UNIQUE_ID = "xyz";
private static MySQLDatabaseConnector dbConn; private MySQLDatabaseConnector dbConn;
private static World world; private World world;
@Mock
private Location location;
@Mock @Mock
static BentoBox plugin = mock(BentoBox.class); static BentoBox plugin = mock(BentoBox.class);
private static IslandWorldManager iwm; private static IslandWorldManager iwm;
@ -78,7 +83,7 @@ public class MySQLDatabaseHandlerTest {
when(plugin.getSettings()).thenReturn(settings); when(plugin.getSettings()).thenReturn(settings);
iwm = mock(IslandWorldManager.class); iwm = mock(IslandWorldManager.class);
when(iwm.getDeathsMax(Mockito.any())).thenReturn(10); when(iwm.getDeathsMax(any())).thenReturn(10);
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger()); when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
@ -97,34 +102,41 @@ public class MySQLDatabaseHandlerTest {
handler = new MySQLDatabaseHandler<>(plugin, Island.class, dbConn); handler = new MySQLDatabaseHandler<>(plugin, Island.class, dbConn);
PowerMockito.mockStatic(Util.class); PowerMockito.mockStatic(Util.class);
when(Util.sameWorld(Mockito.any(), Mockito.any())).thenReturn(true); when(Util.sameWorld(any(), any())).thenReturn(true);
// Flags // Flags
FlagsManager fm = mock(FlagsManager.class); FlagsManager fm = mock(FlagsManager.class);
when(plugin.getFlagsManager()).thenReturn(fm); when(plugin.getFlagsManager()).thenReturn(fm);
when(fm.getFlags()).thenReturn(new ArrayList<>()); when(fm.getFlags()).thenReturn(new ArrayList<>());
// Location
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
} }
@Test @Test
public void testSaveNullObject() { public void testSaveNullObject() {
handler.saveObject(null); handler.saveObject(null);
verify(plugin).logError(eq("MySQL database request to store a null. "));
} }
@Test @Test
public void testSaveObject() { public void testSaveObject() {
handler.saveObject(instance); handler.saveObject(instance);
}
@Test
public void testSaveObject2() {
BentoBox plugin = mock(BentoBox.class); BentoBox plugin = mock(BentoBox.class);
Settings settings = mock(Settings.class); Settings settings = mock(Settings.class);
when(plugin.getSettings()).thenReturn(settings); when(plugin.getSettings()).thenReturn(settings);
when(iwm.getDeathsMax(Mockito.any())).thenReturn(10); when(iwm.getDeathsMax(any())).thenReturn(10);
Players players = new Players(); Players players = new Players();
players.setUniqueId(UUID.randomUUID().toString()); players.setUniqueId(UUID.randomUUID().toString());
players.setDeaths(world, 23); players.setDeaths(world, 23);
Location location = mock(Location.class);
Mockito.when(location.getWorld()).thenReturn(world);
Mockito.when(location.getBlockX()).thenReturn(0);
Mockito.when(location.getBlockY()).thenReturn(0);
Mockito.when(location.getBlockZ()).thenReturn(0);
players.setHomeLocation(location); players.setHomeLocation(location);
players.setHomeLocation(location, 1); players.setHomeLocation(location, 1);
@ -140,6 +152,10 @@ public class MySQLDatabaseHandlerTest {
MySQLDatabaseHandler<Players> h = new MySQLDatabaseHandler<>(plugin, Players.class, dbConn); MySQLDatabaseHandler<Players> h = new MySQLDatabaseHandler<>(plugin, Players.class, dbConn);
h.saveObject(players); h.saveObject(players);
}
@Test
public void testSaveObject3() {
Island island = new Island(); Island island = new Island();
island.setUniqueId(UNIQUE_ID); island.setUniqueId(UNIQUE_ID);
island.setCenter(location); island.setCenter(location);