StandardMessenger implements Messenger { public PluginMessageListenerRegistration registerIncomingPluginChannel(Plugin plugin, String channel, PluginMessageListener listener) { if (plugin == null) { throw new IllegalArgumentException("Plugin cannot be null"); } validateChannel(channel); if (isReservedChannel(channel)) { throw new ReservedChannelException(channel); } if (listener == null) { throw new IllegalArgumentException("Listener cannot be null"); } PluginMessageListenerRegistration result = new PluginMessageListenerRegistration(this, plugin, channel, listener); addToIncoming(result); return result; }  boolean isReservedChannel(String channel); void registerOutgoingPluginChannel(Plugin plugin, String channel); void unregisterOutgoingPluginChannel(Plugin plugin, String channel); void unregisterOutgoingPluginChannel(Plugin plugin); PluginMessageListenerRegistration registerIncomingPluginChannel(Plugin plugin, String channel, PluginMessageListener listener); void unregisterIncomingPluginChannel(Plugin plugin, String channel, PluginMessageListener listener); void unregisterIncomingPluginChannel(Plugin plugin, String channel); void unregisterIncomingPluginChannel(Plugin plugin); Set<String> getOutgoingChannels(); Set<String> getOutgoingChannels(Plugin plugin); Set<String> getIncomingChannels(); Set<String> getIncomingChannels(Plugin plugin); Set<PluginMessageListenerRegistration> getIncomingChannelRegistrations(Plugin plugin); Set<PluginMessageListenerRegistration> getIncomingChannelRegistrations(String channel); Set<PluginMessageListenerRegistration> getIncomingChannelRegistrations(Plugin plugin, String channel); boolean isRegistrationValid(PluginMessageListenerRegistration registration); boolean isIncomingChannelRegistered(Plugin plugin, String channel); boolean isOutgoingChannelRegistered(Plugin plugin, String channel); void dispatchIncomingMessage(Player source, String channel, byte[] message); static void validateChannel(String channel); static void validatePluginMessage(Messenger messenger, Plugin source, String channel, byte[] message);  }
@Test public void testRegisterIncomingPluginChannel() { Messenger messenger = getMessenger(); TestPlugin plugin = getPlugin(); TestMessageListener listener = new TestMessageListener("foo", "bar".getBytes()); Player player = TestPlayer.getInstance(); PluginMessageListenerRegistration registration = messenger.registerIncomingPluginChannel(plugin, "foo", listener); assertTrue(registration.isValid()); assertTrue(messenger.isIncomingChannelRegistered(plugin, "foo")); messenger.dispatchIncomingMessage(player, "foo", "bar".getBytes()); assertTrue(listener.hasReceived()); messenger.unregisterIncomingPluginChannel(plugin, "foo", listener); listener.reset(); assertFalse(registration.isValid()); assertFalse(messenger.isIncomingChannelRegistered(plugin, "foo")); messenger.dispatchIncomingMessage(player, "foo", "bar".getBytes()); assertFalse(listener.hasReceived()); }
@Test(expected = ReservedChannelException.class) public void testReservedIncomingRegistration() { Messenger messenger = getMessenger(); TestPlugin plugin = getPlugin(); messenger.registerIncomingPluginChannel(plugin, "REGISTER", new TestMessageListener("foo", "bar".getBytes())); }
@Test(expected = IllegalArgumentException.class) public void testDuplicateIncomingRegistration() { Messenger messenger = getMessenger(); TestPlugin plugin = getPlugin(); TestMessageListener listener = new TestMessageListener("foo", "bar".getBytes()); messenger.registerIncomingPluginChannel(plugin, "baz", listener); messenger.registerIncomingPluginChannel(plugin, "baz", listener); }