blob: c1458e3df5f29ae1914017236afe66b6572062a6 [file] [log] [blame]
Simon Huntff663742015-05-14 13:33:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Huntff663742015-05-14 13:33:05 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.event;
17
18import org.junit.Before;
19import org.junit.Test;
20
21import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertTrue;
23
24/**
25 * Unit tests for {@link ListenerRegistry}.
26 */
27public class ListenerRegistryTest {
28
Simon Huntff663742015-05-14 13:33:05 -070029 private static final TestEvent FOO_EVENT =
30 new TestEvent(TestEvent.Type.FOO, "foo");
31 private static final TestEvent BAR_EVENT =
32 new TestEvent(TestEvent.Type.BAR, "bar");
33
34 private TestListener listener;
35 private TestListener secondListener;
36 private TestListenerRegistry manager;
37
38 @Before
39 public void setUp() {
40 listener = new TestListener();
41 secondListener = new TestListener();
42 manager = new TestListenerRegistry();
43 }
44
45 @Test
46 public void basics() {
47 manager.addListener(listener);
48 manager.addListener(secondListener);
49
50 manager.process(BAR_EVENT);
51 assertTrue("BAR not processed", listener.events.contains(BAR_EVENT));
52 assertTrue("BAR not processed", secondListener.events.contains(BAR_EVENT));
53
54 manager.removeListener(listener);
55
56 manager.process(FOO_EVENT);
57 assertFalse("FOO processed", listener.events.contains(FOO_EVENT));
58 assertTrue("FOO not processed", secondListener.events.contains(FOO_EVENT));
59 }
60
61 @Test
62 public void badListener() {
63 listener = new BrokenListener();
64
65 manager.addListener(listener);
66 manager.addListener(secondListener);
67
68 manager.process(BAR_EVENT);
69 assertFalse("BAR processed", listener.events.contains(BAR_EVENT));
70 assertFalse("error not reported", manager.errors.isEmpty());
71 assertTrue("BAR not processed", secondListener.events.contains(BAR_EVENT));
72 }
73
Simon Huntff663742015-05-14 13:33:05 -070074}