Fix eclipse/checkstyle warnings
[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> registry;
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         registry = new ListenerRegistry<>();
37     }
38
39     @Test
40     public void testCreateNewInstance() {
41         assertNotNull("Intance of listener registry shouldn't be null.", registry);
42     }
43
44     @Test
45     public void tetGetListenersMethod() {
46         assertTrue("Listener registry should have any listeners.", Iterables.isEmpty(registry.getListeners()));
47     }
48
49     @Test
50     public void testRegisterMethod() {
51
52         final ListenerRegistration<TestEventListener> listenerRegistration = registry.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         registry.register(null);
58     }
59
60     @Test
61     public void testRegisterWithType() {
62         final ListenerRegistration<ExtendedTestEventListener> listenerRegistration = registry.registerWithType(
63             extendedTestEventListener);
64         assertEquals("Listeners should be the same.", extendedTestEventListener, listenerRegistration.getInstance());
65     }
66
67     @Test
68     public void testIteratorMethod() {
69         final Iterator<ListenerRegistration<TestEventListener>> listenerIterator = registry.iterator();
70         assertNotNull("Listener iterator shouldn't be null.", listenerIterator);
71     }
72
73     @Test
74     public void testCreateMethod() {
75         final ListenerRegistry<EventListener> emptyRegistry = ListenerRegistry.create();
76         assertTrue("List of listeners in listener registry should be empty.",
77             Iterables.isEmpty(emptyRegistry.getListeners()));
78     }
79
80     interface TestEventListener extends EventListener {
81
82     }
83
84     interface ExtendedTestEventListener extends TestEventListener {
85
86     }
87 }