mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-24 03:05:17 +01:00
Verify only one listener per event; #367 verify listener not using MONITOR
This commit is contained in:
parent
f1557b29c3
commit
f835a95bac
@ -2,13 +2,16 @@ package fr.xephi.authme.listener;
|
|||||||
|
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.EventPriority;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.not;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
@ -41,6 +44,27 @@ public final class ListenerConsistencyTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #367: Event listeners with EventPriority.MONITOR should not change events
|
||||||
|
@Test
|
||||||
|
public void shouldNotHaveMonitorLevelEventHandlers() {
|
||||||
|
for (Class<?> listener : LISTENERS) {
|
||||||
|
verifyListenerIsNotUsingMonitorPriority(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotHaveMultipleHandlersForSameEvent() {
|
||||||
|
Set<String> events = new HashSet<>();
|
||||||
|
for (Class<?> listener : LISTENERS) {
|
||||||
|
for (Method method : listener.getDeclaredMethods()) {
|
||||||
|
if (events.contains(method.getName())) {
|
||||||
|
fail("More than one method '" + method.getName() + "' exists (e.g. class: " + listener + ")");
|
||||||
|
}
|
||||||
|
events.add(method.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void checkCanceledAttribute(Class<?> listenerClass) {
|
private static void checkCanceledAttribute(Class<?> listenerClass) {
|
||||||
final String clazz = listenerClass.getSimpleName();
|
final String clazz = listenerClass.getSimpleName();
|
||||||
Method[] methods = listenerClass.getDeclaredMethods();
|
Method[] methods = listenerClass.getDeclaredMethods();
|
||||||
@ -67,6 +91,17 @@ public final class ListenerConsistencyTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void verifyListenerIsNotUsingMonitorPriority(Class<?> listenerClass) {
|
||||||
|
final String clazz = listenerClass.getSimpleName();
|
||||||
|
for (Method method : listenerClass.getDeclaredMethods()) {
|
||||||
|
if (isTestableMethod(method) && method.isAnnotationPresent(EventHandler.class)) {
|
||||||
|
EventHandler eventHandlerAnnotation = method.getAnnotation(EventHandler.class);
|
||||||
|
assertThat("Method " + clazz + "#" + method.getName() + " does not use EventPriority.MONITOR",
|
||||||
|
eventHandlerAnnotation.priority(), not(equalTo(EventPriority.MONITOR)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean isTestableMethod(Method method) {
|
private static boolean isTestableMethod(Method method) {
|
||||||
// A method like "access$000" is created by the compiler when a private member is being accessed by an inner
|
// A method like "access$000" is created by the compiler when a private member is being accessed by an inner
|
||||||
// class, so we need to ignore such methods
|
// class, so we need to ignore such methods
|
||||||
|
Loading…
Reference in New Issue
Block a user