211129262bc9d8f29176c98c42268cd38043d826
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / ListenerRegistryTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.Iterables;
15 import java.util.EventListener;
16 import java.util.Iterator;
17 import org.junit.Before;
18 import org.junit.Rule;
19 import org.junit.Test;
20 import org.junit.rules.ExpectedException;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22
23 public class ListenerRegistryTest {
24
25     private TestEventListener testEventListener;
26     private ExtendedTestEventListener extendedTestEventListener;
27     private ListenerRegistry<TestEventListener> listenerRegistry;
28
29     @Rule
30     public ExpectedException expException = ExpectedException.none();
31
32     @Before
33     public void init() {
34         testEventListener = new TestEventListener() {};
35         extendedTestEventListener = new ExtendedTestEventListener() {};
36         listenerRegistry = new ListenerRegistry<>();
37     }
38
39     @Test
40     public void testCreateNewInstance() {
41         assertNotNull("Intance of listener registry shouldn't be null.", listenerRegistry);
42     }
43
44     @Test
45     public void tetGetListenersMethod() {
46         assertTrue("Listener regisdtry should have any listeners.", Iterables.isEmpty(listenerRegistry.getListeners()));
47     }
48
49     @Test
50     public void testRegisterMethod() {
51
52         final ListenerRegistration<TestEventListener> listenerRegistration = listenerRegistry.register(testEventListener);
53         assertEquals("Listeners should be the same.", testEventListener, listenerRegistration.getInstance());
54
55         expException.expect(IllegalArgumentException.class);
56         expException.expectMessage("Listener should not be null.");
57         listenerRegistry.register(null);
58     }
59
60     @Test
61     public void testRegisterWithType() {
62         final ListenerRegistration<ExtendedTestEventListener> listenerRegistration = listenerRegistry.registerWithType(extendedTestEventListener);
63         assertEquals("Listeners should be the same.", extendedTestEventListener, listenerRegistration.getInstance());
64     }
65
66     @Test
67     public void testIteratorMethod() {
68         final Iterator<ListenerRegistration<TestEventListener>> listenerIterator = listenerRegistry.iterator();
69         assertNotNull("Listener iterator shouldn't be null.", listenerIterator);
70     }
71
72     @Test
73     public void testCreateMethod() {
74         final ListenerRegistry<EventListener> emptyListenerRegistry = ListenerRegistry.create();
75         assertTrue("List of listeners in listener registry should be empty.", Iterables.isEmpty(emptyListenerRegistry.getListeners()));
76     }
77
78     interface TestEventListener extends EventListener {
79
80     }
81
82     interface ExtendedTestEventListener extends TestEventListener {
83
84     }
85 }