Search in sources :

Example 1 with SendMethod

use of at.pcgamingfreaks.Bungee.Message.Sender.SendMethod in project Bukkit_Bungee_PluginLib by GeorgH93.

the class MessageTest method testSend.

@Test
public void testSend() throws Exception {
    int sendMessageCalls = 0;
    int doSendCalls = 0;
    Message message = new Message("");
    CommandSender mockedCommandSender = mock(CommandSender.class);
    ProxiedPlayer mockedProxiedPlayer = mock(ProxiedPlayer.class);
    Field method = Message.class.getDeclaredField("method");
    method.setAccessible(true);
    ChatSender mockedSender = mock(ChatSender.class);
    doNothing().when(mockedSender).doSend(any(ProxiedPlayer.class), anyString(), anyObject());
    SendMethod sendMethod = (SendMethod) method.get(message);
    Field defaultSender = sendMethod.getClass().getDeclaredField("defaultSender");
    defaultSender.setAccessible(true);
    defaultSender.set(sendMethod, mockedSender);
    message.send(mockedCommandSender, (Object[]) null);
    // noinspection deprecation
    verify(mockedCommandSender, times(++sendMessageCalls)).sendMessage(anyString());
    message.send(mockedCommandSender);
    // noinspection deprecation
    verify(mockedCommandSender, times(++sendMessageCalls)).sendMessage(anyString());
    message.send(mockedCommandSender, "Test");
    // noinspection deprecation
    verify(mockedCommandSender, times(++sendMessageCalls)).sendMessage(anyString());
    message.send(mockedProxiedPlayer, (Object[]) null);
    verify(mockedSender, times(++doSendCalls)).doSend(any(ProxiedPlayer.class), anyString(), anyObject());
    message.send(mockedProxiedPlayer);
    verify(mockedSender, times(++doSendCalls)).doSend(any(ProxiedPlayer.class), anyString(), anyObject());
    message.send(mockedProxiedPlayer, "Test");
    verify(mockedSender, times(++doSendCalls)).doSend(any(ProxiedPlayer.class), anyString(), anyObject());
    doSendCalls = 0;
    List<ProxiedPlayer> proxiedPlayers = new ArrayList<>();
    message.send(proxiedPlayers);
    verify(mockedSender, times(doSendCalls)).doSend(anyCollectionOf(ProxiedPlayer.class), anyString(), anyObject());
    proxiedPlayers.add(mockedProxiedPlayer);
    message.send(proxiedPlayers, (Object[]) null);
    verify(mockedSender, times(++doSendCalls)).doSend(anyCollectionOf(ProxiedPlayer.class), anyString(), anyObject());
    message.send(proxiedPlayers);
    verify(mockedSender, times(++doSendCalls)).doSend(anyCollectionOf(ProxiedPlayer.class), anyString(), anyObject());
    message.send(proxiedPlayers, "Test");
    verify(mockedSender, times(++doSendCalls)).doSend(anyCollectionOf(ProxiedPlayer.class), anyString(), anyObject());
    message.setSendMethod(null);
    message.send(mockedCommandSender);
    // noinspection deprecation
    verify(mockedCommandSender, times(sendMessageCalls)).sendMessage(anyString());
    message.send(proxiedPlayers);
    verify(mockedSender, times(doSendCalls)).doSend(anyCollectionOf(ProxiedPlayer.class), anyString(), anyObject());
    defaultSender.setAccessible(false);
    method.setAccessible(false);
}
Also used : ProxiedPlayer(net.md_5.bungee.api.connection.ProxiedPlayer) Field(java.lang.reflect.Field) ChatSender(at.pcgamingfreaks.Bungee.Message.Sender.ChatSender) SendMethod(at.pcgamingfreaks.Bungee.Message.Sender.SendMethod) ArrayList(java.util.ArrayList) CommandSender(net.md_5.bungee.api.CommandSender) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with SendMethod

use of at.pcgamingfreaks.Bungee.Message.Sender.SendMethod in project Bukkit_Bungee_PluginLib by GeorgH93.

the class MessageTest method testBroadcast.

@PrepareForTest({ ProxyServer.class })
@Test
public void testBroadcast() throws NoSuchFieldException, IllegalAccessException {
    int sendMessageCalls = 0;
    int doBroadcastCalls = 0;
    Message message = new Message("");
    Field method = Message.class.getDeclaredField("method");
    method.setAccessible(true);
    ChatSender mockedSender = mock(ChatSender.class);
    doNothing().when(mockedSender).doBroadcast(anyString(), anyObject());
    SendMethod sendMethod = (SendMethod) method.get(message);
    Field defaultSender = sendMethod.getClass().getDeclaredField("defaultSender");
    defaultSender.setAccessible(true);
    defaultSender.set(sendMethod, mockedSender);
    CommandSender mockedCommandSender = mock(CommandSender.class);
    // noinspection deprecation
    doNothing().when(mockedCommandSender).sendMessage(anyString());
    ProxyServer mockedProxyServer = mock(ProxyServer.class);
    when(mockedProxyServer.getConsole()).thenReturn(mockedCommandSender);
    Field proxyServer = ProxyServer.class.getDeclaredField("instance");
    proxyServer.setAccessible(true);
    proxyServer.set(mockedProxyServer, mockedProxyServer);
    message.broadcast("Test");
    // noinspection deprecation
    verify(mockedCommandSender, times(++sendMessageCalls)).sendMessage(anyString());
    verify(mockedSender, times(++doBroadcastCalls)).doBroadcast(anyString(), anyObject());
    message.broadcast((Object[]) null);
    // noinspection deprecation
    verify(mockedCommandSender, times(++sendMessageCalls)).sendMessage(anyString());
    verify(mockedSender, times(++doBroadcastCalls)).doBroadcast(anyString(), anyObject());
    message.broadcast();
    // noinspection deprecation
    verify(mockedCommandSender, times(++sendMessageCalls)).sendMessage(anyString());
    verify(mockedSender, times(++doBroadcastCalls)).doBroadcast(anyString(), anyObject());
    message.setSendMethod(null);
    message.broadcast();
    // noinspection deprecation
    verify(mockedCommandSender, times(sendMessageCalls)).sendMessage(anyString());
    verify(mockedSender, times(doBroadcastCalls)).doBroadcast(anyString(), anyObject());
    defaultSender.setAccessible(false);
    proxyServer.setAccessible(false);
    method.setAccessible(false);
}
Also used : Field(java.lang.reflect.Field) ChatSender(at.pcgamingfreaks.Bungee.Message.Sender.ChatSender) SendMethod(at.pcgamingfreaks.Bungee.Message.Sender.SendMethod) CommandSender(net.md_5.bungee.api.CommandSender) ProxyServer(net.md_5.bungee.api.ProxyServer) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ChatSender (at.pcgamingfreaks.Bungee.Message.Sender.ChatSender)2 SendMethod (at.pcgamingfreaks.Bungee.Message.Sender.SendMethod)2 Field (java.lang.reflect.Field)2 CommandSender (net.md_5.bungee.api.CommandSender)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ArrayList (java.util.ArrayList)1 ProxyServer (net.md_5.bungee.api.ProxyServer)1 ProxiedPlayer (net.md_5.bungee.api.connection.ProxiedPlayer)1