You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/04/12 18:23:14 UTC

[isis] 01/02: ISIS-1935 adding tests

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 7b234536b1b7d0b3471ddbdebc2c1cdfc7020862
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Apr 12 20:07:32 2018 +0200

    ISIS-1935 adding tests
---
 .../EventBusServiceDefaultUsingAxonSimpleTest.java | 131 ++++++++++++++++++---
 .../adapter/EventBusImplementationForGuava.java    |   4 +-
 .../EventBusServiceDefaultUsingGuavaTest.java      | 131 ++++++++++++++++++---
 3 files changed, 228 insertions(+), 38 deletions(-)

diff --git a/core/plugins/eventbus-axon/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultUsingAxonSimpleTest.java b/core/plugins/eventbus-axon/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultUsingAxonSimpleTest.java
index 1a070e1..0f03b53 100644
--- a/core/plugins/eventbus-axon/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultUsingAxonSimpleTest.java
+++ b/core/plugins/eventbus-axon/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultUsingAxonSimpleTest.java
@@ -49,24 +49,55 @@ public class EventBusServiceDefaultUsingAxonSimpleTest {
     }
 
     public static class Post extends EventBusServiceDefaultTest {
-
-        public static class Subscriber {
-            Object obj;
+    	
+    	private final static String EVENTBUS_IMPL_NAME = "axon";
+    	
+    	public static class Type1 {
+    		String name = "1";
+    	}
+    	
+    	public static class Type3 {
+    		String name = "3";
+    	}
+
+        public static class Subscriber1 {
+        	Type1 obj;
+        	@org.axonframework.eventhandling.annotation.EventHandler
+            public void on1(Type1 obj) {
+                this.obj = obj;
+            }
+        }
+        
+        public static class Subscriber2 {
+        	Type1 obj;
+        	@org.axonframework.eventhandling.annotation.EventHandler
+            public void on2(Type1 obj) {
+                this.obj = obj;
+            }
+        }
+        
+        public static class Subscriber3 {
+        	Type3 obj;
         	@org.axonframework.eventhandling.annotation.EventHandler
-            public void on(Object obj) {
+            public void on3(Type3 obj) {
                 this.obj = obj;
             }
         }
+        
 
         @Rule
         public ExpectedException expectedException = ExpectedException.none();
 
-        Subscriber subscriber;
+        Subscriber1 subscriber1;
+        Subscriber2 subscriber2;
+        Subscriber3 subscriber3;
 
         @Before
         public void setUp() throws Exception {
             super.setUp();
-            subscriber = new Subscriber();
+            subscriber1 = new Subscriber1();
+            subscriber2 = new Subscriber2();
+            subscriber3 = new Subscriber3();
         }
 
         @Test
@@ -74,17 +105,17 @@ public class EventBusServiceDefaultUsingAxonSimpleTest {
             // given
             eventBusService.init(ImmutableMap.of(
                     EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "true",
-                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, "axon"));
+                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, EVENTBUS_IMPL_NAME));
             assertThat(eventBusService.isAllowLateRegistration(), is(true));
-            assertThat(eventBusService.getImplementation(), is("axon"));
+            assertThat(eventBusService.getImplementation(), is(EVENTBUS_IMPL_NAME));
 
             eventBusService.post(new Object());
 
             // when
-            eventBusService.register(subscriber);
+            eventBusService.register(subscriber1);
 
             // then
-            assertThat(subscriber.obj, is(nullValue()));
+            assertThat(subscriber1.obj, is(nullValue()));
         }
 
         @Test
@@ -92,9 +123,9 @@ public class EventBusServiceDefaultUsingAxonSimpleTest {
             // given
             eventBusService.init(ImmutableMap.of(
                     EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "false",
-                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, "axon"));
+                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, EVENTBUS_IMPL_NAME));
             assertThat(eventBusService.isAllowLateRegistration(), is(false));
-            assertThat(eventBusService.getImplementation(), is("axon"));
+            assertThat(eventBusService.getImplementation(), is(EVENTBUS_IMPL_NAME));
 
             eventBusService.post(new Object());
 
