From 18c2b389a432902f5def65970f0c4e450fabb03c Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sun, 26 Mar 2023 13:58:46 -0500 Subject: [PATCH] Improve unit tests for server ping --- .../protocol/wrappers/WrappedServerPing.java | 5 +- .../wrappers/ping/LegacyServerPing.java | 12 +-- .../wrappers/ping/ServerPingImpl.java | 5 +- .../wrappers/ping/ServerPingRecord.java | 10 +- .../wrappers/WrappedServerPingTest.java | 93 +++++++++++++------ 5 files changed, 83 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/comphenix/protocol/wrappers/WrappedServerPing.java b/src/main/java/com/comphenix/protocol/wrappers/WrappedServerPing.java index c2a12d72..fa058b2d 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/WrappedServerPing.java +++ b/src/main/java/com/comphenix/protocol/wrappers/WrappedServerPing.java @@ -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); } /** diff --git a/src/main/java/com/comphenix/protocol/wrappers/ping/LegacyServerPing.java b/src/main/java/com/comphenix/protocol/wrappers/ping/LegacyServerPing.java index 2b9d0256..08cca1b2 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/ping/LegacyServerPing.java +++ b/src/main/java/com/comphenix/protocol/wrappers/ping/LegacyServerPing.java @@ -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()); diff --git a/src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingImpl.java b/src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingImpl.java index fc940587..d8744d04 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingImpl.java +++ b/src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingImpl.java @@ -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(); diff --git a/src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingRecord.java b/src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingRecord.java index c0e91708..d4b543a2 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingRecord.java +++ b/src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingRecord.java @@ -106,7 +106,7 @@ public final class ServerPingRecord implements ServerPingImpl { private static AutoWrapper 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 playersHandle = Optional.ofNullable(playerSample != null ? SAMPLE_WRAPPER.unwrap(playerSample) : null); Optional versionHandle = Optional.ofNullable(serverData != null ? DATA_WRAPPER.unwrap(serverData) : null); Optional favHandle = Optional.ofNullable(favicon != null ? FAVICON_WRAPPER.unwrap(favicon) : null); diff --git a/src/test/java/com/comphenix/protocol/wrappers/WrappedServerPingTest.java b/src/test/java/com/comphenix/protocol/wrappers/WrappedServerPingTest.java index 0e8c23c4..16be896a 100644 --- a/src/test/java/com/comphenix/protocol/wrappers/WrappedServerPingTest.java +++ b/src/test/java/com/comphenix/protocol/wrappers/WrappedServerPingTest.java @@ -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 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); } }