Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.event; |
| 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | |
| 21 | import static org.junit.Assert.assertFalse; |
| 22 | import static org.junit.Assert.assertTrue; |
| 23 | |
| 24 | /** |
| 25 | * Unit tests for {@link ListenerRegistry}. |
| 26 | */ |
| 27 | public class ListenerRegistryTest { |
| 28 | |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 29 | 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 Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 74 | } |