Paper Plugins
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
package org.bukkit.event;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.PluginLoader;
|
||||
import org.bukkit.plugin.SimplePluginManager;
|
||||
import org.bukkit.plugin.TestPlugin;
|
||||
import org.bukkit.plugin.java.JavaPluginLoader;
|
||||
import org.bukkit.support.AbstractTestingBase;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SyntheticEventTest extends AbstractTestingBase {
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void test() {
|
||||
final JavaPluginLoader loader = new JavaPluginLoader(Bukkit.getServer());
|
||||
TestPlugin plugin = new TestPlugin(getClass().getName()) {
|
||||
@Override
|
||||
public PluginLoader getPluginLoader() {
|
||||
return loader;
|
||||
}
|
||||
};
|
||||
SimplePluginManager pluginManager = new SimplePluginManager(Bukkit.getServer(), null);
|
||||
|
||||
TestEvent event = new TestEvent(false);
|
||||
Impl impl = new Impl();
|
||||
|
||||
pluginManager.registerEvents(impl, plugin);
|
||||
pluginManager.callEvent(event);
|
||||
|
||||
assertEquals(1, impl.callCount);
|
||||
}
|
||||
|
||||
public abstract static class Base<E extends Event> implements Listener {
|
||||
int callCount = 0;
|
||||
|
||||
public void accept(E evt) {
|
||||
callCount++;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Impl extends Base<TestEvent> {
|
||||
@Override
|
||||
@EventHandler
|
||||
public void accept(TestEvent evt) {
|
||||
super.accept(evt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
package org.bukkit.plugin;
|
||||
|
||||
import static org.bukkit.support.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.TestEvent;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.support.AbstractTestingBase;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class PluginManagerTest extends AbstractTestingBase {
|
||||
private class MutableObject {
|
||||
volatile Object value = null;
|
||||
}
|
||||
|
||||
private static final PluginManager pm = Bukkit.getServer().getPluginManager();
|
||||
|
||||
private final MutableObject store = new MutableObject();
|
||||
|
||||
@Test
|
||||
public void testAsyncSameThread() {
|
||||
final Event event = new TestEvent(true);
|
||||
try {
|
||||
pm.callEvent(event);
|
||||
} catch (IllegalStateException ex) {
|
||||
assertThat(event.getEventName() + " cannot be triggered asynchronously from primary server thread.", is(ex.getMessage()));
|
||||
return;
|
||||
}
|
||||
throw new IllegalStateException("No exception thrown");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSyncSameThread() {
|
||||
final Event event = new TestEvent(false);
|
||||
pm.callEvent(event);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsyncLocked() throws InterruptedException {
|
||||
final Event event = new TestEvent(true);
|
||||
Thread secondThread = new Thread(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
synchronized (pm) {
|
||||
pm.callEvent(event);
|
||||
}
|
||||
} catch (Throwable ex) {
|
||||
store.value = ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
secondThread.start();
|
||||
secondThread.join();
|
||||
assertThat(store.value, is(instanceOf(IllegalStateException.class)));
|
||||
assertThat(event.getEventName() + " cannot be triggered asynchronously from inside synchronized code.", is(((Throwable) store.value).getMessage()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsyncUnlocked() throws InterruptedException {
|
||||
final Event event = new TestEvent(true);
|
||||
Thread secondThread = new Thread(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
pm.callEvent(event);
|
||||
} catch (Throwable ex) {
|
||||
store.value = ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
secondThread.start();
|
||||
secondThread.join();
|
||||
if (store.value != null) {
|
||||
throw new RuntimeException((Throwable) store.value);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSyncUnlocked() throws InterruptedException {
|
||||
final Event event = new TestEvent(false);
|
||||
Thread secondThread = new Thread(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
pm.callEvent(event);
|
||||
} catch (Throwable ex) {
|
||||
store.value = ex;
|
||||
assertThat(event.getEventName() + " cannot be triggered asynchronously from another thread.", is(ex.getMessage()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
secondThread.start();
|
||||
secondThread.join();
|
||||
if (store.value == null) {
|
||||
throw new IllegalStateException("No exception thrown");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSyncLocked() throws InterruptedException {
|
||||
final Event event = new TestEvent(false);
|
||||
Thread secondThread = new Thread(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
synchronized (pm) {
|
||||
pm.callEvent(event);
|
||||
}
|
||||
} catch (Throwable ex) {
|
||||
store.value = ex;
|
||||
assertThat(event.getEventName() + " cannot be triggered asynchronously from another thread.", is(ex.getMessage()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
secondThread.start();
|
||||
secondThread.join();
|
||||
if (store.value == null) {
|
||||
throw new IllegalStateException("No exception thrown");
|
||||
}
|
||||
}
|
||||
|
||||
@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(pm.getPermission(name), is(perm), "Permission \"" + name + "\" was not added");
|
||||
pm.removePermission(name);
|
||||
assertThat(pm.getPermission(name), is(nullValue()), "Permission \"" + name + "\" was not removed");
|
||||
}
|
||||
|
||||
private void testRemovePermissionByPermission(final String name) {
|
||||
final Permission perm = new Permission(name);
|
||||
pm.addPermission(perm);
|
||||
assertThat(pm.getPermission(name), is(perm), "Permission \"" + name + "\" was not added");
|
||||
pm.removePermission(perm);
|
||||
assertThat(pm.getPermission(name), is(nullValue()), "Permission \"" + name + "\" was not removed");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
pm.clearPlugins();
|
||||
assertThat(pm.getPermissions(), is(empty()));
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,12 @@ public class TestPlugin extends PluginBase {
|
||||
public PluginDescriptionFile getDescription() {
|
||||
return new PluginDescriptionFile(pluginName, "1.0", "test.test");
|
||||
}
|
||||
// Paper start
|
||||
@Override
|
||||
public io.papermc.paper.plugin.configuration.PluginMeta getPluginMeta() {
|
||||
return getDescription();
|
||||
}
|
||||
// Paper end
|
||||
|
||||
@Override
|
||||
public FileConfiguration getConfig() {
|
||||
|
||||
@@ -27,8 +27,7 @@ public final class TestServer {
|
||||
Thread creatingThread = Thread.currentThread();
|
||||
when(instance.isPrimaryThread()).then(mock -> Thread.currentThread().equals(creatingThread));
|
||||
|
||||
PluginManager pluginManager = new SimplePluginManager(instance, new SimpleCommandMap(instance));
|
||||
when(instance.getPluginManager()).thenReturn(pluginManager);
|
||||
// Paper - remove plugin manager for Paper Plugins
|
||||
|
||||
Logger logger = Logger.getLogger(TestServer.class.getCanonicalName());
|
||||
when(instance.getLogger()).thenReturn(logger);
|
||||
|
||||
Reference in New Issue
Block a user