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

View File

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

View File

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

View File

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