2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
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;
23 public class ListenerRegistryTest {
25 private TestEventListener testEventListener;
26 private ExtendedTestEventListener extendedTestEventListener;
27 private ListenerRegistry<TestEventListener> listenerRegistry;
30 public ExpectedException expException = ExpectedException.none();
34 testEventListener = new TestEventListener() {};
35 extendedTestEventListener = new ExtendedTestEventListener() {};
36 listenerRegistry = new ListenerRegistry<>();
40 public void testCreateNewInstance() {
41 assertNotNull("Intance of listener registry shouldn't be null.", listenerRegistry);
45 public void tetGetListenersMethod() {
46 assertTrue("Listener regisdtry should have any listeners.", Iterables.isEmpty(listenerRegistry.getListeners()));
50 public void testRegisterMethod() {
52 final ListenerRegistration<TestEventListener> listenerRegistration = listenerRegistry.register(testEventListener);
53 assertEquals("Listeners should be the same.", testEventListener, listenerRegistration.getInstance());
55 expException.expect(IllegalArgumentException.class);
56 expException.expectMessage("Listener should not be null.");
57 listenerRegistry.register(null);
61 public void testRegisterWithType() {
62 final ListenerRegistration<ExtendedTestEventListener> listenerRegistration = listenerRegistry.registerWithType(extendedTestEventListener);
63 assertEquals("Listeners should be the same.", extendedTestEventListener, listenerRegistration.getInstance());
67 public void testIteratorMethod() {
68 final Iterator<ListenerRegistration<TestEventListener>> listenerIterator = listenerRegistry.iterator();
69 assertNotNull("Listener iterator shouldn't be null.", listenerIterator);
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()));
78 interface TestEventListener extends EventListener {
82 interface ExtendedTestEventListener extends TestEventListener {