1
1
package com .comphenix .protocol .wrappers ;
2
2
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 ;
7
5
8
6
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 ;
9
10
import com .comphenix .protocol .wrappers .WrappedServerPing .CompressedImage ;
10
11
import com .google .common .io .Resources ;
11
12
import org .junit .jupiter .api .BeforeAll ;
12
13
import org .junit .jupiter .api .Test ;
13
14
import org .yaml .snakeyaml .external .biz .base64Coder .Base64Coder ;
14
15
16
+ import static org .junit .jupiter .api .Assertions .*;
17
+
15
18
public class WrappedServerPingTest {
16
19
17
20
@ BeforeAll
@@ -20,32 +23,68 @@ public static void initializeBukkit() {
20
23
}
21
24
22
25
@ 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 );
50
89
}
51
90
}
0 commit comments