Search in sources :

Example 1 with ProxyServer

use of net.md_5.bungee.api.ProxyServer in project Bukkit_Bungee_PluginLib by GeorgH93.

the class TestObjects method initProxyServer.

public static void initProxyServer() throws NoSuchFieldException, IllegalAccessException {
    initPlayers();
    ProxyServer mockedProxyServer = mock(ProxyServer.class);
    when(mockedProxyServer.getPlayers()).thenReturn(getPlayers());
    Field instance = ProxyServer.class.getDeclaredField("instance");
    instance.setAccessible(true);
    instance.set(mockedProxyServer, mockedProxyServer);
    instance.setAccessible(false);
}
Also used : Field(java.lang.reflect.Field) ProxyServer(net.md_5.bungee.api.ProxyServer)

Example 2 with ProxyServer

use of net.md_5.bungee.api.ProxyServer in project Bukkit_Bungee_PluginLib by GeorgH93.

the class TestObjects method initMockedPlugin.

public static void initMockedPlugin() {
    File mockedFile = mock(File.class);
    when(mockedFile.getParentFile()).thenReturn(new File(""));
    when(mockedFile.getName()).thenReturn("FileName");
    TaskScheduler mockedTaskScheduler = mock(TaskScheduler.class);
    when(mockedTaskScheduler.runAsync(any(Plugin.class), any(Runnable.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((Runnable) invocationOnMock.getArguments()[1]).run();
            return null;
        }
    });
    ProxyServer mockedProxyServer = mock(ProxyServer.class);
    when(mockedProxyServer.getPluginsFolder()).thenReturn(new File(""));
    when(mockedProxyServer.getScheduler()).thenReturn(mockedTaskScheduler);
    PluginDescription mockedPluginDescription = mock(PluginDescription.class);
    when(mockedPluginDescription.getName()).thenReturn("TestPlugin");
    when(mockedPluginDescription.getVersion()).thenReturn("1.0");
    when(mockedPluginDescription.getFile()).thenReturn(mockedFile);
    mockedPlugin = mock(Plugin.class);
    when(mockedPlugin.getProxy()).thenReturn(mockedProxyServer);
    when(mockedPlugin.getDescription()).thenReturn(mockedPluginDescription);
    when(mockedPlugin.getLogger()).thenReturn(Logger.getLogger("TestLogger"));
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) TaskScheduler(net.md_5.bungee.api.scheduler.TaskScheduler) File(java.io.File) PluginDescriptionFile(org.bukkit.plugin.PluginDescriptionFile) PluginDescription(net.md_5.bungee.api.plugin.PluginDescription) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) Plugin(net.md_5.bungee.api.plugin.Plugin) ProxyServer(net.md_5.bungee.api.ProxyServer)

Example 3 with ProxyServer

use of net.md_5.bungee.api.ProxyServer 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

ProxyServer (net.md_5.bungee.api.ProxyServer)3 Field (java.lang.reflect.Field)2 ChatSender (at.pcgamingfreaks.Bungee.Message.Sender.ChatSender)1 SendMethod (at.pcgamingfreaks.Bungee.Message.Sender.SendMethod)1 File (java.io.File)1 CommandSender (net.md_5.bungee.api.CommandSender)1 Plugin (net.md_5.bungee.api.plugin.Plugin)1 PluginDescription (net.md_5.bungee.api.plugin.PluginDescription)1 TaskScheduler (net.md_5.bungee.api.scheduler.TaskScheduler)1 PluginDescriptionFile (org.bukkit.plugin.PluginDescriptionFile)1 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1