|
1 |
| -package world.bentobox.bentobox.listeners.flags; |
| 1 | +package world.bentobox.bentobox; |
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertEquals; |
3 | 4 | import static org.mockito.ArgumentMatchers.any;
|
4 | 5 | import static org.mockito.ArgumentMatchers.anyString;
|
| 6 | +import static org.mockito.Mockito.atLeast; |
5 | 7 | import static org.mockito.Mockito.mock;
|
| 8 | +import static org.mockito.Mockito.verify; |
6 | 9 | import static org.mockito.Mockito.when;
|
7 | 10 |
|
8 | 11 | import java.util.Collections;
|
|
19 | 22 | import org.bukkit.block.Block;
|
20 | 23 | import org.bukkit.entity.Entity;
|
21 | 24 | import org.bukkit.entity.Player;
|
| 25 | +import org.bukkit.entity.Player.Spigot; |
22 | 26 | import org.bukkit.event.entity.EntityExplodeEvent;
|
23 | 27 | import org.bukkit.event.entity.PlayerDeathEvent;
|
24 | 28 | import org.bukkit.inventory.ItemFactory;
|
|
31 | 35 | import org.eclipse.jdt.annotation.Nullable;
|
32 | 36 | import org.junit.After;
|
33 | 37 | import org.junit.runner.RunWith;
|
| 38 | +import org.mockito.ArgumentCaptor; |
34 | 39 | import org.mockito.Mock;
|
35 | 40 | import org.mockito.Mockito;
|
36 | 41 | import org.mockito.stubbing.Answer;
|
|
40 | 45 |
|
41 | 46 | import com.google.common.collect.ImmutableSet;
|
42 | 47 |
|
43 |
| -import world.bentobox.bentobox.BentoBox; |
44 |
| -import world.bentobox.bentobox.Settings; |
| 48 | +import net.md_5.bungee.api.chat.TextComponent; |
45 | 49 | import world.bentobox.bentobox.api.configuration.WorldSettings;
|
46 | 50 | import world.bentobox.bentobox.api.user.Notifier;
|
47 | 51 | import world.bentobox.bentobox.api.user.User;
|
@@ -97,6 +101,8 @@ public abstract class AbstractCommonSetup {
|
97 | 101 | protected Notifier notifier;
|
98 | 102 | @Mock
|
99 | 103 | protected FlagsManager fm;
|
| 104 | + @Mock |
| 105 | + protected Spigot spigot; |
100 | 106 |
|
101 | 107 |
|
102 | 108 | public void setUp() throws Exception {
|
@@ -129,6 +135,7 @@ public void setUp() throws Exception {
|
129 | 135 | when(mockPlayer.getWorld()).thenReturn(world);
|
130 | 136 | when(mockPlayer.getName()).thenReturn("tastybento");
|
131 | 137 | when(mockPlayer.getInventory()).thenReturn(inv);
|
| 138 | + when(mockPlayer.spigot()).thenReturn(spigot); |
132 | 139 |
|
133 | 140 | User.setPlugin(plugin);
|
134 | 141 | User.clearUsers();
|
@@ -176,12 +183,13 @@ public void setUp() throws Exception {
|
176 | 183 | when(plugin.getSettings()).thenReturn(settings);
|
177 | 184 | when(settings.getFakePlayers()).thenReturn(new HashSet<>());
|
178 | 185 |
|
179 |
| - PowerMockito.mockStatic(Util.class); |
| 186 | + PowerMockito.mockStatic(Util.class, Mockito.CALLS_REAL_METHODS); |
180 | 187 | when(Util.getWorld(any())).thenReturn(mock(World.class));
|
| 188 | + |
181 | 189 | // Util
|
182 | 190 | when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
|
183 | 191 | // Util translate color codes (used in user translate methods)
|
184 |
| - when(Util.translateColorCodes(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class)); |
| 192 | + //when(Util.translateColorCodes(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class)); |
185 | 193 |
|
186 | 194 | // Tags
|
187 | 195 | for (Material m : Material.values()) {
|
@@ -217,6 +225,70 @@ public void tearDown() throws Exception {
|
217 | 225 | Mockito.framework().clearInlineMocks();
|
218 | 226 | }
|
219 | 227 |
|
| 228 | + /** |
| 229 | + * Check that spigot sent the message |
| 230 | + * @param message - message to check |
| 231 | + */ |
| 232 | + public void checkSpigotMessage(String expectedMessage) { |
| 233 | + checkSpigotMessage(expectedMessage, 1); |
| 234 | + } |
| 235 | + |
| 236 | + /* |
| 237 | + public void checkSpigotMessage(String expectedMessage, boolean shouldBePresent) { |
| 238 | + // Capture the argument passed to spigot().sendMessage(...) if messages are sent |
| 239 | + ArgumentCaptor<TextComponent> captor = ArgumentCaptor.forClass(TextComponent.class); |
| 240 | + |
| 241 | + if (shouldBePresent) { |
| 242 | + // If we expect a message to be present, verify that sendMessage() was called at least once |
| 243 | + verify(spigot, atLeastOnce()).sendMessage(captor.capture()); |
| 244 | + |
| 245 | + // Get all captured TextComponents |
| 246 | + List<TextComponent> capturedMessages = captor.getAllValues(); |
| 247 | + |
| 248 | + // Check if any captured message contains the expected text |
| 249 | + boolean messageFound = capturedMessages.stream() |
| 250 | + .map(component -> component.toPlainText()) // Convert each TextComponent to plain text |
| 251 | + .anyMatch(messageText -> messageText.contains(expectedMessage)); // Check if the expected message is present |
| 252 | + |
| 253 | + // Assert that the message was found |
| 254 | + assertTrue("Expected message not found: " + expectedMessage, messageFound); |
| 255 | + |
| 256 | + } else { |
| 257 | + // If we expect no messages with this text, capture any sent messages to ensure none match the given message |
| 258 | + verify(spigot, atLeast(0)).sendMessage(captor.capture()); |
| 259 | + |
| 260 | + // Get all captured TextComponents |
| 261 | + List<TextComponent> capturedMessages = captor.getAllValues(); |
| 262 | + |
| 263 | + // Check that none of the captured messages contain the forbidden text |
| 264 | + boolean messageFound = capturedMessages.stream().map(component -> component.toPlainText()) // Convert each TextComponent to plain text |
| 265 | + .anyMatch(messageText -> messageText.contains(expectedMessage)); // Check if the message is present |
| 266 | + |
| 267 | + // Assert that the message was NOT found |
| 268 | + assertFalse("Unexpected message found: " + expectedMessage, messageFound); |
| 269 | + } |
| 270 | + }*/ |
| 271 | + |
| 272 | + public void checkSpigotMessage(String expectedMessage, int expectedOccurrences) { |
| 273 | + // Capture the argument passed to spigot().sendMessage(...) if messages are sent |
| 274 | + ArgumentCaptor<TextComponent> captor = ArgumentCaptor.forClass(TextComponent.class); |
| 275 | + |
| 276 | + // Verify that sendMessage() was called at least 0 times (capture any sent messages) |
| 277 | + verify(spigot, atLeast(0)).sendMessage(captor.capture()); |
| 278 | + |
| 279 | + // Get all captured TextComponents |
| 280 | + List<TextComponent> capturedMessages = captor.getAllValues(); |
| 281 | + |
| 282 | + // Count the number of occurrences of the expectedMessage in the captured messages |
| 283 | + long actualOccurrences = capturedMessages.stream().map(component -> component.toPlainText()) // Convert each TextComponent to plain text |
| 284 | + .filter(messageText -> messageText.contains(expectedMessage)) // Check if the message contains the expected text |
| 285 | + .count(); // Count how many times the expected message appears |
| 286 | + |
| 287 | + // Assert that the number of occurrences matches the expectedOccurrences |
| 288 | + assertEquals("Expected message occurrence mismatch: " + expectedMessage, expectedOccurrences, |
| 289 | + actualOccurrences); |
| 290 | + } |
| 291 | + |
220 | 292 | /**
|
221 | 293 | * Get the explode event
|
222 | 294 | * @param entity
|
|
0 commit comments