@@ -102,7 +133,7 @@ public class EventBusServiceDefaultUsingAxonSimpleTest {
             expectedException.expect(IllegalStateException.class);
 
             // when
-            eventBusService.register(new Subscriber());
+            eventBusService.register(new Subscriber1());
         }
 
         @Test
@@ -110,18 +141,82 @@ public class EventBusServiceDefaultUsingAxonSimpleTest {
             // given
             eventBusService.init(ImmutableMap.of(
                     EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "false",
-                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, "axon"));
+                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, EVENTBUS_IMPL_NAME));
             assertThat(eventBusService.isAllowLateRegistration(), is(false));
-            assertThat(eventBusService.getImplementation(), is("axon"));
+            assertThat(eventBusService.getImplementation(), is(EVENTBUS_IMPL_NAME));
 
-            eventBusService.register(subscriber);
+            eventBusService.register(subscriber1);
 
             // when
-            final Object event = new Object();
+            final Type1 event = new Type1();
             eventBusService.post(event);
 
             // then
-            assertThat(subscriber.obj, is(event));
+            assertThat(subscriber1.obj, is(event));
+        }
+        
+        @Test
+        public void multiple_subscribers_receive_same_event_if_same_type() throws Exception {
+            // given
+            eventBusService.init(ImmutableMap.of(
+                    EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "false",
+                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, EVENTBUS_IMPL_NAME));
+            assertThat(eventBusService.isAllowLateRegistration(), is(false));
+            assertThat(eventBusService.getImplementation(), is(EVENTBUS_IMPL_NAME));
+
+            eventBusService.register(subscriber1);
+            eventBusService.register(subscriber2);
+            eventBusService.register(subscriber3);
+
+            // when
+            final Type1 event1 = new Type1();
+            eventBusService.post(event1);
+
+            // then
+            assertThat(subscriber1.obj, is(event1));
+            assertThat(subscriber2.obj, is(event1));
+            assertThat(subscriber3.obj, is(nullValue()));
+            
+            // when
+            final Type3 event3 = new Type3();
+            eventBusService.post(event3);
+            
+            // then
+            assertThat(subscriber1.obj, is(event1));
+            assertThat(subscriber2.obj, is(event1));
+            assertThat(subscriber3.obj, is(event3));
+        }
+        
+        @Test
+        public void multiple_subscribers_eventlistener() throws Exception {
+            // given
+            eventBusService.init(ImmutableMap.of(
+                    EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "false",
+                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, EVENTBUS_IMPL_NAME));
+            assertThat(eventBusService.isAllowLateRegistration(), is(false));
+            assertThat(eventBusService.getImplementation(), is(EVENTBUS_IMPL_NAME));
+
+            eventBusService.addEventListener(Type1.class, x->subscriber1.obj=x);
+            eventBusService.addEventListener(Type1.class, x->subscriber2.obj=x);
+            eventBusService.addEventListener(Type3.class, x->subscriber3.obj=x);
+            
+            // when
+            final Type1 event1 = new Type1();
+            eventBusService.post(event1);
+
+            // then
+            assertThat(subscriber1.obj, is(event1));
+            assertThat(subscriber2.obj, is(event1));
+            assertThat(subscriber3.obj, is(nullValue()));
+            
+            // when
+            final Type3 event3 = new Type3();
+            eventBusService.post(event3);
+            
+            // then
+            assertThat(subscriber1.obj, is(event1));
+            assertThat(subscriber2.obj, is(event1));
+            assertThat(subscriber3.obj, is(event3));
         }
 
     }
