Improve unit tests for server ping

This commit is contained in:
Dan Mulloy 2023-03-26 13:58:46 -05:00
parent 1912a9c871
commit 18c2b389a4
No known key found for this signature in database
GPG Key ID: E3B02DE32FB04AC1
5 changed files with 83 additions and 42 deletions

View File

@ -112,8 +112,7 @@ public class WrappedServerPing implements ClonableWrapper {
* @return The message of the day.
*/
public WrappedChatComponent getMotD() {
Object handle = impl.getMotD();
return handle != null ? WrappedChatComponent.fromHandle(handle) : null;
return impl.getMotD();
}
/**
@ -121,7 +120,7 @@ public class WrappedServerPing implements ClonableWrapper {
* @param description - message of the day.
*/
public void setMotD(WrappedChatComponent description) {
impl.setMotD(description != null ? description.getHandle() : null);
impl.setMotD(description);
}
/**

View File

@ -137,8 +137,8 @@ public final class LegacyServerPing extends AbstractWrapper implements ServerPin
* @return The message of the day.
*/
@Override
public Object getMotD() {
return DESCRIPTION.get(handle);
public WrappedChatComponent getMotD() {
return WrappedChatComponent.fromHandle(DESCRIPTION.get(handle));
}
/**
@ -146,8 +146,8 @@ public final class LegacyServerPing extends AbstractWrapper implements ServerPin
* @param description - message of the day.
*/
@Override
public void setMotD(Object description) {
DESCRIPTION.set(handle, description);
public void setMotD(WrappedChatComponent description) {
DESCRIPTION.set(handle, description != null ? description.getHandle() : null);
}
/**
@ -365,11 +365,11 @@ public final class LegacyServerPing extends AbstractWrapper implements ServerPin
*/
public LegacyServerPing deepClone() {
LegacyServerPing copy = new LegacyServerPing();
Object motd = getMotD();
WrappedChatComponent motd = getMotD();
copy.setPlayers(getPlayers());
copy.setFavicon(getFavicon());
copy.setMotD(motd != null ? WrappedChatComponent.fromHandle(motd).getHandle() : null);
copy.setMotD(motd != null ? motd : null);
copy.setVersionName(getVersionName());
copy.setVersionProtocol(getVersionProtocol());

View File

@ -2,13 +2,14 @@ package com.comphenix.protocol.wrappers.ping;
import java.util.Optional;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.google.common.collect.ImmutableList;
public interface ServerPingImpl extends Cloneable {
Object getMotD();
void setMotD(Object description);
WrappedChatComponent getMotD();
void setMotD(WrappedChatComponent description);
int getPlayersMaximum();
void setPlayersMaximum(int maxPlayers);
int getPlayersOnline();

View File

@ -106,7 +106,7 @@ public final class ServerPingRecord implements ServerPingImpl {
private static AutoWrapper<Favicon> FAVICON_WRAPPER;
private Object description;
private WrappedChatComponent description;
private PlayerSample playerSample;
private ServerData serverData;
private Favicon favicon;
@ -163,12 +163,12 @@ public final class ServerPingRecord implements ServerPingImpl {
}
@Override
public Object getMotD() {
public WrappedChatComponent getMotD() {
return description;
}
@Override
public void setMotD(Object description) {
public void setMotD(WrappedChatComponent description) {
this.description = description;
}
@ -279,7 +279,9 @@ public final class ServerPingRecord implements ServerPingImpl {
@Override
public Object getHandle() {
Object descHandle = description != null ? description : DEFAULT_DESCRIPTION;
WrappedChatComponent wrappedDescription = description != null ? description : DEFAULT_DESCRIPTION;
Object descHandle = wrappedDescription.getHandle();
Optional<Object> playersHandle = Optional.ofNullable(playerSample != null ? SAMPLE_WRAPPER.unwrap(playerSample) : null);
Optional<Object> versionHandle = Optional.ofNullable(serverData != null ? DATA_WRAPPER.unwrap(serverData) : null);
Optional<Object> favHandle = Optional.ofNullable(favicon != null ? FAVICON_WRAPPER.unwrap(favicon) : null);

View File

@ -1,17 +1,20 @@
package com.comphenix.protocol.wrappers;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.util.Optional;
import com.comphenix.protocol.BukkitInitialization;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.utility.MinecraftProtocolVersion;
import com.comphenix.protocol.wrappers.WrappedServerPing.CompressedImage;
import com.google.common.io.Resources;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import static org.junit.jupiter.api.Assertions.*;
public class WrappedServerPingTest {
@BeforeAll
@ -20,32 +23,68 @@ public class WrappedServerPingTest {
}
@Test
public void test() {
try {
CompressedImage tux = CompressedImage.fromPng(Resources.getResource("tux.png").openStream());
byte[] original = tux.getDataCopy();
public void fullTest() throws IOException {
PacketContainer packet = new PacketContainer(PacketType.Status.Server.SERVER_INFO);
Optional<WrappedServerPing> optionalPing = packet.getServerPings().optionRead(0);
assertTrue(optionalPing.isPresent());
WrappedServerPing serverPing = new WrappedServerPing();
serverPing.setMotD("Hello, this is a test.");
serverPing.setPlayersOnline(5);
serverPing.setPlayersMaximum(10);
serverPing.setVersionName("Minecraft 123");
serverPing.setVersionProtocol(4);
serverPing.setFavicon(tux);
serverPing.setEnforceSecureChat(true);
WrappedServerPing serverPing = optionalPing.get();
assertNotNull(serverPing.getMotD());
assertNotNull(serverPing.getFavicon());
assertNotNull(serverPing.getPlayers());
assertNotNull(serverPing.getVersionName());
assertEquals(5, serverPing.getPlayersOnline());
assertEquals(10, serverPing.getPlayersMaximum());
assertEquals("Minecraft 123", serverPing.getVersionName());
assertEquals(4, serverPing.getVersionProtocol());
assertTrue(serverPing.isEnforceSecureChat());
CompressedImage tux = CompressedImage.fromPng(Resources.getResource("tux.png").openStream());
byte[] original = tux.getDataCopy();
assertArrayEquals(original, serverPing.getFavicon().getData());
serverPing.setMotD("Hello, this is a test.");
serverPing.setPlayersOnline(5);
serverPing.setPlayersMaximum(10);
serverPing.setVersionName("Minecraft 123");
serverPing.setVersionProtocol(4);
serverPing.setFavicon(tux);
serverPing.setEnforceSecureChat(true);
CompressedImage copy = CompressedImage.fromBase64Png(Base64Coder.encodeLines(tux.getData()));
assertArrayEquals(copy.getData(), serverPing.getFavicon().getData());
} catch (Throwable ex) {
fail("Encountered an exception testing ServerPing", ex);
}
packet.getServerPings().write(0, serverPing);
WrappedServerPing roundTrip = packet.getServerPings().read(0);
assertEquals(5, roundTrip.getPlayersOnline());
assertEquals(10, roundTrip.getPlayersMaximum());
assertEquals("Minecraft 123", roundTrip.getVersionName());
assertEquals(4, roundTrip.getVersionProtocol());
assertTrue(roundTrip.isEnforceSecureChat());
assertArrayEquals(original, roundTrip.getFavicon().getData());
CompressedImage copy = CompressedImage.fromBase64Png(Base64Coder.encodeLines(tux.getData()));
assertArrayEquals(copy.getData(), roundTrip.getFavicon().getData());
}
@Test
public void testDefaultData() {
PacketContainer packet = new PacketContainer(PacketType.Status.Server.SERVER_INFO);
packet.getServerPings().write(0, new WrappedServerPing());
WrappedServerPing serverPing = packet.getServerPings().read(0);
assertEquals(serverPing.getMotD(), WrappedChatComponent.fromLegacyText("A Minecraft Server"));
assertEquals(serverPing.getVersionProtocol(), MinecraftProtocolVersion.getCurrentVersion());
}
@Test
public void testSetPartialData() {
PacketContainer packet = new PacketContainer(PacketType.Status.Server.SERVER_INFO);
WrappedServerPing serverPing = new WrappedServerPing();
serverPing.setPlayersOnline(69);
serverPing.setPlayersMaximum(420);
packet.getServerPings().write(0, serverPing);
WrappedServerPing roundTrip = packet.getServerPings().read(0);
assertEquals(roundTrip.getMotD(), WrappedChatComponent.fromLegacyText("A Minecraft Server"));
assertEquals(roundTrip.getVersionProtocol(), MinecraftProtocolVersion.getCurrentVersion());
assertEquals(roundTrip.getPlayersOnline(), 69);
assertEquals(roundTrip.getPlayersMaximum(), 420);
}
}