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> registry;
30 public ExpectedException expException = ExpectedException.none();
34 testEventListener = new TestEventListener() {};
35 extendedTestEventListener = new ExtendedTestEventListener() {};
36 registry = new ListenerRegistry<>();
40 public void testCreateNewInstance() {
41 assertNotNull("Intance of listener registry shouldn't be null.", registry);
45 public void tetGetListenersMethod() {
46 assertTrue("Listener registry should have any listeners.", Iterables.isEmpty(registry.getListeners()));
50 public void testRegisterMethod() {
52 final ListenerRegistration<TestEventListener> listenerRegistration = registry.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 registry.register(null);
61 public void testRegisterWithType() {
62 final ListenerRegistration<ExtendedTestEventListener> listenerRegistration = registry.registerWithType(
63 extendedTestEventListener);
64 assertEquals("Listeners should be the same.", extendedTestEventListener, listenerRegistration.getInstance());
68 public void testIteratorMethod() {
69 final Iterator<ListenerRegistration<TestEventListener>> listenerIterator = registry.iterator();
70 assertNotNull("Listener iterator shouldn't be null.", listenerIterator);
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()));
80 interface TestEventListener extends EventListener {
84 interface ExtendedTestEventListener extends TestEventListener {