1
1
package world .bentobox .islandfly .listeners ;
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 ;
5
6
import static org .mockito .ArgumentMatchers .eq ;
7
+ import static org .mockito .Mockito .atLeast ;
6
8
import static org .mockito .Mockito .mock ;
7
9
import static org .mockito .Mockito .never ;
8
10
import static org .mockito .Mockito .verify ;
15
17
import org .bukkit .Bukkit ;
16
18
import org .bukkit .Location ;
17
19
import org .bukkit .entity .Player ;
20
+ import org .bukkit .entity .Player .Spigot ;
18
21
import org .bukkit .scheduler .BukkitScheduler ;
19
22
import org .eclipse .jdt .annotation .NonNull ;
20
23
import org .junit .After ;
21
24
import org .junit .Before ;
22
25
import org .junit .Test ;
23
26
import org .junit .runner .RunWith ;
27
+ import org .mockito .ArgumentCaptor ;
24
28
import org .mockito .Mock ;
25
29
import org .mockito .stubbing .Answer ;
26
30
import org .powermock .api .mockito .PowerMockito ;
27
31
import org .powermock .core .classloader .annotations .PrepareForTest ;
28
32
import org .powermock .modules .junit4 .PowerMockRunner ;
29
33
34
+ import net .md_5 .bungee .api .chat .TextComponent ;
30
35
import world .bentobox .bentobox .BentoBox ;
31
36
import world .bentobox .bentobox .api .events .flags .FlagProtectionChangeEvent ;
32
37
import world .bentobox .bentobox .api .flags .Flag ;
@@ -66,6 +71,8 @@ public class FlyFlagListenerTest {
66
71
private Player op ;
67
72
@ Mock
68
73
private Island island ;
74
+ @ Mock
75
+ private Spigot spigot ;
69
76
70
77
/**
71
78
* @throws java.lang.Exception
@@ -95,17 +102,21 @@ public void setUp() throws Exception {
95
102
@ NonNull
96
103
List <Player > list = new ArrayList <>();
97
104
when (p1 .getUniqueId ()).thenReturn (UUID .randomUUID ());
105
+ when (p1 .spigot ()).thenReturn (spigot );
98
106
User .getInstance (p1 );
99
107
when (p1 .isFlying ()).thenReturn (true );
100
108
when (p2 .getUniqueId ()).thenReturn (UUID .randomUUID ());
109
+ when (p2 .spigot ()).thenReturn (spigot );
101
110
User .getInstance (p2 );
102
111
when (p2 .isFlying ()).thenReturn (true );
103
112
when (p2 .isOnline ()).thenReturn (true );
104
113
when (p2 .getLocation ()).thenReturn (mock (Location .class ));
105
114
when (p3 .getUniqueId ()).thenReturn (UUID .randomUUID ());
115
+ when (p3 .spigot ()).thenReturn (spigot );
106
116
User .getInstance (p3 );
107
117
when (p3 .isFlying ()).thenReturn (false );
108
118
when (op .getUniqueId ()).thenReturn (UUID .randomUUID ());
119
+ when (op .spigot ()).thenReturn (spigot );
109
120
User .getInstance (op );
110
121
when (op .isFlying ()).thenReturn (true );
111
122
when (op .isOp ()).thenReturn (true );
@@ -148,7 +159,7 @@ public void testOnFlagChangeOtherFlag() {
148
159
public void testOnFlagChange () {
149
160
ffl .onFlagChange (e );
150
161
verify (p1 , never ()).sendMessage (anyString ());
151
- verify ( p2 ). sendMessage ( eq ( "islandfly.fly-turning-off-alert" ) );
162
+ this . checkSpigotMessage ( "islandfly.fly-turning-off-alert" );
152
163
verify (p3 , never ()).sendMessage (anyString ());
153
164
verify (op , never ()).sendMessage (anyString ());
154
165
verify (scheduler ).runTaskLater (eq (plugin ), any (Runnable .class ), eq (100L ));
@@ -162,13 +173,13 @@ public void testOnFlagChangeZeroTime() {
162
173
when (settings .getFlyTimeout ()).thenReturn (0 );
163
174
ffl .onFlagChange (e );
164
175
verify (p1 , never ()).sendMessage (anyString ());
165
- verify ( p2 ). sendMessage ( eq ( "islandfly.fly-turning-off-alert" ) );
176
+ this . checkSpigotMessage ( "islandfly.fly-turning-off-alert" );
166
177
verify (p3 , never ()).sendMessage (anyString ());
167
178
verify (op , never ()).sendMessage (anyString ());
168
179
169
180
verify (p2 ).setFlying (false );
170
181
verify (p2 ).setAllowFlight (false );
171
- verify ( p2 ). sendMessage ("islandfly.disable-fly" );
182
+ checkSpigotMessage ("islandfly.disable-fly" );
172
183
173
184
}
174
185
@@ -179,7 +190,7 @@ public void testOnFlagChangeZeroTime() {
179
190
public void testDisableAllowedAgain () {
180
191
when (island .isAllowed (any (), any ())).thenReturn (true );
181
192
ffl .disable (p2 , User .getInstance (p2 ), island );
182
- verify ( p2 ). sendMessage ( eq ( "islandfly.reallowed-fly" ) );
193
+ checkSpigotMessage ( "islandfly.reallowed-fly" );
183
194
}
184
195
185
196
/**
@@ -189,6 +200,35 @@ public void testDisableAllowedAgain() {
189
200
public void testDisable () {
190
201
when (island .isAllowed (any (), any ())).thenReturn (false );
191
202
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 );
193
232
}
233
+
194
234
}
0 commit comments