diff --git a/core/plugins/eventbus-guava/src/main/java/org/apache/isis/core/runtime/services/eventbus/adapter/EventBusImplementationForGuava.java b/core/plugins/eventbus-guava/src/main/java/org/apache/isis/core/runtime/services/eventbus/adapter/EventBusImplementationForGuava.java
index e83066f..36e7ec1 100644
--- a/core/plugins/eventbus-guava/src/main/java/org/apache/isis/core/runtime/services/eventbus/adapter/EventBusImplementationForGuava.java
+++ b/core/plugins/eventbus-guava/src/main/java/org/apache/isis/core/runtime/services/eventbus/adapter/EventBusImplementationForGuava.java
@@ -73,13 +73,13 @@ public class EventBusImplementationForGuava extends EventBusImplementationAbstra
     @Override
     public <T> EventListener<T> addEventListener(Class<T> targetType, Consumer<T> onEvent) {
     	final EventListener<T> eventListener = new GuavaEventListener<>(onEvent);
-    	register(eventListener);
+    	eventBus.register(eventListener);
     	return eventListener;
     }
 
     @Override
     public <T> void removeEventListener(EventListener<T> eventListener) {
-    	this.eventBus.unregister(eventListener);
+    	eventBus.unregister(eventListener);
     }
 
 	// -- HELPER
diff --git a/core/plugins/eventbus-guava/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultUsingGuavaTest.java b/core/plugins/eventbus-guava/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultUsingGuavaTest.java
index fcd32a7..6d4753b 100644
--- a/core/plugins/eventbus-guava/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultUsingGuavaTest.java
+++ b/core/plugins/eventbus-guava/src/test/java/org/apache/isis/core/runtime/services/eventbus/EventBusServiceDefaultUsingGuavaTest.java
@@ -50,24 +50,55 @@ public class EventBusServiceDefaultUsingGuavaTest {
     }
 
     public static class Post extends EventBusServiceDefaultTest {
-
-        public static class Subscriber {
-            Object obj;
+    	
+    	private final static String EVENTBUS_IMPL_NAME = "guava";
+    	
+    	public static class Type1 {
+    		String name = "1";
+    	}
+    	
+    	public static class Type3 {
+    		String name = "3";
+    	}
+
+        public static class Subscriber1 {
+        	Type1 obj;
+            @Subscribe
+            public void on1(Type1 obj) {
+                this.obj = obj;
+            }
+        }
+        
+        public static class Subscriber2 {
+        	Type1 obj;
+            @Subscribe
+            public void on2(Type1 obj) {
+                this.obj = obj;
+            }
+        }
+        
+        public static class Subscriber3 {
+        	Type3 obj;
             @Subscribe
-            public void on(Object obj) {
+            public void on3(Type3 obj) {
                 this.obj = obj;
             }
         }
+        
 
         @Rule
         public ExpectedException expectedException = ExpectedException.none();
 
-        Subscriber subscriber;
+        Subscriber1 subscriber1;
+        Subscriber2 subscriber2;
+        Subscriber3 subscriber3;
 
         @Before
         public void setUp() throws Exception {
             super.setUp();
-            subscriber = new Subscriber();
+            subscriber1 = new Subscriber1();
+            subscriber2 = new Subscriber2();
+            subscriber3 = new Subscriber3();
         }
 
         @Test
@@ -75,17 +106,17 @@ public class EventBusServiceDefaultUsingGuavaTest {
             // given
             eventBusService.init(ImmutableMap.of(
                     EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "true",
-                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, "guava"));
+                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, EVENTBUS_IMPL_NAME));
             assertThat(eventBusService.isAllowLateRegistration(), is(true));
-            assertThat(eventBusService.getImplementation(), is("guava"));
+            assertThat(eventBusService.getImplementation(), is(EVENTBUS_IMPL_NAME));
 
             eventBusService.post(new Object());
 
             // when
-            eventBusService.register(subscriber);
+            eventBusService.register(subscriber1);
 
             // then
-            assertThat(subscriber.obj, is(nullValue()));
+            assertThat(subscriber1.obj, is(nullValue()));
         }
 
         @Test
@@ -93,9 +124,9 @@ public class EventBusServiceDefaultUsingGuavaTest {
             // given
             eventBusService.init(ImmutableMap.of(
                     EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "false",
-                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, "guava"));
+                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, EVENTBUS_IMPL_NAME));
             assertThat(eventBusService.isAllowLateRegistration(), is(false));
