addon-challenges/src/test/java/world/bentobox/challenges/utils/UtilsTest.java

200 lines
7.2 KiB
Java
Raw Normal View History

Bug fix release (#228) * Fix issue when users could not select non-block items as icons for challenges and levels. (#190) * English update (#193) * Organized imports * Minor code cleanup * Updated English locale file. * Translate zh-CN.yml via GitLocalize (#188) * Make default translation looking a bit nicer (#192) * Make default translation looking a bit nicer * Updating a few friendly names and rewording some phrases * Add generic .gitignore * Fix novice level Update `chiseledmaker` name in `novice` level. * Adapt literal style (#197) Improve translations and process as a YAML string. * Test coverage (#199) * Test coverage for Challenges Command * Added CompleteChallengeCommand test class * Added Utils test class * Added ChallengesGUI test class * Fix code smells from sonarcloud analysis * Added .gitignore * Added Travis CI config file * WIP ChallengesManager Test class * Added ChallengesManager test class * Removed debug * Removed code smells. * Added ChallengesAddon test class. * Added onDisbale test * Added new TryToComplete test class - WIP Covers inventory challenges. * Added Island Challenge entity tests to TryToComplete test class * Fix a bug with challenge deletion. If challenge has been left in a level, then system did not remove challenge from it and was kept as ghost challenge, preventing from completing level. * Fixes tests * Updated travis.yml * All strings to spanish (#200) * Translate es.yml via GitLocalize * Translate es.yml via GitLocalize * Translate es.yml via GitLocalize * Fix LevelListRequestHandler. This handler did not return list of strings but list of challenge levels, that is incorrect. Not it should work correctly. * Create ro.yml * Create id.yml * Remove blanks files now that GitLocalize is fixed. * Initial Russian translation (#207) * Translate ru.yml via GitLocalize Co-authored-by: @mt-gitlocalize @IPeredero @LoveBiscuit * Changed build character from # to b * Add German translation (#210) * Translate de.yml via GitLocalize * Translate de.yml via GitLocalize * Translate de.yml via GitLocalize Co-authored-by: xXjojojXx <36734820+xXjojojXx@users.noreply.github.com> Co-authored-by: FunnysBanana <51290016+FunnysBanana@users.noreply.github.com> Co-authored-by: mt-gitlocalize <mt@gitlocalize.com> * Czech translation. Credit @Polda18 * Added a uniqueId sanitization when creating challenges/levels This will help fixing issues with spaces, hyphens and accents in non-English languages. * Fixes bug with checking entities in nether and end (#219) https://github.com/BentoBoxWorld/Challenges/issues/218 Adds test case to check for compliance. * Add 7 new placeholders for Challenges Addon. - `[gamemode]_challenge_total_completion_count` returns number of sum of challenge completions for user. - `[gamemode]_challenge_completed_count` returns number of completed challenges (at least once) for user. - `[gamemode]_challenge_uncompleted_count` returns number of uncompleted challenges for user. - `[gamemode]_challenge_completed_level_count` returns number of completed levels for user. - `[gamemode]_challenge_uncompleted_level_count` returns number of uncompleted levels for user. - `[gamemode]_challenge_unlocked_level_count` returns number of unlocked levels for user. - `[gamemode]_challenge_locked_level_count` returns number of locked levels for user. Fixes #224 * Add 2 new placeholders: - `[gamemode]_challenge_latest_level_name` returns latest unlocked challenge level name - `[gamemode]_challenge_latest_level_id` returns latest unlocked challenge level id Fixes #226 * Fix broken tests due to placeholder additions. https://github.com/BentoBoxWorld/Challenges/commit/b5ecffb725d78c154f469d7e2868574ca7ecff30 https://github.com/BentoBoxWorld/Challenges/commit/2958ca8b6c98f9c8d389aa93c4a2846f578fff43 * Added default perms for aoneblock * Downgrade to 0.8.1 version * Add option to quit from conversation by writing "cancel" in chat. Move sanitizeInput to a GuiUtil class. * Change latest version to 0.8.1
2020-04-22 00:19:46 +02:00
package world.bentobox.challenges.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
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 world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.AddonDescription;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.challenges.config.SettingsUtils.VisibilityMode;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class})
public class UtilsTest {
@Mock
private IslandWorldManager iwm;
@Mock
private GameModeAddon gameModeAddon;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Mock item factory (for itemstacks)
PowerMockito.mockStatic(Bukkit.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
// IWM getAddon
AddonDescription desc = new AddonDescription.Builder("main", "name", "1.0").build();
when(gameModeAddon.getDescription()).thenReturn(desc);
Optional<GameModeAddon> optionalAddon = Optional.of(gameModeAddon);
when(iwm.getAddon(any())).thenReturn(optionalAddon);
when(plugin.getIWM()).thenReturn(iwm);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link world.bentobox.challenges.utils.Utils#groupEqualItems(java.util.List)}.
*/
@Test
public void testGroupEqualItemsEmpty() {
assertTrue(Utils.groupEqualItems(Collections.emptyList()).isEmpty());
}
/**
* Test method for {@link world.bentobox.challenges.utils.Utils#groupEqualItems(java.util.List)}.
*/
@Test
public void testGroupEqualItems() {
List<ItemStack> requiredItems = new ArrayList<>();
// First item
ItemStack is = mock(ItemStack.class);
when(is.getAmount()).thenReturn(1);
when(is.getType()).thenReturn(Material.ACACIA_FENCE);
when(is.getMaxStackSize()).thenReturn(64);
when(is.isSimilar(any())).thenReturn(true);
when(is.clone()).thenReturn(is);
requiredItems.add(is);
for (int i = 0; i < 9; i++) {
ItemStack is2 = mock(ItemStack.class);
when(is2.getAmount()).thenReturn(1);
when(is2.getType()).thenReturn(Material.ACACIA_FENCE);
when(is2.getMaxStackSize()).thenReturn(64);
when(is2.isSimilar(any())).thenReturn(true);
when(is2.clone()).thenReturn(is);
requiredItems.add(is2);
}
List<ItemStack> list = Utils.groupEqualItems(requiredItems);
// Result should be two stacks stack of 64 doors and 36 doors
assertEquals(1, list.size());
verify(is, times(9)).setAmount(2);
}
/**
* Test method for {@link world.bentobox.challenges.utils.Utils#groupEqualItems(java.util.List)}.
*/
@Test
public void testGroupEqualItemsUnique() {
List<ItemStack> requiredItems = new ArrayList<>();
// First item
ItemStack is = mock(ItemStack.class);
when(is.getAmount()).thenReturn(1);
when(is.getType()).thenReturn(Material.ACACIA_FENCE);
when(is.getMaxStackSize()).thenReturn(64);
when(is.isSimilar(any())).thenReturn(false);
when(is.clone()).thenReturn(is);
requiredItems.add(is);
for (int i = 0; i < 9; i++) {
ItemStack is2 = mock(ItemStack.class);
when(is2.getAmount()).thenReturn(1);
when(is2.getType()).thenReturn(Material.values()[i+20]);
when(is2.getMaxStackSize()).thenReturn(64);
when(is2.isSimilar(any())).thenReturn(false);
when(is2.clone()).thenReturn(is);
requiredItems.add(is2);
}
List<ItemStack> list = Utils.groupEqualItems(requiredItems);
// Result should be two stacks stack of 64 doors and 36 doors
assertEquals(10, list.size());
verify(is, never()).setAmount(2);
}
/**
* Test method for {@link world.bentobox.challenges.utils.Utils#canIgnoreMeta(org.bukkit.Material)}.
*/
@Test
public void testCanIgnoreMeta() {
assertTrue(Utils.canIgnoreMeta(Material.FIREWORK_ROCKET));
assertTrue(Utils.canIgnoreMeta(Material.ENCHANTED_BOOK));
assertTrue(Utils.canIgnoreMeta(Material.WRITTEN_BOOK));
assertTrue(Utils.canIgnoreMeta(Material.FILLED_MAP));
assertFalse(Utils.canIgnoreMeta(Material.CHISELED_RED_SANDSTONE));
}
/**
* Test method for {@link world.bentobox.challenges.utils.Utils#getGameMode(org.bukkit.World)}.
*/
@Test
public void testGetGameModeNoGameMode() {
when(iwm.getAddon(any())).thenReturn(Optional.empty());
assertNull(Utils.getGameMode(mock(World.class)));
}
/**
* Test method for {@link world.bentobox.challenges.utils.Utils#getGameMode(org.bukkit.World)}.
*/
@Test
public void testGetGameMode() {
assertEquals("name", Utils.getGameMode(mock(World.class)));
}
/**
* Test method for {@link world.bentobox.challenges.utils.Utils#getNextValue(T[], java.lang.Object)}.
*/
@Test
public void testGetNextValue() {
assertEquals(VisibilityMode.HIDDEN, Utils.getNextValue(VisibilityMode.values(), VisibilityMode.VISIBLE));
assertEquals(VisibilityMode.TOGGLEABLE, Utils.getNextValue(VisibilityMode.values(), VisibilityMode.HIDDEN));
assertEquals(VisibilityMode.VISIBLE, Utils.getNextValue(VisibilityMode.values(), VisibilityMode.TOGGLEABLE));
}
/**
* Test method for {@link world.bentobox.challenges.utils.Utils#getPreviousValue(T[], java.lang.Object)}.
*/
@Test
public void testGetPreviousValue() {
assertEquals(VisibilityMode.TOGGLEABLE, Utils.getPreviousValue(VisibilityMode.values(), VisibilityMode.VISIBLE));
assertEquals(VisibilityMode.VISIBLE, Utils.getPreviousValue(VisibilityMode.values(), VisibilityMode.HIDDEN));
assertEquals(VisibilityMode.HIDDEN, Utils.getPreviousValue(VisibilityMode.values(), VisibilityMode.TOGGLEABLE));
}
}