diff options
author | Martin Stockhammer <martin_s@apache.org> | 2019-10-04 00:29:59 +0200 |
---|---|---|
committer | Martin Stockhammer <martin_s@apache.org> | 2019-10-04 00:29:59 +0200 |
commit | ff28da65a6b9562b7a1848a244fd103ea8ff951d (patch) | |
tree | d6a6e95c99082d14935ade2e8a0697d9f5d6f2df /archiva-modules/archiva-base/archiva-common | |
parent | 6766beb8ac281e96166ce51e1b75a4b05cee7c51 (diff) | |
download | archiva-ff28da65a6b9562b7a1848a244fd103ea8ff951d.tar.gz archiva-ff28da65a6b9562b7a1848a244fd103ea8ff951d.zip |
Implementing event manager test
Diffstat (limited to 'archiva-modules/archiva-base/archiva-common')
-rw-r--r-- | archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/event/EventManagerTest.java | 98 |
1 files changed, 96 insertions, 2 deletions
diff --git a/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/event/EventManagerTest.java b/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/event/EventManagerTest.java index e486c53fd..f894cb574 100644 --- a/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/event/EventManagerTest.java +++ b/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/event/EventManagerTest.java @@ -19,9 +19,12 @@ package org.apache.archiva.event; * under the License. */ -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; /** * @author Martin Stockhammer <martin_s@apache.org> @@ -29,18 +32,109 @@ import static org.junit.Assert.*; public class EventManagerTest { + private class TestHandler implements EventHandler<Event> { + + private List<Event> eventList = new ArrayList<>( ); + @Override + public void handle( Event event ) + { + eventList.add( event ); + } + + public List<Event> getEventList() { + return eventList; + } + } + + private EventType<Event> testType = new EventType<>( "TEST" ); + private EventType<Event> testTestType = new EventType<>( testType,"TEST.TEST" ); + private EventType<Event> otherType = new EventType( "OTHER" ); + @Test public void registerEventHandler( ) { + EventManager eventManager = new EventManager( this ); + TestHandler handler1 = new TestHandler( ); + TestHandler handler2 = new TestHandler( ); + TestHandler handler3 = new TestHandler( ); + TestHandler handler4 = new TestHandler( ); + + eventManager.registerEventHandler( Event.ANY, handler1 ); + eventManager.registerEventHandler( testType, handler2 ); + eventManager.registerEventHandler( testTestType, handler3 ); + eventManager.registerEventHandler( otherType, handler4 ); + + Event event1 = new Event( testType, this ); + eventManager.fireEvent( event1 ); + assertEquals( 1, handler1.eventList.size( ) ); + assertEquals( 1, handler2.eventList.size( ) ); + assertEquals( 0, handler3.eventList.size( ) ); + assertEquals( 0, handler4.eventList.size( ) ); + + Event event2 = new Event( testTestType, event1 ); + eventManager.fireEvent( event2 ); + assertEquals( 2, handler1.eventList.size( ) ); + assertEquals( 2, handler2.eventList.size( ) ); + assertEquals( 1, handler3.eventList.size( ) ); + assertEquals( 0, handler4.eventList.size( ) ); + + Event event3 = new Event( otherType, event1 ); + eventManager.fireEvent( event3 ); + assertEquals( 3, handler1.eventList.size( ) ); + assertEquals( 2, handler2.eventList.size( ) ); + assertEquals( 1, handler3.eventList.size( ) ); + assertEquals( 1, handler4.eventList.size( ) ); + + + } @Test public void unregisterEventHandler( ) { + EventManager eventManager = new EventManager( this ); + TestHandler handler1 = new TestHandler( ); + TestHandler handler2 = new TestHandler( ); + TestHandler handler3 = new TestHandler( ); + TestHandler handler4 = new TestHandler( ); + + eventManager.registerEventHandler( Event.ANY, handler1 ); + eventManager.registerEventHandler( testType, handler2 ); + eventManager.registerEventHandler( testTestType, handler3 ); + eventManager.registerEventHandler( otherType, handler4 ); + + eventManager.unregisterEventHandler( Event.ANY, handler1 ); + Event event1 = new Event( testType, this ); + eventManager.fireEvent( event1 ); + assertEquals( 0, handler1.eventList.size( ) ); + assertEquals( 1, handler2.eventList.size( ) ); + assertEquals( 0, handler3.eventList.size( ) ); + assertEquals( 0, handler4.eventList.size( ) ); + + eventManager.unregisterEventHandler( otherType, handler2 ); + Event event2 = new Event( testType, this ); + eventManager.fireEvent( event2 ); + assertEquals( 0, handler1.eventList.size( ) ); + assertEquals( 2, handler2.eventList.size( ) ); + assertEquals( 0, handler3.eventList.size( ) ); + assertEquals( 0, handler4.eventList.size( ) ); } @Test public void fireEvent( ) { + Object other = new Object( ); + EventManager eventManager = new EventManager( this ); + assertThrows( NullPointerException.class, ( ) -> eventManager.fireEvent( null ) ); + Event event = new Event( EventType.ROOT, other ); + assertEquals( other, event.getSource( ) ); + TestHandler handler = new TestHandler( ); + eventManager.registerEventHandler( EventType.ROOT, handler ); + eventManager.fireEvent( event ); + assertEquals( 1, handler.getEventList( ).size( ) ); + Event newEvent = handler.getEventList( ).get( 0 ); + assertNotEquals( event, newEvent ); + assertEquals( this, newEvent.getSource( ) ); + } }
\ No newline at end of file |