-            assertThat(eventBusService.getImplementation(), is("guava"));
+            assertThat(eventBusService.getImplementation(), is(EVENTBUS_IMPL_NAME));
 
             eventBusService.post(new Object());
 
@@ -103,7 +134,7 @@ public class EventBusServiceDefaultUsingGuavaTest {
             expectedException.expect(IllegalStateException.class);
 
             // when
-            eventBusService.register(new Subscriber());
+            eventBusService.register(new Subscriber1());
         }
 
         @Test
@@ -111,18 +142,82 @@ public class EventBusServiceDefaultUsingGuavaTest {
             // given
             eventBusService.init(ImmutableMap.of(
                     EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "false",
-                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, "guava"));
+                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, EVENTBUS_IMPL_NAME));
             assertThat(eventBusService.isAllowLateRegistration(), is(false));
-            assertThat(eventBusService.getImplementation(), is("guava"));
+            assertThat(eventBusService.getImplementation(), is(EVENTBUS_IMPL_NAME));
 
-            eventBusService.register(subscriber);
+            eventBusService.register(subscriber1);
 
             // when
-            final Object event = new Object();
+            final Type1 event = new Type1();
             eventBusService.post(event);
 
             // then
-            assertThat(subscriber.obj, is(event));
+            assertThat(subscriber1.obj, is(event));
+        }
+        
+        @Test
+        public void multiple_subscribers_receive_same_event_if_same_type() throws Exception {
+            // given
+            eventBusService.init(ImmutableMap.of(
+                    EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "false",
+                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, EVENTBUS_IMPL_NAME));
+            assertThat(eventBusService.isAllowLateRegistration(), is(false));
+            assertThat(eventBusService.getImplementation(), is(EVENTBUS_IMPL_NAME));
+
+            eventBusService.register(subscriber1);
+            eventBusService.register(subscriber2);
+            eventBusService.register(subscriber3);
+
+            // when
+            final Type1 event1 = new Type1();
+            eventBusService.post(event1);
+
+            // then
+            assertThat(subscriber1.obj, is(event1));
+            assertThat(subscriber2.obj, is(event1));
+            assertThat(subscriber3.obj, is(nullValue()));
+            
+            // when
+            final Type3 event3 = new Type3();
+            eventBusService.post(event3);
+            
+            // then
+            assertThat(subscriber1.obj, is(event1));
+            assertThat(subscriber2.obj, is(event1));
+            assertThat(subscriber3.obj, is(event3));
+        }
+        
+        @Test
+        public void multiple_subscribers_eventlistener() throws Exception {
+            // given
+            eventBusService.init(ImmutableMap.of(
+                    EventBusServiceDefault.KEY_ALLOW_LATE_REGISTRATION, "false",
+                    EventBusServiceDefault.KEY_EVENT_BUS_IMPLEMENTATION, EVENTBUS_IMPL_NAME));
+            assertThat(eventBusService.isAllowLateRegistration(), is(false));
+            assertThat(eventBusService.getImplementation(), is(EVENTBUS_IMPL_NAME));
+
+            eventBusService.addEventListener(Type1.class, x->subscriber1.obj=x);
+            eventBusService.addEventListener(Type1.class, x->subscriber2.obj=x);
+            eventBusService.addEventListener(Type3.class, x->subscriber3.obj=x);
+            
+            // when
+            final Type1 event1 = new Type1();
+            eventBusService.post(event1);
+
+            // then
+            assertThat(subscriber1.obj, is(event1));
+            assertThat(subscriber2.obj, is(event1));
+            assertThat(subscriber3.obj, is(nullValue()));
+            
+            // when
+            final Type3 event3 = new Type3();
+            eventBusService.post(event3);
+            
+            // then
+            assertThat(subscriber1.obj, is(event1));
+            assertThat(subscriber2.obj, is(event1));
+            assertThat(subscriber3.obj, is(event3));
         }
 
     }

-- 
To stop receiving notification emails like this one, please contact
ahuber@apache.org.