Fix tests

This commit is contained in:
tastybento 2024-04-29 21:37:08 -07:00
parent 63cc0a01d9
commit e33823d0c0
23 changed files with 68 additions and 17 deletions

View File

@ -17,10 +17,10 @@ import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import com.google.common.base.Enums;
import com.google.common.base.Optional;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util;
/**
* Handles breeding protection
@ -41,8 +41,10 @@ public class BreedingListener extends FlagListener {
bi.put(EntityType.HORSE, Arrays.asList(Material.GOLDEN_APPLE, Material.GOLDEN_CARROT));
bi.put(EntityType.DONKEY, Arrays.asList(Material.GOLDEN_APPLE, Material.GOLDEN_CARROT));
bi.put(EntityType.COW, Collections.singletonList(Material.WHEAT));
bi.put(Util.findFirstMatchingEnum(EntityType.class, "MUSHROOM_COW", "MOOSHROOM"),
Collections.singletonList(Material.WHEAT));
Optional<EntityType> mc = Enums.getIfPresent(EntityType.class, "MUSHROOM_COW");
if (mc.isPresent()) {
bi.put(mc.get(), Collections.singletonList(Material.WHEAT));
}
bi.put(EntityType.SHEEP, Collections.singletonList(Material.WHEAT));
bi.put(EntityType.PIG, Arrays.asList(Material.CARROT, Material.POTATO, Material.BEETROOT));
bi.put(EntityType.CHICKEN, Arrays.asList(Material.WHEAT_SEEDS, Material.PUMPKIN_SEEDS, Material.MELON_SEEDS, Material.BEETROOT_SEEDS));

View File

@ -1,5 +1,6 @@
package world.bentobox.bentobox.listeners.flags.protection;
import java.io.IOException;
import java.util.List;
import org.bukkit.Location;
@ -17,6 +18,9 @@ import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import com.google.common.base.Enums;
import com.google.common.base.Optional;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util;
@ -26,17 +30,26 @@ import world.bentobox.bentobox.util.Util;
* @author tastybento
*/
public class TNTListener extends FlagListener {
private static final EntityType PRIMED_TNT = Util.findFirstMatchingEnum(EntityType.class, "PRIMED_TNT", "TNT");
private static final EntityType MINECART_TNT = Util.findFirstMatchingEnum(EntityType.class, "MINECART_TNT",
"TNT_MINECART");
/**
* Contains {@link EntityType}s that generates an explosion.
* @since 1.5.0
*/
private static final List<EntityType> TNT_TYPES = List.of(PRIMED_TNT, MINECART_TNT);
private static final List<EntityType> TNT_TYPES = List.of(
findFirstMatchingEnum(EntityType.class, "PRIMED_TNT", "TNT"),
findFirstMatchingEnum(EntityType.class, "MINECART_TNT", "TNT_MINECART"));
private static <T extends Enum<T>> T findFirstMatchingEnum(Class<T> enumClass, String... values) {
if (enumClass == null || values == null) {
return null;
}
for (String value : values) {
Optional<T> enumConstant = Enums.getIfPresent(enumClass, value.toUpperCase());
if (enumConstant.isPresent()) {
return enumConstant.get();
}
}
return null; // Return null if no match is found
}
/**
* Contains {@link Material}s that can be used to prime a TNT.
* @since 1.5.0

View File

@ -145,7 +145,6 @@ public class ItemParser {
ItemParser.setCustomModelData(returnValue, customModelData);
}
} catch (Exception exception) {
exception.printStackTrace();
BentoBox.getInstance().logError("Could not parse item " + text + " " + exception.getLocalizedMessage());
returnValue = defaultItemStack;
}

View File

@ -1,5 +1,6 @@
package world.bentobox.bentobox.util;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -815,15 +816,19 @@ public class Util {
* @param values an array of string values which are potential matches for the enum constants
* @param <T> the type parameter of the enum
* @return the first matching enum constant if a match is found; otherwise, returns null
* @throws IOException
* @throws NullPointerException if either {@code enumClass} or {@code values} are null
*/
public static <T extends Enum<T>> T findFirstMatchingEnum(Class<T> enumClass, String... values) {
if (enumClass == null || values == null) {
return null;
}
for (String value : values) {
Optional<T> enumConstant = Enums.getIfPresent(enumClass, value.toUpperCase());
if (enumConstant.isPresent()) {
return enumConstant.get();
}
}
return null; // Return null or throw an exception if no match is found
return null; // Return null if no match is found
}
}

View File

