|
| 1 | +package world.bentobox.stranger; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.ArgumentMatchers.anyString; |
| 6 | +import static org.mockito.Mockito.atLeast; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.verify; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.Comparator; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Optional; |
| 19 | +import java.util.UUID; |
| 20 | + |
| 21 | +import org.bukkit.Bukkit; |
| 22 | +import org.bukkit.Location; |
| 23 | +import org.bukkit.World; |
| 24 | +import org.bukkit.block.Block; |
| 25 | +import org.bukkit.entity.Entity; |
| 26 | +import org.bukkit.entity.EntityType; |
| 27 | +import org.bukkit.entity.Player; |
| 28 | +import org.bukkit.entity.Player.Spigot; |
| 29 | +import org.bukkit.event.entity.EntityExplodeEvent; |
| 30 | +import org.bukkit.event.entity.PlayerDeathEvent; |
| 31 | +import org.bukkit.inventory.ItemFactory; |
| 32 | +import org.bukkit.inventory.ItemStack; |
| 33 | +import org.bukkit.inventory.PlayerInventory; |
| 34 | +import org.bukkit.metadata.FixedMetadataValue; |
| 35 | +import org.bukkit.metadata.MetadataValue; |
| 36 | +import org.bukkit.plugin.PluginManager; |
| 37 | +import org.bukkit.scheduler.BukkitScheduler; |
| 38 | +import org.bukkit.util.Vector; |
| 39 | +import org.eclipse.jdt.annotation.Nullable; |
| 40 | +import org.junit.jupiter.api.AfterEach; |
| 41 | +import org.junit.jupiter.api.BeforeEach; |
| 42 | +import org.mockbukkit.mockbukkit.MockBukkit; |
| 43 | +import org.mockbukkit.mockbukkit.ServerMock; |
| 44 | +import org.mockito.ArgumentCaptor; |
| 45 | +import org.mockito.Mock; |
| 46 | +import org.mockito.MockedStatic; |
| 47 | +import org.mockito.Mockito; |
| 48 | +import org.mockito.MockitoAnnotations; |
| 49 | +import org.mockito.stubbing.Answer; |
| 50 | + |
| 51 | +import com.google.common.collect.ImmutableSet; |
| 52 | + |
| 53 | +import net.md_5.bungee.api.chat.TextComponent; |
| 54 | +import world.bentobox.bentobox.BentoBox; |
| 55 | +import world.bentobox.bentobox.api.configuration.WorldSettings; |
| 56 | +import world.bentobox.bentobox.api.user.Notifier; |
| 57 | +import world.bentobox.bentobox.api.user.User; |
| 58 | +import world.bentobox.bentobox.database.objects.Island; |
| 59 | +import world.bentobox.bentobox.database.objects.Players; |
| 60 | +import world.bentobox.bentobox.managers.BlueprintsManager; |
| 61 | +import world.bentobox.bentobox.managers.FlagsManager; |
| 62 | +import world.bentobox.bentobox.managers.HooksManager; |
| 63 | +import world.bentobox.bentobox.managers.IslandWorldManager; |
| 64 | +import world.bentobox.bentobox.managers.IslandsManager; |
| 65 | +import world.bentobox.bentobox.managers.LocalesManager; |
| 66 | +import world.bentobox.bentobox.managers.PlaceholdersManager; |
| 67 | +import world.bentobox.bentobox.managers.PlayersManager; |
| 68 | +import world.bentobox.bentobox.util.Util; |
| 69 | + |
| 70 | +/** |
| 71 | + * Common items for testing. Don't forget to use super.setUp()! |
| 72 | + * <p> |
| 73 | + * Sets up BentoBox plugin, pluginManager and ItemFactory. |
| 74 | + * Location, world, playersManager and player. |
| 75 | + * IWM, Addon and WorldSettings. IslandManager with one |
| 76 | + * island with protection and nothing allowed by default. |
| 77 | + * Owner of island is player with same UUID. |
| 78 | + * Locales, placeholders. |
| 79 | + * @author tastybento |
| 80 | + * |
| 81 | + */ |
| 82 | +public abstract class CommonTestSetup { |
| 83 | + |
| 84 | + protected UUID uuid = UUID.randomUUID(); |
| 85 | + |
| 86 | + @Mock |
| 87 | + protected Player mockPlayer; |
| 88 | + @Mock |
| 89 | + protected PluginManager pim; |
| 90 | + @Mock |
| 91 | + protected ItemFactory itemFactory; |
| 92 | + @Mock |
| 93 | + protected Location location; |
| 94 | + @Mock |
| 95 | + protected World world; |
| 96 | + @Mock |
| 97 | + protected IslandWorldManager iwm; |
| 98 | + @Mock |
| 99 | + protected IslandsManager im; |
| 100 | + @Mock |
| 101 | + protected Island island; |
| 102 | + @Mock |
| 103 | + protected BentoBox plugin; |
| 104 | + @Mock |
| 105 | + protected PlayerInventory inv; |
| 106 | + @Mock |
| 107 | + protected Notifier notifier; |
| 108 | + @Mock |
| 109 | + protected FlagsManager fm; |
| 110 | + @Mock |
| 111 | + protected Spigot spigot; |
| 112 | + @Mock |
| 113 | + protected HooksManager hooksManager; |
| 114 | + @Mock |
| 115 | + protected BlueprintsManager bm; |
| 116 | + |
| 117 | + protected ServerMock server; |
| 118 | + |
| 119 | + protected MockedStatic<Bukkit> mockedBukkit; |
| 120 | + protected MockedStatic<Util> mockedUtil; |
| 121 | + |
| 122 | + protected AutoCloseable closeable; |
| 123 | + |
| 124 | + @Mock |
| 125 | + protected BukkitScheduler sch; |
| 126 | + @Mock |
| 127 | + protected LocalesManager lm; |
| 128 | + |
| 129 | + |
| 130 | + @BeforeEach |
| 131 | + public void setUp() throws Exception { |
| 132 | + MockitoAnnotations.openMocks(this); |
| 133 | + // Processes the @Mock annotations and initializes the field |
| 134 | + closeable = MockitoAnnotations.openMocks(this); |
| 135 | + server = MockBukkit.mock(); |
| 136 | + // Bukkit |
| 137 | + // Set up plugin |
| 138 | + WhiteBox.setInternalState(BentoBox.class, "instance", plugin); |
| 139 | + |
| 140 | + // Register the static mock |
| 141 | + mockedBukkit = Mockito.mockStatic(Bukkit.class, Mockito.RETURNS_DEEP_STUBS); |
| 142 | + mockedBukkit.when(Bukkit::getMinecraftVersion).thenReturn("1.21.10"); |
| 143 | + mockedBukkit.when(Bukkit::getBukkitVersion).thenReturn(""); |
| 144 | + mockedBukkit.when(Bukkit::getPluginManager).thenReturn(pim); |
| 145 | + mockedBukkit.when(Bukkit::getItemFactory).thenReturn(itemFactory); |
| 146 | + mockedBukkit.when(Bukkit::getServer).thenReturn(server); |
| 147 | + // Location |
| 148 | + when(location.getWorld()).thenReturn(world); |
| 149 | + when(location.getBlockX()).thenReturn(0); |
| 150 | + when(location.getBlockY()).thenReturn(0); |
| 151 | + when(location.getBlockZ()).thenReturn(0); |
| 152 | + when(location.toVector()).thenReturn(new Vector(0,0,0)); |
| 153 | + when(location.clone()).thenReturn(location); // Paper |
| 154 | + |
| 155 | + // Players Manager and meta data |
| 156 | + PlayersManager pm = mock(PlayersManager.class); |
| 157 | + when(plugin.getPlayers()).thenReturn(pm); |
| 158 | + Players players = mock(Players.class); |
| 159 | + when(players.getMetaData()).thenReturn(Optional.empty()); |
| 160 | + when(pm.getPlayer(any(UUID.class))).thenReturn(players); |
| 161 | + |
| 162 | + // Player |
| 163 | + when(mockPlayer.getUniqueId()).thenReturn(uuid); |
| 164 | + when(mockPlayer.getLocation()).thenReturn(location); |
| 165 | + when(mockPlayer.getWorld()).thenReturn(world); |
| 166 | + when(mockPlayer.getName()).thenReturn("tastybento"); |
| 167 | + when(mockPlayer.getInventory()).thenReturn(inv); |
| 168 | + when(mockPlayer.spigot()).thenReturn(spigot); |
| 169 | + when(mockPlayer.getType()).thenReturn(EntityType.PLAYER); |
| 170 | + when(mockPlayer.getWorld()).thenReturn(world); |
| 171 | + |
| 172 | + User.setPlugin(plugin); |
| 173 | + User.clearUsers(); |
| 174 | + User.getInstance(mockPlayer); |
| 175 | + |
| 176 | + // IWM |
| 177 | + when(plugin.getIWM()).thenReturn(iwm); |
| 178 | + when(iwm.inWorld(any(Location.class))).thenReturn(true); |
| 179 | + when(iwm.inWorld(any(World.class))).thenReturn(true); |
| 180 | + when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); |
| 181 | + // Addon |
| 182 | + when(iwm.getAddon(any())).thenReturn(Optional.empty()); |
| 183 | + |
| 184 | + // World Settings |
| 185 | + WorldSettings worldSet = new TestWorldSettings(); |
| 186 | + when(iwm.getWorldSettings(any())).thenReturn(worldSet); |
| 187 | + |
| 188 | + // Island Manager |
| 189 | + when(plugin.getIslands()).thenReturn(im); |
| 190 | + Optional<Island> optionalIsland = Optional.of(island); |
| 191 | + when(im.getProtectedIslandAt(any())).thenReturn(optionalIsland); |
| 192 | + |
| 193 | + // Island - nothing is allowed by default |
| 194 | + when(island.isAllowed(any())).thenReturn(false); |
| 195 | + when(island.isAllowed(any(User.class), any())).thenReturn(false); |
| 196 | + when(island.getOwner()).thenReturn(uuid); |
| 197 | + when(island.getMemberSet()).thenReturn(ImmutableSet.of(uuid)); |
| 198 | + |
| 199 | + // Enable reporting from Flags class |
| 200 | + MetadataValue mdv = new FixedMetadataValue(plugin, "_why_debug"); |
| 201 | + when(mockPlayer.getMetadata(anyString())).thenReturn(Collections.singletonList(mdv)); |
| 202 | + |
| 203 | + // Locales & Placeholders |
| 204 | + when(lm.get(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class)); |
| 205 | + PlaceholdersManager phm = mock(PlaceholdersManager.class); |
| 206 | + when(plugin.getPlaceholdersManager()).thenReturn(phm); |
| 207 | + when(phm.replacePlaceholders(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class)); |
| 208 | + when(plugin.getLocalesManager()).thenReturn(lm); |
| 209 | + // Notifier |
| 210 | + when(plugin.getNotifier()).thenReturn(notifier); |
| 211 | + |
| 212 | + // Fake players |
| 213 | + //Settings settings = new Settings(); |
| 214 | + //when(plugin.getSettings()).thenReturn(settings); |
| 215 | + |
| 216 | + //Util |
| 217 | + mockedUtil = Mockito.mockStatic(Util.class, Mockito.CALLS_REAL_METHODS); |
| 218 | + mockedUtil.when(() -> Util.getWorld(any())).thenReturn(mock(World.class)); |
| 219 | + Util.setPlugin(plugin); |
| 220 | + |
| 221 | + // Util |
| 222 | + mockedUtil.when(() -> Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); |
| 223 | + // Util translate color codes (used in user translate methods) |
| 224 | + //mockedUtil.when(() -> translateColorCodes(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class)); |
| 225 | + |
| 226 | + // Server & Scheduler |
| 227 | + mockedBukkit.when(() -> Bukkit.getScheduler()).thenReturn(sch); |
| 228 | + |
| 229 | + // Hooks |
| 230 | + when(hooksManager.getHook(anyString())).thenReturn(Optional.empty()); |
| 231 | + when(plugin.getHooks()).thenReturn(hooksManager); |
| 232 | + |
| 233 | + // Blueprints Manager |
| 234 | + when(plugin.getBlueprintsManager()).thenReturn(bm); |
| 235 | + |
| 236 | + // Namespace |
| 237 | + when(plugin.namespace()).thenReturn("bentobox"); |
| 238 | + |
| 239 | + |
| 240 | + // Tags |
| 241 | + /* |
| 242 | + for (Material m : Material.values()) { |
| 243 | + if (m.name().contains("_SIGN")) { |
| 244 | + when(Tag.ALL_SIGNS.isTagged(m)).thenReturn(true); |
| 245 | + when(Tag.SIGNS.isTagged(m)).thenReturn(true); |
| 246 | + } |
| 247 | + if (m.name().contains("_WALL_SIGN")) { |
| 248 | + when(Tag.WALL_SIGNS.isTagged(m)).thenReturn(true); |
| 249 | + } |
| 250 | + if (m.name().contains("_TRAPDOOR")) { |
| 251 | + when(Tag.TRAPDOORS.isTagged(m)).thenReturn(true); |
| 252 | + } |
| 253 | + if (m.name().contains("FENCE")) { |
| 254 | + when(Tag.FENCES.isTagged(m)).thenReturn(true); |
| 255 | + } |
| 256 | + if (m.name().contains("_DOOR")) { |
| 257 | + when(Tag.DOORS.isTagged(m)).thenReturn(true); |
| 258 | + } |
| 259 | + if (m.name().contains("_BOAT") || m.name().contains("_RAFT")) { |
| 260 | + when(Tag.ITEMS_BOATS.isTagged(m)).thenReturn(true); |
| 261 | + } |
| 262 | +
|
| 263 | + }*/ |
| 264 | + } |
| 265 | + |
| 266 | + /** |
| 267 | + * @throws Exception |
| 268 | + */ |
| 269 | + @AfterEach |
| 270 | + public void tearDown() throws Exception { |
| 271 | + // IMPORTANT: Explicitly close the mock to prevent leakage |
| 272 | + mockedBukkit.closeOnDemand(); |
| 273 | + mockedUtil.closeOnDemand(); |
| 274 | + closeable.close(); |
| 275 | + MockBukkit.unmock(); |
| 276 | + User.clearUsers(); |
| 277 | + Mockito.framework().clearInlineMocks(); |
| 278 | + deleteAll(new File("database")); |
| 279 | + deleteAll(new File("database_backup")); |
| 280 | + } |
| 281 | + |
| 282 | + protected static void deleteAll(File file) throws IOException { |
| 283 | + if (file.exists()) { |
| 284 | + if (file.isDirectory()) { |
| 285 | + Files.walk(file.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); |
| 286 | + } else { |
| 287 | + file.delete(); |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + } |
| 292 | + |
| 293 | + /** |
| 294 | + * Check that spigot sent the message |
| 295 | + * @param message - message to check |
| 296 | + */ |
| 297 | + public void checkSpigotMessage(String expectedMessage) { |
| 298 | + checkSpigotMessage(expectedMessage, 1); |
| 299 | + } |
| 300 | + |
| 301 | + @SuppressWarnings("deprecation") |
| 302 | + public void checkSpigotMessage(String expectedMessage, int expectedOccurrences) { |
| 303 | + // Capture the argument passed to spigot().sendMessage(...) if messages are sent |
| 304 | + ArgumentCaptor<TextComponent> captor = ArgumentCaptor.forClass(TextComponent.class); |
| 305 | + |
| 306 | + // Verify that sendMessage() was called at least 0 times (capture any sent messages) |
| 307 | + verify(spigot, atLeast(0)).sendMessage(captor.capture()); |
| 308 | + |
| 309 | + // Get all captured TextComponents |
| 310 | + List<TextComponent> capturedMessages = captor.getAllValues(); |
| 311 | + |
| 312 | + // Count the number of occurrences of the expectedMessage in the captured messages |
| 313 | + long actualOccurrences = capturedMessages.stream().map(component -> component.toLegacyText()) // Convert each TextComponent to plain text |
| 314 | + .filter(messageText -> messageText.contains(expectedMessage)) // Check if the message contains the expected text |
| 315 | + .count(); // Count how many times the expected message appears |
| 316 | + |
| 317 | + // Assert that the number of occurrences matches the expectedOccurrences |
| 318 | + assertEquals(expectedOccurrences, |
| 319 | + actualOccurrences, "Expected message occurrence mismatch: " + expectedMessage); |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * Get the explode event |
| 324 | + * @param entity |
| 325 | + * @param l |
| 326 | + * @param list |
| 327 | + * @return |
| 328 | + */ |
| 329 | + public EntityExplodeEvent getExplodeEvent(Entity entity, Location l, List<Block> list) { |
| 330 | + //return new EntityExplodeEvent(entity, l, list, 0, null); |
| 331 | + return new EntityExplodeEvent(entity, l, list, 0, null); |
| 332 | + } |
| 333 | + |
| 334 | + public PlayerDeathEvent getPlayerDeathEvent(Player player, List<ItemStack> drops, int droppedExp, int newExp, |
| 335 | + int newTotalExp, int newLevel, @Nullable String deathMessage) { |
| 336 | + //Technically this null is not allowed, but it works right now |
| 337 | + return new PlayerDeathEvent(player, null, drops, droppedExp, newExp, |
| 338 | + newTotalExp, newLevel, deathMessage); |
| 339 | + } |
| 340 | + |
| 341 | +} |
0 commit comments