diff --git a/.travis.yml b/.travis.yml index 5051d49..9a05a85 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,38 @@ language: java -jdk: - - oraclejdk8 - - openjdk8 - - oraclejdk9 - - openjdk10 +dist: trusty + before_install: - chmod +x gradlew -script: -- ./gradlew test -- ./gradlew jacocoTestReport +before_script: +- wget https://github.com/sormuras/bach/raw/master/install-jdk.sh + +jobs: + include: + - stage: jdk.java.net - OpenJDK - GPL + env: JDK=8 + script: + - jdk_switcher use openjdk8 + - ./gradlew test + - ./gradlew jacocoTestReport + - # stage: ... + env: JDK=9 + script: + - source install-jdk.sh -C -F 9 + - ./gradlew test + - ./gradlew jacocoTestReport + - # stage: ... + env: JDK=10 + script: + - source install-jdk.sh -C -F 10 + - ./gradlew test + - ./gradlew jacocoTestReport + + after_success: -- bash <(curl -s https://codecov.io/bash) \ No newline at end of file +- bash <(curl -s https://codecov.io/bash) + +after_script: +- echo JAVA_HOME = ${JAVA_HOME} +- echo PATH = ${PATH} +- ls ${JAVA_HOME} +- java -version \ No newline at end of file diff --git a/LICENSE_HEADER b/LICENSE_HEADER index 6c8053b..ad0340b 100644 --- a/LICENSE_HEADER +++ b/LICENSE_HEADER @@ -1,4 +1,4 @@ - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index 1d8e006..0769409 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,25 @@ -![trident](/img/trident_logo.png) +# Trident [![Build Status](https://api.travis-ci.org/ClubObsidian/trident.svg?branch=master)](https://travis-ci.org/ClubObsidian/trident) [![build artifacts](https://jitpack.io/v/clubobsidian/trident.svg)](https://jitpack.io/#clubobsidian/trident) [![codecov](https://codecov.io/gh/ClubObsidian/trident/branch/master/graph/badge.svg)](https://codecov.io/gh/ClubObsidian/trident) +[![Known Vulnerabilities](https://snyk.io//test/github/ClubObsidian/trident/badge.svg?targetFile=build.gradle)](https://snyk.io//test/github/ClubObsidian/trident?targetFile=build.gradle) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) -[![discord](https://discordapp.com/api/guilds/482823104905609248/widget.png)](https://discord.gg/EY5Tq6r) -[Javadocs](https://jitpack.io/com/github/clubobsidian/trident/1.0.8/javadoc/) +[![Discord](https://img.shields.io/discord/482823104905609248.svg?logo=discord)](https://discord.gg/EY5Tq6r) +[![javadocs](https://img.shields.io/badge/Javadocs-2.0.0-success.svg)](https://jitpack.io/com/github/clubobsidian/trident/2.0.0/javadoc/) -A dead simpile annotation event system that allows different event executor implementations. +A dead simpile annotation-based event bus that allows different event executor implementations. Trident implements reflection and generated Javassist method executors. +## Inspiration + +Inspiration for Trident. + +* [Event4j](https://github.com/Techcable/Event4J) +* [Guava](https://github.com/google/guava) +* [mbassador](https://github.com/bennidi/mbassador) + ## Build Artifacts Build artifacts are hosted via [Jitpack.](https://jitpack.io/#clubobsidian/Trident/) @@ -24,7 +33,7 @@ repositories { maven { url 'https://jitpack.io' } } -compile 'com.github.clubobsidian:trident:1.1.0' +compile 'com.github.clubobsidian:trident:2.0.0' ``` ### Maven @@ -40,13 +49,13 @@ compile 'com.github.clubobsidian:trident:1.1.0' com.github.clubobsidian trident - 1.1.0 + 2.0.0 ``` ## Dependencies -* Javassist +* [Javassist](https://github.com/jboss-javassist/javassist) ## Development diff --git a/build.gradle b/build.gradle index c0b7df7..eb2af46 100644 --- a/build.gradle +++ b/build.gradle @@ -1,33 +1,32 @@ -buildscript { - repositories { - jcenter() - } - dependencies { - classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.4' - } +plugins { + id 'java' + id 'com.github.johnrengelman.shadow' version '5.0.0' + id 'jacoco' + id 'eclipse' + id 'idea' } -apply plugin: 'java' -apply plugin: 'com.github.johnrengelman.shadow' -apply plugin: 'jacoco' -apply plugin: 'eclipse' -apply plugin: 'idea' - - sourceCompatibility = 1.8 targetCompatibility = 1.8 +group = 'com.clubobsidian' +version = '2.0.0' + repositories { mavenCentral() jcenter() } +configurations { + shade + compile.extendsFrom shade +} + shadowJar { baseName = 'trident' classifier = null - version = null relocate 'javassist', 'com.clubobsidian.trident.javassist' } @@ -38,10 +37,11 @@ dependencies { } jacocoTestReport { - reports { - xml.enabled true - html.enabled false - } + reports { + xml.enabled true + html.enabled false + html.destination file("${buildDir}/jacocoHtml") + } } task javadocJar(type: Jar, dependsOn: javadoc) { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 16d2805..f4d7b2b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/com/clubobsidian/trident/Cancelable.java b/src/main/java/com/clubobsidian/trident/Cancelable.java index f5eefc1..ac8bcde 100644 --- a/src/main/java/com/clubobsidian/trident/Cancelable.java +++ b/src/main/java/com/clubobsidian/trident/Cancelable.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/main/java/com/clubobsidian/trident/Event.java b/src/main/java/com/clubobsidian/trident/Event.java index 68d4771..b6c209f 100644 --- a/src/main/java/com/clubobsidian/trident/Event.java +++ b/src/main/java/com/clubobsidian/trident/Event.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/main/java/com/clubobsidian/trident/EventManager.java b/src/main/java/com/clubobsidian/trident/EventBus.java similarity index 81% rename from src/main/java/com/clubobsidian/trident/EventManager.java rename to src/main/java/com/clubobsidian/trident/EventBus.java index 6958b74..09ef21d 100644 --- a/src/main/java/com/clubobsidian/trident/EventManager.java +++ b/src/main/java/com/clubobsidian/trident/EventBus.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -22,21 +22,22 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; +import com.clubobsidian.trident.event.DeadEvent; import com.clubobsidian.trident.util.ClassUtil; import com.clubobsidian.trident.util.EventDoublyLinkedList; import com.clubobsidian.trident.util.EventNode; /** - * Abstract class for implementing EventManager - * classes. For an example see @see com.clubobsidian.trident.impl.javaassist.JavaAssistEventManager + * Abstract class for implementing EventBus + * classes. For an example see @see com.clubobsidian.trident.impl.javaassist.JavaAssistEventBus * * @author virustotalop */ -public abstract class EventManager { +public abstract class EventBus { - private Map> registeredEventListeners; + private Map> registeredEventListeners; private Map, EventDoublyLinkedList> registeredExecutors; - public EventManager() + public EventBus() { this.registeredEventListeners = new ConcurrentHashMap<>(); this.registeredExecutors = new ConcurrentHashMap<>(); @@ -51,7 +52,13 @@ public boolean callEvent(final Event event) EventDoublyLinkedList executors = this.registeredExecutors.get(event.getClass()); if(executors == null) + { + if(!(event instanceof DeadEvent)) + { + this.callEvent(new DeadEvent(event)); + } return false; + } boolean ran = false; EventNode node = executors.getHead(); @@ -74,6 +81,7 @@ public boolean callEvent(final Event event) executor.execute(event); node = node.getNext(); } + return ran; } @@ -81,9 +89,13 @@ public boolean callEvent(final Event event) * @param listener listener to be registered * @return if the listener was registered */ - public boolean registerEvents(final Listener listener) + public boolean registerEvents(final Object listener) { - if(this.registeredEventListeners.keySet().contains(listener)) + if(listener == null) + { + return false; + } + else if(this.registeredEventListeners.keySet().contains(listener)) { return false; } @@ -109,6 +121,10 @@ public boolean registerEvents(final Listener listener) boolean ignoreCanceled = handler.ignoreCanceled(); MethodExecutor executor = this.createMethodExecutor(listener, method, ignoreCanceled); + + if(executor == null) + return false; + this.registeredExecutors.get(eventClass).insert(executor, handler.priority()); this.registeredEventListeners.get(listener).add(executor); } @@ -118,14 +134,14 @@ public boolean registerEvents(final Listener listener) return true; } - protected abstract MethodExecutor createMethodExecutor(Listener listener, Method method, boolean ignoreCanceled); + protected abstract MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCanceled); /** * * @param listener listener to be unregistered * @return if the listener was unregistered */ - public boolean unregisterEvents(Listener listener) + public boolean unregisterEvents(Object listener) { Queue executors = this.registeredEventListeners.remove(listener); if(executors == null) @@ -142,5 +158,4 @@ public boolean unregisterEvents(Listener listener) } return true; } - -} +} \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/EventHandler.java b/src/main/java/com/clubobsidian/trident/EventHandler.java index 19a2957..984c9c7 100644 --- a/src/main/java/com/clubobsidian/trident/EventHandler.java +++ b/src/main/java/com/clubobsidian/trident/EventHandler.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/main/java/com/clubobsidian/trident/EventPriority.java b/src/main/java/com/clubobsidian/trident/EventPriority.java index 936ef08..37384f1 100644 --- a/src/main/java/com/clubobsidian/trident/EventPriority.java +++ b/src/main/java/com/clubobsidian/trident/EventPriority.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/main/java/com/clubobsidian/trident/MethodExecutor.java b/src/main/java/com/clubobsidian/trident/MethodExecutor.java index c8b4f2c..8ab0be4 100644 --- a/src/main/java/com/clubobsidian/trident/MethodExecutor.java +++ b/src/main/java/com/clubobsidian/trident/MethodExecutor.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -26,10 +26,10 @@ */ public abstract class MethodExecutor { - private Listener listener; + private Object listener; private Method method; private boolean ignoreCanceled; - public MethodExecutor(Listener listener, Method method, boolean ignoreCanceled) + public MethodExecutor(Object listener, Method method, boolean ignoreCanceled) { this.listener = listener; this.method = method; @@ -39,7 +39,7 @@ public MethodExecutor(Listener listener, Method method, boolean ignoreCanceled) /** * @return listener to execute events on */ - public Listener getListener() + public Object getListener() { return this.listener; } diff --git a/src/main/java/com/clubobsidian/trident/impl/javaassist/JavaAssistEventManager.java b/src/main/java/com/clubobsidian/trident/event/DeadEvent.java similarity index 51% rename from src/main/java/com/clubobsidian/trident/impl/javaassist/JavaAssistEventManager.java rename to src/main/java/com/clubobsidian/trident/event/DeadEvent.java index e9bb2db..37ee383 100644 --- a/src/main/java/com/clubobsidian/trident/impl/javaassist/JavaAssistEventManager.java +++ b/src/main/java/com/clubobsidian/trident/event/DeadEvent.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,22 +13,32 @@ See the License for the specific language governing permissions and limitations under the License. */ -package com.clubobsidian.trident.impl.javaassist; +package com.clubobsidian.trident.event; -import java.lang.reflect.Method; - -import com.clubobsidian.trident.EventManager; -import com.clubobsidian.trident.Listener; -import com.clubobsidian.trident.MethodExecutor; +import com.clubobsidian.trident.Event; /** - * {@inheritDoc} + * Event that is called if an event is not + * listened to by a listener. + * Extends {@link Event} + * + * @author virustotalop */ -public class JavaAssistEventManager extends EventManager { +public class DeadEvent extends Event { - @Override - protected MethodExecutor createMethodExecutor(Listener listener, Method method, boolean ignoreCanceled) + private Event event; + public DeadEvent(Event event) + { + this.event = event; + } + + /** + * Returns an event that was + * not fired. + * @return event not fired + */ + public Event getDeadEvent() { - return JavaAssistUtil.generateMethodExecutor(listener, method, ignoreCanceled); + return this.event; } -} +} \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/impl/javaassist/JavaAssistUtil.java b/src/main/java/com/clubobsidian/trident/eventbus/javassist/JavassistEventBus.java similarity index 59% rename from src/main/java/com/clubobsidian/trident/impl/javaassist/JavaAssistUtil.java rename to src/main/java/com/clubobsidian/trident/eventbus/javassist/JavassistEventBus.java index d1bcd10..24ff8d6 100644 --- a/src/main/java/com/clubobsidian/trident/impl/javaassist/JavaAssistUtil.java +++ b/src/main/java/com/clubobsidian/trident/eventbus/javassist/JavassistEventBus.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package com.clubobsidian.trident.impl.javaassist; +package com.clubobsidian.trident.eventbus.javassist; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -21,7 +21,8 @@ import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicInteger; -import com.clubobsidian.trident.Listener; +import com.clubobsidian.trident.Event; +import com.clubobsidian.trident.EventBus; import com.clubobsidian.trident.MethodExecutor; import javassist.CannotCompileException; @@ -33,35 +34,36 @@ import javassist.LoaderClassPath; import javassist.NotFoundException; -public final class JavaAssistUtil { +/** + * {@inheritDoc} + */ +public class JavassistEventBus extends EventBus { - private static ClassPool pool; private static ConcurrentMap map; static { - try - { - map = new ConcurrentHashMap<>(); - pool = new ClassPool(true); - pool.insertClassPath("com.clubobsidian.trident.impl.javaassist.MethodCallback"); - pool.insertClassPath("com.clubobsidian.trident.Event"); - pool.insertClassPath("com.clubobsidian.trident.Listener"); - } - catch (NotFoundException e) - { - e.printStackTrace(); - } + JavassistEventBus.map = new ConcurrentHashMap<>(); + } + + private ClassPool pool; + public JavassistEventBus() + { + this.pool = new ClassPool(true); } - private JavaAssistUtil() {} + public ClassPool getClassPool() + { + return this.pool; + } - public static ClassPool getClassPool() + @Override + protected MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCanceled) { - return JavaAssistUtil.pool; + return this.generateMethodExecutor(listener, method, ignoreCanceled); } - - public static MethodExecutor generateMethodExecutor(final Listener listener, final Method method, final boolean ignoreCanceled) + + private MethodExecutor generateMethodExecutor(Object listener, final Method method, final boolean ignoreCanceled) { if(listener == null || method == null) return null; @@ -71,9 +73,17 @@ public static MethodExecutor generateMethodExecutor(final Listener listener, fin ClassLoader classLoader = listener.getClass().getClassLoader(); Class listenerClass = Class.forName(listener.getClass().getName(), true, classLoader); - pool.insertClassPath(new LoaderClassPath(classLoader)); - pool.insertClassPath(new ClassClassPath(listenerClass)); - + + this.addClassToPool(Event.class); + + LoaderClassPath loaderClassPath = new LoaderClassPath(classLoader); + + //If class path exists, remove it first and then add + this.pool.removeClassPath(loaderClassPath); + this.pool.insertClassPath(loaderClassPath); + + this.addClassToPool(listenerClass); + String callbackClassName = listener.getClass().getName() + method.getName(); AtomicInteger collision = map.get(callbackClassName); @@ -82,7 +92,7 @@ public static MethodExecutor generateMethodExecutor(final Listener listener, fin { collision = new AtomicInteger(0); classNum = 0; - map.put(callbackClassName, collision); + JavassistEventBus.map.put(callbackClassName, collision); } else { @@ -91,8 +101,19 @@ public static MethodExecutor generateMethodExecutor(final Listener listener, fin callbackClassName += classNum; - CtClass methodExecutorClass = pool.makeClass(callbackClassName); - methodExecutorClass.setSuperclass(pool.get("com.clubobsidian.trident.MethodExecutor")); + CtClass checkMethodExecutorClass = this.pool.getOrNull(callbackClassName); + if(checkMethodExecutorClass != null) + { + if(checkMethodExecutorClass.isFrozen()) + { + return null; + } + } + + CtClass methodExecutorClass = this.pool.makeClass(callbackClassName); + + + methodExecutorClass.setSuperclass(this.pool.get("com.clubobsidian.trident.MethodExecutor")); String eventType = method.getParameterTypes()[0].getName(); String listenerType = listener.getClass().getName(); @@ -107,7 +128,7 @@ public static MethodExecutor generateMethodExecutor(final Listener listener, fin CtMethod call = CtNewMethod.make(sb.toString(), methodExecutorClass); methodExecutorClass.addMethod(call); - Class cl = methodExecutorClass.toClass(classLoader, JavaAssistEventManager.class.getProtectionDomain()); + Class cl = methodExecutorClass.toClass(classLoader, JavassistEventBus.class.getProtectionDomain()); return (MethodExecutor) cl.getDeclaredConstructors()[0].newInstance(listener, method, ignoreCanceled); } catch (NotFoundException | CannotCompileException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException | ClassNotFoundException e) @@ -116,4 +137,11 @@ public static MethodExecutor generateMethodExecutor(final Listener listener, fin } return null; } -} + + private void addClassToPool(Class clazz) + { + ClassClassPath classClassPath = new ClassClassPath(clazz); + this.pool.removeClassPath(classClassPath); + this.pool.insertClassPath(classClassPath); + } +} \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/eventbus/package-info.java b/src/main/java/com/clubobsidian/trident/eventbus/package-info.java new file mode 100644 index 0000000..857b273 --- /dev/null +++ b/src/main/java/com/clubobsidian/trident/eventbus/package-info.java @@ -0,0 +1 @@ +package com.clubobsidian.trident.eventbus; \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/impl/reflection/ReflectionEventManager.java b/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionEventBus.java similarity index 67% rename from src/main/java/com/clubobsidian/trident/impl/reflection/ReflectionEventManager.java rename to src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionEventBus.java index 244bf62..971cae2 100644 --- a/src/main/java/com/clubobsidian/trident/impl/reflection/ReflectionEventManager.java +++ b/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionEventBus.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,24 +13,21 @@ See the License for the specific language governing permissions and limitations under the License. */ -package com.clubobsidian.trident.impl.reflection; +package com.clubobsidian.trident.eventbus.reflection; import java.lang.reflect.Method; -import com.clubobsidian.trident.EventManager; -import com.clubobsidian.trident.Listener; +import com.clubobsidian.trident.EventBus; import com.clubobsidian.trident.MethodExecutor; /** * {@inheritDoc} */ -public class ReflectionEventManager extends EventManager { +public class ReflectionEventBus extends EventBus { @Override - protected MethodExecutor createMethodExecutor(Listener listener, Method method, boolean ignoreCanceled) + protected MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCanceled) { return new ReflectionMethodExecutor(listener, method, ignoreCanceled); } - - -} +} \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/impl/reflection/ReflectionMethodExecutor.java b/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionMethodExecutor.java similarity index 82% rename from src/main/java/com/clubobsidian/trident/impl/reflection/ReflectionMethodExecutor.java rename to src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionMethodExecutor.java index d1c5991..dbba86a 100644 --- a/src/main/java/com/clubobsidian/trident/impl/reflection/ReflectionMethodExecutor.java +++ b/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionMethodExecutor.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,13 +13,12 @@ See the License for the specific language governing permissions and limitations under the License. */ -package com.clubobsidian.trident.impl.reflection; +package com.clubobsidian.trident.eventbus.reflection; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import com.clubobsidian.trident.Event; -import com.clubobsidian.trident.Listener; import com.clubobsidian.trident.MethodExecutor; /** @@ -27,7 +26,7 @@ */ public class ReflectionMethodExecutor extends MethodExecutor { - public ReflectionMethodExecutor(Listener listener, Method method, boolean ignoreCanceled) + public ReflectionMethodExecutor(Object listener, Method method, boolean ignoreCanceled) { super(listener, method, ignoreCanceled); } @@ -44,4 +43,4 @@ public void execute(final Event event) e.printStackTrace(); } } -} +} \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/util/ClassUtil.java b/src/main/java/com/clubobsidian/trident/util/ClassUtil.java index 10e028f..f0f423c 100644 --- a/src/main/java/com/clubobsidian/trident/util/ClassUtil.java +++ b/src/main/java/com/clubobsidian/trident/util/ClassUtil.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java b/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java index ebf8115..9040dd2 100644 --- a/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java +++ b/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/main/java/com/clubobsidian/trident/util/EventNode.java b/src/main/java/com/clubobsidian/trident/util/EventNode.java index 19004db..0b7fc55 100644 --- a/src/main/java/com/clubobsidian/trident/util/EventNode.java +++ b/src/main/java/com/clubobsidian/trident/util/EventNode.java @@ -1,4 +1,4 @@ -/* Copyright 2018 Club Obsidian and contributors. +/* Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/test/java/com/clubobsidian/trident/test/DeadEventTest.java b/src/test/java/com/clubobsidian/trident/test/DeadEventTest.java new file mode 100644 index 0000000..6681cf8 --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/DeadEventTest.java @@ -0,0 +1,38 @@ +package com.clubobsidian.trident.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.clubobsidian.trident.EventBus; +import com.clubobsidian.trident.eventbus.javassist.JavassistEventBus; +import com.clubobsidian.trident.test.impl.TestDeadEventListener; +import com.clubobsidian.trident.test.impl.TestEvent; + +public class DeadEventTest { + + @Test + public void testNoListener() + { + EventBus eventBus = new JavassistEventBus(); + boolean listenedTo = eventBus.callEvent(new TestEvent()); + + assertFalse("Event was listened to", listenedTo); + + } + + @Test + public void testDeadListener() + { + EventBus eventBus = new JavassistEventBus(); + TestDeadEventListener deadEventListener = new TestDeadEventListener(); + eventBus.registerEvents(deadEventListener); + + eventBus.callEvent(new TestEvent()); + + System.out.println(deadEventListener.getTimesRan()); + assertTrue("Dead event triggered twice", deadEventListener.getTimesRan() == 1); + } + +} diff --git a/src/test/java/trident/JavaAssistEventManagerTest.java b/src/test/java/com/clubobsidian/trident/test/EventBusTest.java similarity index 54% rename from src/test/java/trident/JavaAssistEventManagerTest.java rename to src/test/java/com/clubobsidian/trident/test/EventBusTest.java index 245173a..faaae2e 100644 --- a/src/test/java/trident/JavaAssistEventManagerTest.java +++ b/src/test/java/com/clubobsidian/trident/test/EventBusTest.java @@ -1,36 +1,24 @@ -/* - Copyright 2018 Club Obsidian and contributors. +package com.clubobsidian.trident.test; - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ -package trident; - -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.Test; import com.clubobsidian.trident.Event; -import com.clubobsidian.trident.EventManager; -import com.clubobsidian.trident.impl.javaassist.JavaAssistEventManager; +import com.clubobsidian.trident.EventBus; +import com.clubobsidian.trident.test.impl.TestCancelableEvent; +import com.clubobsidian.trident.test.impl.TestEvent; +import com.clubobsidian.trident.test.impl.TestListener; +import com.clubobsidian.trident.test.impl.TestOrderEvent; -public class JavaAssistEventManagerTest { +public abstract class EventBusTest { @Test public void testEventFiring() { TestListener test = new TestListener("test"); - EventManager manager = new JavaAssistEventManager(); + EventBus manager = this.createNewEventBus(); boolean registered = manager.registerEvents(test); assertTrue("Event is not registered", registered); @@ -49,7 +37,7 @@ public void testEventFiring() public void testEventCancellable() { TestListener test = new TestListener("test"); - EventManager manager = new JavaAssistEventManager(); + EventBus manager = this.createNewEventBus(); manager.registerEvents(test); manager.callEvent(new TestCancelableEvent()); @@ -61,7 +49,7 @@ public void testEventCancellable() public void testOrder() { TestListener test = new TestListener(""); - EventManager manager = new JavaAssistEventManager(); + EventBus manager = this.createNewEventBus(); manager.registerEvents(test); manager.callEvent(new TestOrderEvent()); @@ -73,7 +61,7 @@ public void testOrder() public void testDoubleRegister() { TestListener test = new TestListener("test"); - EventManager manager = new JavaAssistEventManager(); + EventBus manager = this.createNewEventBus(); manager.registerEvents(test); assertFalse("Event was double registered", manager.registerEvents(test)); @@ -83,7 +71,7 @@ public void testDoubleRegister() public void testCalledEvent() { Event event = new TestEvent(); - EventManager manager = new JavaAssistEventManager(); + EventBus manager = this.createNewEventBus(); assertFalse("Event call ran when event did not exist", manager.callEvent(event)); } @@ -92,10 +80,42 @@ public void testCalledEvent() public void testUnregister() { TestListener test = new TestListener("test"); - EventManager manager = new JavaAssistEventManager(); + EventBus manager = this.createNewEventBus(); manager.registerEvents(test); assertTrue("Listener was not registered", manager.unregisterEvents(test)); assertFalse("Listener was still registered", manager.unregisterEvents(test)); } + + @Test + public void testEmptyCallEvent() + { + EventBus manager = this.createNewEventBus(); + boolean called = manager.callEvent(new TestEvent()); + + assertFalse("Event was called when a listener is not registered", called); + } + + @Test + public void testNullRegister() + { + EventBus manager = this.createNewEventBus(); + boolean registered = manager.registerEvents(null); + + assertFalse("Listener was still registered event though the listener was null", registered); + } + + @Test + public void testPrivateListener() + { + EventBus manager = this.createNewEventBus(); + boolean registered = manager.registerEvents(new PrivateListener()); + + assertTrue("Listener was still registered event though the listener was private", registered); + } + + protected abstract EventBus createNewEventBus(); + + + private class PrivateListener {} } \ No newline at end of file diff --git a/src/test/java/trident/EventDoublyLinkedListTest.java b/src/test/java/com/clubobsidian/trident/test/EventDoublyLinkedListTest.java similarity index 72% rename from src/test/java/trident/EventDoublyLinkedListTest.java rename to src/test/java/com/clubobsidian/trident/test/EventDoublyLinkedListTest.java index 8db6b16..a10aa5e 100644 --- a/src/test/java/trident/EventDoublyLinkedListTest.java +++ b/src/test/java/com/clubobsidian/trident/test/EventDoublyLinkedListTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package trident; +package com.clubobsidian.trident.test; import static org.junit.Assert.assertTrue; @@ -25,7 +25,9 @@ import com.clubobsidian.trident.EventPriority; import com.clubobsidian.trident.MethodExecutor; -import com.clubobsidian.trident.impl.javaassist.JavaAssistUtil; +import com.clubobsidian.trident.test.impl.TestEvent; +import com.clubobsidian.trident.test.impl.TestListener; +import com.clubobsidian.trident.test.impl.TestMethodExecutor; import com.clubobsidian.trident.util.EventDoublyLinkedList; import com.clubobsidian.trident.util.EventNode; @@ -39,7 +41,7 @@ public void testOrder() EventDoublyLinkedList list = new EventDoublyLinkedList(); TestListener test = new TestListener("test"); - MethodExecutor executor = JavaAssistUtil.generateMethodExecutor(test, test.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor = new TestMethodExecutor(test, test.getClass().getDeclaredMethod("test", TestEvent.class), false); EventNode low = list.insert(executor, EventPriority.LOW); assertTrue("Low could not be inserted at head", low != null); @@ -47,7 +49,7 @@ public void testOrder() TestListener test2 = new TestListener("test2"); - MethodExecutor executor2 = JavaAssistUtil.generateMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor2 = new TestMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); EventNode lowest = list.insert(executor2, EventPriority.LOWEST); assertTrue("Lowest could not be inserted", lowest != null); @@ -55,7 +57,7 @@ public void testOrder() TestListener test3 = new TestListener("test3"); - MethodExecutor executor3 = JavaAssistUtil.generateMethodExecutor(test3, test3.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor3 = new TestMethodExecutor(test3, test3.getClass().getDeclaredMethod("test", TestEvent.class), false); EventNode nextLowest = list.insert(executor3, EventPriority.LOWEST); assertTrue("NextLowest could not be inserted", nextLowest != null); @@ -63,7 +65,7 @@ public void testOrder() TestListener test4 = new TestListener("test4"); - MethodExecutor executor4 = JavaAssistUtil.generateMethodExecutor(test4, test4.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor4 = new TestMethodExecutor(test4, test4.getClass().getDeclaredMethod("test", TestEvent.class), false); EventNode monitor = list.insert(executor4, EventPriority.MONITOR); assertTrue("Monitor could not be inserted", monitor != null); @@ -71,14 +73,14 @@ public void testOrder() TestListener test5 = new TestListener("test5"); - MethodExecutor executor5 = JavaAssistUtil.generateMethodExecutor(test5, test5.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor5 = new TestMethodExecutor(test5, test5.getClass().getDeclaredMethod("test", TestEvent.class), false); EventNode high = list.insert(executor5, EventPriority.HIGH); assertTrue("High could not be inserted", high != null); assertTrue("High next node is not priority monitor", list.find(executor5).getNext().getData().equals(executor4)); TestListener test6 = new TestListener("test6"); - MethodExecutor executor6 = JavaAssistUtil.generateMethodExecutor(test6, test6.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor6 = new TestMethodExecutor(test6, test6.getClass().getDeclaredMethod("test", TestEvent.class), false); EventNode normal = list.insert(executor6, EventPriority.NORMAL); assertTrue("Normal could not be inserted", normal != null); @@ -86,7 +88,7 @@ public void testOrder() TestListener test7 = new TestListener("test7"); - MethodExecutor executor7 = JavaAssistUtil.generateMethodExecutor(test7, test7.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor7 = new TestMethodExecutor(test7, test7.getClass().getDeclaredMethod("test", TestEvent.class), false); EventNode highest = list.insert(executor7, EventPriority.HIGHEST); assertTrue("Highest could not be inserted", highest != null); @@ -105,7 +107,7 @@ public void testRemoval() { EventDoublyLinkedList list = new EventDoublyLinkedList(); TestListener test = new TestListener("test1"); - MethodExecutor executor = JavaAssistUtil.generateMethodExecutor(test, test.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor = new TestMethodExecutor(test, test.getClass().getDeclaredMethod("test", TestEvent.class), false); list.insert(executor, EventPriority.MONITOR); assertTrue("Monitor is not head", list.getHead().getData().equals(executor)); @@ -114,7 +116,7 @@ public void testRemoval() for(int i = EventPriority.values()[0].getValue(); i < (EventPriority.values()[EventPriority.values().length - 1]).getValue() + 1; i++) { - list.insert(JavaAssistUtil.generateMethodExecutor(new TestListener("test" + i), test.getClass().getDeclaredMethod("test", TestEvent.class) , false), EventPriority.getByValue(i)); + list.insert(new TestMethodExecutor(new TestListener("test" + i), test.getClass().getDeclaredMethod("test", TestEvent.class) , false), EventPriority.getByValue(i)); } EventNode node = list.getHead(); @@ -131,7 +133,7 @@ public void testRemoval() for(int i = (EventPriority.values()[EventPriority.values().length - 1]).getValue(); i > -1 ; i--) { - list.insert(JavaAssistUtil.generateMethodExecutor(new TestListener("test" + i), test.getClass().getDeclaredMethod("test", TestEvent.class), false), EventPriority.getByValue(i)); + list.insert(new TestMethodExecutor(new TestListener("test" + i), test.getClass().getDeclaredMethod("test", TestEvent.class), false), EventPriority.getByValue(i)); } node = list.getHead(); @@ -154,7 +156,7 @@ public void testRemoval() rand.setSeed(System.nanoTime()); int next = rand.nextInt(6); - EventNode inserted = list.insert(JavaAssistUtil.generateMethodExecutor(new TestListener("test" + i), test.getClass().getDeclaredMethod("test", TestEvent.class), false), EventPriority.getByValue(next)); + EventNode inserted = list.insert(new TestMethodExecutor(new TestListener("test" + i), test.getClass().getDeclaredMethod("test", TestEvent.class), false), EventPriority.getByValue(next)); assertTrue("Insert failed for random insert for removal", inserted != null); } @@ -184,7 +186,7 @@ public void emptyLinkedListTest() TestListener test = new TestListener("test1"); try { - MethodExecutor executor = JavaAssistUtil.generateMethodExecutor(test, test.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor = new TestMethodExecutor(test, test.getClass().getDeclaredMethod("test", TestEvent.class), false); assertTrue("Executor was removed", list.remove(executor) == null); } catch (NoSuchMethodException | SecurityException e) @@ -200,12 +202,12 @@ public void nextIsNullAndNotHeadRemove() { EventDoublyLinkedList list = new EventDoublyLinkedList(); TestListener test1 = new TestListener("test1"); - MethodExecutor executor1 = JavaAssistUtil.generateMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor1 = new TestMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); list.insert(executor1, EventPriority.LOWEST); TestListener test2 = new TestListener("test2"); - MethodExecutor executor2 = JavaAssistUtil.generateMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor2 = new TestMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); list.insert(executor2, EventPriority.LOW); @@ -228,17 +230,17 @@ public void foundIsMiddleNodeRemoveTest() { EventDoublyLinkedList list = new EventDoublyLinkedList(); TestListener test1 = new TestListener("test1"); - MethodExecutor executor1 = JavaAssistUtil.generateMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor1 = new TestMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); list.insert(executor1, EventPriority.LOWEST); TestListener test2 = new TestListener("test2"); - MethodExecutor executor2 = JavaAssistUtil.generateMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor2 = new TestMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); list.insert(executor2, EventPriority.LOW); TestListener test3 = new TestListener("test3"); - MethodExecutor executor3 = JavaAssistUtil.generateMethodExecutor(test3, test3.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor3 = new TestMethodExecutor(test3, test3.getClass().getDeclaredMethod("test", TestEvent.class), false); list.insert(executor3, EventPriority.NORMAL); @@ -260,7 +262,7 @@ public void findMethodExecutorThatDoesNotExistTest() { EventDoublyLinkedList list = new EventDoublyLinkedList(); TestListener test1 = new TestListener("test1"); - MethodExecutor executor1 = JavaAssistUtil.generateMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor1 = new TestMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); assertTrue("Executor was found while not inserted", list.find(executor1) == null); } catch (NoSuchMethodException | SecurityException e) @@ -293,12 +295,12 @@ public void findIfFoundNodePriorityIsTheSameTest() { EventDoublyLinkedList list = new EventDoublyLinkedList(); TestListener test1 = new TestListener("test1"); - MethodExecutor executor1 = JavaAssistUtil.generateMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor1 = new TestMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); list.insert(executor1, EventPriority.LOWEST); TestListener test2 = new TestListener("test2"); - MethodExecutor executor2 = JavaAssistUtil.generateMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor executor2 = new TestMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); EventPriority priority = EventPriority.LOWEST; @@ -313,10 +315,18 @@ public void findIfFoundNodePriorityIsTheSameTest() } @Test - public void nullCaseTest() + public void methodExecutorNullTest() { EventDoublyLinkedList list = new EventDoublyLinkedList(); EventNode inserted = list.insert(null, null); - assertTrue("Inserted was not null", inserted == null); + assertTrue("Inserted event node was not null for a null method executor", inserted == null); + } + + @Test + public void eventPriorityNullTest() + { + EventDoublyLinkedList list = new EventDoublyLinkedList(); + EventNode inserted = list.insert(new TestMethodExecutor(null, null, false), null); + assertTrue("Inserted event node was not null for a null event priority", inserted == null); } } \ No newline at end of file diff --git a/src/test/java/com/clubobsidian/trident/test/EventPriorityTest.java b/src/test/java/com/clubobsidian/trident/test/EventPriorityTest.java new file mode 100644 index 0000000..04bf09b --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/EventPriorityTest.java @@ -0,0 +1,24 @@ +package com.clubobsidian.trident.test; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.clubobsidian.trident.EventPriority; + +public class EventPriorityTest { + + @Test + public void testOutOfLowerBounds() + { + EventPriority priority = EventPriority.getByValue(-1); + assertTrue("Priority was not null when going below bounds", priority == null); + } + + @Test + public void testOutOfUpperBounds() + { + EventPriority priority = EventPriority.getByValue(EventPriority.values().length); + assertTrue("Priority was not null when going above bounds", priority == null); + } +} \ No newline at end of file diff --git a/src/test/java/com/clubobsidian/trident/test/EventTest.java b/src/test/java/com/clubobsidian/trident/test/EventTest.java new file mode 100644 index 0000000..81873a1 --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/EventTest.java @@ -0,0 +1,18 @@ +package com.clubobsidian.trident.test; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.clubobsidian.trident.Event; +import com.clubobsidian.trident.test.impl.TestEvent; + +public class EventTest { + + @Test + public void testName() + { + Event event = new TestEvent(); + assertTrue("Event name is not \"TestEvent\"", event.getName().equals("TestEvent")); + } +} \ No newline at end of file diff --git a/src/test/java/trident/IgnoreCanceledTest.java b/src/test/java/com/clubobsidian/trident/test/IgnoreCanceledTest.java similarity index 72% rename from src/test/java/trident/IgnoreCanceledTest.java rename to src/test/java/com/clubobsidian/trident/test/IgnoreCanceledTest.java index b50089e..e96b68d 100644 --- a/src/test/java/trident/IgnoreCanceledTest.java +++ b/src/test/java/com/clubobsidian/trident/test/IgnoreCanceledTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,15 +13,17 @@ See the License for the specific language governing permissions and limitations under the License. */ -package trident; +package com.clubobsidian.trident.test; import static org.junit.Assert.assertTrue; import org.junit.Test; -import com.clubobsidian.trident.EventManager; -import com.clubobsidian.trident.impl.javaassist.JavaAssistEventManager; +import com.clubobsidian.trident.EventBus; +import com.clubobsidian.trident.eventbus.javassist.JavassistEventBus; +import com.clubobsidian.trident.test.impl.TestCancelableEvent; +import com.clubobsidian.trident.test.impl.TestListenerIgnore; public class IgnoreCanceledTest { @@ -29,12 +31,11 @@ public class IgnoreCanceledTest { public void ignoredCanceled() { TestListenerIgnore listener = new TestListenerIgnore(); - EventManager manager = new JavaAssistEventManager(); + EventBus manager = new JavassistEventBus(); manager.registerEvents(listener); manager.callEvent(new TestCancelableEvent()); assertTrue("Event was not canceled", listener.isCanceled()); assertTrue("Event was not ignored", listener.getIgnored()); } - -} +} \ No newline at end of file diff --git a/src/test/java/com/clubobsidian/trident/test/JavaAssistEventBusTest.java b/src/test/java/com/clubobsidian/trident/test/JavaAssistEventBusTest.java new file mode 100644 index 0000000..84bce65 --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/JavaAssistEventBusTest.java @@ -0,0 +1,93 @@ +/* + Copyright 2019 Club Obsidian and contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package com.clubobsidian.trident.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import com.clubobsidian.trident.EventBus; +import com.clubobsidian.trident.eventbus.javassist.JavassistEventBus; +import com.clubobsidian.trident.test.impl.TestListener; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class JavaAssistEventBusTest extends EventBusTest { + + @Test + public void testClassPoolExists() + { + JavassistEventBus eventBus = new JavassistEventBus(); + assertTrue("No class pool for JavaAssist event bus", eventBus.getClassPool() != null); + } + + @Test + public void testNullMethodOnMethodExecutor() + { + JavassistEventBus eventBus = new JavassistEventBus(); + try + { + Method generate = eventBus.getClass().getDeclaredMethod("generateMethodExecutor", Object.class, Method.class, boolean.class); + generate.setAccessible(true); + Object methodExecutor = generate.invoke(eventBus, null, null, false); + assertTrue("Method executor is not null even though a null listener was passed", methodExecutor == null); + } + catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) + { + e.printStackTrace(); + } + } + + @Test + public void testFrozenMethodExecutor() + { + try + { + JavassistEventBus eventBus = new JavassistEventBus(); + eventBus.registerEvents(new TestListener("data")); + Field mapField = JavassistEventBus.class.getDeclaredField("map"); + mapField.setAccessible(true); + ConcurrentMap savedMap = (ConcurrentMap) mapField.get(null); + + mapField.set(null, new ConcurrentHashMap()); + boolean registered = eventBus.registerEvents(new TestListener("data")); + + mapField.set(null, savedMap); + System.out.println("Set map back to prior state"); + + assertFalse("Was able to register event with invalid map", registered); + } + catch (IllegalArgumentException | SecurityException | IllegalAccessException | NoSuchFieldException e) + { + e.printStackTrace(); + } + } + + @Override + protected EventBus createNewEventBus() + { + return new JavassistEventBus(); + } +} \ No newline at end of file diff --git a/src/test/java/trident/MultiThreadingTest.java b/src/test/java/com/clubobsidian/trident/test/MultiThreadingTest.java similarity index 63% rename from src/test/java/trident/MultiThreadingTest.java rename to src/test/java/com/clubobsidian/trident/test/MultiThreadingTest.java index 5e3eaf9..04023bb 100644 --- a/src/test/java/trident/MultiThreadingTest.java +++ b/src/test/java/com/clubobsidian/trident/test/MultiThreadingTest.java @@ -1,19 +1,4 @@ -/* - Copyright 2018 Club Obsidian and contributors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ -package trident; +package com.clubobsidian.trident.test; import static org.junit.Assert.assertTrue; @@ -23,7 +8,9 @@ import com.clubobsidian.trident.EventPriority; import com.clubobsidian.trident.MethodExecutor; -import com.clubobsidian.trident.impl.javaassist.JavaAssistUtil; +import com.clubobsidian.trident.test.impl.TestEvent; +import com.clubobsidian.trident.test.impl.TestListener; +import com.clubobsidian.trident.test.impl.TestMethodExecutor; import com.clubobsidian.trident.util.EventDoublyLinkedList; import com.clubobsidian.trident.util.EventNode; @@ -45,7 +32,7 @@ public void run() { int inc = count.incrementAndGet(); TestListener testListener = new TestListener("test" + inc); - MethodExecutor testExecutor = JavaAssistUtil.generateMethodExecutor(testListener, testListener.getClass().getDeclaredMethod("test", TestEvent.class), false); + MethodExecutor testExecutor = new TestMethodExecutor(testListener, testListener.getClass().getDeclaredMethod("test", TestEvent.class), false); EventNode node = list.insert(testExecutor, EventPriority.LOWEST); assertTrue("Node was null when inserted", node != null); } @@ -74,4 +61,4 @@ public void run() } assertTrue("Node count not equal when running from multiple thread", nodeCount == count.get()); } -} +} \ No newline at end of file diff --git a/src/test/java/com/clubobsidian/trident/test/OtherEventBusTest.java b/src/test/java/com/clubobsidian/trident/test/OtherEventBusTest.java new file mode 100644 index 0000000..5d61a83 --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/OtherEventBusTest.java @@ -0,0 +1,20 @@ +package com.clubobsidian.trident.test; + +import static org.junit.Assert.assertFalse; + +import org.junit.Test; + +import com.clubobsidian.trident.EventBus; +import com.clubobsidian.trident.test.impl.TestEventBus; +import com.clubobsidian.trident.test.impl.TestListener; + +public class OtherEventBusTest { + + @Test + public void testNullExecutor() + { + EventBus bus = new TestEventBus(); + boolean registered = bus.registerEvents(new TestListener("data")); + assertFalse("Listener was registered even though MethodExecutor is null", registered); + } +} \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/Listener.java b/src/test/java/com/clubobsidian/trident/test/ReflectionEventBusTest.java similarity index 61% rename from src/main/java/com/clubobsidian/trident/Listener.java rename to src/test/java/com/clubobsidian/trident/test/ReflectionEventBusTest.java index 23fc4b8..58b317c 100644 --- a/src/main/java/com/clubobsidian/trident/Listener.java +++ b/src/test/java/com/clubobsidian/trident/test/ReflectionEventBusTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,11 +13,16 @@ See the License for the specific language governing permissions and limitations under the License. */ -package com.clubobsidian.trident; +package com.clubobsidian.trident.test; -/** - * Interface for listeners to implement. - * - * @author virustotalop - */ -public interface Listener {} +import com.clubobsidian.trident.EventBus; +import com.clubobsidian.trident.eventbus.reflection.ReflectionEventBus; + +public class ReflectionEventBusTest extends EventBusTest { + + @Override + protected EventBus createNewEventBus() + { + return new ReflectionEventBus(); + } +} \ No newline at end of file diff --git a/src/test/java/trident/ReflectionMiscEventTest.java b/src/test/java/com/clubobsidian/trident/test/ReflectionMiscEventTest.java similarity index 54% rename from src/test/java/trident/ReflectionMiscEventTest.java rename to src/test/java/com/clubobsidian/trident/test/ReflectionMiscEventTest.java index 96b71dd..1a00954 100644 --- a/src/test/java/trident/ReflectionMiscEventTest.java +++ b/src/test/java/com/clubobsidian/trident/test/ReflectionMiscEventTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package trident; +package com.clubobsidian.trident.test; import static org.junit.Assert.assertTrue; @@ -22,9 +22,12 @@ import org.junit.Test; -import com.clubobsidian.trident.Listener; import com.clubobsidian.trident.MethodExecutor; -import com.clubobsidian.trident.impl.reflection.ReflectionMethodExecutor; +import com.clubobsidian.trident.eventbus.reflection.ReflectionMethodExecutor; +import com.clubobsidian.trident.test.impl.TestWrongArgumentListener; +import com.clubobsidian.trident.test.impl.TestEvent; +import com.clubobsidian.trident.test.impl.TestListener; +import com.clubobsidian.trident.test.impl.TestWrongArgumentEvent; public class ReflectionMiscEventTest { @@ -33,7 +36,7 @@ public void methodExecutorTest() { try { - Listener listener = new TestListener("test"); + TestListener listener = new TestListener("test"); Method testEventMethod = listener.getClass().getDeclaredMethod("test", TestEvent.class); MethodExecutor executor = new ReflectionMethodExecutor(listener, testEventMethod, false); @@ -45,4 +48,22 @@ public void methodExecutorTest() e.printStackTrace(); } } + + @Test + public void methodExecutorPrivateTest() + { + try + { + TestWrongArgumentListener listener = new TestWrongArgumentListener("test"); + Method testEventMethod = listener.getClass().getDeclaredMethod("test", TestEvent.class); + MethodExecutor executor = new ReflectionMethodExecutor(listener, testEventMethod, false); + executor.execute(new TestWrongArgumentEvent()); + + assertTrue("Executor was able to execute method on a listener with a different event class", listener.getTest() == false); + } + catch (NoSuchMethodException | SecurityException e) + { + e.printStackTrace(); + } + } } diff --git a/src/test/java/trident/UtilTest.java b/src/test/java/com/clubobsidian/trident/test/UtilTest.java similarity index 88% rename from src/test/java/trident/UtilTest.java rename to src/test/java/com/clubobsidian/trident/test/UtilTest.java index 9adf8d7..1dd87a2 100644 --- a/src/test/java/trident/UtilTest.java +++ b/src/test/java/com/clubobsidian/trident/test/UtilTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package trident; +package com.clubobsidian.trident.test; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; @@ -21,7 +21,7 @@ import org.junit.Test; import com.clubobsidian.trident.Event; - +import com.clubobsidian.trident.test.impl.TestEvent; import com.clubobsidian.trident.util.ClassUtil; public class UtilTest { diff --git a/src/test/java/trident/TestCancelableEvent.java b/src/test/java/com/clubobsidian/trident/test/impl/TestCancelableEvent.java similarity index 90% rename from src/test/java/trident/TestCancelableEvent.java rename to src/test/java/com/clubobsidian/trident/test/impl/TestCancelableEvent.java index 0e4775b..8725695 100644 --- a/src/test/java/trident/TestCancelableEvent.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestCancelableEvent.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package trident; +package com.clubobsidian.trident.test.impl; import com.clubobsidian.trident.Cancelable; diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestDeadEventListener.java b/src/test/java/com/clubobsidian/trident/test/impl/TestDeadEventListener.java new file mode 100644 index 0000000..76e551f --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestDeadEventListener.java @@ -0,0 +1,26 @@ +package com.clubobsidian.trident.test.impl; + +import com.clubobsidian.trident.EventHandler; +import com.clubobsidian.trident.event.DeadEvent; + +public class TestDeadEventListener { + + private int timesRan; + public TestDeadEventListener() + { + this.timesRan = 0; + } + + + @EventHandler + public void onDeadEvent(DeadEvent event) + { + System.out.println("Dead event: " + event.getDeadEvent().getName()); + this.timesRan += 1; + } + + public int getTimesRan() + { + return this.timesRan; + } +} \ No newline at end of file diff --git a/src/test/java/trident/TestEvent.java b/src/test/java/com/clubobsidian/trident/test/impl/TestEvent.java similarity index 88% rename from src/test/java/trident/TestEvent.java rename to src/test/java/com/clubobsidian/trident/test/impl/TestEvent.java index a6badc3..abda403 100644 --- a/src/test/java/trident/TestEvent.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestEvent.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package trident; +package com.clubobsidian.trident.test.impl; /** * diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestEventBus.java b/src/test/java/com/clubobsidian/trident/test/impl/TestEventBus.java new file mode 100644 index 0000000..667bc1c --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestEventBus.java @@ -0,0 +1,15 @@ +package com.clubobsidian.trident.test.impl; + +import java.lang.reflect.Method; + +import com.clubobsidian.trident.EventBus; +import com.clubobsidian.trident.MethodExecutor; + +public class TestEventBus extends EventBus { + + @Override + protected MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCanceled) + { + return null; + } +} \ No newline at end of file diff --git a/src/test/java/trident/TestEventSuper.java b/src/test/java/com/clubobsidian/trident/test/impl/TestEventSuper.java similarity index 88% rename from src/test/java/trident/TestEventSuper.java rename to src/test/java/com/clubobsidian/trident/test/impl/TestEventSuper.java index 80af965..7f1e7b3 100644 --- a/src/test/java/trident/TestEventSuper.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestEventSuper.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package trident; +package com.clubobsidian.trident.test.impl; import com.clubobsidian.trident.Event; diff --git a/src/test/java/trident/TestListener.java b/src/test/java/com/clubobsidian/trident/test/impl/TestListener.java similarity index 92% rename from src/test/java/trident/TestListener.java rename to src/test/java/com/clubobsidian/trident/test/impl/TestListener.java index 4c8f156..b8c97d9 100644 --- a/src/test/java/trident/TestListener.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestListener.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,13 +13,12 @@ See the License for the specific language governing permissions and limitations under the License. */ -package trident; +package com.clubobsidian.trident.test.impl; import com.clubobsidian.trident.EventHandler; import com.clubobsidian.trident.EventPriority; -import com.clubobsidian.trident.Listener; -public class TestListener implements Listener { +public class TestListener { private String data; private boolean test; diff --git a/src/test/java/trident/TestListenerIgnore.java b/src/test/java/com/clubobsidian/trident/test/impl/TestListenerIgnore.java similarity index 87% rename from src/test/java/trident/TestListenerIgnore.java rename to src/test/java/com/clubobsidian/trident/test/impl/TestListenerIgnore.java index 5dda96b..c25d364 100644 --- a/src/test/java/trident/TestListenerIgnore.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestListenerIgnore.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,13 +13,12 @@ See the License for the specific language governing permissions and limitations under the License. */ -package trident; +package com.clubobsidian.trident.test.impl; import com.clubobsidian.trident.EventHandler; import com.clubobsidian.trident.EventPriority; -import com.clubobsidian.trident.Listener; -public class TestListenerIgnore implements Listener { +public class TestListenerIgnore { private boolean canceled = false; private boolean ignored = true; diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestMethodExecutor.java b/src/test/java/com/clubobsidian/trident/test/impl/TestMethodExecutor.java new file mode 100644 index 0000000..aa3e298 --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestMethodExecutor.java @@ -0,0 +1,20 @@ +package com.clubobsidian.trident.test.impl; + +import java.lang.reflect.Method; + +import com.clubobsidian.trident.Event; +import com.clubobsidian.trident.MethodExecutor; + +public class TestMethodExecutor extends MethodExecutor { + + public TestMethodExecutor(Object listener, Method method, boolean ignoreCanceled) + { + super(listener, method, ignoreCanceled); + } + + @Override + public void execute(Event event) + { + + } +} \ No newline at end of file diff --git a/src/test/java/trident/TestOrderEvent.java b/src/test/java/com/clubobsidian/trident/test/impl/TestOrderEvent.java similarity index 87% rename from src/test/java/trident/TestOrderEvent.java rename to src/test/java/com/clubobsidian/trident/test/impl/TestOrderEvent.java index 6f969f1..d66f26a 100644 --- a/src/test/java/trident/TestOrderEvent.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestOrderEvent.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package trident; +package com.clubobsidian.trident.test.impl; import com.clubobsidian.trident.Event; diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentEvent.java b/src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentEvent.java new file mode 100644 index 0000000..fa15ccf --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentEvent.java @@ -0,0 +1,5 @@ +package com.clubobsidian.trident.test.impl; + +public class TestWrongArgumentEvent extends TestEventSuper { + +} diff --git a/src/main/java/com/clubobsidian/trident/impl/package-info.java b/src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentListener.java similarity index 55% rename from src/main/java/com/clubobsidian/trident/impl/package-info.java rename to src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentListener.java index 55a6ced..f670036 100644 --- a/src/main/java/com/clubobsidian/trident/impl/package-info.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentListener.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Club Obsidian and contributors. + Copyright 2019 Club Obsidian and contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,4 +13,30 @@ See the License for the specific language governing permissions and limitations under the License. */ -package com.clubobsidian.trident.impl; +package com.clubobsidian.trident.test.impl; + +public class TestWrongArgumentListener { + + private String data; + private boolean test; + public TestWrongArgumentListener(String data) + { + this.data = data; + this.test = false; + } + + public String getData() + { + return this.data; + } + + public boolean getTest() + { + return this.test; + } + + public void test(TestEvent event) + { + this.test = !test; + } +} \ No newline at end of file diff --git a/src/test/java/trident/JavaAssistMiscEventTest.java b/src/test/java/trident/JavaAssistMiscEventTest.java deleted file mode 100644 index 7bd8d1f..0000000 --- a/src/test/java/trident/JavaAssistMiscEventTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - Copyright 2018 Club Obsidian and contributors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ -package trident; - -import static org.junit.Assert.assertTrue; - -import java.lang.reflect.Method; - -import org.junit.Test; - -import com.clubobsidian.trident.Event; -import com.clubobsidian.trident.EventPriority; -import com.clubobsidian.trident.Listener; -import com.clubobsidian.trident.MethodExecutor; -import com.clubobsidian.trident.impl.javaassist.JavaAssistUtil; - -public class JavaAssistMiscEventTest { - - @Test - public void eventNameTest() - { - Event event = new TestEvent(); - assertTrue(event.getName().equals("TestEvent")); - } - - @Test - public void methodExecutorTest() - { - try - { - Listener listener = new TestListener("test"); - Method testEventMethod = listener.getClass().getDeclaredMethod("test", TestEvent.class); - MethodExecutor executor = JavaAssistUtil.generateMethodExecutor(listener, testEventMethod, false); - - assertTrue("Listeners are not equal for method executor", listener.equals(executor.getListener())); - assertTrue("Methods are not equal for method executor", testEventMethod.equals(executor.getMethod())); - } - catch (NoSuchMethodException | SecurityException e) - { - e.printStackTrace(); - } - } - - @Test - public void methodExecutorNullTest() - { - try - { - Listener listener = null; - Method testEventMethod = null; - MethodExecutor executor = JavaAssistUtil.generateMethodExecutor(listener, testEventMethod, false); - - assertTrue("Method executor was not null", executor == null); - } - catch (SecurityException e) - { - e.printStackTrace(); - } - } - - @Test - public void eventPriorityTest() - { - assertTrue("-1 event priority is not null", EventPriority.getByValue(-1) == null); - assertTrue("Length event priority is not null", EventPriority.getByValue(EventPriority.values().length) == null); - } - - @Test - public void classPoolTest() - { - assertTrue("Class pool is null", JavaAssistUtil.getClassPool() != null); - } -} \ No newline at end of file diff --git a/src/test/java/trident/ReflectionEventManagerTest.java b/src/test/java/trident/ReflectionEventManagerTest.java deleted file mode 100644 index 21921c5..0000000 --- a/src/test/java/trident/ReflectionEventManagerTest.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - Copyright 2018 Club Obsidian and contributors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ -package trident; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.junit.Test; - -import com.clubobsidian.trident.Event; -import com.clubobsidian.trident.EventManager; -import com.clubobsidian.trident.impl.reflection.ReflectionEventManager; - -public class ReflectionEventManagerTest { - - @Test - public void testEventFiring() - { - TestListener test = new TestListener("test"); - EventManager manager = new ReflectionEventManager(); - boolean registered = manager.registerEvents(test); - - assertTrue("Event is not registered", registered); - assertFalse("Test is not false", test.getTest()); - - manager.callEvent(new TestEvent()); - - assertTrue("Test is not true", test.getTest()); - - manager.callEvent(new TestEvent()); - - assertFalse("Test is not false", test.getTest()); - } - - @Test - public void testEventCancellable() - { - TestListener test = new TestListener("test"); - EventManager manager = new ReflectionEventManager(); - manager.registerEvents(test); - - manager.callEvent(new TestCancelableEvent()); - - assertTrue("Test is not true, event was not cancelled", test.getTest()); - } - - @Test - public void testOrder() - { - TestListener test = new TestListener(""); - EventManager manager = new ReflectionEventManager(); - manager.registerEvents(test); - - manager.callEvent(new TestOrderEvent()); - - assertTrue("Events were not listened in the correct order", test.getData().equals("012345")); - } - - @Test - public void testDoubleRegister() - { - TestListener test = new TestListener("test"); - EventManager manager = new ReflectionEventManager(); - manager.registerEvents(test); - - assertFalse("Event was double registered", manager.registerEvents(test)); - } - - @Test - public void testCalledEvent() - { - Event event = new TestEvent(); - EventManager manager = new ReflectionEventManager(); - - assertFalse("Event call ran when event did not exist", manager.callEvent(event)); - } - - @Test - public void testUnregister() - { - TestListener test = new TestListener("test"); - EventManager manager = new ReflectionEventManager(); - manager.registerEvents(test); - - assertTrue("Listener was not registered", manager.unregisterEvents(test)); - assertFalse("Listener was still registered", manager.unregisterEvents(test)); - } -} \ No newline at end of file