@ -36,6 +36,8 @@ 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;
@ -93,6 +95,10 @@ public class TestBentoBox extends AbstractCommonSetup {
when(ownerOfIsland.getUniqueId()).thenReturn(uuid);
when(visitorToIsland.getUniqueId()).thenReturn(VISITOR_UUID);
// Util
PowerMockito.mockStatic(Util.class);
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
island.setOwner(uuid);
island.setProtectionRange(100);
HashMap<UUID, Integer> members = new HashMap<>();

View File

@ -155,6 +155,7 @@ public class AdminSettingsCommandTest extends RanksManagerBeforeClassTest {
PowerMockito.mockStatic(Util.class);
when(Util.getUUID(anyString())).thenReturn(uuid);
when(Util.tabLimit(any(), any())).thenCallRealMethod();
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// Settings
Settings settings = new Settings();

View File

@ -160,6 +160,7 @@ public class IslandGoCommandTest {
when(iwm.getAddon(any())).thenReturn(Optional.empty());
PowerMockito.mockStatic(Util.class);
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// Locales
LocalesManager lm = mock(LocalesManager.class);

View File

@ -215,6 +215,7 @@ public class CycleClickTest {
// Util
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// Event
when(Bukkit.getPluginManager()).thenReturn(pim);

View File

@ -92,6 +92,7 @@ public class IslandToggleClickTest {
when(user.getUniqueId()).thenReturn(uuid);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(mock(World.class));
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
FlagsManager fm = mock(FlagsManager.class);
when(flag.isSetForWorld(any())).thenReturn(false);

View File

@ -134,6 +134,7 @@ public class StandardSpawnProtectionListenerTest {
// Util translate color codes (used in user translate methods)
when(Util.translateColorCodes(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// Set up class
ssp = new StandardSpawnProtectionListener(plugin);

View File

@ -169,6 +169,8 @@ public abstract class AbstractCommonSetup {
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(mock(World.class));
// Util
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// Util translate color codes (used in user translate methods)
when(Util.translateColorCodes(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));

View File

@ -4,6 +4,7 @@ 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.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@ -81,6 +82,9 @@ public class TNTListenerTest extends AbstractCommonSetup {
when(entity.getWorld()).thenReturn(world);
when(entity.getLocation()).thenReturn(location);
// Util
when(Util.findFirstMatchingEnum(any(), anyString())).thenCallRealMethod();
listener = new TNTListener();
listener.setPlugin(plugin);

View File

@ -231,6 +231,7 @@ public class PVPListenerTest {
// Util translate color codes (used in user translate methods)
when(Util.translateColorCodes(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
}

View File

@ -82,6 +82,7 @@ public class CleanSuperFlatListenerTest {
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS);
when(Util.getWorld(any())).thenReturn(world);
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// Regenerator
when(Util.getRegenerator()).thenReturn(regenerator);

View File

@ -102,6 +102,11 @@ public class EnterExitListenerTest {
Settings s = mock(Settings.class);
when(plugin.getSettings()).thenReturn(s);
// Util
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
@ -189,9 +194,6 @@ public class EnterExitListenerTest {
// Listener
listener = new EnterExitListener();
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(any())).thenReturn(ws);

View File

@ -137,6 +137,7 @@ public class InvincibleVisitorsListenerTest {
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(mock(World.class));
when(Util.prettifyText(anyString())).thenCallRealMethod();
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// Util translate color codes (used in user translate methods)
when(Util.translateColorCodes(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
FlagsManager fm = mock(FlagsManager.class);

View File

@ -103,6 +103,7 @@ public class IslandRespawnListenerTest {
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// World Settings
WorldSettings ws = mock(WorldSettings.class);

View File

@ -66,6 +66,11 @@ public class OfflineGrowthListenerTest {
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Util
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// Owner
UUID uuid = UUID.randomUUID();
@ -92,9 +97,6 @@ public class OfflineGrowthListenerTest {
when(block.getLocation()).thenReturn(inside);
when(block.getType()).thenReturn(Material.KELP);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
// World Settings
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);

View File

@ -94,6 +94,7 @@ public class OfflineRedstoneListenerTest {
// Util
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// World Settings
when(iwm.inWorld(any(World.class))).thenReturn(true);

View File

@ -94,6 +94,7 @@ public class PistonPushListenerTest {
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// World Settings
IslandWorldManager iwm = mock(IslandWorldManager.class);

View File

@ -97,6 +97,7 @@ public class RemoveMobsListenerTest {
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// World Settings
IslandWorldManager iwm = mock(IslandWorldManager.class);

View File

@ -87,6 +87,9 @@ public class FlagsManagerTest {
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
//PowerMockito.mockStatic(Flags.class);
// Util
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
}
@After

View File

@ -238,6 +238,7 @@ public class IslandsManagerTest extends AbstractCommonSetup {
// Worlds translate to world
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// Island
when(island.getOwner()).thenReturn(uuid);