Skip to content

Commit 18c2b38

Browse files
committed
Improve unit tests for server ping
1 parent 1912a9c commit 18c2b38

File tree

5 files changed

+87
-46
lines changed

5 files changed

+87
-46
lines changed

src/main/java/com/comphenix/protocol/wrappers/WrappedServerPing.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,15 @@ public static WrappedServerPing fromJson(String json) {
112112
* @return The message of the day.
113113
*/
114114
public WrappedChatComponent getMotD() {
115-
Object handle = impl.getMotD();
116-
return handle != null ? WrappedChatComponent.fromHandle(handle) : null;
115+
return impl.getMotD();
117116
}
118117

119118
/**
120119
* Set the message of the day.
121120
* @param description - message of the day.
122121
*/
123122
public void setMotD(WrappedChatComponent description) {
124-
impl.setMotD(description != null ? description.getHandle() : null);
123+
impl.setMotD(description);
125124
}
126125

127126
/**

src/main/java/com/comphenix/protocol/wrappers/ping/LegacyServerPing.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,17 @@ public static LegacyServerPing fromJson(String json) {
137137
* @return The message of the day.
138138
*/
139139
@Override
140-
public Object getMotD() {
141-
return DESCRIPTION.get(handle);
140+
public WrappedChatComponent getMotD() {
141+
return WrappedChatComponent.fromHandle(DESCRIPTION.get(handle));
142142
}
143143

144144
/**
145145
* Set the message of the day.
146146
* @param description - message of the day.
147147
*/
148148
@Override
149-
public void setMotD(Object description) {
150-
DESCRIPTION.set(handle, description);
149+
public void setMotD(WrappedChatComponent description) {
150+
DESCRIPTION.set(handle, description != null ? description.getHandle() : null);
151151
}
152152

153153
/**
@@ -365,11 +365,11 @@ public void setVersionProtocol(int protocol) {
365365
*/
366366
public LegacyServerPing deepClone() {
367367
LegacyServerPing copy = new LegacyServerPing();
368-
Object motd = getMotD();
368+
WrappedChatComponent motd = getMotD();
369369

370370
copy.setPlayers(getPlayers());
371371
copy.setFavicon(getFavicon());
372-
copy.setMotD(motd != null ? WrappedChatComponent.fromHandle(motd).getHandle() : null);
372+
copy.setMotD(motd != null ? motd : null);
373373
copy.setVersionName(getVersionName());
374374
copy.setVersionProtocol(getVersionProtocol());
375375

src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import java.util.Optional;
44

5+
import com.comphenix.protocol.wrappers.WrappedChatComponent;
56
import com.comphenix.protocol.wrappers.WrappedGameProfile;
67

78
import com.google.common.collect.ImmutableList;
89

910
public interface ServerPingImpl extends Cloneable {
10-
Object getMotD();
11-
void setMotD(Object description);
11+
WrappedChatComponent getMotD();
12+
void setMotD(WrappedChatComponent description);
1213
int getPlayersMaximum();
1314
void setPlayersMaximum(int maxPlayers);
1415
int getPlayersOnline();

src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingRecord.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Favicon() {
106106

107107
private static AutoWrapper<Favicon> FAVICON_WRAPPER;
108108

109-
private Object description;
109+
private WrappedChatComponent description;
110110
private PlayerSample playerSample;
111111
private ServerData serverData;
112112
private Favicon favicon;
@@ -163,12 +163,12 @@ public ServerPingRecord() {
163163
}
164164

165165
@Override
166-
public Object getMotD() {
166+
public WrappedChatComponent getMotD() {
167167
return description;
168168
}
169169

170170
@Override
171-
public void setMotD(Object description) {
171+
public void setMotD(WrappedChatComponent description) {
172172
this.description = description;
173173
}
174174

@@ -279,7 +279,9 @@ public void setPlayersVisible(boolean visible) {
279279

280280
@Override
281281
public Object getHandle() {
282-
Object descHandle = description != null ? description : DEFAULT_DESCRIPTION;
282+
WrappedChatComponent wrappedDescription = description != null ? description : DEFAULT_DESCRIPTION;
283+
Object descHandle = wrappedDescription.getHandle();
284+
283285
Optional<Object> playersHandle = Optional.ofNullable(playerSample != null ? SAMPLE_WRAPPER.unwrap(playerSample) : null);
284286
Optional<Object> versionHandle = Optional.ofNullable(serverData != null ? DATA_WRAPPER.unwrap(serverData) : null);
285287
Optional<Object> favHandle = Optional.ofNullable(favicon != null ? FAVICON_WRAPPER.unwrap(favicon) : null);
Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.comphenix.protocol.wrappers;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4-
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertTrue;
6-
import static org.junit.jupiter.api.Assertions.fail;
3+
import java.io.IOException;
4+
import java.util.Optional;
75

86
import com.comphenix.protocol.BukkitInitialization;
7+
import com.comphenix.protocol.PacketType;
8+
import com.comphenix.protocol.events.PacketContainer;
9+
import com.comphenix.protocol.utility.MinecraftProtocolVersion;
910
import com.comphenix.protocol.wrappers.WrappedServerPing.CompressedImage;
1011
import com.google.common.io.Resources;
1112
import org.junit.jupiter.api.BeforeAll;
1213
import org.junit.jupiter.api.Test;
1314
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
1415

16+
import static org.junit.jupiter.api.Assertions.*;
17+
1518
public class WrappedServerPingTest {
1619

1720
@BeforeAll
@@ -20,32 +23,68 @@ public static void initializeBukkit() {
2023
}
2124

2225
@Test
23-
public void test() {
24-
try {
25-
CompressedImage tux = CompressedImage.fromPng(Resources.getResource("tux.png").openStream());
26-
byte[] original = tux.getDataCopy();
27-
28-
WrappedServerPing serverPing = new WrappedServerPing();
29-
serverPing.setMotD("Hello, this is a test.");
30-
serverPing.setPlayersOnline(5);
31-
serverPing.setPlayersMaximum(10);
32-
serverPing.setVersionName("Minecraft 123");
33-
serverPing.setVersionProtocol(4);
34-
serverPing.setFavicon(tux);
35-
serverPing.setEnforceSecureChat(true);
36-
37-
assertEquals(5, serverPing.getPlayersOnline());
38-
assertEquals(10, serverPing.getPlayersMaximum());
39-
assertEquals("Minecraft 123", serverPing.getVersionName());
40-
assertEquals(4, serverPing.getVersionProtocol());
41-
assertTrue(serverPing.isEnforceSecureChat());
42-
43-
assertArrayEquals(original, serverPing.getFavicon().getData());
44-
45-
CompressedImage copy = CompressedImage.fromBase64Png(Base64Coder.encodeLines(tux.getData()));
46-
assertArrayEquals(copy.getData(), serverPing.getFavicon().getData());
47-
} catch (Throwable ex) {
48-
fail("Encountered an exception testing ServerPing", ex);
49-
}
26+
public void fullTest() throws IOException {
27+
PacketContainer packet = new PacketContainer(PacketType.Status.Server.SERVER_INFO);
28+
Optional<WrappedServerPing> optionalPing = packet.getServerPings().optionRead(0);
29+
assertTrue(optionalPing.isPresent());
30+
31+
WrappedServerPing serverPing = optionalPing.get();
32+
assertNotNull(serverPing.getMotD());
33+
assertNotNull(serverPing.getFavicon());
34+
assertNotNull(serverPing.getPlayers());
35+
assertNotNull(serverPing.getVersionName());
36+
37+
CompressedImage tux = CompressedImage.fromPng(Resources.getResource("tux.png").openStream());
38+
byte[] original = tux.getDataCopy();
39+
40+
serverPing.setMotD("Hello, this is a test.");
41+
serverPing.setPlayersOnline(5);
42+
serverPing.setPlayersMaximum(10);
43+
serverPing.setVersionName("Minecraft 123");
44+
serverPing.setVersionProtocol(4);
45+
serverPing.setFavicon(tux);
46+
serverPing.setEnforceSecureChat(true);
47+
48+
packet.getServerPings().write(0, serverPing);
49+
50+
WrappedServerPing roundTrip = packet.getServerPings().read(0);
51+
52+
assertEquals(5, roundTrip.getPlayersOnline());
53+
assertEquals(10, roundTrip.getPlayersMaximum());
54+
assertEquals("Minecraft 123", roundTrip.getVersionName());
55+
assertEquals(4, roundTrip.getVersionProtocol());
56+
assertTrue(roundTrip.isEnforceSecureChat());
57+
58+
assertArrayEquals(original, roundTrip.getFavicon().getData());
59+
60+
CompressedImage copy = CompressedImage.fromBase64Png(Base64Coder.encodeLines(tux.getData()));
61+
assertArrayEquals(copy.getData(), roundTrip.getFavicon().getData());
62+
}
63+
64+
@Test
65+
public void testDefaultData() {
66+
PacketContainer packet = new PacketContainer(PacketType.Status.Server.SERVER_INFO);
67+
packet.getServerPings().write(0, new WrappedServerPing());
68+
69+
WrappedServerPing serverPing = packet.getServerPings().read(0);
70+
assertEquals(serverPing.getMotD(), WrappedChatComponent.fromLegacyText("A Minecraft Server"));
71+
assertEquals(serverPing.getVersionProtocol(), MinecraftProtocolVersion.getCurrentVersion());
72+
}
73+
74+
@Test
75+
public void testSetPartialData() {
76+
PacketContainer packet = new PacketContainer(PacketType.Status.Server.SERVER_INFO);
77+
78+
WrappedServerPing serverPing = new WrappedServerPing();
79+
serverPing.setPlayersOnline(69);
80+
serverPing.setPlayersMaximum(420);
81+
82+
packet.getServerPings().write(0, serverPing);
83+
84+
WrappedServerPing roundTrip = packet.getServerPings().read(0);
85+
assertEquals(roundTrip.getMotD(), WrappedChatComponent.fromLegacyText("A Minecraft Server"));
86+
assertEquals(roundTrip.getVersionProtocol(), MinecraftProtocolVersion.getCurrentVersion());
87+
assertEquals(roundTrip.getPlayersOnline(), 69);
88+
assertEquals(roundTrip.getPlayersMaximum(), 420);
5089
}
5190
}

0 commit comments

Comments
 (0)