Skip to content

Commit

Permalink
Fix tests, optimize tests, reord imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Oct 5, 2024
1 parent 97aff7a commit 29c5057
Show file tree
Hide file tree
Showing 68 changed files with 592 additions and 983 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
Map<String, Island> getNameIslandMap(User user) {
Map<String, Island> islandMap = new HashMap<>();
int index = 0;
System.out.println("Getting for " + user.getName());
for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) {
System.out.println("Island - " + island);
index++;
if (island.getName() != null && !island.getName().isBlank()) {
// Name has been set
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/world/bentobox/bentobox/lists/Flags.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import world.bentobox.bentobox.listeners.flags.protection.ElytraListener;
import world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener;
import world.bentobox.bentobox.listeners.flags.protection.ExperiencePickupListener;
import world.bentobox.bentobox.listeners.flags.protection.ExplosionListener;
import world.bentobox.bentobox.listeners.flags.protection.FireListener;
import world.bentobox.bentobox.listeners.flags.protection.HurtingListener;
import world.bentobox.bentobox.listeners.flags.protection.InventoryListener;
Expand All @@ -39,7 +40,6 @@
import world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener;
import world.bentobox.bentobox.listeners.flags.protection.SculkShriekerListener;
import world.bentobox.bentobox.listeners.flags.protection.ShearingListener;
import world.bentobox.bentobox.listeners.flags.protection.ExplosionListener;
import world.bentobox.bentobox.listeners.flags.protection.TeleportationListener;
import world.bentobox.bentobox.listeners.flags.protection.ThrowingListener;
import world.bentobox.bentobox.listeners.flags.settings.DecayListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package world.bentobox.bentobox.managers.island;

import java.util.Map.Entry;

import java.util.TreeMap;

import world.bentobox.bentobox.database.objects.Island;
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/world/bentobox/bentobox/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,21 @@
import org.bukkit.attribute.Attribute;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.*;
import org.bukkit.entity.Allay;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Bat;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Flying;
import org.bukkit.entity.IronGolem;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.entity.PufferFish;
import org.bukkit.entity.Shulker;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Snowman;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.WaterMob;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package world.bentobox.bentobox.listeners.flags;
package world.bentobox.bentobox;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
Expand All @@ -19,6 +22,7 @@
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Player.Spigot;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemFactory;
Expand All @@ -31,6 +35,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
Expand All @@ -40,8 +45,7 @@

import com.google.common.collect.ImmutableSet;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import net.md_5.bungee.api.chat.TextComponent;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
Expand Down Expand Up @@ -97,6 +101,8 @@ public abstract class AbstractCommonSetup {
protected Notifier notifier;
@Mock
protected FlagsManager fm;
@Mock
protected Spigot spigot;


public void setUp() throws Exception {
Expand Down Expand Up @@ -129,6 +135,7 @@ public void setUp() throws Exception {
when(mockPlayer.getWorld()).thenReturn(world);
when(mockPlayer.getName()).thenReturn("tastybento");
when(mockPlayer.getInventory()).thenReturn(inv);
when(mockPlayer.spigot()).thenReturn(spigot);

User.setPlugin(plugin);
User.clearUsers();
Expand Down Expand Up @@ -176,12 +183,13 @@ public void setUp() throws Exception {
when(plugin.getSettings()).thenReturn(settings);
when(settings.getFakePlayers()).thenReturn(new HashSet<>());

PowerMockito.mockStatic(Util.class);
PowerMockito.mockStatic(Util.class, Mockito.CALLS_REAL_METHODS);
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));
//when(Util.translateColorCodes(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));

// Tags
for (Material m : Material.values()) {
Expand Down Expand Up @@ -217,6 +225,70 @@ public void tearDown() throws Exception {
Mockito.framework().clearInlineMocks();
}

/**
* Check that spigot sent the message
* @param message - message to check
*/
public void checkSpigotMessage(String expectedMessage) {
checkSpigotMessage(expectedMessage, 1);
}

/*
public void checkSpigotMessage(String expectedMessage, boolean shouldBePresent) {
// Capture the argument passed to spigot().sendMessage(...) if messages are sent
ArgumentCaptor<TextComponent> captor = ArgumentCaptor.forClass(TextComponent.class);
if (shouldBePresent) {
// If we expect a message to be present, verify that sendMessage() was called at least once
verify(spigot, atLeastOnce()).sendMessage(captor.capture());
// Get all captured TextComponents
List<TextComponent> capturedMessages = captor.getAllValues();
// Check if any captured message contains the expected text
boolean messageFound = capturedMessages.stream()
.map(component -> component.toPlainText()) // Convert each TextComponent to plain text
.anyMatch(messageText -> messageText.contains(expectedMessage)); // Check if the expected message is present
// Assert that the message was found
assertTrue("Expected message not found: " + expectedMessage, messageFound);
} else {
// If we expect no messages with this text, capture any sent messages to ensure none match the given message
verify(spigot, atLeast(0)).sendMessage(captor.capture());
// Get all captured TextComponents
List<TextComponent> capturedMessages = captor.getAllValues();
// Check that none of the captured messages contain the forbidden text
boolean messageFound = capturedMessages.stream().map(component -> component.toPlainText()) // Convert each TextComponent to plain text
.anyMatch(messageText -> messageText.contains(expectedMessage)); // Check if the message is present
// Assert that the message was NOT found
assertFalse("Unexpected message found: " + expectedMessage, messageFound);
}
}*/

public void checkSpigotMessage(String expectedMessage, int expectedOccurrences) {
// Capture the argument passed to spigot().sendMessage(...) if messages are sent
ArgumentCaptor<TextComponent> captor = ArgumentCaptor.forClass(TextComponent.class);

// Verify that sendMessage() was called at least 0 times (capture any sent messages)
verify(spigot, atLeast(0)).sendMessage(captor.capture());

// Get all captured TextComponents
List<TextComponent> capturedMessages = captor.getAllValues();

// Count the number of occurrences of the expectedMessage in the captured messages
long actualOccurrences = capturedMessages.stream().map(component -> component.toPlainText()) // Convert each TextComponent to plain text
.filter(messageText -> messageText.contains(expectedMessage)) // Check if the message contains the expected text
.count(); // Count how many times the expected message appears

// Assert that the number of occurrences matches the expectedOccurrences
assertEquals("Expected message occurrence mismatch: " + expectedMessage, expectedOccurrences,
actualOccurrences);
}

/**
* Get the explode event
* @param entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package world.bentobox.bentobox.managers;
package world.bentobox.bentobox;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
Expand Down Expand Up @@ -29,18 +29,18 @@
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.DatabaseSetup;
import world.bentobox.bentobox.managers.RanksManager;

/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ BentoBox.class, DatabaseSetup.class, RanksManager.class, Bukkit.class })
public abstract class RanksManagerBeforeClassTest {
public abstract class RanksManagerBeforeClassTest extends AbstractCommonSetup {

// Constants that define the hard coded rank values
public static final String ADMIN_RANK_REF = "ranks.admin";
Expand Down Expand Up @@ -68,8 +68,6 @@ public abstract class RanksManagerBeforeClassTest {
TRUSTED_RANK_REF, TRUSTED_RANK, COOP_RANK_REF, COOP_RANK, VISITOR_RANK_REF, VISITOR_RANK, BANNED_RANK_REF,
BANNED_RANK);

@Mock
public BentoBox plugin;
@Mock
public RanksManager rm;

Expand Down Expand Up @@ -113,6 +111,7 @@ public static void beforeClass() throws IllegalAccessException, InvocationTarget

@Before
public void setUp() throws Exception {
super.setUp();
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getBukkitVersion()).thenReturn("");
// Set up plugin
Expand Down
1 change: 0 additions & 1 deletion src/test/java/world/bentobox/bentobox/TestBentoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -34,16 +30,15 @@
import org.powermock.modules.junit4.PowerMockRunner;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.util.Util;

/**
Expand All @@ -59,27 +54,15 @@ public class AdminInfoCommandTest extends RanksManagerBeforeClassTest {
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private PlayersManager pm;

private Island island;

private AdminInfoCommand iic;

@Mock
private Player player;
@Mock
private World world;
@Mock
private PlaceholdersManager phm;
@Mock
private @NonNull Location location;
@Mock
private IslandWorldManager iwm;

/**
*/
@Before
public void setUp() throws Exception {
super.setUp();
Expand All @@ -96,12 +79,12 @@ public void setUp() throws Exception {
when(plugin.getCommandsManager()).thenReturn(cm);

// Player
when(player.isOp()).thenReturn(false);
when(mockPlayer.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getName()).thenReturn("tastybento");
when(user.getWorld()).thenReturn(world);
when(user.getPlayer()).thenReturn(player);
when(user.getPlayer()).thenReturn(mockPlayer);
when(user.isPlayer()).thenReturn(true);
//user = User.getInstance(player);
// Set the User class plugin as this one
Expand Down Expand Up @@ -164,6 +147,7 @@ public void testExecuteUserStringListOfStringTooManyArgs() {
@Test
public void testExecuteUserStringListOfStringNoArgsConsole() {
CommandSender console = mock(CommandSender.class);
when(console.spigot()).thenReturn(spigot);
User sender = User.getInstance(console);
assertFalse(iic.execute(sender, "", Collections.emptyList()));
verify(user, never()).sendMessage("commands.help.header", "[label]", "commands.help.console");
Expand Down
Loading

0 comments on commit 29c5057

Please sign in to comment.