Fixed ChallengesCommandTest tests

This commit is contained in:
tastybento 2021-09-23 17:35:26 -07:00
parent c21cae91e1
commit 7b8cab3689
2 changed files with 39 additions and 30 deletions

View File

@ -16,9 +16,9 @@ public class ChallengesPlayerCommand extends CompositeCommand
public ChallengesPlayerCommand(ChallengesAddon addon, CompositeCommand cmd) public ChallengesPlayerCommand(ChallengesAddon addon, CompositeCommand cmd)
{ {
super(addon, super(addon,
cmd, cmd,
addon.getChallengesSettings().getPlayerMainCommand().split(" ")[0], addon.getChallengesSettings().getPlayerMainCommand().split(" ")[0],
addon.getChallengesSettings().getPlayerMainCommand().split(" ")); addon.getChallengesSettings().getPlayerMainCommand().split(" "));
} }
@ -29,7 +29,7 @@ public class ChallengesPlayerCommand extends CompositeCommand
public boolean canExecute(User user, String label, List<String> args) public boolean canExecute(User user, String label, List<String> args)
{ {
if (!this.getIWM().inWorld(user.getWorld()) || if (!this.getIWM().inWorld(user.getWorld()) ||
Util.getWorld(this.getWorld()) != Util.getWorld(user.getWorld())) { !Util.sameWorld(this.getWorld(), user.getWorld())) {
// Not a GameMode world. // Not a GameMode world.
Utils.sendMessage(user, user.getTranslation("general.errors.wrong-world")); Utils.sendMessage(user, user.getTranslation("general.errors.wrong-world"));
return false; return false;
@ -44,12 +44,12 @@ public class ChallengesPlayerCommand extends CompositeCommand
if (user.isOp() || user.hasPermission(this.getPermissionPrefix() + "admin.challenges")) if (user.isOp() || user.hasPermission(this.getPermissionPrefix() + "admin.challenges"))
{ {
String topLabel = this.getIWM().getAddon(this.getWorld()). String topLabel = this.getIWM().getAddon(this.getWorld()).
map(GameModeAddon::getAdminCommand). map(GameModeAddon::getAdminCommand).
map(optionalAdminCommand -> optionalAdminCommand.map(CompositeCommand::getTopLabel).orElse(this.getTopLabel())). map(optionalAdminCommand -> optionalAdminCommand.map(CompositeCommand::getTopLabel).orElse(this.getTopLabel())).
orElse(this.getTopLabel()); orElse(this.getTopLabel());
Utils.sendMessage(user, user.getTranslation("challenges.errors.no-challenges-admin", Utils.sendMessage(user, user.getTranslation("challenges.errors.no-challenges-admin",
"[command]", "[command]",
topLabel + " " + this.<ChallengesAddon>getAddon().getChallengesSettings().getAdminMainCommand().split(" ")[0])); topLabel + " " + this.<ChallengesAddon>getAddon().getChallengesSettings().getAdminMainCommand().split(" ")[0]));
} }
else else
@ -66,7 +66,7 @@ public class ChallengesPlayerCommand extends CompositeCommand
Utils.sendMessage(user, user.getTranslation("general.errors.no-island")); Utils.sendMessage(user, user.getTranslation("general.errors.no-island"));
return false; return false;
} else if (ChallengesAddon.CHALLENGES_WORLD_PROTECTION.isSetForWorld(this.getWorld()) && } else if (ChallengesAddon.CHALLENGES_WORLD_PROTECTION.isSetForWorld(this.getWorld()) &&
!this.getIslands().locationIsOnIsland(user.getPlayer(), user.getLocation())) !this.getIslands().locationIsOnIsland(user.getPlayer(), user.getLocation()))
{ {
// Do not open gui if player is not on the island, but challenges requires island for // Do not open gui if player is not on the island, but challenges requires island for
// completion. // completion.
@ -85,10 +85,10 @@ public class ChallengesPlayerCommand extends CompositeCommand
if (user.isPlayer()) if (user.isPlayer())
{ {
ChallengesPanel.open(this.getAddon(), ChallengesPanel.open(this.getAddon(),
this.getWorld(), this.getWorld(),
user, user,
this.getTopLabel(), this.getTopLabel(),
this.getPermissionPrefix()); this.getPermissionPrefix());
return true; return true;
} }

View File

@ -27,6 +27,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
@ -42,6 +43,7 @@ import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.managers.ChallengesManager; import world.bentobox.challenges.managers.ChallengesManager;
import world.bentobox.challenges.config.Settings; import world.bentobox.challenges.config.Settings;
@ -52,7 +54,7 @@ import world.bentobox.challenges.config.SettingsUtils.VisibilityMode;
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, ChatColor.class}) @PrepareForTest({Bukkit.class, BentoBox.class, ChatColor.class, Util.class})
public class ChallengesCommandTest { public class ChallengesCommandTest {
@Mock @Mock
@ -75,7 +77,7 @@ public class ChallengesCommandTest {
private IslandWorldManager iwm; private IslandWorldManager iwm;
@Mock @Mock
private GameModeAddon gameModeAddon; private GameModeAddon gameModeAddon;
@Mock
private Settings settings; private Settings settings;
/** /**
@ -106,7 +108,7 @@ public class ChallengesCommandTest {
when(iwm.getAddon(any())).thenReturn(optionalAddon); when(iwm.getAddon(any())).thenReturn(optionalAddon);
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
// Game Mode Addon // Game Mode Addon
@NonNull @NonNull
Optional<CompositeCommand> optionalAdmin = Optional.of(ic); Optional<CompositeCommand> optionalAdmin = Optional.of(ic);
when(gameModeAddon.getAdminCommand()).thenReturn(optionalAdmin); when(gameModeAddon.getAdminCommand()).thenReturn(optionalAdmin);
@ -124,6 +126,8 @@ public class ChallengesCommandTest {
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1); when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1);
when(user.isPlayer()).thenReturn(true); when(user.isPlayer()).thenReturn(true);
when(user.getTranslationOrNothing(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
when(user.getWorld()).thenReturn(world);
// Mock item factory (for itemstacks) // Mock item factory (for itemstacks)
PowerMockito.mockStatic(Bukkit.class); PowerMockito.mockStatic(Bukkit.class);
@ -143,13 +147,17 @@ public class ChallengesCommandTest {
when(ChatColor.translateAlternateColorCodes(any(char.class), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class)); when(ChatColor.translateAlternateColorCodes(any(char.class), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
// Settings // Settings
settings = new Settings();
when(addon.getChallengesSettings()).thenReturn(settings); when(addon.getChallengesSettings()).thenReturn(settings);
when(settings.getVisibilityMode()).thenReturn(VisibilityMode.VISIBLE); settings.setVisibilityMode(VisibilityMode.VISIBLE);
// Island // Island
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(any(), any(User.class))).thenReturn(island); when(im.getIsland(any(), any(User.class))).thenReturn(island);
// Util
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS);
when(Util.sameWorld(any(), any())).thenReturn(true);
// Command under test // Command under test
cc = new ChallengesPlayerCommand(addon, ic); cc = new ChallengesPlayerCommand(addon, ic);
} }
@ -161,7 +169,7 @@ public class ChallengesCommandTest {
public void testCanExecuteWrongWorld() { public void testCanExecuteWrongWorld() {
when(iwm.inWorld(any(World.class))).thenReturn(false); when(iwm.inWorld(any(World.class))).thenReturn(false);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList())); assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(user).sendMessage("general.errors.wrong-world"); verify(user).getTranslation("general.errors.wrong-world");
} }
/** /**
@ -169,10 +177,11 @@ public class ChallengesCommandTest {
*/ */
@Test @Test
public void testCanExecuteNoChallenges() { public void testCanExecuteNoChallenges() {
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false); when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList())); assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(addon).logError("There are no challenges set up in world!"); verify(addon).logError("There are no challenges set up in world!");
verify(user).sendMessage("challenges.errors.no-challenges"); verify(user).getTranslation("challenges.errors.no-challenges");
} }
/** /**
@ -184,8 +193,8 @@ public class ChallengesCommandTest {
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false); when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList())); assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(addon).logError("There are no challenges set up in world!"); verify(addon).logError("There are no challenges set up in world!");
verify(user).sendMessage("challenges.errors.no-challenges-admin", "[command]", "bsb challenges"); verify(user).getTranslation("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
verify(user, never()).sendMessage("challenges.errors.no-challenges"); verify(user, never()).getTranslation("challenges.errors.no-challenges");
} }
/** /**
@ -197,8 +206,8 @@ public class ChallengesCommandTest {
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false); when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList())); assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(addon).logError("There are no challenges set up in world!"); verify(addon).logError("There are no challenges set up in world!");
verify(user).sendMessage("challenges.errors.no-challenges-admin", "[command]", "bsb challenges"); verify(user).getTranslation("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
verify(user, never()).sendMessage("challenges.errors.no-challenges"); verify(user, never()).getTranslation("challenges.errors.no-challenges");
} }
/** /**
@ -211,8 +220,8 @@ public class ChallengesCommandTest {
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false); when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList())); assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(addon).logError("There are no challenges set up in world!"); verify(addon).logError("There are no challenges set up in world!");
verify(user).sendMessage("challenges.errors.no-challenges-admin", "[command]", "bsb challenges"); verify(user).getTranslation("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
verify(user, never()).sendMessage("challenges.errors.no-challenges"); verify(user, never()).getTranslation("challenges.errors.no-challenges");
} }
/** /**
@ -222,16 +231,16 @@ public class ChallengesCommandTest {
public void testCanExecuteNoIsland() { public void testCanExecuteNoIsland() {
when(im.getIsland(any(), any(User.class))).thenReturn(null); when(im.getIsland(any(), any(User.class))).thenReturn(null);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList())); assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(user).sendMessage("general.errors.no-island"); verify(user).getTranslation("general.errors.no-island");
} }
/** /**
* Test method for {@link ChallengesPlayerCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. * Test method for {@link ChallengesPlayerCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testCanExecuteSuccess() { public void testCanExecuteSuccess() {
assertTrue(cc.canExecute(user, "challenges", Collections.emptyList())); assertTrue(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(user, never()).sendMessage(anyString()); verify(user, never()).sendMessage(anyString());
} }
/** /**
@ -248,7 +257,7 @@ public class ChallengesCommandTest {
* Test method for {@link ChallengesPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. * Test method for {@link ChallengesPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testExecuteUserStringListOfStringUser() { public void testExecuteUserStringListOfStringUser() {
assertTrue(cc.execute(user, "challenges", Collections.emptyList())); assertTrue(cc.execute(user, "challenges", Collections.emptyList()));
} }