From 5f86617569ce7b98a0a5861572e23f7c43fedf90 Mon Sep 17 00:00:00 2001 From: Philipp Heuer <10275049+PhilippHeuer@users.noreply.github.com> Date: Sun, 11 Feb 2024 22:46:09 +0100 Subject: [PATCH] chore: formatting, remove unused buildSrc (#196) * chore: formatting, remove unused buildSrc * chore: use string notation for all dependencies --- buildSrc/build.gradle.kts | 7 -- buildSrc/src/main/kotlin/globals.kt | 18 --- buildSrc/src/main/kotlin/project.kt | 38 ------ .../events4j/core/EventManager.java | 2 +- .../events4j/core/EventManagerTest.java | 32 ++--- handler-reactor/build.gradle.kts | 6 +- .../reactor/util/Events4JSubscriber.java | 8 +- .../reactor/ReactorEventHandlerTest.java | 28 +++-- .../events4j/simple/SimpleEventHandler.java | 5 +- .../SimpleEventHandlerSubscription.java | 2 +- .../simple/SimpleEventHandlerTest.java | 27 ++-- .../simple/listener/TestEventHandler.java | 6 +- .../events4j/simple/util/ClassUtilTest.java | 17 +-- .../events4j/spring/SpringEventHandler.java | 1 - .../domain/SpringListenerSubscription.java | 84 ++++++------- .../spring/SpringEventHandlerTest.java | 118 +++++++++--------- kotlin/build.gradle.kts | 4 +- 17 files changed, 173 insertions(+), 230 deletions(-) delete mode 100644 buildSrc/build.gradle.kts delete mode 100644 buildSrc/src/main/kotlin/globals.kt delete mode 100644 buildSrc/src/main/kotlin/project.kt diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts deleted file mode 100644 index b6413e3..0000000 --- a/buildSrc/build.gradle.kts +++ /dev/null @@ -1,7 +0,0 @@ -plugins { - `kotlin-dsl` -} - -repositories { - mavenCentral() -} diff --git a/buildSrc/src/main/kotlin/globals.kt b/buildSrc/src/main/kotlin/globals.kt deleted file mode 100644 index 0204a1d..0000000 --- a/buildSrc/src/main/kotlin/globals.kt +++ /dev/null @@ -1,18 +0,0 @@ -import org.gradle.api.Project -import org.gradle.api.internal.project.DefaultProject -import org.gradle.util.Path - -val Project.mavenRepositoryUrl: String - get() = System.getenv("MAVEN_REPO_URL") ?: findProperty("maven.repository.url").toString() - -val Project.mavenRepositoryUsername: String - get() = System.getenv("MAVEN_REPO_USERNAME") ?: findProperty("maven.repository.username").toString() - -val Project.mavenRepositoryPassword: String - get() = System.getenv("MAVEN_REPO_PASSWORD") ?: findProperty("maven.repository.password").toString() - -val Project.artifactId: String - get() = (this as DefaultProject).identityPath.path.replace(Path.SEPARATOR, "-").let { - if (rootProject.name.equals(it.substring(1), true)) rootProject.name - else (rootProject.name + if (it.startsWith("-") && it.length > 1) it else "") - }.toLowerCase() diff --git a/buildSrc/src/main/kotlin/project.kt b/buildSrc/src/main/kotlin/project.kt deleted file mode 100644 index cb23635..0000000 --- a/buildSrc/src/main/kotlin/project.kt +++ /dev/null @@ -1,38 +0,0 @@ -import org.gradle.api.publish.maven.MavenPom -import org.gradle.api.publish.maven.MavenPomDeveloperSpec - -fun MavenPom.default() { - url.set("https://github.com/PhilippHeuer/events4j") - issueManagement { - system.set("GitHub") - url.set("https://github.com/PhilippHeuer/events4j/issues") - } - inceptionYear.set("2019") - developers { all } - licenses { - license { - name.set("MIT Licence") - distribution.set("repo") - url.set("https://opensource.org/licenses/MIT") - } - } - scm { - connection.set("scm:git:https://github.com/PhilippHeuer/events4j.git") - developerConnection.set("scm:git:git@github.com:PhilippHeuer/events4j.git") - url.set("https://github.com/PhilippHeuer/events4j") - } -} - -val MavenPomDeveloperSpec.all: Unit - get() { - PhilippHeuer() - } - -fun MavenPomDeveloperSpec.PhilippHeuer() { - developer { - id.set("PhilippHeuer") - name.set("Philipp Heuer") - email.set("git@philippheuer.me") - roles.addAll("maintainer") - } -} diff --git a/core/src/main/java/com/github/philippheuer/events4j/core/EventManager.java b/core/src/main/java/com/github/philippheuer/events4j/core/EventManager.java index ccd811b..173697a 100644 --- a/core/src/main/java/com/github/philippheuer/events4j/core/EventManager.java +++ b/core/src/main/java/com/github/philippheuer/events4j/core/EventManager.java @@ -45,7 +45,7 @@ public class EventManager implements IEventManager { /** * is Stopped? */ - private volatile boolean isStopped = false; + private volatile boolean isStopped; /** * Default EventHandler diff --git a/core/src/test/java/com/github/philippheuer/events4j/core/EventManagerTest.java b/core/src/test/java/com/github/philippheuer/events4j/core/EventManagerTest.java index beeca6f..86912ad 100644 --- a/core/src/test/java/com/github/philippheuer/events4j/core/EventManagerTest.java +++ b/core/src/test/java/com/github/philippheuer/events4j/core/EventManagerTest.java @@ -1,56 +1,60 @@ package com.github.philippheuer.events4j.core; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + import com.github.philippheuer.events4j.api.domain.IDisposable; import com.github.philippheuer.events4j.core.domain.TestEventObject; import com.github.philippheuer.events4j.simple.SimpleEventHandler; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @Slf4j -public class EventManagerTest { +class EventManagerTest { private static EventManager eventManager; @BeforeAll - public static void initializeEventManager() { + static void initializeEventManager() { eventManager = new EventManager(); eventManager.autoDiscovery(); eventManager.setDefaultEventHandler(SimpleEventHandler.class); } @Test - public void testAutoDiscovery() { - Assertions.assertEquals(2, eventManager.getEventHandlers().size(), "should have discovered 2 handlers"); + void autoDiscovery() { + assertEquals(2, eventManager.getEventHandlers().size(), "should have discovered 2 handlers"); } @Test - public void testGetEventHandlerByClass() { + void getEventHandlerByClass() { SimpleEventHandler eventHandler = eventManager.getEventHandler(SimpleEventHandler.class); - Assertions.assertNotNull(eventHandler, "should fine a eventHandler for class SimpleEventHandler"); + assertNotNull(eventHandler, "should fine a eventHandler for class SimpleEventHandler"); } @Test - public void testHasEventHandlerByClass() { - Assertions.assertTrue(eventManager.hasEventHandler(SimpleEventHandler.class), "should fine a eventHandler for class SimpleEventHandler"); + void hasEventHandlerByClass() { + assertTrue(eventManager.hasEventHandler(SimpleEventHandler.class), "should fine a eventHandler for class SimpleEventHandler"); } @Test - public void testUniqueOnEvent() { + void uniqueOnEvent() { // Register Listener IDisposable disposableA = eventManager.onEvent("test", TestEventObject.class, System.out::println); IDisposable disposableB = eventManager.onEvent("test", TestEventObject.class, System.out::println); // Verify - Assertions.assertEquals(1, eventManager.getActiveSubscriptions().size()); - Assertions.assertNotNull(disposableA); - Assertions.assertNull(disposableB); + assertEquals(1, eventManager.getActiveSubscriptions().size()); + assertNotNull(disposableA); + assertNull(disposableB); } @AfterAll - public static void shutdownEventManager() { + static void shutdownEventManager() { eventManager.close(); } diff --git a/handler-reactor/build.gradle.kts b/handler-reactor/build.gradle.kts index 8920f9c..e6911e7 100644 --- a/handler-reactor/build.gradle.kts +++ b/handler-reactor/build.gradle.kts @@ -10,7 +10,7 @@ dependencies { testImplementation(project(":core")) // Reactor - see https://repo1.maven.org/maven2/io/projectreactor/reactor-bom/Dysprosium-SR12/reactor-bom-Dysprosium-SR12.pom - api(group = "io.projectreactor", name = "reactor-core", version = "3.6.2") - api(group = "io.projectreactor.addons", name = "reactor-extra", version = "3.5.1") - testImplementation(group = "io.projectreactor", name = "reactor-test", version = "3.6.2") + api("io.projectreactor:reactor-core:3.6.2") + api("io.projectreactor.addons:reactor-extra:3.5.1") + testImplementation("io.projectreactor:reactor-test:3.6.2") } diff --git a/handler-reactor/src/main/java/com/github/philippheuer/events4j/reactor/util/Events4JSubscriber.java b/handler-reactor/src/main/java/com/github/philippheuer/events4j/reactor/util/Events4JSubscriber.java index 246a350..0a17761 100644 --- a/handler-reactor/src/main/java/com/github/philippheuer/events4j/reactor/util/Events4JSubscriber.java +++ b/handler-reactor/src/main/java/com/github/philippheuer/events4j/reactor/util/Events4JSubscriber.java @@ -11,9 +11,9 @@ public class Events4JSubscriber implements Subscriber, Disposable, IDispos private Subscription subscription; - private Consumer consumer; + private final Consumer consumer; - private boolean isDisposed = false; + private boolean isDisposed; public Events4JSubscriber(Consumer consumer) { this.consumer = consumer; @@ -43,7 +43,7 @@ public void onComplete() { @Override public void dispose() { - if (isDisposed == false) { + if (!isDisposed) { subscription.cancel(); isDisposed = true; subscription = null; @@ -54,4 +54,4 @@ public void dispose() { public boolean isDisposed() { return isDisposed; } -} \ No newline at end of file +} diff --git a/handler-reactor/src/test/java/com/github/philippheuer/events4j/reactor/ReactorEventHandlerTest.java b/handler-reactor/src/test/java/com/github/philippheuer/events4j/reactor/ReactorEventHandlerTest.java index f3db951..efb79a3 100644 --- a/handler-reactor/src/test/java/com/github/philippheuer/events4j/reactor/ReactorEventHandlerTest.java +++ b/handler-reactor/src/test/java/com/github/philippheuer/events4j/reactor/ReactorEventHandlerTest.java @@ -1,5 +1,7 @@ package com.github.philippheuer.events4j.reactor; +import static org.junit.jupiter.api.Assertions.assertEquals; + import com.github.philippheuer.events4j.api.domain.IDisposable; import com.github.philippheuer.events4j.api.service.IEventHandler; import com.github.philippheuer.events4j.core.EventManager; @@ -7,10 +9,10 @@ import com.github.philippheuer.events4j.reactor.domain.TestEvent; import com.github.philippheuer.events4j.reactor.domain.TestEventObject; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import reactor.core.Disposable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Reactor EventHandler Test @@ -19,18 +21,18 @@ * @version %I%, %G% * @since 1.0 */ -public class ReactorEventHandlerTest { +class ReactorEventHandlerTest { - private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ReactorEventHandlerTest.class); + private static final Logger log = LoggerFactory.getLogger(ReactorEventHandlerTest.class); private static EventManager eventManager; - private static int eventsProcessed = 0; + private static int eventsProcessed; private static final Class REACTOR_EVENTHANDLER = ReactorEventHandler.class; @BeforeAll - public static void beforeAll() { + static void beforeAll() { eventManager = new EventManager(); ReactorEventHandler reactorEventHandler = new ReactorEventHandler(); eventManager.registerEventHandler(reactorEventHandler); @@ -41,13 +43,13 @@ public static void beforeAll() { * Tests if events can be dispatched */ @Test - public void testReactorEventHandlerWithTestEventObject() throws Exception { + void reactorEventHandlerWithTestEventObject() throws Exception { // Register Listener IDisposable disposable = eventManager.onEvent(TestEventObject.class, event -> { log.info("Received a event."); eventsProcessed = eventsProcessed + 1; }); - Assertions.assertEquals(1, eventManager.getActiveSubscriptions().size()); + assertEquals(1, eventManager.getActiveSubscriptions().size()); // dispatch eventManager.publish(new TestEventObject()); @@ -57,15 +59,15 @@ public void testReactorEventHandlerWithTestEventObject() throws Exception { disposable.dispose(); // Verify - Assertions.assertEquals(0, eventManager.getActiveSubscriptions().size()); - Assertions.assertEquals(1, eventsProcessed, "one event should have been handled"); + assertEquals(0, eventManager.getActiveSubscriptions().size()); + assertEquals(1, eventsProcessed, "one event should have been handled"); } /** * Tests if events can be dispatched */ @Test - public void testReactorEventHandlerWithTestEvent() throws Exception { + void reactorEventHandlerWithTestEvent() throws Exception { // Register Listener IDisposable disposable = eventManager.getEventHandler(REACTOR_EVENTHANDLER).onEvent(Event.class, event -> { log.info("Received event [{}] that was fired at {}.", event.getEventId(), event.getFiredAtInstant().toString()); @@ -82,11 +84,11 @@ public void testReactorEventHandlerWithTestEvent() throws Exception { Thread.sleep(1000); // Verify - Assertions.assertEquals(1, eventsProcessed, "only one event should have been handled, since we disposed the handler after the first publish call"); + assertEquals(1, eventsProcessed, "only one event should have been handled, since we disposed the handler after the first publish call"); } @AfterAll - public static void afterAll() throws Exception { + static void afterAll() throws Exception { // shutdown eventManager.close(); } diff --git a/handler-simple/src/main/java/com/github/philippheuer/events4j/simple/SimpleEventHandler.java b/handler-simple/src/main/java/com/github/philippheuer/events4j/simple/SimpleEventHandler.java index 39eb38f..7e263a0 100644 --- a/handler-simple/src/main/java/com/github/philippheuer/events4j/simple/SimpleEventHandler.java +++ b/handler-simple/src/main/java/com/github/philippheuer/events4j/simple/SimpleEventHandler.java @@ -124,7 +124,7 @@ private void handleAnnotationHandlers(Object event) { for (Map.Entry, ConcurrentMap>> e : methodListeners.entrySet()) { if (e.getKey().isAssignableFrom(event.getClass())) { ConcurrentMap> eventClass = e.getValue(); - eventClass.forEach((k, v) -> { + eventClass.forEach((k, v) -> v.forEach(object -> { try { // Invoke Event @@ -134,8 +134,7 @@ private void handleAnnotationHandlers(Object event) { } catch (Exception ex) { log.error("Unhandled exception caught dispatching event " + event.getClass().getSimpleName(), ex); } - }); - }); + })); } } } diff --git a/handler-simple/src/main/java/com/github/philippheuer/events4j/simple/domain/SimpleEventHandlerSubscription.java b/handler-simple/src/main/java/com/github/philippheuer/events4j/simple/domain/SimpleEventHandlerSubscription.java index c9f04be..2e12859 100644 --- a/handler-simple/src/main/java/com/github/philippheuer/events4j/simple/domain/SimpleEventHandlerSubscription.java +++ b/handler-simple/src/main/java/com/github/philippheuer/events4j/simple/domain/SimpleEventHandlerSubscription.java @@ -11,7 +11,7 @@ public class SimpleEventHandlerSubscription implements IDisposable { @Getter - private boolean isDisposed = false; + private boolean isDisposed; @Getter(AccessLevel.NONE) private final SimpleEventHandler simpleEventHandler; diff --git a/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/SimpleEventHandlerTest.java b/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/SimpleEventHandlerTest.java index 5de4ec1..4693550 100644 --- a/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/SimpleEventHandlerTest.java +++ b/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/SimpleEventHandlerTest.java @@ -1,5 +1,7 @@ package com.github.philippheuer.events4j.simple; +import static org.junit.jupiter.api.Assertions.assertEquals; + import com.github.philippheuer.events4j.api.domain.IDisposable; import com.github.philippheuer.events4j.core.EventManager; import com.github.philippheuer.events4j.core.domain.Event; @@ -8,19 +10,18 @@ import com.github.philippheuer.events4j.simple.listener.TestEventHandler; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @Slf4j -public class SimpleEventHandlerTest { +class SimpleEventHandlerTest { private static EventManager eventManager; - private static int eventsHandled = 0; + private static int eventsHandled; @BeforeAll - public static void beforeAll() { + static void beforeAll() { eventManager = new EventManager(); SimpleEventHandler simpleEventHandler = new SimpleEventHandler(); eventManager.setDefaultEventHandler(SimpleEventHandler.class); @@ -31,14 +32,14 @@ public static void beforeAll() { * Tests if events can be dispatched */ @Test - public void testObjectEvent() throws Exception { + void objectEvent() throws Exception { eventsHandled = 0; // Consumer based handler IDisposable disposable = eventManager.onEvent(TestEventObject.class, testEvent -> { eventsHandled = eventsHandled + 1; }); - Assertions.assertEquals(1, eventManager.getActiveSubscriptions().size()); + assertEquals(1, eventManager.getActiveSubscriptions().size()); // Dispatch eventManager.publish(new TestEventObject()); @@ -47,15 +48,15 @@ public void testObjectEvent() throws Exception { disposable.dispose(); // Verify - Assertions.assertEquals(0, eventManager.getActiveSubscriptions().size()); - Assertions.assertEquals(1, eventsHandled, "one event should have been handled"); + assertEquals(0, eventManager.getActiveSubscriptions().size()); + assertEquals(1, eventsHandled, "one event should have been handled"); } /** * Tests if events can be dispatched */ @Test - public void testConsumerHandler() throws Exception { + void consumerHandler() throws Exception { eventsHandled = 0; // Consumer based handler @@ -73,14 +74,14 @@ public void testConsumerHandler() throws Exception { eventManager.publish(new TestEvent()); // Verify - Assertions.assertEquals(1, eventsHandled, "only one event should have been handled, since we disposed the handler after the first publish call"); + assertEquals(1, eventsHandled, "only one event should have been handled, since we disposed the handler after the first publish call"); } /** * Tests if events can be dispatched */ @Test - public void testAnnotationHandler() throws Exception { + void annotationHandler() throws Exception { // Register Listener TestEventHandler testEventHandler = new TestEventHandler(); eventManager.getEventHandler(SimpleEventHandler.class).registerListener(testEventHandler); @@ -90,11 +91,11 @@ public void testAnnotationHandler() throws Exception { eventManager.publish(testEvent); // Verify - Assertions.assertEquals(1, testEventHandler.eventsProcessed, "check that one event was processed"); + assertEquals(1, testEventHandler.eventsProcessed, "check that one event was processed"); } @AfterAll - public static void afterAll() throws Exception { + static void afterAll() throws Exception { // Shutdown eventManager.close(); } diff --git a/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/listener/TestEventHandler.java b/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/listener/TestEventHandler.java index 1f7cf15..bd06d5d 100644 --- a/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/listener/TestEventHandler.java +++ b/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/listener/TestEventHandler.java @@ -2,12 +2,14 @@ import com.github.philippheuer.events4j.simple.domain.EventSubscriber; import com.github.philippheuer.events4j.simple.domain.TestEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class TestEventHandler { - private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TestEventHandler.class); + private static final Logger log = LoggerFactory.getLogger(TestEventHandler.class); - public int eventsProcessed = 0; + public int eventsProcessed; /** * TestEvent Listener diff --git a/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/util/ClassUtilTest.java b/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/util/ClassUtilTest.java index 697e150..ead4fe7 100644 --- a/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/util/ClassUtilTest.java +++ b/handler-simple/src/test/java/com/github/philippheuer/events4j/simple/util/ClassUtilTest.java @@ -1,24 +1,25 @@ package com.github.philippheuer.events4j.simple.util; +import static org.junit.jupiter.api.Assertions.assertEquals; + import com.github.philippheuer.events4j.api.domain.IEvent; import com.github.philippheuer.events4j.core.domain.Event; import com.github.philippheuer.events4j.simple.domain.TestEvent; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.ArrayList; -public class ClassUtilTest { +class ClassUtilTest { @Test - public void testInheritanceTree() throws Exception { + void inheritanceTree() throws Exception { ArrayList> inheritanceTree = new ArrayList<>(ClassUtil.getInheritanceTree(TestEvent.class)); - Assertions.assertEquals(TestEvent.class, inheritanceTree.get(0)); - Assertions.assertEquals(Event.class, inheritanceTree.get(1)); - Assertions.assertEquals(IEvent.class, inheritanceTree.get(2)); - Assertions.assertEquals(Object.class, inheritanceTree.get(3)); - Assertions.assertEquals(4, inheritanceTree.size()); + assertEquals(TestEvent.class, inheritanceTree.get(0)); + assertEquals(Event.class, inheritanceTree.get(1)); + assertEquals(IEvent.class, inheritanceTree.get(2)); + assertEquals(Object.class, inheritanceTree.get(3)); + assertEquals(4, inheritanceTree.size()); } } diff --git a/handler-spring/src/main/java/com/github/philippheuer/events4j/spring/SpringEventHandler.java b/handler-spring/src/main/java/com/github/philippheuer/events4j/spring/SpringEventHandler.java index 07ed2ba..269a89a 100644 --- a/handler-spring/src/main/java/com/github/philippheuer/events4j/spring/SpringEventHandler.java +++ b/handler-spring/src/main/java/com/github/philippheuer/events4j/spring/SpringEventHandler.java @@ -10,7 +10,6 @@ import org.springframework.context.ApplicationListener; import org.springframework.context.PayloadApplicationEvent; import org.springframework.context.event.ApplicationEventMulticaster; -import org.springframework.lang.NonNullApi; import org.springframework.stereotype.Service; import java.util.function.Consumer; diff --git a/handler-spring/src/main/java/com/github/philippheuer/events4j/spring/domain/SpringListenerSubscription.java b/handler-spring/src/main/java/com/github/philippheuer/events4j/spring/domain/SpringListenerSubscription.java index 906026c..3ca7b27 100644 --- a/handler-spring/src/main/java/com/github/philippheuer/events4j/spring/domain/SpringListenerSubscription.java +++ b/handler-spring/src/main/java/com/github/philippheuer/events4j/spring/domain/SpringListenerSubscription.java @@ -1,42 +1,42 @@ -package com.github.philippheuer.events4j.spring.domain; - -import com.github.philippheuer.events4j.api.domain.IDisposable; -import lombok.AccessLevel; -import lombok.Getter; -import org.springframework.context.ApplicationListener; -import org.springframework.context.event.ApplicationEventMulticaster; - -@Getter -public class SpringListenerSubscription implements IDisposable { - - @Getter - private boolean isDisposed = false; - - @Getter(AccessLevel.NONE) - private final ApplicationEventMulticaster applicationEventMulticaster; - - @Getter(AccessLevel.NONE) - private final ApplicationListener listener; - - public SpringListenerSubscription(ApplicationEventMulticaster applicationEventMulticaster, ApplicationListener listener) { - this.applicationEventMulticaster = applicationEventMulticaster; - this.listener = listener; - } - - /** - * Dispose - */ - public void dispose() { - if (!isDisposed) { - applicationEventMulticaster.removeApplicationListener(listener); - - // disposed - isDisposed = true; - } - } - - public static SpringListenerSubscription of(ApplicationEventMulticaster applicationEventMulticaster, ApplicationListener listener) { - return new SpringListenerSubscription(applicationEventMulticaster, listener); - } - -} +package com.github.philippheuer.events4j.spring.domain; + +import com.github.philippheuer.events4j.api.domain.IDisposable; +import lombok.AccessLevel; +import lombok.Getter; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ApplicationEventMulticaster; + +@Getter +public class SpringListenerSubscription implements IDisposable { + + @Getter + private boolean isDisposed; + + @Getter(AccessLevel.NONE) + private final ApplicationEventMulticaster applicationEventMulticaster; + + @Getter(AccessLevel.NONE) + private final ApplicationListener listener; + + public SpringListenerSubscription(ApplicationEventMulticaster applicationEventMulticaster, ApplicationListener listener) { + this.applicationEventMulticaster = applicationEventMulticaster; + this.listener = listener; + } + + /** + * Dispose + */ + public void dispose() { + if (!isDisposed) { + applicationEventMulticaster.removeApplicationListener(listener); + + // disposed + isDisposed = true; + } + } + + public static SpringListenerSubscription of(ApplicationEventMulticaster applicationEventMulticaster, ApplicationListener listener) { + return new SpringListenerSubscription(applicationEventMulticaster, listener); + } + +} diff --git a/handler-spring/src/test/java/com/github/philippheuer/events4j/spring/SpringEventHandlerTest.java b/handler-spring/src/test/java/com/github/philippheuer/events4j/spring/SpringEventHandlerTest.java index 3196ded..b45e287 100644 --- a/handler-spring/src/test/java/com/github/philippheuer/events4j/spring/SpringEventHandlerTest.java +++ b/handler-spring/src/test/java/com/github/philippheuer/events4j/spring/SpringEventHandlerTest.java @@ -1,60 +1,58 @@ -package com.github.philippheuer.events4j.spring; - -import com.github.philippheuer.events4j.api.domain.IDisposable; -import com.github.philippheuer.events4j.spring.domain.TestEventObject; -import com.github.philippheuer.events4j.spring.domain.TestEventObjectExtended; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; - -import java.util.concurrent.atomic.AtomicInteger; - -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = {SpringEventHandler.class}) -@EnableAutoConfiguration -public class SpringEventHandlerTest { - - @Test - public void testOnEventListeners(@Autowired SpringEventHandler eventHandler) throws Exception { - AtomicInteger eventsHandled = new AtomicInteger(0); - - // Consumer based handler - IDisposable disposable = eventHandler.onEvent(TestEventObject.class, testEvent -> { - eventsHandled.addAndGet(1); - }); - - // Dispatch - eventHandler.publish(new TestEventObject()); - eventHandler.publish(new TestEventObject()); - - // Dispose Handler - disposable.dispose(); - - // Dispatch 2nd Event - eventHandler.publish(new TestEventObject()); - - // Verify - Assertions.assertEquals(2, eventsHandled.get(), "one event should have been handled"); - } - - @Test - public void testOnEventListenersInheritance(@Autowired SpringEventHandler eventHandler) throws Exception { - AtomicInteger eventsHandled = new AtomicInteger(0); - - // Consumer based handler - eventHandler.onEvent(TestEventObject.class, testEvent -> { - eventsHandled.addAndGet(1); - }); - eventHandler.onEvent(TestEventObjectExtended.class, testEvent -> { - eventsHandled.addAndGet(1); - }); - - // Dispatch - eventHandler.publish(new TestEventObjectExtended()); - - // Verify - Assertions.assertEquals(2, eventsHandled.get()); - } - -} +package com.github.philippheuer.events4j.spring; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.github.philippheuer.events4j.api.domain.IDisposable; +import com.github.philippheuer.events4j.spring.domain.TestEventObject; +import com.github.philippheuer.events4j.spring.domain.TestEventObjectExtended; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.concurrent.atomic.AtomicInteger; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = {SpringEventHandler.class}) +@EnableAutoConfiguration +class SpringEventHandlerTest { + + @Test + void onEventListeners(@Autowired SpringEventHandler eventHandler) throws Exception { + AtomicInteger eventsHandled = new AtomicInteger(0); + + // Consumer based handler + IDisposable disposable = eventHandler.onEvent(TestEventObject.class, testEvent -> + eventsHandled.addAndGet(1)); + + // Dispatch + eventHandler.publish(new TestEventObject()); + eventHandler.publish(new TestEventObject()); + + // Dispose Handler + disposable.dispose(); + + // Dispatch 2nd Event + eventHandler.publish(new TestEventObject()); + + // Verify + assertEquals(2, eventsHandled.get(), "one event should have been handled"); + } + + @Test + void onEventListenersInheritance(@Autowired SpringEventHandler eventHandler) throws Exception { + AtomicInteger eventsHandled = new AtomicInteger(0); + + // Consumer based handler + eventHandler.onEvent(TestEventObject.class, testEvent -> + eventsHandled.addAndGet(1)); + eventHandler.onEvent(TestEventObjectExtended.class, testEvent -> + eventsHandled.addAndGet(1)); + + // Dispatch + eventHandler.publish(new TestEventObjectExtended()); + + // Verify + assertEquals(2, eventsHandled.get()); + } + +} diff --git a/kotlin/build.gradle.kts b/kotlin/build.gradle.kts index e687696..d5c49da 100644 --- a/kotlin/build.gradle.kts +++ b/kotlin/build.gradle.kts @@ -12,10 +12,10 @@ projectConfiguration { dependencies { // Project api(project(":api")) - api(group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version = "1.7.3") + api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3") // Testing - testImplementation(group = "org.jetbrains.kotlinx", name="kotlinx-coroutines-test", version = "1.7.3") + testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3") testImplementation(project(":core")) testImplementation(project(":handler-simple")) }