11package world .bentobox .islandfly .listeners ;
22
3+ import static org .junit .Assert .assertEquals ;
34import static org .mockito .ArgumentMatchers .any ;
45import static org .mockito .ArgumentMatchers .anyString ;
56import static org .mockito .ArgumentMatchers .eq ;
7+ import static org .mockito .Mockito .atLeast ;
68import static org .mockito .Mockito .mock ;
79import static org .mockito .Mockito .never ;
810import static org .mockito .Mockito .verify ;
1517import org .bukkit .Bukkit ;
1618import org .bukkit .Location ;
1719import org .bukkit .entity .Player ;
20+ import org .bukkit .entity .Player .Spigot ;
1821import org .bukkit .scheduler .BukkitScheduler ;
1922import org .eclipse .jdt .annotation .NonNull ;
2023import org .junit .After ;
2124import org .junit .Before ;
2225import org .junit .Test ;
2326import org .junit .runner .RunWith ;
27+ import org .mockito .ArgumentCaptor ;
2428import org .mockito .Mock ;
2529import org .mockito .stubbing .Answer ;
2630import org .powermock .api .mockito .PowerMockito ;
2731import org .powermock .core .classloader .annotations .PrepareForTest ;
2832import org .powermock .modules .junit4 .PowerMockRunner ;
2933
34+ import net .md_5 .bungee .api .chat .TextComponent ;
3035import world .bentobox .bentobox .BentoBox ;
3136import world .bentobox .bentobox .api .events .flags .FlagProtectionChangeEvent ;
3237import world .bentobox .bentobox .api .flags .Flag ;
@@ -66,6 +71,8 @@ public class FlyFlagListenerTest {
6671 private Player op ;
6772 @ Mock
6873 private Island island ;
74+ @ Mock
75+ private Spigot spigot ;
6976
7077 /**
7178 * @throws java.lang.Exception
@@ -95,17 +102,21 @@ public void setUp() throws Exception {
95102 @ NonNull
96103 List <Player > list = new ArrayList <>();
97104 when (p1 .getUniqueId ()).thenReturn (UUID .randomUUID ());
105+ when (p1 .spigot ()).thenReturn (spigot );
98106 User .getInstance (p1 );
99107 when (p1 .isFlying ()).thenReturn (true );
100108 when (p2 .getUniqueId ()).thenReturn (UUID .randomUUID ());
109+ when (p2 .spigot ()).thenReturn (spigot );
101110 User .getInstance (p2 );
102111 when (p2 .isFlying ()).thenReturn (true );
103112 when (p2 .isOnline ()).thenReturn (true );
104113 when (p2 .getLocation ()).thenReturn (mock (Location .class ));
105114 when (p3 .getUniqueId ()).thenReturn (UUID .randomUUID ());
115+ when (p3 .spigot ()).thenReturn (spigot );
106116 User .getInstance (p3 );
107117 when (p3 .isFlying ()).thenReturn (false );
108118 when (op .getUniqueId ()).thenReturn (UUID .randomUUID ());
119+ when (op .spigot ()).thenReturn (spigot );
109120 User .getInstance (op );
110121 when (op .isFlying ()).thenReturn (true );
111122 when (op .isOp ()).thenReturn (true );
@@ -148,7 +159,7 @@ public void testOnFlagChangeOtherFlag() {
148159 public void testOnFlagChange () {
149160 ffl .onFlagChange (e );
150161 verify (p1 , never ()).sendMessage (anyString ());
151- verify ( p2 ). sendMessage ( eq ( "islandfly.fly-turning-off-alert" ) );
162+ this . checkSpigotMessage ( "islandfly.fly-turning-off-alert" );
152163 verify (p3 , never ()).sendMessage (anyString ());
153164 verify (op , never ()).sendMessage (anyString ());
154165 verify (scheduler ).runTaskLater (eq (plugin ), any (Runnable .class ), eq (100L ));
@@ -162,13 +173,13 @@ public void testOnFlagChangeZeroTime() {
162173 when (settings .getFlyTimeout ()).thenReturn (0 );
163174 ffl .onFlagChange (e );
164175 verify (p1 , never ()).sendMessage (anyString ());
165- verify ( p2 ). sendMessage ( eq ( "islandfly.fly-turning-off-alert" ) );
176+ this . checkSpigotMessage ( "islandfly.fly-turning-off-alert" );
166177 verify (p3 , never ()).sendMessage (anyString ());
167178 verify (op , never ()).sendMessage (anyString ());
168179
169180 verify (p2 ).setFlying (false );
170181 verify (p2 ).setAllowFlight (false );
171- verify ( p2 ). sendMessage ("islandfly.disable-fly" );
182+ checkSpigotMessage ("islandfly.disable-fly" );
172183
173184 }
174185
@@ -179,7 +190,7 @@ public void testOnFlagChangeZeroTime() {
179190 public void testDisableAllowedAgain () {
180191 when (island .isAllowed (any (), any ())).thenReturn (true );
181192 ffl .disable (p2 , User .getInstance (p2 ), island );
182- verify ( p2 ). sendMessage ( eq ( "islandfly.reallowed-fly" ) );
193+ checkSpigotMessage ( "islandfly.reallowed-fly" );
183194 }
184195
185196 /**
@@ -189,6 +200,35 @@ public void testDisableAllowedAgain() {
189200 public void testDisable () {
190201 when (island .isAllowed (any (), any ())).thenReturn (false );
191202 ffl .disable (p2 , User .getInstance (p2 ), island );
192- verify (p2 ).sendMessage (eq ("islandfly.disable-fly" ));
203+ this .checkSpigotMessage ("islandfly.disable-fly" );
204+ }
205+
206+ /**
207+ * Check that spigot sent the message
208+ * @param message - message to check
209+ */
210+ public void checkSpigotMessage (String expectedMessage ) {
211+ checkSpigotMessage (expectedMessage , 1 );
212+ }
213+
214+ public void checkSpigotMessage (String expectedMessage , int expectedOccurrences ) {
215+ // Capture the argument passed to spigot().sendMessage(...) if messages are sent
216+ ArgumentCaptor <TextComponent > captor = ArgumentCaptor .forClass (TextComponent .class );
217+
218+ // Verify that sendMessage() was called at least 0 times (capture any sent messages)
219+ verify (spigot , atLeast (0 )).sendMessage (captor .capture ());
220+
221+ // Get all captured TextComponents
222+ List <TextComponent > capturedMessages = captor .getAllValues ();
223+
224+ // Count the number of occurrences of the expectedMessage in the captured messages
225+ long actualOccurrences = capturedMessages .stream ().map (component -> component .toLegacyText ()) // Convert each TextComponent to plain text
226+ .filter (messageText -> messageText .contains (expectedMessage )) // Check if the message contains the expected text
227+ .count (); // Count how many times the expected message appears
228+
229+ // Assert that the number of occurrences matches the expectedOccurrences
230+ assertEquals ("Expected message occurrence mismatch: " + expectedMessage , expectedOccurrences ,
231+ actualOccurrences );
193232 }
233+
194234}
0 commit comments