1 package org.apache.archiva.event;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.junit.jupiter.api.Test;
24 import java.util.ArrayList;
25 import java.util.List;
27 import static org.junit.jupiter.api.Assertions.*;
30 * @author Martin Stockhammer <martin_s@apache.org>
32 public class EventManagerTest
35 private class TestHandler implements EventHandler<Event> {
37 private List<Event> eventList = new ArrayList<>( );
39 public void handle( Event event )
41 eventList.add( event );
44 public List<Event> getEventList() {
49 private EventType<Event> testType = new EventType<>( "TEST" );
50 private EventType<Event> testTestType = new EventType<>( testType,"TEST.TEST" );
51 private EventType<Event> otherType = new EventType( "OTHER" );
54 public void registerEventHandler( )
56 EventManager eventManager = new EventManager( this );
57 TestHandler handler1 = new TestHandler( );
58 TestHandler handler2 = new TestHandler( );
59 TestHandler handler3 = new TestHandler( );
60 TestHandler handler4 = new TestHandler( );
62 eventManager.registerEventHandler( Event.ANY, handler1 );
63 eventManager.registerEventHandler( testType, handler2 );
64 eventManager.registerEventHandler( testTestType, handler3 );
65 eventManager.registerEventHandler( otherType, handler4 );
67 Event event1 = new Event( testType, this );
68 eventManager.fireEvent( event1 );
69 assertEquals( 1, handler1.eventList.size( ) );
70 assertEquals( 1, handler2.eventList.size( ) );
71 assertEquals( 0, handler3.eventList.size( ) );
72 assertEquals( 0, handler4.eventList.size( ) );
74 Event event2 = new Event( testTestType, event1 );
75 eventManager.fireEvent( event2 );
76 assertEquals( 2, handler1.eventList.size( ) );
77 assertEquals( 2, handler2.eventList.size( ) );
78 assertEquals( 1, handler3.eventList.size( ) );
79 assertEquals( 0, handler4.eventList.size( ) );
81 Event event3 = new Event( otherType, event1 );
82 eventManager.fireEvent( event3 );
83 assertEquals( 3, handler1.eventList.size( ) );
84 assertEquals( 2, handler2.eventList.size( ) );
85 assertEquals( 1, handler3.eventList.size( ) );
86 assertEquals( 1, handler4.eventList.size( ) );
93 public void unregisterEventHandler( )
95 EventManager eventManager = new EventManager( this );
96 TestHandler handler1 = new TestHandler( );
97 TestHandler handler2 = new TestHandler( );
98 TestHandler handler3 = new TestHandler( );
99 TestHandler handler4 = new TestHandler( );
101 eventManager.registerEventHandler( Event.ANY, handler1 );
102 eventManager.registerEventHandler( testType, handler2 );
103 eventManager.registerEventHandler( testTestType, handler3 );
104 eventManager.registerEventHandler( otherType, handler4 );
106 eventManager.unregisterEventHandler( Event.ANY, handler1 );
107 Event event1 = new Event( testType, this );
108 eventManager.fireEvent( event1 );
109 assertEquals( 0, handler1.eventList.size( ) );
110 assertEquals( 1, handler2.eventList.size( ) );
111 assertEquals( 0, handler3.eventList.size( ) );
112 assertEquals( 0, handler4.eventList.size( ) );
114 eventManager.unregisterEventHandler( otherType, handler2 );
115 Event event2 = new Event( testType, this );
116 eventManager.fireEvent( event2 );
117 assertEquals( 0, handler1.eventList.size( ) );
118 assertEquals( 2, handler2.eventList.size( ) );
119 assertEquals( 0, handler3.eventList.size( ) );
120 assertEquals( 0, handler4.eventList.size( ) );
124 public void fireEvent( )
126 Object other = new Object( );
127 EventManager eventManager = new EventManager( this );
128 assertThrows( NullPointerException.class, ( ) -> eventManager.fireEvent( null ) );
129 Event event = new Event( EventType.ROOT, other );
130 assertEquals( other, event.getSource( ) );
131 TestHandler handler = new TestHandler( );
132 eventManager.registerEventHandler( EventType.ROOT, handler );
133 eventManager.fireEvent( event );
134 assertEquals( 1, handler.getEventList( ).size( ) );
135 Event newEvent = handler.getEventList( ).get( 0 );
136 assertNotEquals( event, newEvent );
137 assertEquals( this, newEvent.getSource( ) );