Cleaned up concepts. 16/5416/3
authorDana Kutenicsova <dkutenic@cisco.com>
Wed, 19 Feb 2014 15:53:42 +0000 (16:53 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 20 Feb 2014 15:07:44 +0000 (15:07 +0000)
Change-Id: Ifba0889a6c063eecc172ec01db22582ac0bf38b1
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/EventBusRegistration.java
concepts/src/main/java/org/opendaylight/protocol/concepts/ListenerRegistration.java
concepts/src/test/java/org/opendaylight/protocol/concepts/IPAddressesAndPrefixesTest.java
concepts/src/test/java/org/opendaylight/protocol/concepts/MultiRegistryTest.java [new file with mode: 0644]
concepts/src/test/java/org/opendaylight/protocol/concepts/SimpleListenerRegistration.java [new file with mode: 0644]

index cfe180bec35288411e23f6abf86d9c223619234d..9a516576a2b3dc2d08456989c748288cdb4d30ff 100644 (file)
@@ -54,7 +54,7 @@ final class EventBusRegistration extends ListenerRegistration<BGPSessionListener
 
        @Subscribe
        public void onMessage(final Notification message) {
-               sendMessage(this.listener, message);
+               sendMessage(this.getListener(), message);
        }
 
        @Override
index 7454f0be1fdbaa2ea081e907eb8088f27845942a..d1694a4aea20739491153d578d75367869f29540 100644 (file)
@@ -22,7 +22,8 @@ import com.google.common.base.Preconditions;
  */
 @ThreadSafe
 public abstract class ListenerRegistration<T extends EventListener> extends AbstractRegistration {
-       protected final T listener;
+
+       private final T listener;
 
        protected ListenerRegistration(final T listener) {
                this.listener = Preconditions.checkNotNull(listener);
index a0a941b0692ca9ebf15676ebc7a90092e53d3bea..7c36feeb69f345fb741f635e80da25061296e88c 100644 (file)
@@ -9,7 +9,7 @@ package org.opendaylight.protocol.concepts;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
 import java.net.UnknownHostException;
@@ -28,20 +28,20 @@ public class IPAddressesAndPrefixesTest {
 
        @Test
        public void test3() {
-               assertTrue("123.123.123.123".equals(new Ipv4Address("123.123.123.123").getValue()));
-               assertTrue("2001::1".equals(new Ipv6Address("2001::1").getValue()));
+               assertEquals("123.123.123.123", new Ipv4Address("123.123.123.123").getValue());
+               assertEquals("2001::1", new Ipv6Address("2001::1").getValue());
        }
 
        @Test
        public void test4() throws UnknownHostException {
-               assertTrue(new IpPrefix(new Ipv4Prefix("123.123.123.123")).getIpv4Prefix() != null);
-               assertTrue(new IpPrefix(new Ipv6Prefix("2001::1")).getIpv6Prefix() != null);
+               assertNotNull(new IpPrefix(new Ipv4Prefix("123.123.123.123")).getIpv4Prefix());
+               assertNotNull(new IpPrefix(new Ipv6Prefix("2001::1")).getIpv6Prefix());
        }
 
        @Test
        public void test5() {
-               assertTrue("123.123.123.123/24".equals(new Ipv4Prefix("123.123.123.123/24").getValue()));
-               assertTrue("2001::1/120".equals(new Ipv6Prefix("2001::1/120").getValue()));
+               assertEquals("123.123.123.123/24", new Ipv4Prefix("123.123.123.123/24").getValue());
+               assertEquals("2001::1/120", new Ipv6Prefix("2001::1/120").getValue());
        }
 
        @Test
@@ -68,6 +68,18 @@ public class IPAddressesAndPrefixesTest {
                }
        }
 
+       @Test
+       public void testAddress6ForBytes() {
+               final byte[] bytes = new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
+               assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForBytes(bytes));
+               try {
+                       Ipv6Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
+                       fail();
+               } catch (final IllegalArgumentException e) {
+                       assertEquals("Failed to construct IPv6 address", e.getMessage());
+               }
+       }
+
        @Test
        public void testPrefixList4ForBytes() {
                final byte[] bytes = new byte[] { 22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100 };
@@ -77,10 +89,17 @@ public class IPAddressesAndPrefixesTest {
                                prefs);
        }
 
+       @Test
+       public void testBytesForAddress() {
+               assertArrayEquals(new byte[] { 12, 58, (byte) 201, 99  }, Ipv4Util.bytesForAddress(new Ipv4Address("12.58.201.99")));
+               assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01  }, Ipv6Util.bytesForAddress(new Ipv6Address("2001::1")));
+       }
+
        @Test
        public void testPrefix6ForBytes() {
                final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
                assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
+               assertArrayEquals(new byte[] {0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x40 }, Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64")));
        }
 
        @Test
@@ -89,7 +108,8 @@ public class IPAddressesAndPrefixesTest {
                prefs.add(new Ipv6Prefix("2001:db8:1:2::/64"));
                prefs.add(new Ipv6Prefix("2001:db8:1:1::/64"));
                prefs.add(new Ipv6Prefix("2001:db8:1::/64"));
-
+               assertEquals(prefs,
+                               Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"), new Ipv6Prefix("2001:db8:1:1::/64"), new Ipv6Prefix("2001:db8:1::/64")));
        }
 
        @Test
diff --git a/concepts/src/test/java/org/opendaylight/protocol/concepts/MultiRegistryTest.java b/concepts/src/test/java/org/opendaylight/protocol/concepts/MultiRegistryTest.java
new file mode 100644 (file)
index 0000000..75c05a4
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.concepts;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.EventListener;
+
+import org.junit.Test;
+
+public class MultiRegistryTest {
+
+       @Test
+       public void testMultiRegistry() {
+               MultiRegistry<Object, Integer> registry = new MultiRegistry<>();
+               String first = "first";
+               String second = "second";
+               String third = "third";
+
+               registry.register(first, 1);
+               registry.register(second, 2);
+               registry.register(third, 3);
+
+               assertEquals(Integer.valueOf(1), registry.get("first"));
+               assertEquals(Integer.valueOf(2), registry.get("second"));
+               assertEquals(Integer.valueOf(3), registry.get("third"));
+
+               registry.register(second, 22);
+
+               assertEquals(Integer.valueOf(22), registry.get("second"));
+
+               registry.register(Character.valueOf('c'), 5);
+
+               assertEquals(Integer.valueOf(5), registry.get('c'));
+       }
+
+       @Test
+       public void testRegistration() {
+               EventListener list1 = new EventListener() {
+               };
+               SimpleListenerRegistration<EventListener> holder = new SimpleListenerRegistration<EventListener>(list1);
+
+               assertEquals(list1, holder.getListener());
+               assertEquals(1, holder.listeners.size());
+
+               holder.removeRegistration();
+
+               assertEquals(0, holder.listeners.size());
+               assertNotNull(holder.getListener());
+       }
+}
diff --git a/concepts/src/test/java/org/opendaylight/protocol/concepts/SimpleListenerRegistration.java b/concepts/src/test/java/org/opendaylight/protocol/concepts/SimpleListenerRegistration.java
new file mode 100644 (file)
index 0000000..114b4ee
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.concepts;
+
+import java.util.ArrayList;
+import java.util.EventListener;
+import java.util.List;
+
+public class SimpleListenerRegistration<K extends EventListener> extends ListenerRegistration<K> {
+
+       public List<K> listeners = new ArrayList<>();
+
+       protected SimpleListenerRegistration(K listener) {
+               super(listener);
+               this.listeners.add(listener);
+       }
+
+       @Override
+       protected void removeRegistration() {
+               this.listeners.clear();
+       }
+}