Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PluginManagerTest.java
1 package org.bukkit.plugin;
2 
3 import static org.hamcrest.Matchers.*;
4 import static org.junit.Assert.*;
5 
6 import org.bukkit.TestServer;
7 import org.bukkit.event.Event;
10 
11 import org.junit.After;
12 import org.junit.Test;
13 
14 public class PluginManagerTest {
15  private class MutableObject {
16  volatile Object value = null;
17  }
18 
19  private static final PluginManager pm = TestServer.getInstance().getPluginManager();
20 
21  private final MutableObject store = new MutableObject();
22 
23  @Test
24  public void testAsyncSameThread() {
25  final Event event = new TestEvent(true);
26  try {
27  pm.callEvent(event);
28  } catch (IllegalStateException ex) {
29  assertThat(event.getEventName() + " cannot be triggered asynchronously from primary server thread.", is(ex.getMessage()));
30  return;
31  }
32  throw new IllegalStateException("No exception thrown");
33  }
34 
35  @Test
36  public void testSyncSameThread() {
37  final Event event = new TestEvent(false);
38  pm.callEvent(event);
39  }
40 
41  @Test
42  public void testAsyncLocked() throws InterruptedException {
43  final Event event = new TestEvent(true);
44  Thread secondThread = new Thread(
45  new Runnable() {
46  public void run() {
47  try {
48  synchronized (pm) {
49  pm.callEvent(event);
50  }
51  } catch (Throwable ex) {
52  store.value = ex;
53  }
54  }
55  }
56  );
57  secondThread.start();
58  secondThread.join();
59  assertThat(store.value, is(instanceOf(IllegalStateException.class)));
60  assertThat(event.getEventName() + " cannot be triggered asynchronously from inside synchronized code.", is(((Throwable) store.value).getMessage()));
61  }
62 
63  @Test
64  public void testAsyncUnlocked() throws InterruptedException {
65  final Event event = new TestEvent(true);
66  Thread secondThread = new Thread(
67  new Runnable() {
68  public void run() {
69  try {
70  pm.callEvent(event);
71  } catch (Throwable ex) {
72  store.value = ex;
73  }
74  }});
75  secondThread.start();
76  secondThread.join();
77  if (store.value != null) {
78  throw new RuntimeException((Throwable) store.value);
79  }
80  }
81 
82  @Test
83  public void testSyncUnlocked() throws InterruptedException {
84  final Event event = new TestEvent(false);
85  Thread secondThread = new Thread(
86  new Runnable() {
87  public void run() {
88  try {
89  pm.callEvent(event);
90  } catch (Throwable ex) {
91  store.value = ex;
92  }
93  }
94  }
95  );
96  secondThread.start();
97  secondThread.join();
98  if (store.value != null) {
99  throw new RuntimeException((Throwable) store.value);
100  }
101  }
102 
103  @Test
104  public void testSyncLocked() throws InterruptedException {
105  final Event event = new TestEvent(false);
106  Thread secondThread = new Thread(
107  new Runnable() {
108  public void run() {
109  try {
110  synchronized (pm) {
111  pm.callEvent(event);
112  }
113  } catch (Throwable ex) {
114  store.value = ex;
115  }
116  }
117  }
118  );
119  secondThread.start();
120  secondThread.join();
121  if (store.value != null) {
122  throw new RuntimeException((Throwable) store.value);
123  }
124  }
125 
126  @Test
127  public void testRemovePermissionByNameLower() {
128  this.testRemovePermissionByName("lower");
129  }
130 
131  @Test
132  public void testRemovePermissionByNameUpper() {
133  this.testRemovePermissionByName("UPPER");
134  }
135 
136  @Test
137  public void testRemovePermissionByNameCamel() {
138  this.testRemovePermissionByName("CaMeL");
139  }
140 
141  public void testRemovePermissionByPermissionLower() {
142  this.testRemovePermissionByPermission("lower");
143  }
144 
145  @Test
146  public void testRemovePermissionByPermissionUpper() {
147  this.testRemovePermissionByPermission("UPPER");
148  }
149 
150  @Test
151  public void testRemovePermissionByPermissionCamel() {
152  this.testRemovePermissionByPermission("CaMeL");
153  }
154 
155  private void testRemovePermissionByName(final String name) {
156  final Permission perm = new Permission(name);
157  pm.addPermission(perm);
158  assertThat("Permission \"" + name + "\" was not added", pm.getPermission(name), is(perm));
159  pm.removePermission(name);
160  assertThat("Permission \"" + name + "\" was not removed", pm.getPermission(name), is(nullValue()));
161  }
162 
163  private void testRemovePermissionByPermission(final String name) {
164  final Permission perm = new Permission(name);
165  pm.addPermission(perm);
166  assertThat("Permission \"" + name + "\" was not added", pm.getPermission(name), is(perm));
167  pm.removePermission(perm);
168  assertThat("Permission \"" + name + "\" was not removed", pm.getPermission(name), is(nullValue()));
169  }
170 
171  @After
172  public void tearDown() {
173  pm.clearPlugins();
174  assertThat(pm.getPermissions(), is(empty()));
175  }
176 }
PluginManager getPluginManager()
Set< Permission > getPermissions()
void removePermission(Permission perm)
void addPermission(Permission perm)
Permission getPermission(String name)