-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed statement bug, added test class.
- Loading branch information
1 parent
daabb95
commit 294aeac
Showing
3 changed files
with
176 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
src/test/java/world/bentobox/bank/commands/user/StatementCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/** | ||
* | ||
*/ | ||
package world.bentobox.bank.commands.user; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyDouble; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Collections; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.Inventory; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.powermock.reflect.Whitebox; | ||
|
||
import net.milkbowl.vault.economy.EconomyResponse; | ||
import net.milkbowl.vault.economy.EconomyResponse.ResponseType; | ||
import world.bentobox.bank.Bank; | ||
import world.bentobox.bank.BankManager; | ||
import world.bentobox.bentobox.BentoBox; | ||
import world.bentobox.bentobox.Settings; | ||
import world.bentobox.bentobox.api.commands.CompositeCommand; | ||
import world.bentobox.bentobox.api.localization.TextVariables; | ||
import world.bentobox.bentobox.api.user.User; | ||
import world.bentobox.bentobox.database.objects.Island; | ||
import world.bentobox.bentobox.hooks.VaultHook; | ||
import world.bentobox.bentobox.managers.IslandWorldManager; | ||
import world.bentobox.bentobox.managers.IslandsManager; | ||
|
||
/** | ||
* @author tastybento | ||
* | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({Bukkit.class, BentoBox.class}) | ||
public class StatementCommandTest { | ||
@Mock | ||
private CompositeCommand ic; | ||
@Mock | ||
private User user; | ||
@Mock | ||
private World world; | ||
@Mock | ||
private BentoBox plugin; | ||
@Mock | ||
private IslandsManager im; | ||
@Mock | ||
private @Nullable Island island; | ||
@Mock | ||
private Bank addon; | ||
@Mock | ||
private BankManager bankManager; | ||
@Mock | ||
private VaultHook vh; | ||
// Class under test | ||
private StatementCommand sc; | ||
@Mock | ||
private Player player; | ||
|
||
/** | ||
* @throws java.lang.Exception | ||
*/ | ||
@Before | ||
public void setUp() throws Exception { | ||
// Set up plugin | ||
BentoBox plugin = mock(BentoBox.class); | ||
Whitebox.setInternalState(BentoBox.class, "instance", plugin); | ||
|
||
when(ic.getWorld()).thenReturn(world); | ||
when(user.getWorld()).thenReturn(world); | ||
when(user.getPlayer()).thenReturn(player); | ||
|
||
// IWM friendly name | ||
IslandWorldManager iwm = mock(IslandWorldManager.class); | ||
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); | ||
when(plugin.getIWM()).thenReturn(iwm); | ||
when(iwm.inWorld(any(World.class))).thenReturn(true); | ||
|
||
// Islands | ||
when(plugin.getIslands()).thenReturn(im); | ||
when(im.getIsland(eq(world), eq(user))).thenReturn(island); | ||
|
||
// Island flag allowed | ||
when(island.isAllowed(eq(user), any())).thenReturn(true); | ||
|
||
when(ic.getAddon()).thenReturn(addon); | ||
when(addon.getBankManager()).thenReturn(bankManager); | ||
when(addon.getVault()).thenReturn(vh); | ||
when(vh.format(anyDouble())).thenAnswer(i -> String.valueOf(i.getArgument(0, Double.class))); | ||
EconomyResponse er = new EconomyResponse(0, 0, ResponseType.SUCCESS, ""); | ||
when(vh.withdraw(eq(user), anyDouble(), eq(world))).thenReturn(er); | ||
|
||
// Settings | ||
when(plugin.getSettings()).thenReturn(new Settings()); | ||
|
||
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); | ||
sc = new StatementCommand(ic); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bank.commands.user.StatementCommand#setup()}. | ||
*/ | ||
@Test | ||
public void testSetup() { | ||
assertTrue(sc.isOnlyPlayer()); | ||
assertEquals("bank.user.statement", sc.getPermission()); | ||
assertEquals("bank.statement.description", sc.getDescription()); | ||
|
||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bank.commands.user.StatementCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testCanExecuteArgs() { | ||
assertFalse(sc.canExecute(user, "statement", Collections.singletonList("fff"))); | ||
verify(user).sendMessage(eq("commands.help.header"), eq(TextVariables.LABEL), eq("BSkyBlock")); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bank.commands.user.StatementCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testCanExecuteNoIsland() { | ||
when(im.getIsland(eq(world), eq(user))).thenReturn(null); | ||
assertFalse(sc.canExecute(user, "statement", Collections.emptyList())); | ||
verify(user).sendMessage(eq("general.errors.no-island")); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bank.commands.user.StatementCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testCanExecuteNoRank() { | ||
when(island.isAllowed(eq(user), any())).thenReturn(false); | ||
assertFalse(sc.canExecute(user, "statement", Collections.emptyList())); | ||
verify(user).sendMessage(eq("bank.errors.no-rank")); | ||
|
||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bank.commands.user.StatementCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testCanExecuteSuccess() { | ||
assertTrue(sc.canExecute(user, "statement", Collections.emptyList())); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bank.commands.user.StatementCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testExecuteUserStringListOfString() { | ||
assertTrue(sc.execute(user, "statement", Collections.emptyList())); | ||
verify(user).closeInventory(); | ||
verify(player).openInventory(any(Inventory.class)); | ||
} | ||
} |