Skip to content

Commit

Permalink
Fix tests failing on 1.16 and 1.17 because those versions didn't impl…
Browse files Browse the repository at this point in the history
…ement certain plugin messaging related methods
  • Loading branch information
willkroboth committed Sep 22, 2023
1 parent 0163561 commit 701d881
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.plugin.messaging.Messenger;
import org.bukkit.plugin.messaging.StandardMessenger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -158,6 +160,15 @@ public WorldMock addSimpleWorld(String name) {
// return MockPlatform.getInstance().getItemFactory();
// }

// 1.16 and 1.17 MockServers do not implement this method, but other versions do
// Easiest to just always override this method
// This is copied from MockBukkit-v1.18
private final StandardMessenger messenger = new StandardMessenger();
@Override
public @NotNull Messenger getMessenger() {
return messenger;
}

// Advancements

List<Advancement> advancements = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package dev.jorel.commandapi.test.network;

import be.seeseemelk.mockbukkit.entity.PlayerMock;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.network.*;
import dev.jorel.commandapi.test.MockPlatform;
import dev.jorel.commandapi.test.TestBase;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.messaging.StandardMessenger;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.exceptions.base.MockitoException;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.*;

public class NetworkTestBase extends TestBase {

Expand Down Expand Up @@ -61,6 +64,23 @@ public void tearDown() {
/***************************************************
* Utility methods for sending and receiving bytes *
***************************************************/

public PlayerMock getPluginMessagingPlayer(String name) {
PlayerMock player = spy(new PlayerMock(server, name));

// PlayerMock#sendPluginMessage is not implemented on versions 1.16 or 1.17
// Easiest to always override the call
doAnswer(invocation -> {
Plugin source = invocation.getArgument(0);
String channel = invocation.getArgument(1);
byte[] message = invocation.getArgument(2);
StandardMessenger.validatePluginMessage(server.getMessenger(), source, channel, message);
return null;
}).when(player).sendPluginMessage(any(), any(), any());

return player;
}

public byte[] getSentBytes(Player target, CommandAPIPacket packet) {
CommandAPIBukkit.get().getMessenger().sendPacket(target, packet);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void tearDown() {

@Test
void sendTestWithProtocolVersionTooOldPacket() {
PlayerMock player = server.addPlayer();
PlayerMock player = getPluginMessagingPlayer("player");

// Packet is encoded as id, VarInt protocol version, then String for the reason inside
assertArrayEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public int getProtocolVersion(Player player) {

@Test
void sendReceiveTestWithSetVersionPacket() {
PlayerMock player = server.addPlayer(); // Protocol version currently 0
PlayerMock player = getPluginMessagingPlayer("player"); // Protocol version currently 0

assertEquals(0, getProtocolVersion(player));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public void tearDown() {

@Test
void sendReceiveTestWithUpdateRequirementsPacket() {
PlayerMock player = Mockito.spy(new PlayerMock(server, "player"));
PlayerMock player = getPluginMessagingPlayer("player");
// Interrupt normal calls to updateCommands, because MockPlayer throws an UnimplementedOperationException
// getPluginMessagingPlayer already returns a mocked spy, so we don't have to spy ourselves
Mockito.doNothing().when(player).updateCommands();
server.addPlayer(player);

// Protocol version currently 0

Expand Down

0 comments on commit 701d881

Please sign in to comment.