diff --git a/LICENSE_HEADER b/LICENSE_HEADER index ad0340b..53369bc 100644 --- a/LICENSE_HEADER +++ b/LICENSE_HEADER @@ -1,4 +1,4 @@ - Copyright 2019 Club Obsidian and contributors. + Copyright 2021 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 3e9129e..d1b473b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![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://img.shields.io/discord/482823104905609248.svg?logo=discord)](https://discord.gg/EY5Tq6r) -[![javadocs](https://img.shields.io/badge/Javadocs-2.1.0-success.svg)](https://jitpack.io/com/github/clubobsidian/trident/2.1.0/javadoc/) +[![javadocs](https://img.shields.io/badge/Javadocs-3.0.0-success.svg)](https://jitpack.io/com/github/clubobsidian/trident/3.0.0/javadoc/) A dead simpile annotation-based event bus that allows different event executor implementations. @@ -33,7 +33,7 @@ repositories { maven { url 'https://jitpack.io' } } -compile 'com.github.clubobsidian:trident:2.1.0' +compile 'com.github.clubobsidian:trident:3.0.0' ``` ### Maven @@ -49,7 +49,7 @@ compile 'com.github.clubobsidian:trident:2.1.0' com.github.clubobsidian trident - 2.1.0 + 3.0.0 ``` diff --git a/build.gradle b/build.gradle index b57b573..2956444 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ sourceCompatibility = 1.8 targetCompatibility = 1.8 group = 'com.clubobsidian' -version = '2.0.0' +version = '3.0.0' repositories { mavenCentral() diff --git a/src/main/java/com/clubobsidian/trident/Cancelable.java b/src/main/java/com/clubobsidian/trident/Cancelable.java deleted file mode 100644 index ac8bcde..0000000 --- a/src/main/java/com/clubobsidian/trident/Cancelable.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - 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; - -/** - * Interface to implement Cancelable. - * Events should implement this interface - * if the event should be able to be cancelled. - * - * @author virustotalop - */ -public interface Cancelable { - - /** - * Returns if the event is cancelled. - * @return if the event is cancelled - */ - public boolean isCanceled(); - - /** - * Set whether or not for an event to be cancelled. - */ - public void setCanceled(boolean cancel); -} diff --git a/src/main/java/com/clubobsidian/trident/Cancellable.java b/src/main/java/com/clubobsidian/trident/Cancellable.java new file mode 100644 index 0000000..8a68a34 --- /dev/null +++ b/src/main/java/com/clubobsidian/trident/Cancellable.java @@ -0,0 +1,38 @@ +/* + * Copyright 2021 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; + +/** + * Interface to implement Cancelable. + * Events should implement this interface + * if the event should be able to be cancelled. + * + * @author virustotalop + */ +public interface Cancellable { + + /** + * Returns if the event is cancelled. + * + * @return if the event is cancelled + */ + boolean isCancelled(); + + /** + * Set whether or not for an event to be cancelled. + */ + void setCancelled(boolean cancel); +} diff --git a/src/main/java/com/clubobsidian/trident/Event.java b/src/main/java/com/clubobsidian/trident/Event.java index b6c209f..bf7796a 100644 --- a/src/main/java/com/clubobsidian/trident/Event.java +++ b/src/main/java/com/clubobsidian/trident/Event.java @@ -1,35 +1,35 @@ -/* - 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. -*/ +/* + * Copyright 2021 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; /** * Superclass of all events. Events should * extend this class. - * + * * @author virustotalop */ public abstract class Event { - - /** - * Uses reflection getClass().getSimpleName() to return - * the name of the event class. - * @return name of the event - */ - public String getName() - { - return this.getClass().getSimpleName(); - } + + /** + * Uses reflection getClass().getSimpleName() to return + * the name of the event class. + * + * @return name of the event + */ + public String getName() { + return this.getClass().getSimpleName(); + } } diff --git a/src/main/java/com/clubobsidian/trident/EventBus.java b/src/main/java/com/clubobsidian/trident/EventBus.java index 09ef21d..0da71f4 100644 --- a/src/main/java/com/clubobsidian/trident/EventBus.java +++ b/src/main/java/com/clubobsidian/trident/EventBus.java @@ -1,161 +1,138 @@ -/* - 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. -*/ +/* + * Copyright 2021 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; +import com.clubobsidian.trident.event.DeadEvent; +import com.clubobsidian.trident.util.ClassUtil; +import com.clubobsidian.trident.util.EventDoublyLinkedList; +import com.clubobsidian.trident.util.EventNode; + import java.lang.reflect.Method; -import java.util.Iterator; import java.util.Map; import java.util.Queue; 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 EventBus - * classes. For an example see @see com.clubobsidian.trident.impl.javaassist.JavaAssistEventBus - * + * classes. For an example see @see com.clubobsidian.trident.eventbus.javassist.JavassistEventBus + * * @author virustotalop */ public abstract class EventBus { - private Map> registeredEventListeners; - private Map, EventDoublyLinkedList> registeredExecutors; - public EventBus() - { - this.registeredEventListeners = new ConcurrentHashMap<>(); - this.registeredExecutors = new ConcurrentHashMap<>(); - } - - /** - * @param event Event to call - * @return if the event was called - */ - 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(); - if(node != null) - { - ran = true; - } - while(node != null) - { - MethodExecutor executor = node.getData(); - if(event instanceof Cancelable) - { - Cancelable cancelable = (Cancelable) event; - if(cancelable.isCanceled() && executor.isIgnoringCanceled()) - { - node = node.getNext(); - continue; - } - } - executor.execute(event); - node = node.getNext(); - } - - return ran; - } - - /** - * @param listener listener to be registered - * @return if the listener was registered - */ - public boolean registerEvents(final Object listener) - { - if(listener == null) - { - return false; - } - else if(this.registeredEventListeners.keySet().contains(listener)) - { - return false; - } - - Class cl = listener.getClass(); - for(Method method : cl.getDeclaredMethods()) - { - for(EventHandler handler : method.getAnnotationsByType(EventHandler.class)) - { - if(method.getParameters().length == 1) - { - Class eventClass = method.getParameterTypes()[0]; - if(ClassUtil.hasEventSuperClass(eventClass)) - { - if(this.registeredExecutors.get(eventClass) == null) - { - this.registeredExecutors.put(eventClass, new EventDoublyLinkedList()); - } - if(this.registeredEventListeners.get(listener) == null) - { - this.registeredEventListeners.put(listener, new ConcurrentLinkedQueue<>()); - } - - 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); - } - } - } - } - return true; - } - - 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(Object listener) - { - Queue executors = this.registeredEventListeners.remove(listener); - if(executors == null) - return false; - - for(EventDoublyLinkedList list : this.registeredExecutors.values()) - { - Iterator it = executors.iterator(); - while(it.hasNext()) - { - MethodExecutor executor = it.next(); - list.remove(executor); - } - } - return true; - } + private final Map> registeredEventListeners; + private final Map, EventDoublyLinkedList> registeredExecutors; + + public EventBus() { + this.registeredEventListeners = new ConcurrentHashMap<>(); + this.registeredExecutors = new ConcurrentHashMap<>(); + } + + /** + * @param event Event to call + * @return if the event was called + */ + 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(); + if (node != null) { + ran = true; + } + while (node != null) { + MethodExecutor executor = node.getData(); + if (event instanceof Cancellable) { + Cancellable cancellable = (Cancellable) event; + if (cancellable.isCancelled() && executor.isIgnoringCancelled()) { + node = node.getNext(); + continue; + } + } + executor.execute(event); + node = node.getNext(); + } + + return ran; + } + + /** + * @param listener listener to be registered + * @return if the listener was registered + */ + public boolean registerEvents(final Object listener) { + if (listener == null) { + return false; + } else if (this.registeredEventListeners.containsKey(listener)) { + return false; + } + + Class cl = listener.getClass(); + for (Method method : cl.getDeclaredMethods()) { + for (EventHandler handler : method.getAnnotationsByType(EventHandler.class)) { + if (method.getParameters().length == 1) { + Class eventClass = method.getParameterTypes()[0]; + if (ClassUtil.hasEventSuperClass(eventClass)) { + if (this.registeredExecutors.get(eventClass) == null) { + this.registeredExecutors.put(eventClass, new EventDoublyLinkedList()); + } + if (this.registeredEventListeners.get(listener) == null) { + this.registeredEventListeners.put(listener, new ConcurrentLinkedQueue<>()); + } + + boolean ignoreCancelled = handler.ignoreCancelled(); + MethodExecutor executor = this.createMethodExecutor(listener, method, ignoreCancelled); + + if (executor == null) { + return false; + } + + this.registeredExecutors.get(eventClass).insert(executor, handler.priority()); + this.registeredEventListeners.get(listener).add(executor); + } + } + } + } + return true; + } + + protected abstract MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCancelled); + + /** + * @param listener listener to be unregistered + * @return if the listener was unregistered + */ + public boolean unregisterEvents(Object listener) { + Queue executors = this.registeredEventListeners.remove(listener); + if (executors == null) + return false; + + for (EventDoublyLinkedList list : this.registeredExecutors.values()) { + for (MethodExecutor executor : executors) { + list.remove(executor); + } + } + 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 984c9c7..66f2c26 100644 --- a/src/main/java/com/clubobsidian/trident/EventHandler.java +++ b/src/main/java/com/clubobsidian/trident/EventHandler.java @@ -1,18 +1,18 @@ -/* - 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. -*/ +/* + * Copyright 2021 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; import java.lang.annotation.ElementType; @@ -25,14 +25,14 @@ * When methods are annotated in * listeners events are registered in * an {@code EventManager} instance. - * + * * @author virustotalop */ @Retention(value = RetentionPolicy.RUNTIME) @Target(value = ElementType.METHOD) public @interface EventHandler { - EventPriority priority() default EventPriority.NORMAL; - boolean ignoreCanceled() default false; - + EventPriority priority() default EventPriority.NORMAL; + boolean ignoreCancelled() default false; + } \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/EventPriority.java b/src/main/java/com/clubobsidian/trident/EventPriority.java index 37384f1..69ebf3b 100644 --- a/src/main/java/com/clubobsidian/trident/EventPriority.java +++ b/src/main/java/com/clubobsidian/trident/EventPriority.java @@ -1,59 +1,56 @@ -/* - 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. -*/ +/* + * Copyright 2021 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; /** * Enum representation of priority for registered events. * Default priority is {@link EventPriority#NORMAL} - * + * * @author virustotalop */ public enum EventPriority { - LOWEST(0), - LOW(1), - NORMAL(2), - HIGH(3), - HIGHEST(4), - MONITOR(5); + LOWEST(0), + LOW(1), + NORMAL(2), + HIGH(3), + HIGHEST(4), + MONITOR(5); + + private final int value; + + EventPriority(final int value) { + this.value = value; + } + + /** + * @return the integer representation of the underlying enum + */ + public int getValue() { + return this.value; + } - private int value; - private EventPriority(final int value) - { - this.value = value; - } - - /** - * @return the integer representation of the underlying enum - */ - public int getValue() - { - return this.value; - } - - /** - * @param value the integer value of an eventpriority - * @return The EventPriority found by the given value - */ - public static EventPriority getByValue(final int value) - { - if(value < 0 || value >= EventPriority.values().length) - { - return null; - } - return EventPriority.values()[value]; - } + /** + * @param value the integer value of an eventpriority + * @return The EventPriority found by the given value + */ + public static EventPriority getByValue(final int value) { + if (value < 0 || value >= EventPriority.values().length) { + return null; + } + return EventPriority.values()[value]; + } } diff --git a/src/main/java/com/clubobsidian/trident/MethodExecutor.java b/src/main/java/com/clubobsidian/trident/MethodExecutor.java index 8ab0be4..4557139 100644 --- a/src/main/java/com/clubobsidian/trident/MethodExecutor.java +++ b/src/main/java/com/clubobsidian/trident/MethodExecutor.java @@ -1,18 +1,18 @@ -/* - 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. -*/ +/* + * Copyright 2021 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; import java.lang.reflect.Method; @@ -21,47 +21,44 @@ * Abstract class for MethodExecutor implementations * to extend. {@link MethodExecutor#execute(Event)} * to be overridden in implementation classes. - * + * * @author virustotalop */ public abstract class MethodExecutor { - private Object listener; - private Method method; - private boolean ignoreCanceled; - public MethodExecutor(Object listener, Method method, boolean ignoreCanceled) - { - this.listener = listener; - this.method = method; - this.ignoreCanceled = ignoreCanceled; - } + private final Object listener; + private final Method method; + private final boolean ignoreCancelled; + + public MethodExecutor(Object listener, Method method, boolean ignoreCancelled) { + this.listener = listener; + this.method = method; + this.ignoreCancelled = ignoreCancelled; + } + + /** + * @return listener to execute events on + */ + public Object getListener() { + return this.listener; + } + + /** + * @return method to execute + */ + public Method getMethod() { + return this.method; + } + + /** + * @return whether or not the method executor is ignoring cancelled + */ + public boolean isIgnoringCancelled() { + return this.ignoreCancelled; + } - /** - * @return listener to execute events on - */ - public Object getListener() - { - return this.listener; - } - - /** - * @return method to execute - */ - public Method getMethod() - { - return this.method; - } - - /** - * @return whether or not the method executor is ignoring canceled - */ - public boolean isIgnoringCanceled() - { - return this.ignoreCanceled; - } - - /** - * @param event that is executed on the class listener - */ - public abstract void execute(Event event); + /** + * @param event that is executed on the class listener + */ + public abstract void execute(Event event); } diff --git a/src/main/java/com/clubobsidian/trident/event/DeadEvent.java b/src/main/java/com/clubobsidian/trident/event/DeadEvent.java index 37ee383..de48e28 100644 --- a/src/main/java/com/clubobsidian/trident/event/DeadEvent.java +++ b/src/main/java/com/clubobsidian/trident/event/DeadEvent.java @@ -1,18 +1,18 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.event; import com.clubobsidian.trident.Event; @@ -21,24 +21,24 @@ * Event that is called if an event is not * listened to by a listener. * Extends {@link Event} - * + * * @author virustotalop */ public class DeadEvent extends Event { - 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 this.event; - } + private final Event event; + + public DeadEvent(Event event) { + this.event = event; + } + + /** + * Returns an event that was + * not fired. + * + * @return event not fired + */ + public Event getDeadEvent() { + return this.event; + } } \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/eventbus/javassist/JavassistEventBus.java b/src/main/java/com/clubobsidian/trident/eventbus/javassist/JavassistEventBus.java index 3b5779c..589f87c 100644 --- a/src/main/java/com/clubobsidian/trident/eventbus/javassist/JavassistEventBus.java +++ b/src/main/java/com/clubobsidian/trident/eventbus/javassist/JavassistEventBus.java @@ -1,149 +1,124 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.eventbus.javassist; -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 com.clubobsidian.trident.Event; import com.clubobsidian.trident.EventBus; import com.clubobsidian.trident.MethodExecutor; +import javassist.*; -import javassist.CannotCompileException; -import javassist.ClassClassPath; -import javassist.ClassPool; -import javassist.CtClass; -import javassist.CtMethod; -import javassist.CtNewMethod; -import javassist.LoaderClassPath; -import javassist.NotFoundException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; /** * {@inheritDoc} */ public class JavassistEventBus extends EventBus { - private static ConcurrentMap map; - - static - { - JavassistEventBus.map = new ConcurrentHashMap<>(); - } - - private ClassPool pool; - public JavassistEventBus() - { - this.pool = new ClassPool(true); - this.setupPool(); - } - - private void setupPool() - { - this.addClassToPool(Event.class); - } - - public ClassPool getClassPool() - { - return this.pool; - } - - @Override - protected MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCanceled) - { - return this.generateMethodExecutor(listener, method, ignoreCanceled); - } - - private MethodExecutor generateMethodExecutor(Object listener, final Method method, final boolean ignoreCanceled) - { - if(listener == null || method == null) - return null; - - try - { - ClassLoader classLoader = listener.getClass().getClassLoader(); - - Class listenerClass = Class.forName(listener.getClass().getName(), true, classLoader); - - this.addClassToPool(Event.class); - - LoaderClassPath loaderClassPath = new LoaderClassPath(classLoader); - - this.pool.insertClassPath(loaderClassPath); - this.addClassToPool(listenerClass); - - String callbackClassName = listener.getClass().getName() + method.getName(); - - AtomicInteger collision = map.get(callbackClassName); - int classNum = -1; - if(collision == null) - { - collision = new AtomicInteger(0); - classNum = 0; - JavassistEventBus.map.put(callbackClassName, collision); - } - else - { - classNum = collision.incrementAndGet(); - } - - callbackClassName += classNum; - - 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(); - - StringBuilder sb = new StringBuilder(); - sb.append("public void execute(" + Event.class.getName() + " event)"); - sb.append("{"); - sb.append(eventType + " ev = " + "((" + eventType + ") event);"); - sb.append("((" + listenerType + ")" + "this.getListener())." + method.getName() + "(ev);"); - sb.append("}"); - - CtMethod call = CtNewMethod.make(sb.toString(), methodExecutorClass); - methodExecutorClass.addMethod(call); - - 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) - { - e.printStackTrace(); + private static final Map map = new ConcurrentHashMap<>(); + + private final ClassPool pool; + + public JavassistEventBus() { + this.pool = new ClassPool(true); + this.setupPool(); + } + + private void setupPool() { + this.addClassToPool(Event.class); + } + + public ClassPool getClassPool() { + return this.pool; + } + + @Override + protected MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCancelled) { + return this.generateMethodExecutor(listener, method, ignoreCancelled); + } + + private MethodExecutor generateMethodExecutor(Object listener, final Method method, final boolean ignoreCancelled) { + if (listener == null || method == null) { + return null; } - return null; - } - - private void addClassToPool(Class clazz) - { - ClassClassPath classClassPath = new ClassClassPath(clazz); - this.pool.insertClassPath(classClassPath); - } + + try { + ClassLoader classLoader = listener.getClass().getClassLoader(); + + Class listenerClass = Class.forName(listener.getClass().getName(), true, classLoader); + + this.addClassToPool(Event.class); + + LoaderClassPath loaderClassPath = new LoaderClassPath(classLoader); + + this.pool.insertClassPath(loaderClassPath); + this.addClassToPool(listenerClass); + + String callbackClassName = listener.getClass().getName() + method.getName(); + + AtomicInteger collision = map.get(callbackClassName); + int classNum = -1; + if (collision == null) { + collision = new AtomicInteger(0); + classNum = 0; + JavassistEventBus.map.put(callbackClassName, collision); + } else { + classNum = collision.incrementAndGet(); + } + + callbackClassName += classNum; + + 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(); + + StringBuilder sb = new StringBuilder(); + sb.append("public void execute(" + Event.class.getName() + " event)"); + sb.append("{"); + sb.append(eventType + " ev = " + "((" + eventType + ") event);"); + sb.append("((" + listenerType + ")" + "this.getListener())." + method.getName() + "(ev);"); + sb.append("}"); + + CtMethod call = CtNewMethod.make(sb.toString(), methodExecutorClass); + methodExecutorClass.addMethod(call); + + Class cl = methodExecutorClass.toClass(classLoader, JavassistEventBus.class.getProtectionDomain()); + return (MethodExecutor) cl.getDeclaredConstructors()[0].newInstance(listener, method, ignoreCancelled); + } catch (NotFoundException | CannotCompileException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException | ClassNotFoundException e) { + e.printStackTrace(); + } + return null; + } + + private void addClassToPool(Class clazz) { + ClassClassPath classClassPath = new ClassClassPath(clazz); + this.pool.insertClassPath(classClassPath); + } } \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/eventbus/methodhandle/MethodHandleEventBus.java b/src/main/java/com/clubobsidian/trident/eventbus/methodhandle/MethodHandleEventBus.java new file mode 100644 index 0000000..9e46284 --- /dev/null +++ b/src/main/java/com/clubobsidian/trident/eventbus/methodhandle/MethodHandleEventBus.java @@ -0,0 +1,38 @@ +/* + * Copyright 2021 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.eventbus.methodhandle; + +import com.clubobsidian.trident.EventBus; +import com.clubobsidian.trident.MethodExecutor; + +import java.lang.reflect.Method; + +/** + * {@inheritDoc} + */ +public class MethodHandleEventBus extends EventBus { + + @Override + protected MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCancelled) { + try { + return new MethodHandleExecutor(listener, method, ignoreCancelled); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/eventbus/methodhandle/MethodHandleExecutor.java b/src/main/java/com/clubobsidian/trident/eventbus/methodhandle/MethodHandleExecutor.java new file mode 100644 index 0000000..364f715 --- /dev/null +++ b/src/main/java/com/clubobsidian/trident/eventbus/methodhandle/MethodHandleExecutor.java @@ -0,0 +1,46 @@ +/* + * Copyright 2021 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.eventbus.methodhandle; + +import com.clubobsidian.trident.Event; +import com.clubobsidian.trident.MethodExecutor; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Method; + +/** + * {@inheritDoc} + */ +public class MethodHandleExecutor extends MethodExecutor { + + private final MethodHandle handle; + + public MethodHandleExecutor(Object listener, Method method, boolean ignoreCancelled) throws IllegalAccessException { + super(listener, method, ignoreCancelled); + this.handle = MethodHandles.lookup().unreflect(method); + } + + @Override + public void execute(final Event event) { + try { + this.handle.invoke(this.getListener(), event); + } catch (Throwable ex) { + ex.printStackTrace(); + } + } +} \ 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 index 857b273..a9f3400 100644 --- a/src/main/java/com/clubobsidian/trident/eventbus/package-info.java +++ b/src/main/java/com/clubobsidian/trident/eventbus/package-info.java @@ -1 +1,17 @@ +/* + * Copyright 2021 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.eventbus; \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionEventBus.java b/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionEventBus.java index 971cae2..06df6c0 100644 --- a/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionEventBus.java +++ b/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionEventBus.java @@ -1,33 +1,32 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.eventbus.reflection; -import java.lang.reflect.Method; - import com.clubobsidian.trident.EventBus; import com.clubobsidian.trident.MethodExecutor; +import java.lang.reflect.Method; + /** * {@inheritDoc} */ public class ReflectionEventBus extends EventBus { - @Override - protected MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCanceled) - { - return new ReflectionMethodExecutor(listener, method, ignoreCanceled); - } + @Override + protected MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCancelled) { + return new ReflectionMethodExecutor(listener, method, ignoreCancelled); + } } \ No newline at end of file diff --git a/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionMethodExecutor.java b/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionMethodExecutor.java index dbba86a..b2ec6d1 100644 --- a/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionMethodExecutor.java +++ b/src/main/java/com/clubobsidian/trident/eventbus/reflection/ReflectionMethodExecutor.java @@ -1,46 +1,41 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.eventbus.reflection; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - import com.clubobsidian.trident.Event; import com.clubobsidian.trident.MethodExecutor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + /** * {@inheritDoc} */ public class ReflectionMethodExecutor extends MethodExecutor { - public ReflectionMethodExecutor(Object listener, Method method, boolean ignoreCanceled) - { - super(listener, method, ignoreCanceled); - } - - @Override - public void execute(final Event event) - { - try - { - this.getMethod().invoke(this.getListener(), event); - } - catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) - { - e.printStackTrace(); - } - } + public ReflectionMethodExecutor(Object listener, Method method, boolean ignoreCancelled) { + super(listener, method, ignoreCancelled); + } + + @Override + public void execute(final Event event) { + try { + this.getMethod().invoke(this.getListener(), event); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + 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 f0f423c..ce2a4b8 100644 --- a/src/main/java/com/clubobsidian/trident/util/ClassUtil.java +++ b/src/main/java/com/clubobsidian/trident/util/ClassUtil.java @@ -1,51 +1,49 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.util; import com.clubobsidian.trident.Event; /** * Utility class for class based operations. - * + * * @author virustotalop */ public final class ClassUtil { - private ClassUtil() {} + private ClassUtil() { + } - /** - * Checks to see if a given class has {@link Event} - * as a super class. Uses reflection to recursively - * call {@link Class#getSuperclass()} to check - * the super class. - * - * @param cl class to check for an event super class - * @return whether or not the given class has {@link Event} - * as a super class. - */ - public static boolean hasEventSuperClass(Class cl) - { - Class superClass = cl; - while((superClass = superClass.getSuperclass()) != null) - { - if(superClass == Event.class) - { - return true; - } - } - return false; - } + /** + * Checks to see if a given class has {@link Event} + * as a super class. Uses reflection to recursively + * call {@link Class#getSuperclass()} to check + * the super class. + * + * @param cl class to check for an event super class + * @return whether or not the given class has {@link Event} + * as a super class. + */ + public static boolean hasEventSuperClass(Class cl) { + Class superClass = cl; + while ((superClass = superClass.getSuperclass()) != null) { + if (superClass == Event.class) { + return true; + } + } + return false; + } } diff --git a/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java b/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java index 9040dd2..e00a981 100644 --- a/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java +++ b/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java @@ -1,174 +1,147 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.util; -import java.io.Serializable; - import com.clubobsidian.trident.EventPriority; import com.clubobsidian.trident.MethodExecutor; -/** - * Class for implementing a doubly linked list +import java.io.Serializable; + +/** + * Class for implementing a doubly linked list * that orders the list based off of creation time and {@link EventPriority}. - * + * * @author virustotalop */ public class EventDoublyLinkedList implements Serializable { - /** - * - */ - private static final long serialVersionUID = 6359060072540225110L; + /** + * + */ + private static final long serialVersionUID = 6359060072540225110L; + + private EventNode head; + + public EventDoublyLinkedList() { + this.head = null; + } + + /** + * @return the first node in the EventDoublyLinkedList + */ + public synchronized EventNode getHead() { + return this.head; + } + + /** + * @param executor executor to insert + * @param priority priority of the MethodExecutor + * @return EventNode created or null if not created + */ + public synchronized EventNode insert(final MethodExecutor executor, final EventPriority priority) { + if (executor == null || priority == null) + return null; + + int priorityValue = priority.getValue(); + EventNode newNode = new EventNode(executor, priorityValue); + if (this.head == null) { + this.head = new EventNode(executor, priorityValue); + return newNode; + } + + EventNode found = this.findInsertionNode(priorityValue); + if (found.equals(this.head) && priorityValue < this.head.getPriority()) { + EventNode oldHead = this.head; + this.head = newNode; + this.head.setNext(oldHead); + oldHead.setPrev(this.head); + return newNode; + } else if (found.getPriority() > priorityValue) { + newNode.setNext(found); + newNode.setPrev(found.getPrev()); + newNode.getPrev().setNext(newNode); + found.setPrev(newNode); + return newNode; + } else if (found.getNext() == null) { + found.setNext(newNode); + newNode.setPrev(found); + return newNode; + } else { + newNode.setNext(found.getNext()); + found.setNext(newNode); + newNode.getNext().setPrev(newNode); + return newNode; + } + } - private EventNode head; - public EventDoublyLinkedList() - { - this.head = null; - } - - /** - * @return the first node in the EventDoublyLinkedList - */ - public synchronized EventNode getHead() - { - return this.head; - } + /** + * @param executor MethodExecutor to remove + * @return the EventNode removed or null if not found + */ + public synchronized EventNode remove(final MethodExecutor executor) { + EventNode found = this.find(executor); + if (found == null) + return null; - /** - * @param executor executor to insert - * @param priority priority of the MethodExecutor - * @return EventNode created or null if not created - */ - public synchronized EventNode insert(final MethodExecutor executor, final EventPriority priority) - { - if(executor == null || priority == null) - return null; + if (found.equals(this.head)) { + if (this.head.getNext() == null) { + this.head = null; + } else { + this.head = this.head.getNext(); + } + return found; + } else if (found.getNext() == null) { + found.getPrev().setNext(null); + return found; + } else { + found.getPrev().setNext(found.getNext()); + return found; + } + } - int priorityValue = priority.getValue(); - EventNode newNode = new EventNode(executor, priorityValue); - if(this.head == null) - { - this.head = new EventNode(executor, priorityValue); - return newNode; - } + /** + * @param executor to find + * @return found EventNode + */ + public synchronized EventNode find(final MethodExecutor executor) { + EventNode node = this.head; + while (node != null) { + if (node.getData().equals(executor)) + return node; + node = node.getNext(); + } + return null; + } - EventNode found = this.findInsertionNode(priorityValue); - if(found.equals(this.head) && priorityValue < this.head.getPriority()) - { - EventNode oldHead = this.head; - this.head = newNode; - this.head.setNext(oldHead); - oldHead.setPrev(this.head); - return newNode; - } - else if(found.getPriority() > priorityValue) - { - newNode.setNext(found); - newNode.setPrev(found.getPrev()); - newNode.getPrev().setNext(newNode); - found.setPrev(newNode); - return newNode; - } - else if(found.getNext() == null) - { - found.setNext(newNode); - newNode.setPrev(found); - return newNode; - } - else - { - newNode.setNext(found.getNext()); - found.setNext(newNode); - newNode.getNext().setPrev(newNode); - return newNode; - } - } - - /** - * @param executor MethodExecutor to remove - * @return the EventNode removed or null if not found - */ - public synchronized EventNode remove(final MethodExecutor executor) - { - EventNode found = this.find(executor); - if(found == null) - return null; - - if(found.equals(this.head)) - { - if(this.head.getNext() == null) - { - this.head = null; - } - else - { - this.head = this.head.getNext(); - } - return found; - } - else if(found.getNext() == null) - { - found.getPrev().setNext(null); - return found; - } - else - { - found.getPrev().setNext(found.getNext()); - return found; - } - } - - /** - * @param executor to find - * @return found EventNode - */ - public synchronized EventNode find(final MethodExecutor executor) - { - EventNode node = this.head; - while(node != null) - { - if(node.getData().equals(executor)) - return node; - node = node.getNext(); - } - return null; - } - - /** - * @return the node to be inserted around - */ - private EventNode findInsertionNode(final int priorityValue) - { - EventNode next = this.head; - while(next != null) - { - if(next.getNext() == null) //if tail - { - return next; - } - else if(next.getPriority() == priorityValue && next.getNext().getPriority() > priorityValue) - { - return next; - } - else if(next.getPriority() > priorityValue) - { - return next; - } - next = next.getNext(); - } - return null; - } + /** + * @return the node to be inserted around + */ + private EventNode findInsertionNode(final int priorityValue) { + EventNode next = this.head; + while (next != null) { + if (next.getNext() == null) //if tail + { + return next; + } else if (next.getPriority() == priorityValue && next.getNext().getPriority() > priorityValue) { + return next; + } else if (next.getPriority() > priorityValue) { + return next; + } + next = next.getNext(); + } + return null; + } } diff --git a/src/main/java/com/clubobsidian/trident/util/EventNode.java b/src/main/java/com/clubobsidian/trident/util/EventNode.java index 0b7fc55..9a3bc0f 100644 --- a/src/main/java/com/clubobsidian/trident/util/EventNode.java +++ b/src/main/java/com/clubobsidian/trident/util/EventNode.java @@ -1,17 +1,18 @@ -/* 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. -*/ +/* + * Copyright 2021 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.util; import com.clubobsidian.trident.MethodExecutor; @@ -19,66 +20,59 @@ /** * Node class for events. Used * in {@link com.clubobsidian.trident.util.EventDoublyLinkedList} - * + * * @author virustotalop */ -public class EventNode -{ - private MethodExecutor data; - private int priority; - private EventNode next; - private EventNode prev; - public EventNode(MethodExecutor data, int priority) - { - this.data = data; - this.priority = priority; - } +public class EventNode { + private MethodExecutor data; + private int priority; + private EventNode next; + private EventNode prev; + + public EventNode(MethodExecutor data, int priority) { + this.data = data; + this.priority = priority; + } - /** - * @return MethodExecutor for the node - */ - public MethodExecutor getData() - { - return this.data; - } + /** + * @return MethodExecutor for the node + */ + public MethodExecutor getData() { + return this.data; + } - /** - * @return integer representation of the {@link com.clubobsidian.trident.Event}'s priority - */ - public int getPriority() - { - return this.priority; - } + /** + * @return integer representation of the {@link com.clubobsidian.trident.Event}'s priority + */ + public int getPriority() { + return this.priority; + } - /** - * @return node next to this node - */ - public synchronized EventNode getNext() - { - return this.next; - } + /** + * @return node next to this node + */ + public synchronized EventNode getNext() { + return this.next; + } - /** - * @param next node to be set as the next node - */ - public synchronized void setNext(final EventNode next) - { - this.next = next; - } + /** + * @param next node to be set as the next node + */ + public synchronized void setNext(final EventNode next) { + this.next = next; + } - /** - * @return node behind this node - */ - public synchronized EventNode getPrev() - { - return this.prev; - } + /** + * @return node behind this node + */ + public synchronized EventNode getPrev() { + return this.prev; + } - /** - * @param prev node to be set as the previous node - */ - public synchronized void setPrev(final EventNode prev) - { - this.prev = prev; - } + /** + * @param prev node to be set as the previous node + */ + public synchronized void setPrev(final EventNode prev) { + this.prev = prev; + } } diff --git a/src/test/java/com/clubobsidian/trident/test/DeadEventTest.java b/src/test/java/com/clubobsidian/trident/test/DeadEventTest.java index 6681cf8..54f45c7 100644 --- a/src/test/java/com/clubobsidian/trident/test/DeadEventTest.java +++ b/src/test/java/com/clubobsidian/trident/test/DeadEventTest.java @@ -1,38 +1,51 @@ -package com.clubobsidian.trident.test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +/* + * Copyright 2021 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. + */ -import org.junit.Test; +package com.clubobsidian.trident.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; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; 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); - } - + @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/com/clubobsidian/trident/test/EventBusTest.java b/src/test/java/com/clubobsidian/trident/test/EventBusTest.java index faaae2e..759dba5 100644 --- a/src/test/java/com/clubobsidian/trident/test/EventBusTest.java +++ b/src/test/java/com/clubobsidian/trident/test/EventBusTest.java @@ -1,121 +1,128 @@ -package com.clubobsidian.trident.test; +/* + * Copyright 2021 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. + */ -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.junit.Test; +package com.clubobsidian.trident.test; import com.clubobsidian.trident.Event; import com.clubobsidian.trident.EventBus; -import com.clubobsidian.trident.test.impl.TestCancelableEvent; +import com.clubobsidian.trident.test.impl.TestCancellableEvent; import com.clubobsidian.trident.test.impl.TestEvent; import com.clubobsidian.trident.test.impl.TestListener; import com.clubobsidian.trident.test.impl.TestOrderEvent; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public abstract class EventBusTest { - @Test - public void testEventFiring() - { - TestListener test = new TestListener("test"); - EventBus manager = this.createNewEventBus(); - 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"); - EventBus manager = this.createNewEventBus(); - 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(""); - EventBus manager = this.createNewEventBus(); - 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"); - EventBus manager = this.createNewEventBus(); - manager.registerEvents(test); - - assertFalse("Event was double registered", manager.registerEvents(test)); - } - - @Test - public void testCalledEvent() - { - Event event = new TestEvent(); - EventBus manager = this.createNewEventBus(); - - assertFalse("Event call ran when event did not exist", manager.callEvent(event)); - } - - @Test - public void testUnregister() - { - TestListener test = new TestListener("test"); - 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 {} + @Test + public void testEventFiring() { + TestListener test = new TestListener("test"); + EventBus manager = this.createNewEventBus(); + 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"); + EventBus manager = this.createNewEventBus(); + manager.registerEvents(test); + + manager.callEvent(new TestCancellableEvent()); + + assertTrue("Test is not true, event was not cancelled", test.getTest()); + } + + @Test + public void testOrder() { + TestListener test = new TestListener(""); + EventBus manager = this.createNewEventBus(); + 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"); + EventBus manager = this.createNewEventBus(); + manager.registerEvents(test); + + assertFalse("Event was double registered", manager.registerEvents(test)); + } + + @Test + public void testCalledEvent() { + Event event = new TestEvent(); + EventBus manager = this.createNewEventBus(); + + assertFalse("Event call ran when event did not exist", manager.callEvent(event)); + } + + @Test + public void testUnregister() { + TestListener test = new TestListener("test"); + 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/com/clubobsidian/trident/test/EventDoublyLinkedListTest.java b/src/test/java/com/clubobsidian/trident/test/EventDoublyLinkedListTest.java index a10aa5e..8a55ab6 100644 --- a/src/test/java/com/clubobsidian/trident/test/EventDoublyLinkedListTest.java +++ b/src/test/java/com/clubobsidian/trident/test/EventDoublyLinkedListTest.java @@ -1,28 +1,20 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.assertTrue; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Random; - -import org.junit.Test; - import com.clubobsidian.trident.EventPriority; import com.clubobsidian.trident.MethodExecutor; import com.clubobsidian.trident.test.impl.TestEvent; @@ -30,303 +22,270 @@ import com.clubobsidian.trident.test.impl.TestMethodExecutor; import com.clubobsidian.trident.util.EventDoublyLinkedList; import com.clubobsidian.trident.util.EventNode; +import org.junit.Test; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Random; + +import static org.junit.Assert.assertTrue; public class EventDoublyLinkedListTest { - @Test - public void testOrder() - { - try - { - EventDoublyLinkedList list = new EventDoublyLinkedList(); - - TestListener test = new TestListener("test"); - 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); - - assertTrue("Low is not head", list.getHead().getData().equals(executor)); - - - TestListener test2 = new TestListener("test2"); - 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); - - assertTrue("Lowest is not head", list.getHead().getData().equals(executor2)); - - - TestListener test3 = new TestListener("test3"); - 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); - - assertTrue("NextLowest is not next to head", list.getHead().getNext().getData().equals(executor3)); - - - TestListener test4 = new TestListener("test4"); - 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); - - assertTrue("Monitor is not tail", list.find(executor4).getData().equals(executor4)); - - - TestListener test5 = new TestListener("test5"); - 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 = 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); - - assertTrue("Normal next node is not priority high", list.find(executor6).getNext().getData().equals(executor5)); - - - TestListener test7 = new TestListener("test7"); - 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); - - assertTrue("Highest next node is not priority monitor", list.find(executor7).getNext().getData().equals(executor4)); - } - catch (NoSuchMethodException | SecurityException e) - { - e.printStackTrace(); - } - } - - @Test - public void testRemoval() - { - try - { - EventDoublyLinkedList list = new EventDoublyLinkedList(); - TestListener test = new TestListener("test1"); - 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)); - list.remove(executor); - assertTrue("Head is not null after monitor removal", list.getHead() == null); - - for(int i = EventPriority.values()[0].getValue(); i < (EventPriority.values()[EventPriority.values().length - 1]).getValue() + 1; i++) - { - list.insert(new TestMethodExecutor(new TestListener("test" + i), test.getClass().getDeclaredMethod("test", TestEvent.class) , false), EventPriority.getByValue(i)); - } - - EventNode node = list.getHead(); - int removalCount = 0; - while(node != null) - { - list.remove(node.getData()); - removalCount += 1; - node = node.getNext(); - } - - assertTrue("Head is not null after traversing after removing all node", list.getHead() == null); - assertTrue("Removed nodes are not equal to EventPriority length", EventPriority.values().length == removalCount); - - for(int i = (EventPriority.values()[EventPriority.values().length - 1]).getValue(); i > -1 ; i--) - { - list.insert(new TestMethodExecutor(new TestListener("test" + i), test.getClass().getDeclaredMethod("test", TestEvent.class), false), EventPriority.getByValue(i)); - } - - node = list.getHead(); - removalCount = 0; - while(node != null) - { - list.remove(node.getData()); - removalCount += 1; - node = node.getNext(); - } - - assertTrue("Head is not null after traversing after removing all nodes reverse", list.getHead() == null); - assertTrue("Removed nodes are not equal to EventPriority length", EventPriority.values().length == removalCount); - - Random rand = new Random(); - - int iterate = 10000; - for(int i = 0; i < iterate; i++) - { - - rand.setSeed(System.nanoTime()); - int next = rand.nextInt(6); - 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); - } - - node = list.getHead(); - removalCount = 0; - while(node != null) - { - list.remove(node.getData()); - removalCount += 1; - node = node.getNext(); - } - - assertTrue("Head is not null after traversing after removing all nodes after random insert", list.getHead() == null); - assertTrue("Removed nodes are not equal to iterating length after random insert", iterate == removalCount); - } - catch (NoSuchMethodException | SecurityException e) - { - e.printStackTrace(); - } - } - - @Test - public void emptyLinkedListTest() - { - EventDoublyLinkedList list = new EventDoublyLinkedList(); - TestListener test = new TestListener("test1"); - try - { - MethodExecutor executor = new TestMethodExecutor(test, test.getClass().getDeclaredMethod("test", TestEvent.class), false); - assertTrue("Executor was removed", list.remove(executor) == null); - } - catch (NoSuchMethodException | SecurityException e) - { - e.printStackTrace(); - } - } - - @Test - public void nextIsNullAndNotHeadRemove() - { - try - { - EventDoublyLinkedList list = new EventDoublyLinkedList(); - TestListener test1 = new TestListener("test1"); - MethodExecutor executor1 = new TestMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); - - list.insert(executor1, EventPriority.LOWEST); - - TestListener test2 = new TestListener("test2"); - MethodExecutor executor2 = new TestMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); - - list.insert(executor2, EventPriority.LOW); - - EventNode removed = list.remove(executor2); - - assertTrue("EventNode data was not equal", removed.getData().equals(executor2)); - - assertTrue("EventNode previous node's next node was not null", removed.getPrev().getNext() == null); - } - catch (NoSuchMethodException | SecurityException e) - { - e.printStackTrace(); - } - } - - @Test - public void foundIsMiddleNodeRemoveTest() - { - try - { - EventDoublyLinkedList list = new EventDoublyLinkedList(); - TestListener test1 = new TestListener("test1"); - MethodExecutor executor1 = new TestMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); - - list.insert(executor1, EventPriority.LOWEST); - - TestListener test2 = new TestListener("test2"); - MethodExecutor executor2 = new TestMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); - - list.insert(executor2, EventPriority.LOW); - - TestListener test3 = new TestListener("test3"); - MethodExecutor executor3 = new TestMethodExecutor(test3, test3.getClass().getDeclaredMethod("test", TestEvent.class), false); - - list.insert(executor3, EventPriority.NORMAL); - - EventNode removed = list.remove(executor2); - - assertTrue("Previous node's next node was not set to removed next node", removed.getPrev().getNext().equals(removed.getNext())); - - } - catch (NoSuchMethodException | SecurityException e) - { - e.printStackTrace(); - } - } - - @Test - public void findMethodExecutorThatDoesNotExistTest() - { - try - { - EventDoublyLinkedList list = new EventDoublyLinkedList(); - TestListener test1 = new TestListener("test1"); - 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) - { - e.printStackTrace(); - } - } - - @Test //This should never happen under normal circumstances but is being used for code coverage - public void findInsertNull() - { - try - { - EventDoublyLinkedList list = new EventDoublyLinkedList(); - - Method findInsertion = list.getClass().getDeclaredMethod("findInsertionNode", int.class); - findInsertion.setAccessible(true); - assertTrue("Null node was not found", findInsertion.invoke(list, 6) == null); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) - { - e.printStackTrace(); - } - } - - @Test - public void findIfFoundNodePriorityIsTheSameTest() - { - try - { - EventDoublyLinkedList list = new EventDoublyLinkedList(); - TestListener test1 = new TestListener("test1"); - MethodExecutor executor1 = new TestMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); - - list.insert(executor1, EventPriority.LOWEST); - - TestListener test2 = new TestListener("test2"); - MethodExecutor executor2 = new TestMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); - - - EventPriority priority = EventPriority.LOWEST; - EventNode inserted = list.insert(executor2, priority); - - assertTrue("Previous node's priority is not the same", inserted.getPriority() == priority.getValue()); - } - catch (NoSuchMethodException | SecurityException e) - { - e.printStackTrace(); - } - } - - @Test - public void methodExecutorNullTest() - { - EventDoublyLinkedList list = new EventDoublyLinkedList(); - EventNode inserted = list.insert(null, 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); - } + @Test + public void testOrder() { + try { + EventDoublyLinkedList list = new EventDoublyLinkedList(); + + TestListener test = new TestListener("test"); + 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); + + assertTrue("Low is not head", list.getHead().getData().equals(executor)); + + + TestListener test2 = new TestListener("test2"); + 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); + + assertTrue("Lowest is not head", list.getHead().getData().equals(executor2)); + + + TestListener test3 = new TestListener("test3"); + 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); + + assertTrue("NextLowest is not next to head", list.getHead().getNext().getData().equals(executor3)); + + + TestListener test4 = new TestListener("test4"); + 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); + + assertTrue("Monitor is not tail", list.find(executor4).getData().equals(executor4)); + + + TestListener test5 = new TestListener("test5"); + 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 = 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); + + assertTrue("Normal next node is not priority high", list.find(executor6).getNext().getData().equals(executor5)); + + + TestListener test7 = new TestListener("test7"); + 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); + + assertTrue("Highest next node is not priority monitor", list.find(executor7).getNext().getData().equals(executor4)); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + } + + @Test + public void testRemoval() { + try { + EventDoublyLinkedList list = new EventDoublyLinkedList(); + TestListener test = new TestListener("test1"); + 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)); + list.remove(executor); + assertTrue("Head is not null after monitor removal", list.getHead() == null); + + for (int i = EventPriority.values()[0].getValue(); i < (EventPriority.values()[EventPriority.values().length - 1]).getValue() + 1; i++) { + list.insert(new TestMethodExecutor(new TestListener("test" + i), test.getClass().getDeclaredMethod("test", TestEvent.class), false), EventPriority.getByValue(i)); + } + + EventNode node = list.getHead(); + int removalCount = 0; + while (node != null) { + list.remove(node.getData()); + removalCount += 1; + node = node.getNext(); + } + + assertTrue("Head is not null after traversing after removing all node", list.getHead() == null); + assertTrue("Removed nodes are not equal to EventPriority length", EventPriority.values().length == removalCount); + + for (int i = (EventPriority.values()[EventPriority.values().length - 1]).getValue(); i > -1; i--) { + list.insert(new TestMethodExecutor(new TestListener("test" + i), test.getClass().getDeclaredMethod("test", TestEvent.class), false), EventPriority.getByValue(i)); + } + + node = list.getHead(); + removalCount = 0; + while (node != null) { + list.remove(node.getData()); + removalCount += 1; + node = node.getNext(); + } + + assertTrue("Head is not null after traversing after removing all nodes reverse", list.getHead() == null); + assertTrue("Removed nodes are not equal to EventPriority length", EventPriority.values().length == removalCount); + + Random rand = new Random(); + + int iterate = 10000; + for (int i = 0; i < iterate; i++) { + + rand.setSeed(System.nanoTime()); + int next = rand.nextInt(6); + 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); + } + + node = list.getHead(); + removalCount = 0; + while (node != null) { + list.remove(node.getData()); + removalCount += 1; + node = node.getNext(); + } + + assertTrue("Head is not null after traversing after removing all nodes after random insert", list.getHead() == null); + assertTrue("Removed nodes are not equal to iterating length after random insert", iterate == removalCount); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + } + + @Test + public void emptyLinkedListTest() { + EventDoublyLinkedList list = new EventDoublyLinkedList(); + TestListener test = new TestListener("test1"); + try { + MethodExecutor executor = new TestMethodExecutor(test, test.getClass().getDeclaredMethod("test", TestEvent.class), false); + assertTrue("Executor was removed", list.remove(executor) == null); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + } + + @Test + public void nextIsNullAndNotHeadRemove() { + try { + EventDoublyLinkedList list = new EventDoublyLinkedList(); + TestListener test1 = new TestListener("test1"); + MethodExecutor executor1 = new TestMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); + + list.insert(executor1, EventPriority.LOWEST); + + TestListener test2 = new TestListener("test2"); + MethodExecutor executor2 = new TestMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); + + list.insert(executor2, EventPriority.LOW); + + EventNode removed = list.remove(executor2); + + assertTrue("EventNode data was not equal", removed.getData().equals(executor2)); + + assertTrue("EventNode previous node's next node was not null", removed.getPrev().getNext() == null); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + } + + @Test + public void foundIsMiddleNodeRemoveTest() { + try { + EventDoublyLinkedList list = new EventDoublyLinkedList(); + TestListener test1 = new TestListener("test1"); + MethodExecutor executor1 = new TestMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); + + list.insert(executor1, EventPriority.LOWEST); + + TestListener test2 = new TestListener("test2"); + MethodExecutor executor2 = new TestMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); + + list.insert(executor2, EventPriority.LOW); + + TestListener test3 = new TestListener("test3"); + MethodExecutor executor3 = new TestMethodExecutor(test3, test3.getClass().getDeclaredMethod("test", TestEvent.class), false); + + list.insert(executor3, EventPriority.NORMAL); + + EventNode removed = list.remove(executor2); + + assertTrue("Previous node's next node was not set to removed next node", removed.getPrev().getNext().equals(removed.getNext())); + + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + } + + @Test + public void findMethodExecutorThatDoesNotExistTest() { + try { + EventDoublyLinkedList list = new EventDoublyLinkedList(); + TestListener test1 = new TestListener("test1"); + 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) { + e.printStackTrace(); + } + } + + @Test //This should never happen under normal circumstances but is being used for code coverage + public void findInsertNull() { + try { + EventDoublyLinkedList list = new EventDoublyLinkedList(); + + Method findInsertion = list.getClass().getDeclaredMethod("findInsertionNode", int.class); + findInsertion.setAccessible(true); + assertTrue("Null node was not found", findInsertion.invoke(list, 6) == null); + } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + + @Test + public void findIfFoundNodePriorityIsTheSameTest() { + try { + EventDoublyLinkedList list = new EventDoublyLinkedList(); + TestListener test1 = new TestListener("test1"); + MethodExecutor executor1 = new TestMethodExecutor(test1, test1.getClass().getDeclaredMethod("test", TestEvent.class), false); + + list.insert(executor1, EventPriority.LOWEST); + + TestListener test2 = new TestListener("test2"); + MethodExecutor executor2 = new TestMethodExecutor(test2, test2.getClass().getDeclaredMethod("test", TestEvent.class), false); + + + EventPriority priority = EventPriority.LOWEST; + EventNode inserted = list.insert(executor2, priority); + + assertTrue("Previous node's priority is not the same", inserted.getPriority() == priority.getValue()); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + } + + @Test + public void methodExecutorNullTest() { + EventDoublyLinkedList list = new EventDoublyLinkedList(); + EventNode inserted = list.insert(null, 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 index 04bf09b..9bdf7c9 100644 --- a/src/test/java/com/clubobsidian/trident/test/EventPriorityTest.java +++ b/src/test/java/com/clubobsidian/trident/test/EventPriorityTest.java @@ -1,24 +1,37 @@ -package com.clubobsidian.trident.test; +/* + * Copyright 2021 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. + */ -import static org.junit.Assert.assertTrue; +package com.clubobsidian.trident.test; +import com.clubobsidian.trident.EventPriority; import org.junit.Test; -import com.clubobsidian.trident.EventPriority; +import static org.junit.Assert.assertTrue; 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); - } + @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 index 81873a1..97c889c 100644 --- a/src/test/java/com/clubobsidian/trident/test/EventTest.java +++ b/src/test/java/com/clubobsidian/trident/test/EventTest.java @@ -1,18 +1,32 @@ -package com.clubobsidian.trident.test; - -import static org.junit.Assert.assertTrue; +/* + * Copyright 2021 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. + */ -import org.junit.Test; +package com.clubobsidian.trident.test; import com.clubobsidian.trident.Event; import com.clubobsidian.trident.test.impl.TestEvent; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; public class EventTest { - @Test - public void testName() - { - Event event = new TestEvent(); - assertTrue("Event name is not \"TestEvent\"", event.getName().equals("TestEvent")); - } + @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/com/clubobsidian/trident/test/IgnoreCanceledTest.java b/src/test/java/com/clubobsidian/trident/test/IgnoreCanceledTest.java deleted file mode 100644 index e96b68d..0000000 --- a/src/test/java/com/clubobsidian/trident/test/IgnoreCanceledTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - 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.assertTrue; - -import org.junit.Test; - -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 { - - @Test - public void ignoredCanceled() - { - TestListenerIgnore listener = new TestListenerIgnore(); - 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/IgnoreCancelledTest.java b/src/test/java/com/clubobsidian/trident/test/IgnoreCancelledTest.java new file mode 100644 index 0000000..741176f --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/IgnoreCancelledTest.java @@ -0,0 +1,39 @@ +/* + * Copyright 2021 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 com.clubobsidian.trident.EventBus; +import com.clubobsidian.trident.eventbus.javassist.JavassistEventBus; +import com.clubobsidian.trident.test.impl.TestCancellableEvent; +import com.clubobsidian.trident.test.impl.TestListenerIgnore; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class IgnoreCancelledTest { + + @Test + public void ignoredCancelled() { + TestListenerIgnore listener = new TestListenerIgnore(); + EventBus manager = new JavassistEventBus(); + manager.registerEvents(listener); + manager.callEvent(new TestCancellableEvent()); + + assertTrue("Event was not cancelled", listener.isCancelled()); + 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 index 84bce65..230f779 100644 --- a/src/test/java/com/clubobsidian/trident/test/JavaAssistEventBusTest.java +++ b/src/test/java/com/clubobsidian/trident/test/JavaAssistEventBusTest.java @@ -1,22 +1,26 @@ -/* - 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. -*/ +/* + * Copyright 2021 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 com.clubobsidian.trident.EventBus; +import com.clubobsidian.trident.eventbus.javassist.JavassistEventBus; +import com.clubobsidian.trident.test.impl.TestListener; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -25,69 +29,54 @@ 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; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; @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")); + @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"); - 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(); + } + } - 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(); - } + @Override + protected EventBus createNewEventBus() { + return new JavassistEventBus(); + } } \ No newline at end of file diff --git a/src/test/java/com/clubobsidian/trident/test/MethodHandleEventBusTest.java b/src/test/java/com/clubobsidian/trident/test/MethodHandleEventBusTest.java new file mode 100644 index 0000000..fa9531a --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/MethodHandleEventBusTest.java @@ -0,0 +1,28 @@ +/* + * Copyright 2021 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 com.clubobsidian.trident.EventBus; +import com.clubobsidian.trident.eventbus.methodhandle.MethodHandleEventBus; +import com.clubobsidian.trident.eventbus.reflection.ReflectionEventBus; + +public class MethodHandleEventBusTest extends EventBusTest { + + @Override + protected EventBus createNewEventBus() { + return new MethodHandleEventBus(); + } +} \ No newline at end of file diff --git a/src/test/java/com/clubobsidian/trident/test/MultiThreadingTest.java b/src/test/java/com/clubobsidian/trident/test/MultiThreadingTest.java index 04023bb..a9ce74e 100644 --- a/src/test/java/com/clubobsidian/trident/test/MultiThreadingTest.java +++ b/src/test/java/com/clubobsidian/trident/test/MultiThreadingTest.java @@ -1,10 +1,20 @@ -package com.clubobsidian.trident.test; - -import static org.junit.Assert.assertTrue; - -import java.util.concurrent.atomic.AtomicInteger; +/* + * Copyright 2021 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. + */ -import org.junit.Test; +package com.clubobsidian.trident.test; import com.clubobsidian.trident.EventPriority; import com.clubobsidian.trident.MethodExecutor; @@ -13,52 +23,49 @@ import com.clubobsidian.trident.test.impl.TestMethodExecutor; import com.clubobsidian.trident.util.EventDoublyLinkedList; import com.clubobsidian.trident.util.EventNode; +import org.junit.Test; + +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.Assert.assertTrue; public class MultiThreadingTest { - - @Test - public void multiThreadingTestEventDoublyLinkedList() - { - AtomicInteger count = new AtomicInteger(0); - final EventDoublyLinkedList list = new EventDoublyLinkedList(); - Runnable testRunnable = new Runnable() - { - @Override - public void run() - { - for(int i = 0; i < 1000; i++) - { - try - { - int inc = count.incrementAndGet(); - TestListener testListener = new TestListener("test" + inc); - 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); - } - catch (NoSuchMethodException | SecurityException e) - { - e.printStackTrace(); - } - } - Thread.currentThread().interrupt(); - } - }; - Thread th1 = new Thread(testRunnable); - Thread th2 = new Thread(testRunnable); - - th1.start(); - th2.start(); - - while(th1.isAlive() || th2.isAlive()); - - int nodeCount = 0; - EventNode node = list.getHead(); - while(node != null) - { - nodeCount+= 1; - node = node.getNext(); - } - assertTrue("Node count not equal when running from multiple thread", nodeCount == count.get()); - } + + @Test + public void multiThreadingTestEventDoublyLinkedList() { + AtomicInteger count = new AtomicInteger(0); + final EventDoublyLinkedList list = new EventDoublyLinkedList(); + Runnable testRunnable = new Runnable() { + @Override + public void run() { + for (int i = 0; i < 1000; i++) { + try { + int inc = count.incrementAndGet(); + TestListener testListener = new TestListener("test" + inc); + 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); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + } + Thread.currentThread().interrupt(); + } + }; + Thread th1 = new Thread(testRunnable); + Thread th2 = new Thread(testRunnable); + + th1.start(); + th2.start(); + + while (th1.isAlive() || th2.isAlive()) ; + + int nodeCount = 0; + EventNode node = list.getHead(); + while (node != null) { + nodeCount += 1; + node = node.getNext(); + } + 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 index 5d61a83..b7cf0d6 100644 --- a/src/test/java/com/clubobsidian/trident/test/OtherEventBusTest.java +++ b/src/test/java/com/clubobsidian/trident/test/OtherEventBusTest.java @@ -1,20 +1,34 @@ -package com.clubobsidian.trident.test; - -import static org.junit.Assert.assertFalse; +/* + * Copyright 2021 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. + */ -import org.junit.Test; +package com.clubobsidian.trident.test; import com.clubobsidian.trident.EventBus; import com.clubobsidian.trident.test.impl.TestEventBus; import com.clubobsidian.trident.test.impl.TestListener; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; 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); - } + @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/test/java/com/clubobsidian/trident/test/ReflectionEventBusTest.java b/src/test/java/com/clubobsidian/trident/test/ReflectionEventBusTest.java index 58b317c..0e7c556 100644 --- a/src/test/java/com/clubobsidian/trident/test/ReflectionEventBusTest.java +++ b/src/test/java/com/clubobsidian/trident/test/ReflectionEventBusTest.java @@ -1,18 +1,18 @@ -/* - 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. -*/ +/* + * Copyright 2021 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 com.clubobsidian.trident.EventBus; @@ -20,9 +20,8 @@ public class ReflectionEventBusTest extends EventBusTest { - @Override - protected EventBus createNewEventBus() - { - return new ReflectionEventBus(); - } + @Override + protected EventBus createNewEventBus() { + return new ReflectionEventBus(); + } } \ No newline at end of file diff --git a/src/test/java/com/clubobsidian/trident/test/ReflectionMiscEventTest.java b/src/test/java/com/clubobsidian/trident/test/ReflectionMiscEventTest.java index 1a00954..d2958f6 100644 --- a/src/test/java/com/clubobsidian/trident/test/ReflectionMiscEventTest.java +++ b/src/test/java/com/clubobsidian/trident/test/ReflectionMiscEventTest.java @@ -1,69 +1,60 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.assertTrue; - -import java.lang.reflect.Method; - -import org.junit.Test; - import com.clubobsidian.trident.MethodExecutor; 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; +import com.clubobsidian.trident.test.impl.TestWrongArgumentListener; +import org.junit.Test; + +import java.lang.reflect.Method; + +import static org.junit.Assert.assertTrue; public class ReflectionMiscEventTest { - @Test - public void methodExecutorTest() - { - try - { - TestListener listener = new TestListener("test"); - Method testEventMethod = listener.getClass().getDeclaredMethod("test", TestEvent.class); - MethodExecutor executor = new ReflectionMethodExecutor(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 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(); - } - } + @Test + public void methodExecutorTest() { + try { + TestListener listener = new TestListener("test"); + Method testEventMethod = listener.getClass().getDeclaredMethod("test", TestEvent.class); + MethodExecutor executor = new ReflectionMethodExecutor(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 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/com/clubobsidian/trident/test/UtilTest.java b/src/test/java/com/clubobsidian/trident/test/UtilTest.java index 1dd87a2..50d7794 100644 --- a/src/test/java/com/clubobsidian/trident/test/UtilTest.java +++ b/src/test/java/com/clubobsidian/trident/test/UtilTest.java @@ -1,39 +1,37 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.assertTrue; -import static org.junit.Assert.assertFalse; - -import org.junit.Test; - import com.clubobsidian.trident.Event; import com.clubobsidian.trident.test.impl.TestEvent; import com.clubobsidian.trident.util.ClassUtil; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class UtilTest { - @Test - public void classUtilTest() - { - Event event = new TestEvent(); - boolean superClass = ClassUtil.hasEventSuperClass(event.getClass()); - assertTrue("Super class for TestEvent was not event", superClass); - - superClass = ClassUtil.hasEventSuperClass(String.class); - assertFalse("Super class is not null for string class", superClass); - } + @Test + public void classUtilTest() { + Event event = new TestEvent(); + boolean superClass = ClassUtil.hasEventSuperClass(event.getClass()); + assertTrue("Super class for TestEvent was not event", superClass); + + superClass = ClassUtil.hasEventSuperClass(String.class); + assertFalse("Super class is not null for string class", superClass); + } } diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestCancelableEvent.java b/src/test/java/com/clubobsidian/trident/test/impl/TestCancelableEvent.java deleted file mode 100644 index 8725695..0000000 --- a/src/test/java/com/clubobsidian/trident/test/impl/TestCancelableEvent.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - 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.impl; - -import com.clubobsidian.trident.Cancelable; - -public class TestCancelableEvent extends TestEvent implements Cancelable { - - private boolean canceled; - - @Override - public boolean isCanceled() - { - return this.canceled; - } - - public void setCanceled(boolean cancelled) - { - this.canceled = cancelled; - } -} diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestCancellableEvent.java b/src/test/java/com/clubobsidian/trident/test/impl/TestCancellableEvent.java new file mode 100644 index 0000000..9d6e524 --- /dev/null +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestCancellableEvent.java @@ -0,0 +1,32 @@ +/* + * Copyright 2021 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.impl; + +import com.clubobsidian.trident.Cancellable; + +public class TestCancellableEvent extends TestEvent implements Cancellable { + + private boolean cancelled; + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } +} diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestDeadEventListener.java b/src/test/java/com/clubobsidian/trident/test/impl/TestDeadEventListener.java index 76e551f..7d57714 100644 --- a/src/test/java/com/clubobsidian/trident/test/impl/TestDeadEventListener.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestDeadEventListener.java @@ -1,3 +1,19 @@ +/* + * Copyright 2021 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.impl; import com.clubobsidian.trident.EventHandler; @@ -5,22 +21,20 @@ 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; - } + 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/com/clubobsidian/trident/test/impl/TestEvent.java b/src/test/java/com/clubobsidian/trident/test/impl/TestEvent.java index abda403..5dd1408 100644 --- a/src/test/java/com/clubobsidian/trident/test/impl/TestEvent.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestEvent.java @@ -1,25 +1,24 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.impl; /** - * * Extends TestEventSuper, is used partially in testing of getting * the super class for the event class. - * */ -public class TestEvent extends TestEventSuper {} +public class TestEvent extends TestEventSuper { +} diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestEventBus.java b/src/test/java/com/clubobsidian/trident/test/impl/TestEventBus.java index 667bc1c..ef969bc 100644 --- a/src/test/java/com/clubobsidian/trident/test/impl/TestEventBus.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestEventBus.java @@ -1,15 +1,30 @@ -package com.clubobsidian.trident.test.impl; +/* + * Copyright 2021 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. + */ -import java.lang.reflect.Method; +package com.clubobsidian.trident.test.impl; import com.clubobsidian.trident.EventBus; import com.clubobsidian.trident.MethodExecutor; +import java.lang.reflect.Method; + public class TestEventBus extends EventBus { - @Override - protected MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCanceled) - { - return null; - } + @Override + protected MethodExecutor createMethodExecutor(Object listener, Method method, boolean ignoreCancelled) { + return null; + } } \ No newline at end of file diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestEventSuper.java b/src/test/java/com/clubobsidian/trident/test/impl/TestEventSuper.java index 7f1e7b3..ec3fef2 100644 --- a/src/test/java/com/clubobsidian/trident/test/impl/TestEventSuper.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestEventSuper.java @@ -1,27 +1,26 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.impl; import com.clubobsidian.trident.Event; /** - * * Used in testing for unit tests. * Should not be called, extended by {@link} TestEvent - * */ -public class TestEventSuper extends Event {} +public class TestEventSuper extends Event { +} diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestListener.java b/src/test/java/com/clubobsidian/trident/test/impl/TestListener.java index b8c97d9..9b68957 100644 --- a/src/test/java/com/clubobsidian/trident/test/impl/TestListener.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestListener.java @@ -1,97 +1,85 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.impl; import com.clubobsidian.trident.EventHandler; import com.clubobsidian.trident.EventPriority; public class TestListener { - - private String data; - private boolean test; - public TestListener(String data) - { - this.data = data; - this.test = false; - } - - public String getData() - { - return this.data; - } - - public boolean getTest() - { - return this.test; - } - - @EventHandler - public void test(TestEvent event) - { - this.test = !test; - } - - @EventHandler - public void testCanceleable(TestCancelableEvent event) - { - event.setCanceled(true); - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void testCancelableHighest(TestCancelableEvent event) - { - if(event.isCanceled()) - { - this.test = true; - } - } - - @EventHandler(priority = EventPriority.LOWEST) - public void testOrderLowest(TestOrderEvent event) - { - this.data += "0"; - } - - @EventHandler(priority = EventPriority.LOW) - public void testOrderLow(TestOrderEvent event) - { - this.data += "1"; - } - - @EventHandler - public void testOrderNormal(TestOrderEvent event) - { - this.data += 2; - } - - @EventHandler(priority = EventPriority.HIGH) - public void testOrderHigh(TestOrderEvent event) - { - this.data += "3"; - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void testOrderHighest(TestOrderEvent event) - { - this.data += "4"; - } - - @EventHandler(priority = EventPriority.MONITOR) - public void testOrderMonitor(TestOrderEvent event) - { - this.data += "5"; - } + + private String data; + private boolean test; + + public TestListener(String data) { + this.data = data; + this.test = false; + } + + public String getData() { + return this.data; + } + + public boolean getTest() { + return this.test; + } + + @EventHandler + public void test(TestEvent event) { + this.test = !test; + } + + @EventHandler + public void testCanceleable(TestCancellableEvent event) { + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void testCancelableHighest(TestCancellableEvent event) { + if (event.isCancelled()) { + this.test = true; + } + } + + @EventHandler(priority = EventPriority.LOWEST) + public void testOrderLowest(TestOrderEvent event) { + this.data += "0"; + } + + @EventHandler(priority = EventPriority.LOW) + public void testOrderLow(TestOrderEvent event) { + this.data += "1"; + } + + @EventHandler + public void testOrderNormal(TestOrderEvent event) { + this.data += 2; + } + + @EventHandler(priority = EventPriority.HIGH) + public void testOrderHigh(TestOrderEvent event) { + this.data += "3"; + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void testOrderHighest(TestOrderEvent event) { + this.data += "4"; + } + + @EventHandler(priority = EventPriority.MONITOR) + public void testOrderMonitor(TestOrderEvent event) { + this.data += "5"; + } } diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestListenerIgnore.java b/src/test/java/com/clubobsidian/trident/test/impl/TestListenerIgnore.java index c25d364..ded5297 100644 --- a/src/test/java/com/clubobsidian/trident/test/impl/TestListenerIgnore.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestListenerIgnore.java @@ -1,18 +1,18 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.impl; import com.clubobsidian.trident.EventHandler; @@ -20,29 +20,25 @@ public class TestListenerIgnore { - private boolean canceled = false; - private boolean ignored = true; + private boolean cancelled = false; + private boolean ignored = true; + + public boolean isCancelled() { + return this.cancelled; + } + + public boolean getIgnored() { + return this.ignored; + } + + @EventHandler + public void setCancelled(TestCancellableEvent e) { + e.setCancelled(true); + this.cancelled = true; + } - public boolean isCanceled() - { - return this.canceled; - } - - public boolean getIgnored() - { - return this.ignored; - } - - @EventHandler - public void setCanceled(TestCancelableEvent e) - { - e.setCanceled(true); - this.canceled = true; - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCanceled = true) - public void test(TestCancelableEvent e) - { - this.ignored = false; - } + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void test(TestCancellableEvent e) { + this.ignored = false; + } } \ No newline at end of file diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestMethodExecutor.java b/src/test/java/com/clubobsidian/trident/test/impl/TestMethodExecutor.java index aa3e298..cf593ba 100644 --- a/src/test/java/com/clubobsidian/trident/test/impl/TestMethodExecutor.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestMethodExecutor.java @@ -1,20 +1,34 @@ -package com.clubobsidian.trident.test.impl; +/* + * Copyright 2021 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. + */ -import java.lang.reflect.Method; +package com.clubobsidian.trident.test.impl; import com.clubobsidian.trident.Event; import com.clubobsidian.trident.MethodExecutor; +import java.lang.reflect.Method; + public class TestMethodExecutor extends MethodExecutor { - public TestMethodExecutor(Object listener, Method method, boolean ignoreCanceled) - { - super(listener, method, ignoreCanceled); - } + public TestMethodExecutor(Object listener, Method method, boolean ignoreCancelled) { + super(listener, method, ignoreCancelled); + } + + @Override + public void execute(Event event) { - @Override - public void execute(Event event) - { - - } + } } \ No newline at end of file diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestOrderEvent.java b/src/test/java/com/clubobsidian/trident/test/impl/TestOrderEvent.java index d66f26a..7f89657 100644 --- a/src/test/java/com/clubobsidian/trident/test/impl/TestOrderEvent.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestOrderEvent.java @@ -1,20 +1,21 @@ -/* - 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. -*/ +/* + * Copyright 2021 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.impl; import com.clubobsidian.trident.Event; -public class TestOrderEvent extends Event {} +public class TestOrderEvent extends 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 index fa15ccf..484cc2e 100644 --- a/src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentEvent.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentEvent.java @@ -1,3 +1,19 @@ +/* + * Copyright 2021 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.impl; public class TestWrongArgumentEvent extends TestEventSuper { diff --git a/src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentListener.java b/src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentListener.java index f670036..ee328bd 100644 --- a/src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentListener.java +++ b/src/test/java/com/clubobsidian/trident/test/impl/TestWrongArgumentListener.java @@ -1,42 +1,39 @@ -/* - Copyright 2019 Club Obsidian and contributors. +/* + * Copyright 2021 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.impl; - 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 +public class TestWrongArgumentListener { - http://www.apache.org/licenses/LICENSE-2.0 + private String data; + private boolean test; - 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.impl; + public TestWrongArgumentListener(String data) { + this.data = data; + this.test = false; + } -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; - } + 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