Remove mocks of InetAddress (which is a sealed class in JDK 19)

This commit is contained in:
ljacqu 2024-04-28 21:42:03 +02:00
parent 793efeda42
commit e06445be64

View File

@ -55,6 +55,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -65,12 +66,12 @@ import java.util.UUID;
import static com.google.common.collect.Sets.newHashSet; import static com.google.common.collect.Sets.newHashSet;
import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock; import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay; 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.containsInAnyOrder;
import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
@ -711,7 +712,7 @@ public class PlayerListenerTest {
UUID uniqueId = UUID.fromString("753493c9-33ba-4a4a-bf61-1bce9d3c9a71"); UUID uniqueId = UUID.fromString("753493c9-33ba-4a4a-bf61-1bce9d3c9a71");
String ip = "12.34.56.78"; 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); given(validationService.isUnrestricted(name)).willReturn(false);
// when // when
@ -749,7 +750,7 @@ public class PlayerListenerTest {
UUID uniqueId = UUID.fromString("753493c9-33ba-4a4a-bf61-1bce9d3c9a71"); UUID uniqueId = UUID.fromString("753493c9-33ba-4a4a-bf61-1bce9d3c9a71");
String ip = "12.34.56.78"; 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); given(validationService.isUnrestricted(name)).willReturn(false);
PlayerAuth auth = PlayerAuth.builder().name(name).build(); PlayerAuth auth = PlayerAuth.builder().name(name).build();
given(dataSource.getAuth(name)).willReturn(auth); given(dataSource.getAuth(name)).willReturn(auth);
@ -773,7 +774,7 @@ public class PlayerListenerTest {
Player player = mockPlayerWithName(name); Player player = mockPlayerWithName(name);
String ip = "12.34.56.78"; 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(validationService.isUnrestricted(name)).willReturn(false);
given(onJoinVerifier.refusePlayerForFullServer(loginEvent)).willReturn(false); given(onJoinVerifier.refusePlayerForFullServer(loginEvent)).willReturn(false);
@ -792,7 +793,7 @@ public class PlayerListenerTest {
// given // given
String name = "inval!dName"; String name = "inval!dName";
UUID uniqueId = UUID.fromString("753493c9-33ba-4a4a-bf61-1bce9d3c9a71"); 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)); AsyncPlayerPreLoginEvent event = spy(new AsyncPlayerPreLoginEvent(name, ip, uniqueId));
given(validationService.isUnrestricted(name)).willReturn(false); given(validationService.isUnrestricted(name)).willReturn(false);
FailedVerificationException exception = new FailedVerificationException( FailedVerificationException exception = new FailedVerificationException(
@ -1107,9 +1108,11 @@ public class PlayerListenerTest {
verifyNoMoreInteractions(event); verifyNoMoreInteractions(event);
} }
private static InetAddress mockAddrWithIp(String ip) { public static InetAddress createInetAddress(String ip) {
InetAddress addr = mock(InetAddress.class); try {
given(addr.getHostAddress()).willReturn(ip); return InetAddress.getByName(ip);
return addr; } catch (UnknownHostException e) {
throw new IllegalArgumentException("Invalid IP address: " + ip, e);
}
} }
} }