Skip to content

Commit 9bffe68

Browse files
authored
JUnit 5 cleanup for packages api thru message (#2993)
* JUnit 5 cleanup for packages api and command * JUnit 5 cleanup for package data and datasource * JUnit cleanup for packages events, initialization, listener * JUnit cleanup for packages mail, message * Revert accidental method rename, remove unnecessary comment
1 parent a23e807 commit 9bffe68

103 files changed

Lines changed: 1144 additions & 1429 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

authme-core/src/test/java/fr/xephi/authme/TestHelper.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.mockito.Mockito;
77

88
import java.io.File;
9+
import java.io.IOException;
910
import java.net.InetAddress;
1011
import java.net.InetSocketAddress;
1112
import java.net.URI;
@@ -119,4 +120,21 @@ public static void returnDefaultsForAllProperties(Settings settings) {
119120
given(settings.getProperty(any(Property.class)))
120121
.willAnswer(invocation -> ((Property<?>) invocation.getArgument(0)).getDefaultValue());
121122
}
123+
124+
/**
125+
* Creates a file with the given name in the provided folder. Throws an exception if the file
126+
* could not be created.
127+
*
128+
* @param folder the folder to create the file in
129+
* @param filename the name of the file to create
130+
* @return the created file
131+
*/
132+
public static File createFile(File folder, String filename) throws IOException {
133+
File file = new File(folder, filename);
134+
boolean created = file.createNewFile();
135+
if (!created) {
136+
throw new IllegalStateException("Could not create file '" + filename + "' in " + folder);
137+
}
138+
return file;
139+
}
122140
}

authme-core/src/test/java/fr/xephi/authme/api/v3/AuthMeApiTest.java

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package fr.xephi.authme.api.v3;
22

3-
import org.mockito.quality.Strictness;
4-
import org.mockito.junit.jupiter.MockitoSettings;
5-
import org.mockito.junit.jupiter.MockitoExtension;
6-
import org.junit.jupiter.api.extension.ExtendWith;
73
import fr.xephi.authme.AuthMe;
84
import fr.xephi.authme.ReflectionTestUtils;
95
import fr.xephi.authme.data.auth.PlayerAuth;
@@ -22,10 +18,11 @@
2218
import org.bukkit.World;
2319
import org.bukkit.entity.Player;
2420
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
2522
import org.mockito.ArgumentCaptor;
26-
import org.mockito.Captor;
2723
import org.mockito.InjectMocks;
2824
import org.mockito.Mock;
25+
import org.mockito.junit.jupiter.MockitoExtension;
2926

