Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
StandardMessengerTest.java
1 package org.bukkit.plugin.messaging;
2 
3 import org.bukkit.entity.Player;
5 import java.util.Collection;
6 import org.junit.Test;
7 import static org.junit.Assert.*;
8 import static org.hamcrest.CoreMatchers.*;
9 
10 public class StandardMessengerTest {
11  public StandardMessenger getMessenger() {
12  return new StandardMessenger();
13  }
14 
15  private int count = 0;
16  public TestPlugin getPlugin() {
17  return new TestPlugin("" + count++);
18  }
19 
20  @Test
21  public void testIsReservedChannel() {
22  Messenger messenger = getMessenger();
23 
24  assertTrue(messenger.isReservedChannel("REGISTER"));
25  assertFalse(messenger.isReservedChannel("register"));
26  assertTrue(messenger.isReservedChannel("UNREGISTER"));
27  assertFalse(messenger.isReservedChannel("unregister"));
28  assertFalse(messenger.isReservedChannel("notReserved"));
29  }
30 
31  @Test
32  public void testRegisterAndUnregisterOutgoingPluginChannel() {
33  Messenger messenger = getMessenger();
34  TestPlugin plugin = getPlugin();
35 
36  assertFalse(messenger.isOutgoingChannelRegistered(plugin, "foo"));
37  messenger.registerOutgoingPluginChannel(plugin, "foo");
38  assertTrue(messenger.isOutgoingChannelRegistered(plugin, "foo"));
39  assertFalse(messenger.isOutgoingChannelRegistered(plugin, "bar"));
40 
41  messenger.unregisterOutgoingPluginChannel(plugin, "foo");
42  assertFalse(messenger.isOutgoingChannelRegistered(plugin, "foo"));
43  }
44 
45  @Test(expected = ReservedChannelException.class)
46  public void testReservedOutgoingRegistration() {
47  Messenger messenger = getMessenger();
48  TestPlugin plugin = getPlugin();
49 
50  messenger.registerOutgoingPluginChannel(plugin, "REGISTER");
51  }
52 
53  @Test
54  public void testUnregisterOutgoingPluginChannel_Plugin() {
55  Messenger messenger = getMessenger();
56  TestPlugin plugin = getPlugin();
57 
58  assertFalse(messenger.isOutgoingChannelRegistered(plugin, "foo"));
59  messenger.registerOutgoingPluginChannel(plugin, "foo");
60  messenger.registerOutgoingPluginChannel(plugin, "bar");
61  assertTrue(messenger.isOutgoingChannelRegistered(plugin, "foo"));
62  assertTrue(messenger.isOutgoingChannelRegistered(plugin, "bar"));
63 
64  messenger.unregisterOutgoingPluginChannel(plugin);
65  assertFalse(messenger.isOutgoingChannelRegistered(plugin, "foo"));
66  assertFalse(messenger.isOutgoingChannelRegistered(plugin, "bar"));
67  }
68 
69  @Test
70  public void testRegisterIncomingPluginChannel() {
71  Messenger messenger = getMessenger();
72  TestPlugin plugin = getPlugin();
73  TestMessageListener listener = new TestMessageListener("foo", "bar".getBytes());
74  Player player = TestPlayer.getInstance();
75  PluginMessageListenerRegistration registration = messenger.registerIncomingPluginChannel(plugin, "foo", listener);
76 
77  assertTrue(registration.isValid());
78  assertTrue(messenger.isIncomingChannelRegistered(plugin, "foo"));
79  messenger.dispatchIncomingMessage(player, "foo", "bar".getBytes());
80  assertTrue(listener.hasReceived());
81 
82  messenger.unregisterIncomingPluginChannel(plugin, "foo", listener);
83  listener.reset();
84 
85  assertFalse(registration.isValid());
86  assertFalse(messenger.isIncomingChannelRegistered(plugin, "foo"));
87  messenger.dispatchIncomingMessage(player, "foo", "bar".getBytes());
88  assertFalse(listener.hasReceived());
89  }
90 
91  @Test(expected = ReservedChannelException.class)
92  public void testReservedIncomingRegistration() {
93  Messenger messenger = getMessenger();
94  TestPlugin plugin = getPlugin();
95 
96  messenger.registerIncomingPluginChannel(plugin, "REGISTER", new TestMessageListener("foo", "bar".getBytes()));
97  }
98 
99  @Test(expected = IllegalArgumentException.class)
100  public void testDuplicateIncomingRegistration() {
101  Messenger messenger = getMessenger();
102  TestPlugin plugin = getPlugin();
103  TestMessageListener listener = new TestMessageListener("foo", "bar".getBytes());
104 
105  messenger.registerIncomingPluginChannel(plugin, "baz", listener);
106  messenger.registerIncomingPluginChannel(plugin, "baz", listener);
107  }
108 
109  @Test
110  public void testUnregisterIncomingPluginChannel_Plugin_String() {
111  Messenger messenger = getMessenger();
112  TestPlugin plugin = getPlugin();
113  TestMessageListener listener1 = new TestMessageListener("foo", "bar".getBytes());
114  TestMessageListener listener2 = new TestMessageListener("baz", "qux".getBytes());
115  Player player = TestPlayer.getInstance();
116  PluginMessageListenerRegistration registration1 = messenger.registerIncomingPluginChannel(plugin, "foo", listener1);
117  PluginMessageListenerRegistration registration2 = messenger.registerIncomingPluginChannel(plugin, "baz", listener2);
118 
119  assertTrue(registration1.isValid());
120  assertTrue(registration2.isValid());
121  messenger.dispatchIncomingMessage(player, "foo", "bar".getBytes());
122  messenger.dispatchIncomingMessage(player, "baz", "qux".getBytes());
123  assertTrue(listener1.hasReceived());
124  assertTrue(listener2.hasReceived());
125 
126  messenger.unregisterIncomingPluginChannel(plugin, "foo");
127  listener1.reset();
128  listener2.reset();
129 
130  assertFalse(registration1.isValid());
131  assertTrue(registration2.isValid());
132  messenger.dispatchIncomingMessage(player, "foo", "bar".getBytes());
133  messenger.dispatchIncomingMessage(player, "baz", "qux".getBytes());
134  assertFalse(listener1.hasReceived());
135  assertTrue(listener2.hasReceived());
136  }
137 
138  @Test
139  public void testUnregisterIncomingPluginChannel_Plugin() {
140  Messenger messenger = getMessenger();
141  TestPlugin plugin = getPlugin();
142  TestMessageListener listener1 = new TestMessageListener("foo", "bar".getBytes());
143  TestMessageListener listener2 = new TestMessageListener("baz", "qux".getBytes());
144  Player player = TestPlayer.getInstance();
145  PluginMessageListenerRegistration registration1 = messenger.registerIncomingPluginChannel(plugin, "foo", listener1);
146  PluginMessageListenerRegistration registration2 = messenger.registerIncomingPluginChannel(plugin, "baz", listener2);
147 
148  assertTrue(registration1.isValid());
149  assertTrue(registration2.isValid());
150  messenger.dispatchIncomingMessage(player, "foo", "bar".getBytes());
151  messenger.dispatchIncomingMessage(player, "baz", "qux".getBytes());
152  assertTrue(listener1.hasReceived());
153  assertTrue(listener2.hasReceived());
154 
155  messenger.unregisterIncomingPluginChannel(plugin);
156  listener1.reset();
157  listener2.reset();
158 
159  assertFalse(registration1.isValid());
160  assertFalse(registration2.isValid());
161  messenger.dispatchIncomingMessage(player, "foo", "bar".getBytes());
162  messenger.dispatchIncomingMessage(player, "baz", "qux".getBytes());
163  assertFalse(listener1.hasReceived());
164  assertFalse(listener2.hasReceived());
165  }
166 
167  @Test
168  public void testGetOutgoingChannels() {
169  Messenger messenger = getMessenger();
170  TestPlugin plugin1 = getPlugin();
171  TestPlugin plugin2 = getPlugin();
172 
173  assertEquals(messenger.getOutgoingChannels());
174 
175  messenger.registerOutgoingPluginChannel(plugin1, "foo");
176  messenger.registerOutgoingPluginChannel(plugin1, "bar");
177  messenger.registerOutgoingPluginChannel(plugin2, "baz");
178  messenger.registerOutgoingPluginChannel(plugin2, "baz");
179 
180  assertEquals(messenger.getOutgoingChannels(), "foo", "bar", "baz");
181  }
182 
183  @Test
184  public void testGetOutgoingChannels_Plugin() {
185  Messenger messenger = getMessenger();
186  TestPlugin plugin1 = getPlugin();
187  TestPlugin plugin2 = getPlugin();
188  TestPlugin plugin3 = getPlugin();
189 
190  messenger.registerOutgoingPluginChannel(plugin1, "foo");
191  messenger.registerOutgoingPluginChannel(plugin1, "bar");
192  messenger.registerOutgoingPluginChannel(plugin2, "baz");
193  messenger.registerOutgoingPluginChannel(plugin2, "qux");
194 
195  assertEquals(messenger.getOutgoingChannels(plugin1), "foo", "bar");
196  assertEquals(messenger.getOutgoingChannels(plugin2), "baz", "qux");
197  assertEquals(messenger.getOutgoingChannels(plugin3));
198  }
199 
200  @Test
201  public void testGetIncomingChannels() {
202  Messenger messenger = getMessenger();
203  TestPlugin plugin1 = getPlugin();
204  TestPlugin plugin2 = getPlugin();
205 
206  assertEquals(messenger.getIncomingChannels());
207 
208  messenger.registerIncomingPluginChannel(plugin1, "foo", new TestMessageListener("foo", "bar".getBytes()));
209  messenger.registerIncomingPluginChannel(plugin1, "bar", new TestMessageListener("foo", "bar".getBytes()));
210  messenger.registerIncomingPluginChannel(plugin2, "baz", new TestMessageListener("foo", "bar".getBytes()));
211  messenger.registerIncomingPluginChannel(plugin2, "baz", new TestMessageListener("foo", "bar".getBytes()));
212 
213  assertEquals(messenger.getIncomingChannels(), "foo", "bar", "baz");
214  }
215 
216  @Test
217  public void testGetIncomingChannels_Plugin() {
218  Messenger messenger = getMessenger();
219  TestPlugin plugin1 = getPlugin();
220  TestPlugin plugin2 = getPlugin();
221  TestPlugin plugin3 = getPlugin();
222 
223  messenger.registerIncomingPluginChannel(plugin1, "foo", new TestMessageListener("foo", "bar".getBytes()));
224  messenger.registerIncomingPluginChannel(plugin1, "bar", new TestMessageListener("foo", "bar".getBytes()));
225  messenger.registerIncomingPluginChannel(plugin2, "baz", new TestMessageListener("foo", "bar".getBytes()));
226  messenger.registerIncomingPluginChannel(plugin2, "qux", new TestMessageListener("foo", "bar".getBytes()));
227 
228  assertEquals(messenger.getIncomingChannels(plugin1), "foo", "bar");
229  assertEquals(messenger.getIncomingChannels(plugin2), "baz", "qux");
230  assertEquals(messenger.getIncomingChannels(plugin3));
231  }
232 
233  @Test
234  public void testGetIncomingChannelRegistrations_Plugin() {
235  Messenger messenger = getMessenger();
236  TestPlugin plugin1 = getPlugin();
237  TestPlugin plugin2 = getPlugin();
238  TestPlugin plugin3 = getPlugin();
239  PluginMessageListenerRegistration registration1 = messenger.registerIncomingPluginChannel(plugin1, "foo", new TestMessageListener("foo", "bar".getBytes()));
240  PluginMessageListenerRegistration registration2 = messenger.registerIncomingPluginChannel(plugin1, "bar", new TestMessageListener("foo", "bar".getBytes()));
241  PluginMessageListenerRegistration registration3 = messenger.registerIncomingPluginChannel(plugin2, "baz", new TestMessageListener("foo", "bar".getBytes()));
242  PluginMessageListenerRegistration registration4 = messenger.registerIncomingPluginChannel(plugin2, "qux", new TestMessageListener("foo", "bar".getBytes()));
243 
244  assertEquals(messenger.getIncomingChannelRegistrations(plugin1), registration1, registration2);
245  assertEquals(messenger.getIncomingChannelRegistrations(plugin2), registration3, registration4);
246  assertEquals(messenger.getIncomingChannels(plugin3));
247  }
248 
249  @Test
250  public void testGetIncomingChannelRegistrations_String() {
251  Messenger messenger = getMessenger();
252  TestPlugin plugin1 = getPlugin();
253  TestPlugin plugin2 = getPlugin();
254  PluginMessageListenerRegistration registration1 = messenger.registerIncomingPluginChannel(plugin1, "foo", new TestMessageListener("foo", "bar".getBytes()));
255  PluginMessageListenerRegistration registration2 = messenger.registerIncomingPluginChannel(plugin1, "bar", new TestMessageListener("foo", "bar".getBytes()));
256  PluginMessageListenerRegistration registration3 = messenger.registerIncomingPluginChannel(plugin2, "foo", new TestMessageListener("foo", "bar".getBytes()));
257  PluginMessageListenerRegistration registration4 = messenger.registerIncomingPluginChannel(plugin2, "bar", new TestMessageListener("foo", "bar".getBytes()));
258 
259  assertEquals(messenger.getIncomingChannelRegistrations("foo"), registration1, registration3);
260  assertEquals(messenger.getIncomingChannelRegistrations("bar"), registration2, registration4);
261  assertEquals(messenger.getIncomingChannelRegistrations("baz"));
262  }
263 
264  @Test
265  public void testGetIncomingChannelRegistrations_Plugin_String() {
266  Messenger messenger = getMessenger();
267  TestPlugin plugin1 = getPlugin();
268  TestPlugin plugin2 = getPlugin();
269  TestPlugin plugin3 = getPlugin();
270  PluginMessageListenerRegistration registration1 = messenger.registerIncomingPluginChannel(plugin1, "foo", new TestMessageListener("foo", "bar".getBytes()));
271  PluginMessageListenerRegistration registration2 = messenger.registerIncomingPluginChannel(plugin1, "foo", new TestMessageListener("foo", "bar".getBytes()));
272  PluginMessageListenerRegistration registration3 = messenger.registerIncomingPluginChannel(plugin1, "bar", new TestMessageListener("foo", "bar".getBytes()));
273  PluginMessageListenerRegistration registration4 = messenger.registerIncomingPluginChannel(plugin2, "bar", new TestMessageListener("foo", "bar".getBytes()));
274  PluginMessageListenerRegistration registration5 = messenger.registerIncomingPluginChannel(plugin2, "baz", new TestMessageListener("foo", "bar".getBytes()));
275  PluginMessageListenerRegistration registration6 = messenger.registerIncomingPluginChannel(plugin2, "baz", new TestMessageListener("foo", "bar".getBytes()));
276 
277  assertEquals(messenger.getIncomingChannelRegistrations(plugin1, "foo"), registration1, registration2);
278  assertEquals(messenger.getIncomingChannelRegistrations(plugin1, "bar"), registration3);
279  assertEquals(messenger.getIncomingChannelRegistrations(plugin2, "bar"), registration4);
280  assertEquals(messenger.getIncomingChannelRegistrations(plugin2, "baz"), registration5, registration6);
281  assertEquals(messenger.getIncomingChannelRegistrations(plugin1, "baz"));
282  assertEquals(messenger.getIncomingChannelRegistrations(plugin3, "qux"));
283  }
284 
285  private static <T> void assertEquals(Collection<T> actual, T... expected) {
286  assertThat("Size of the array", actual.size(), is(expected.length));
287  assertThat(actual, hasItems(expected));
288  }
289 }
boolean isReservedChannel(String channel)
Set< PluginMessageListenerRegistration > getIncomingChannelRegistrations(Plugin plugin)
boolean isOutgoingChannelRegistered(Plugin plugin, String channel)
boolean isIncomingChannelRegistered(Plugin plugin, String channel)
PluginMessageListenerRegistration registerIncomingPluginChannel(Plugin plugin, String channel, PluginMessageListener listener)
void dispatchIncomingMessage(Player source, String channel, byte[] message)
void registerOutgoingPluginChannel(Plugin plugin, String channel)
void unregisterIncomingPluginChannel(Plugin plugin, String channel, PluginMessageListener listener)
void unregisterOutgoingPluginChannel(Plugin plugin, String channel)