You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2015/11/02 19:35:54 UTC
[10/12] isis git commit: ISIS-830: improved error message for if
register 'late'.
ISIS-830: improved error message for if register 'late'.
We throw the IllegalStateException if any events have been posted.
Also:
- added unit tests around this functionality
Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/c5eaf8b2
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/c5eaf8b2
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/c5eaf8b2
Branch: refs/heads/master
Commit: c5eaf8b2b8442b45b0aaadf5d991338e719780ac
Parents: a751983
Author: Dan Haywood <da...@haywood-associates.co.uk>
Authored: Mon Nov 2 18:11:58 2015 +0000
Committer: Dan Haywood <da...@haywood-associates.co.uk>
Committed: Mon Nov 2 18:11:58 2015 +0000
----------------------------------------------------------------------
.../services/eventbus/EventBusService.java | 8 +-
.../eventbus/EventBusServiceDefault.java | 6 +-
.../eventbus/EventBusServiceDefaultTest.java | 88 +++++++++++++++++++-
3 files changed, 95 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/isis/blob/c5eaf8b2/core/applib/src/main/java/org/apache/isis/applib/services/eventbus/EventBusService.java
----------------------------------------------------------------------
diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/eventbus/EventBusService.java b/core/applib/src/main/java/org/apache/isis/applib/services/eventbus/EventBusService.java
index 37b049d..ef84d36 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/services/eventbus/EventBusService.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/services/eventbus/EventBusService.java
@@ -163,6 +163,7 @@ public abstract class EventBusService {
}
}
+
/**
* Notionally allows subscribers to unregister from the event bus; however this is a no-op.
*
@@ -207,13 +208,18 @@ public abstract class EventBusService {
getEventBusImplementation().post(event);
}
+
+ protected boolean hasPosted() {
+ return this.eventBusImplementation != null;
+ }
+
//endregion
//region > getEventBus
/**
- * Lazily populated in {@link #getEventBusImplementation()}.
+ * Lazily populated in {@link #getEventBusImplementation()} as result of the first {@link #post(Object)}.
*/
protected EventBusImplementation eventBusImplementation;
http://git-wip-us.apache.org/repos/asf/isis/blob/c5eaf8b2/core/runtime/src/main/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefault.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefault.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefault.java
index 296d758..71ca9c7 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefault.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefault.java
@@ -61,14 +61,14 @@ public abstract class EventBusServiceDefault extends EventBusService {
throw new IllegalArgumentException("Request-scoped services must register their proxy, not themselves");
}
// a singleton
- if (!allowLateRegistration && this.eventBusImplementation != null) {
+ if (!allowLateRegistration && hasPosted()) {
// ... coming too late to the party.
- throw new IllegalStateException("Event bus has already been created; too late to register any further (singleton) subscribers");
+ throw new IllegalStateException("Events have already been posted; too late to register any further (singleton) subscribers");
}
}
super.register(domainService);
}
-
+
//endregion
//region > init, shutdown
http://git-wip-us.apache.org/repos/asf/isis/blob/c5eaf8b2/core/runtime/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultTest.java b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultTest.java
index 3bb8287..1943f86 100644
--- a/core/runtime/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultTest.java
+++ b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultTest.java
@@ -19,11 +19,15 @@ package org.apache.isis.core.runtime.services.eventbus;
import java.util.Collections;
import com.google.common.collect.ImmutableMap;
+import com.google.common.eventbus.Subscribe;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
public class EventBusServiceDefaultTest {
@@ -32,7 +36,8 @@ public class EventBusServiceDefaultTest {
@Before
public void setUp() throws Exception {
- eventBusService = new EventBusServiceDefault(){};
+ eventBusService = new EventBusServiceDefault() {
+ };
}
public static class Init extends EventBusServiceDefaultTest {
@@ -75,7 +80,6 @@ public class EventBusServiceDefaultTest {
assertThat(eventBusService.isAllowLateRegistration(), is(false));
}
-
@Test
public void implementation_setToGuava() throws Exception {
eventBusService.init(ImmutableMap.of(EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, "guava"));
@@ -108,10 +112,88 @@ public class EventBusServiceDefaultTest {
@Test
public void implementation_setToAnythingElse() throws Exception {
- eventBusService.init(ImmutableMap.of(EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, "com.mycompany.my.event.bus.Implementation"));
+ eventBusService.init(ImmutableMap.of(EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION,
+ "com.mycompany.my.event.bus.Implementation"));
assertThat(eventBusService.getImplementation(), is("com.mycompany.my.event.bus.Implementation"));
}
}
+ public static class Post extends EventBusServiceDefaultTest {
+
+ public static class Subscriber {
+ Object obj;
+ @Subscribe
+ public void on(Object obj) {
+ this.obj = obj;
+ }
+ }
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ Subscriber subscriber;
+
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ subscriber = new Subscriber();
+ }
+
+ @Test
+ public void allow_late_registration_means_can_register_after_post() throws Exception {
+ // given
+ eventBusService.init(ImmutableMap.of(
+ EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "true",
+ EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, "guava"));
+ assertThat(eventBusService.isAllowLateRegistration(), is(true));
+ assertThat(eventBusService.getImplementation(), is("guava"));
+
+ eventBusService.post(new Object());
+
+ // when
+ eventBusService.register(subscriber);
+
+ // then
+ assertThat(subscriber.obj, is(nullValue()));
+ }
+
+ @Test
+ public void disallow_late_registration_means_cannot_register_after_post() throws Exception {
+ // given
+ eventBusService.init(ImmutableMap.of(
+ EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "false",
+ EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, "guava"));
+ assertThat(eventBusService.isAllowLateRegistration(), is(false));
+ assertThat(eventBusService.getImplementation(), is("guava"));
+
+ eventBusService.post(new Object());
+
+ // expect
+ expectedException.expect(IllegalStateException.class);
+
+ // when
+ eventBusService.register(new Subscriber());
+ }
+
+ @Test
+ public void disallow_late_registration_means_can_register_before_post() throws Exception {
+ // given
+ eventBusService.init(ImmutableMap.of(
+ EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "false",
+ EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, "guava"));
+ assertThat(eventBusService.isAllowLateRegistration(), is(false));
+ assertThat(eventBusService.getImplementation(), is("guava"));
+
+ eventBusService.register(subscriber);
+
+ // when
+ final Object event = new Object();
+ eventBusService.post(event);
+
+ // then
+ assertThat(subscriber.obj, is(event));
+ }
+
+ }
}
\ No newline at end of file