3027
import java.time.Instant;
3128
import java.util.Arrays;
@@ -55,8 +52,7 @@
5552
* Test for {@link AuthMeApi}.
5653
*/
5754
@ExtendWith(MockitoExtension.class)
58-
@MockitoSettings(strictness = Strictness.WARN)
59-
public class AuthMeApiTest {
55+
class AuthMeApiTest {
6056

6157
@InjectMocks
6258
private AuthMeApi api;
@@ -75,11 +71,9 @@ public class AuthMeApiTest {
7571
private AuthMe authMe;
7672
@Mock
7773
private GeoIpService geoIpService;
78-
@Captor
79-
private ArgumentCaptor<PlayerAuth> authCaptor;
8074

8175
@Test
82-
public void shouldReturnInstanceOrNull() {
76+
void shouldReturnInstanceOrNull() {
8377
AuthMeApi result = AuthMeApi.getInstance();
8478
assertThat(result, sameInstance(api));
8579

@@ -88,7 +82,7 @@ public void shouldReturnInstanceOrNull() {
8882
}
8983

9084
@Test
91-
public void shouldReturnIfPlayerIsAuthenticated() {
85+
void shouldReturnIfPlayerIsAuthenticated() {
9286
// given
9387
String name = "Bobby";
9488
Player player = mockPlayerWithName(name);
@@ -103,7 +97,7 @@ public void shouldReturnIfPlayerIsAuthenticated() {
10397
}
10498

10599
@Test
106-
public void shouldReturnIfPlayerIsNpc() {
100+
void shouldReturnIfPlayerIsNpc() {
107101
// given
108102
Player player = mock(Player.class);
109103
given(player.hasMetadata("NPC")).willReturn(true);
@@ -117,7 +111,7 @@ public void shouldReturnIfPlayerIsNpc() {
117111
}
118112

119113
@Test
120-
public void shouldReturnIfPlayerIsUnrestricted() {
114+
void shouldReturnIfPlayerIsUnrestricted() {
121115
// given
122116
String name = "Tester";
123117
Player player = mockPlayerWithName(name);
@@ -132,7 +126,7 @@ public void shouldReturnIfPlayerIsUnrestricted() {
132126
}
133127

134128
@Test
135-
public void shouldGetLastLocation() {
129+
void shouldGetLastLocation() {
136130
// given
137131
String name = "Gary";
138132
Player player = mockPlayerWithName(name);
@@ -164,7 +158,7 @@ public void shouldGetLastLocation() {
164158
}
165159

166160
@Test
167-
public void shouldGetLastIp() {
161+
void shouldGetLastIp() {
168162
// given
169163
String name = "Gabriel";
170164
Player player = mockPlayerWithName(name);
@@ -182,7 +176,7 @@ public void shouldGetLastIp() {
182176
}
183177

184178
@Test
185-
public void shouldReturnNullAsLastIpForUnknownUser() {
179+
void shouldReturnNullAsLastIpForUnknownUser() {
186180
// given
187181
String name = "Harrison";
188182
given(playerCache.getAuth(name)).willReturn(null);
@@ -198,7 +192,7 @@ public void shouldReturnNullAsLastIpForUnknownUser() {
198192
}
199193

200194
@Test
201-
public void shouldGetLastLogin() {
195+
void shouldGetLastLogin() {
202196
// given
203197
String name = "David";
204198
PlayerAuth auth = PlayerAuth.builder().name(name)
@@ -215,7 +209,7 @@ public void shouldGetLastLogin() {
215209
}
216210

217211
@Test
218-
public void shouldHandleNullLastLogin() {
212+
void shouldHandleNullLastLogin() {
219213
// given
220214
String name = "John";
221215
PlayerAuth auth = PlayerAuth.builder().name(name)
@@ -232,7 +226,7 @@ public void shouldHandleNullLastLogin() {
232226
}
233227

234228
@Test
235-
public void shouldGetLastLoginTime() {
229+
void shouldGetLastLoginTime() {
236230
// given
237231
String name = "David";
238232
PlayerAuth auth = PlayerAuth.builder().name(name)
@@ -249,13 +243,16 @@ public void shouldGetLastLoginTime() {
249243
}
250244

251245
@Test
252-
public void testGetLastLoginMillis() {
253-
AuthMeApi result = AuthMeApi.getInstance();
254-
assertThat(result.getLastLoginTime("notAPlayer"), nullValue());
246+
void shouldReturnNullLastLoginForUnknownPlayer() {
247+
// given / when
248+
Instant result = api.getLastLoginTime("notAPlayer");
249+
250+
// then
251+
assertThat(result, nullValue());
255252
}
256253

257254
@Test
258-
public void shouldHandleNullLastLoginTime() {
255+
void shouldHandleNullLastLoginTime() {
259256
// given
260257
String name = "John";
261258
PlayerAuth auth = PlayerAuth.builder().name(name)
@@ -272,7 +269,7 @@ public void shouldHandleNullLastLoginTime() {
272269
}
273270

274271
@Test
275-
public void shouldReturnNullForUnavailablePlayer() {
272+
void shouldReturnNullForUnavailablePlayer() {
276273
// given
277274
String name = "Numan";
278275
Player player = mockPlayerWithName(name);
@@ -286,7 +283,7 @@ public void shouldReturnNullForUnavailablePlayer() {
286283
}
287284

288285
@Test
289-
public void shouldCheckForRegisteredName() {
286+
void shouldCheckForRegisteredName() {
290287
// given
291288
String name = "toaster";
292289
given(dataSource.isAuthAvailable(name)).willReturn(true);
@@ -299,7 +296,7 @@ public void shouldCheckForRegisteredName() {
299296
}
300297

301298
@Test
302-
public void shouldCheckPassword() {
299+
void shouldCheckPassword() {
303300
// given
304301
String playerName = "Robert";
305302
String password = "someSecretPhrase2983";
@@ -314,7 +311,7 @@ public void shouldCheckPassword() {
314311
}
315312

316313
@Test
317-
public void shouldReturnAuthNames() {
314+
void shouldReturnAuthNames() {
318315
// given
319316
String[] names = {"bobby", "peter", "elisabeth", "craig"};
320317
List<PlayerAuth> auths = Arrays.stream(names)
@@ -330,7 +327,7 @@ public void shouldReturnAuthNames() {
330327
}
331328

332329
@Test
333-
public void shouldReturnAuthRealNames() {
330+
void shouldReturnAuthRealNames() {
334331
// given
335332
String[] names = {"Bobby", "peter", "Elisabeth", "CRAIG"};
336333
List<PlayerAuth> auths = Arrays.stream(names)
@@ -346,7 +343,7 @@ public void shouldReturnAuthRealNames() {
346343
}
347344

348345
@Test
349-
public void shouldUnregisterPlayer() {
346+
void shouldUnregisterPlayer() {
350347
// given
351348
Player player = mock(Player.class);
352349
String name = "Donald";
@@ -360,7 +357,7 @@ public void shouldUnregisterPlayer() {
360357
}
361358

362359
@Test
363-
public void shouldUnregisterPlayerByName() {
360+
void shouldUnregisterPlayerByName() {
364361
// given
365362
Server server = mock(Server.class);
366363
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
@@ -376,7 +373,7 @@ public void shouldUnregisterPlayerByName() {
376373
}
377374

378375
@Test
379-
public void shouldChangePassword() {
376+
void shouldChangePassword() {
380377
// given
381378
String name = "Bobby12";
382379
String password = "resetPw!";
@@ -389,7 +386,7 @@ public void shouldChangePassword() {
389386
}
390387

391388
@Test
392-
public void shouldReturnAuthMeInstance() {
389+
void shouldReturnAuthMeInstance() {
393390
// given / when
394391
AuthMe result = api.getPlugin();
395392

@@ -398,7 +395,7 @@ public void shouldReturnAuthMeInstance() {
398395
}
399396

400397
@Test
401-
public void shouldReturnVersion() {
398+
void shouldReturnVersion() {
402399
// given / when
403400
String result = api.getPluginVersion();
404401

@@ -407,7 +404,7 @@ public void shouldReturnVersion() {
407404
}
408405

409406
@Test
410-
public void shouldForceLogin() {
407+
void shouldForceLogin() {
411408
// given
412409
Player player = mock(Player.class);
413410

@@ -419,7 +416,7 @@ public void shouldForceLogin() {
419416
}
420417

421418
@Test
422-
public void shouldForceLogout() {
419+
void shouldForceLogout() {
423420
// given
424421
Player player = mock(Player.class);
425422

@@ -431,7 +428,7 @@ public void shouldForceLogout() {
431428
}
432429

433430
@Test
434-
public void shouldForceRegister() {
431+
void shouldForceRegister() {
435432
// given
436433
Player player = mock(Player.class);
437434
String pass = "test235";
@@ -445,7 +442,7 @@ public void shouldForceRegister() {
445442
}
446443

447444
@Test
448-
public void shouldForceRegisterAndNotAutoLogin() {
445+
void shouldForceRegisterAndNotAutoLogin() {
449446
// given
450447
Player player = mock(Player.class);
451448
String pass = "test235";
@@ -459,7 +456,7 @@ public void shouldForceRegisterAndNotAutoLogin() {
459456
}
460457

461458
@Test
462-
public void shouldRegisterPlayer() {
459+
void shouldRegisterPlayer() {
463460
// given
464461
String name = "Marco";
465462
String password = "myP4ss";
@@ -473,14 +470,15 @@ public void shouldRegisterPlayer() {
473470
// then
474471
assertThat(result, equalTo(true));
475472
verify(passwordSecurity).computeHash(password, name.toLowerCase(Locale.ROOT));
473+
ArgumentCaptor<PlayerAuth> authCaptor = ArgumentCaptor.forClass(PlayerAuth.class);
476474
verify(dataSource).saveAuth(authCaptor.capture());
477475
assertThat(authCaptor.getValue().getNickname(), equalTo(name.toLowerCase(Locale.ROOT)));
478476
assertThat(authCaptor.getValue().getRealName(), equalTo(name));
479477
assertThat(authCaptor.getValue().getPassword(), equalTo(hashedPassword));
480478
}
481479

482480
@Test
483-
public void shouldNotRegisterAlreadyRegisteredPlayer() {
481+
void shouldNotRegisterAlreadyRegisteredPlayer() {
484482
// given
485483
String name = "jonah";
486484
given(dataSource.isAuthAvailable(name)).willReturn(true);
@@ -495,7 +493,7 @@ public void shouldNotRegisterAlreadyRegisteredPlayer() {
495493
}
496494

497495
@Test
498-
public void shouldGetNamesByIp() {
496+
void shouldGetNamesByIp() {
499497
// given
500498
String ip = "123.123.123.123";
501499
List<String> names = Arrays.asList("Morgan", "Batista", "QUINN");
@@ -510,7 +508,7 @@ public void shouldGetNamesByIp() {
510508
}
511509

512510
@Test
513-
public void shouldReturnGeoIpInfo() {
511+
void shouldReturnGeoIpInfo() {
514512
// given
515513
String ip = "127.127.12.1";
516514
given(geoIpService.getCountryCode(ip)).willReturn("XA");
@@ -526,7 +524,7 @@ public void shouldReturnGeoIpInfo() {
526524
}
527525

528526
@Test
529-
public void shouldReturnAuthMePlayerInfo() {
527+
void shouldReturnAuthMePlayerInfo() {
530528
// given
531529
PlayerAuth auth = PlayerAuth.builder()
532530
.name("bobb")
@@ -545,7 +543,7 @@ public void shouldReturnAuthMePlayerInfo() {
545543
}
546544

547545
@Test
548-
public void shouldReturnNullForNonExistentAuth() {
546+
void shouldReturnNullForNonExistentAuth() {
549547
// given / when
550548
Optional<AuthMePlayer> result = api.getPlayerInfo("doesNotExist");
551549

@@ -561,5 +559,3 @@ private static Player mockPlayerWithName(String name) {
561559
return player;
562560
}
563561
}
564-
565-

0 commit comments

Comments
 (0)