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);
}
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);
}
Aggregations