Skip to content

Commit

Permalink
Remove mocks of InetAddress (which is a sealed class in JDK 19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacqu committed Apr 28, 2024
1 parent 793efed commit e06445b
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.mockito.junit.MockitoJUnitRunner;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -65,12 +66,12 @@
import static com.google.common.collect.Sets.newHashSet;
import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -711,7 +712,7 @@ public void shouldPerformAllJoinVerificationsSuccessfullyPreLoginLowest() throws
UUID uniqueId = UUID.fromString("753493c9-33ba-4a4a-bf61-1bce9d3c9a71");
String ip = "12.34.56.78";

AsyncPlayerPreLoginEvent preLoginEvent = spy(new AsyncPlayerPreLoginEvent(name, mockAddrWithIp(ip), uniqueId));
AsyncPlayerPreLoginEvent preLoginEvent = spy(new AsyncPlayerPreLoginEvent(name, createInetAddress(ip), uniqueId));
given(validationService.isUnrestricted(name)).willReturn(false);

// when
Expand Down Expand Up @@ -749,7 +750,7 @@ public void shouldPerformAllJoinVerificationsSuccessfullyPreLoginHighest() throw
UUID uniqueId = UUID.fromString("753493c9-33ba-4a4a-bf61-1bce9d3c9a71");
String ip = "12.34.56.78";

AsyncPlayerPreLoginEvent preLoginEvent = spy(new AsyncPlayerPreLoginEvent(name, mockAddrWithIp(ip), uniqueId));
AsyncPlayerPreLoginEvent preLoginEvent = spy(new AsyncPlayerPreLoginEvent(name, createInetAddress(ip), uniqueId));
given(validationService.isUnrestricted(name)).willReturn(false);
PlayerAuth auth = PlayerAuth.builder().name(name).build();
given(dataSource.getAuth(name)).willReturn(auth);
Expand All @@ -773,7 +774,7 @@ public void shouldPerformAllJoinVerificationsSuccessfullyLogin() {
Player player = mockPlayerWithName(name);
String ip = "12.34.56.78";

PlayerLoginEvent loginEvent = spy(new PlayerLoginEvent(player, "", mockAddrWithIp(ip)));
PlayerLoginEvent loginEvent = spy(new PlayerLoginEvent(player, "", createInetAddress(ip)));
given(validationService.isUnrestricted(name)).willReturn(false);
given(onJoinVerifier.refusePlayerForFullServer(loginEvent)).willReturn(false);

Expand All @@ -792,7 +793,7 @@ public void shouldAbortPlayerJoinForInvalidName() throws FailedVerificationExcep
// given
String name = "inval!dName";
UUID uniqueId = UUID.fromString("753493c9-33ba-4a4a-bf61-1bce9d3c9a71");
InetAddress ip = mockAddrWithIp("33.32.33.33");
InetAddress ip = createInetAddress("33.32.33.33");
AsyncPlayerPreLoginEvent event = spy(new AsyncPlayerPreLoginEvent(name, ip, uniqueId));
given(validationService.isUnrestricted(name)).willReturn(false);
FailedVerificationException exception = new FailedVerificationException(
Expand Down Expand Up @@ -1107,9 +1108,11 @@ private static void verifyNoModifyingCalls(AsyncPlayerPreLoginEvent event) {
verifyNoMoreInteractions(event);
}

private static InetAddress mockAddrWithIp(String ip) {
InetAddress addr = mock(InetAddress.class);
given(addr.getHostAddress()).willReturn(ip);
return addr;
public static InetAddress createInetAddress(String ip) {
try {
return InetAddress.getByName(ip);
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Invalid IP address: " + ip, e);
}
}
}

0 comments on commit e06445b

Please sign in to comment.