Test PluginManager.removePermission
Static methods are death to testability. However, irrelevant static methods can be negotiated with until a later time in which they can be removed. When instantiating a new Permission object, static calls are made to the Bukkit class during a recalculatePermissibles logic path. This recalculatePermissibles call should probably be moved appropriately, but until the time such testing can be accomplished itself, these tests work around that situation by simply verifying the static Bukkit server references are satisfied since what is called as a result is irrelevant currently. This commit also updates a few other tests for PluginManagerTest to work towards the standard of using the Hamcrest unit testing library. By: EdGruberman <ed@rjump.com>
This commit is contained in:
@ -1,12 +1,14 @@
|
||||
package org.bukkit.plugin;
|
||||
|
||||
import static junit.framework.Assert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.TestServer;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.TestEvent;
|
||||
import org.bukkit.permissions.Permission;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PluginManagerTest {
|
||||
@ -14,10 +16,9 @@ public class PluginManagerTest {
|
||||
volatile Object value = null;
|
||||
}
|
||||
|
||||
final Server server = TestServer.getInstance();
|
||||
final SimpleCommandMap commandMap = new SimpleCommandMap(server);
|
||||
final PluginManager pm = new SimplePluginManager(server, commandMap);
|
||||
final MutableObject store = new MutableObject();
|
||||
private static final PluginManager pm = TestServer.getInstance().getPluginManager();
|
||||
|
||||
private final MutableObject store = new MutableObject();
|
||||
|
||||
@Test
|
||||
public void testAsyncSameThread() {
|
||||
@ -25,7 +26,7 @@ public class PluginManagerTest {
|
||||
try {
|
||||
pm.callEvent(event);
|
||||
} catch (IllegalStateException ex) {
|
||||
assertEquals(event.getEventName() + " cannot be triggered asynchronously from primary server thread.", ex.getMessage());
|
||||
assertThat(event.getEventName() + " cannot be triggered asynchronously from primary server thread.", is(ex.getMessage()));
|
||||
return;
|
||||
}
|
||||
throw new IllegalStateException("No exception thrown");
|
||||
@ -53,8 +54,8 @@ public class PluginManagerTest {
|
||||
}});
|
||||
secondThread.start();
|
||||
secondThread.join();
|
||||
assertTrue(store.value instanceof IllegalStateException);
|
||||
assertEquals(event.getEventName() + " cannot be triggered asynchronously from inside synchronized code.", ((Throwable) store.value).getMessage());
|
||||
assertThat(store.value, is(instanceOf(IllegalStateException.class)));
|
||||
assertThat(event.getEventName() + " cannot be triggered asynchronously from inside synchronized code.", is(((Throwable) store.value).getMessage()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -115,4 +116,55 @@ public class PluginManagerTest {
|
||||
throw new RuntimeException((Throwable) store.value);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemovePermissionByNameLower() {
|
||||
this.testRemovePermissionByName("lower");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemovePermissionByNameUpper() {
|
||||
this.testRemovePermissionByName("UPPER");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemovePermissionByNameCamel() {
|
||||
this.testRemovePermissionByName("CaMeL");
|
||||
}
|
||||
|
||||
public void testRemovePermissionByPermissionLower() {
|
||||
this.testRemovePermissionByPermission("lower");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemovePermissionByPermissionUpper() {
|
||||
this.testRemovePermissionByPermission("UPPER");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemovePermissionByPermissionCamel() {
|
||||
this.testRemovePermissionByPermission("CaMeL");
|
||||
}
|
||||
|
||||
private void testRemovePermissionByName(final String name) {
|
||||
final Permission perm = new Permission(name);
|
||||
pm.addPermission(perm);
|
||||
assertThat("Permission \"" + name + "\" was not added", pm.getPermission(name), is(perm));
|
||||
pm.removePermission(name);
|
||||
assertThat("Permission \"" + name + "\" was not removed", pm.getPermission(name), is(nullValue()));
|
||||
}
|
||||
|
||||
private void testRemovePermissionByPermission(final String name) {
|
||||
final Permission perm = new Permission(name);
|
||||
pm.addPermission(perm);
|
||||
assertThat("Permission \"" + name + "\" was not added", pm.getPermission(name), is(perm));
|
||||
pm.removePermission(perm);
|
||||
assertThat("Permission \"" + name + "\" was not removed", pm.getPermission(name), is(nullValue()));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
pm.clearPlugins();
|
||||
assertThat(pm.getPermissions(), is(empty()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user