Modernize concepts 36/108536/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 20 Oct 2023 12:34:34 +0000 (14:34 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 20 Oct 2023 12:34:34 +0000 (14:34 +0200)
Use local variable type inference and migrate to JUnit5.

Change-Id: If80b8eb6b5c5ae49c248bcf8c4710c96b1cbc51f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
concepts/src/main/java/org/opendaylight/protocol/concepts/HandlerRegistry.java
concepts/src/main/java/org/opendaylight/protocol/concepts/MultiRegistry.java
concepts/src/test/java/org/opendaylight/protocol/concepts/ASNumberTest.java
concepts/src/test/java/org/opendaylight/protocol/concepts/BandwidthTest.java
concepts/src/test/java/org/opendaylight/protocol/concepts/DefaultInstanceReferenceTest.java
concepts/src/test/java/org/opendaylight/protocol/concepts/HandlerRegistryTest.java
concepts/src/test/java/org/opendaylight/protocol/concepts/ISOSystemIdentifierTest.java
concepts/src/test/java/org/opendaylight/protocol/concepts/MultiRegistryTest.java

index 5afff1efa5e21054e16b22bffc9b48d476a41b4c..6cbb264b3e55a966aaa952f8fedefd37fe44dca5 100644 (file)
@@ -14,22 +14,22 @@ public class HandlerRegistry<C, P, S> {
     private final MultiRegistry<Integer, P> parsers = new MultiRegistry<>();
 
     public Registration registerParser(final int type, final P parser) {
-        return this.parsers.register(type, parser);
+        return parsers.register(type, parser);
     }
 
     public P getParser(final int type) {
-        return this.parsers.get(type);
+        return parsers.get(type);
     }
 
     public Registration registerSerializer(final Class<? extends C> clazz, final S serializer) {
-        return this.serializers.register(clazz, serializer);
+        return serializers.register(clazz, serializer);
     }
 
     public S getSerializer(final Class<? extends C> clazz) {
-        return this.serializers.get(clazz);
+        return serializers.get(clazz);
     }
 
     public Iterable<S> getAllSerializers() {
-        return this.serializers.getAllValues();
+        return serializers.getAllValues();
     }
 }
index edfb706726e5ec23561980933670335d22d4db64..4e90603719678bb7108e3f67e11f066109e642a6 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.protocol.concepts;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.ListMultimap;
-import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import org.checkerframework.checker.lock.qual.GuardedBy;
@@ -34,6 +33,7 @@ import org.slf4j.LoggerFactory;
  */
 public final class MultiRegistry<K, V> {
     private static final Logger LOG = LoggerFactory.getLogger(MultiRegistry.class);
+
     private final ConcurrentMap<K, V> current = new ConcurrentHashMap<>();
 
     @GuardedBy("this")
@@ -41,18 +41,18 @@ public final class MultiRegistry<K, V> {
 
     @Holding("this")
     private void updateCurrent(final K key) {
-        final List<V> values = this.candidates.get(key);
+        final var values = candidates.get(key);
 
         // Simple case: no candidates
         if (values.isEmpty()) {
-            this.current.remove(key);
+            current.remove(key);
             return;
         }
 
         V best = values.get(0);
-        for (V v : values) {
-            final Class<?> vc = v.getClass();
-            final Class<?> bc = best.getClass();
+        for (var v : values) {
+            final var vc = v.getClass();
+            final var bc = best.getClass();
             if (bc.isAssignableFrom(vc)) {
                 LOG.debug("{} is superclass of {}, preferring the latter", bc, vc);
                 best = v;
@@ -64,11 +64,11 @@ public final class MultiRegistry<K, V> {
         }
 
         LOG.debug("New best value {}", best);
-        this.current.put(key, best);
+        current.put(key, best);
     }
 
     public synchronized Registration register(final K key, final V value) {
-        this.candidates.put(key, value);
+        candidates.put(key, value);
         updateCurrent(key);
 
         return new AbstractRegistration() {
@@ -83,10 +83,10 @@ public final class MultiRegistry<K, V> {
     }
 
     public V get(final K key) {
-        return this.current.get(key);
+        return current.get(key);
     }
 
     public Iterable<V> getAllValues() {
-        return Iterables.unmodifiableIterable(this.current.values());
+        return Iterables.unmodifiableIterable(current.values());
     }
 }
index 42b82102599de94765d8a8c024f8b366e6125111..115d93528249304e0cfad901cd21a0d81d3a87b4 100644 (file)
@@ -10,55 +10,46 @@ package org.opendaylight.protocol.concepts;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.not;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 
 import java.util.HashSet;
-import java.util.Set;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
 import org.opendaylight.yangtools.yang.common.Uint32;
 
-public class ASNumberTest {
-    private AsNumber asn1;
-    private AsNumber asn3;
-    private AsNumber asn4;
-
-    @Before
-    public void setUp() {
-        asn1 = new AsNumber(Uint32.valueOf(4294967295L));
-        asn3 = new AsNumber(Uint32.valueOf(200));
-        asn4 = new AsNumber(Uint32.valueOf(429496335L));
-    }
+class ASNumberTest {
+    private static final AsNumber ASN1 = new AsNumber(Uint32.valueOf(4294967295L));
+    private static final AsNumber ASN3 = new AsNumber(Uint32.valueOf(200));
+    private static final AsNumber ASN4 = new AsNumber(Uint32.valueOf(429496335L));
 
     @Test
-    public void testHashCode() {
-        final Set<AsNumber> set = new HashSet<>();
+    void testHashCode() {
+        final var set = new HashSet<AsNumber>();
 
-        set.add(asn1);
+        set.add(ASN1);
         assertEquals(1, set.size());
 
-        set.add(asn3);
+        set.add(ASN3);
         assertEquals(2, set.size());
     }
 
     @Test
-    public void testGetters() {
-        assertEquals(4294967295L, asn1.getValue().longValue());
+    void testGetters() {
+        assertEquals(4294967295L, ASN1.getValue().longValue());
     }
 
     @Test
-    public void testEquals() {
-        assertThat(asn1, not(equalTo(asn3)));
-        assertThat(asn1, not(equalTo(asn4)));
-        assertThat(asn1, not(equalTo(new Object())));
-        assertNotEquals(asn1, new Object());
+    void testEquals() {
+        assertThat(ASN1, not(equalTo(ASN3)));
+        assertThat(ASN1, not(equalTo(ASN4)));
+        assertThat(ASN1, not(equalTo(new Object())));
+        assertNotEquals(ASN1, new Object());
     }
 
     @Test
-    public void testToString() {
-        assertEquals("AsNumber{value=4294967295}", asn1.toString());
-        assertEquals("AsNumber{value=200}", asn3.toString());
+    void testToString() {
+        assertEquals("AsNumber{value=4294967295}", ASN1.toString());
+        assertEquals("AsNumber{value=200}", ASN3.toString());
     }
 }
index c32cbb9537dc77c94e2e96cef6412ef602a19b22..229c25e3dcc3c72e7b28d404a35350d3b28f9a9f 100644 (file)
@@ -10,60 +10,50 @@ package org.opendaylight.protocol.concepts;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.not;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 
 import io.netty.buffer.Unpooled;
 import java.util.HashSet;
-import java.util.Set;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
 
-public class BandwidthTest {
-    private Bandwidth b1;
-    private Bandwidth b2;
-    private Bandwidth b3;
-    private Bandwidth b4;
-
-    @Before
-    public void setUp() {
-        this.b1 = new Bandwidth(Unpooled.copyInt(1000).array());
-        this.b2 = new Bandwidth(Unpooled.copyInt(2000).array());
-        this.b3 = new Bandwidth(Unpooled.copyInt(2000).array());
-        this.b4 = new Bandwidth(Unpooled.copyInt(100).array());
-    }
+class BandwidthTest {
+    private static final Bandwidth B1 = new Bandwidth(Unpooled.copyInt(1000).array());
+    private static final Bandwidth B2 = new Bandwidth(Unpooled.copyInt(2000).array());
+    private static final Bandwidth B3 = new Bandwidth(Unpooled.copyInt(2000).array());
+    private static final Bandwidth B4 = new Bandwidth(Unpooled.copyInt(100).array());
 
     @Test
-    public void testBitsBytes() {
-        assertEquals(1000.0, Unpooled.wrappedBuffer(this.b1.getValue()).readInt(), 0.1);
+    void testBitsBytes() {
+        assertEquals(1000.0, Unpooled.wrappedBuffer(B1.getValue()).readInt(), 0.1);
     }
 
     @Test
-    public void testEquals() {
-        assertFalse(this.b1.equals(null));
-        assertThat(this.b1, not(equalTo(new Object())));
-        assertThat(this.b1, equalTo(this.b1));
-        assertThat(this.b1, not(equalTo(this.b2)));
-        assertEquals(this.b2, this.b3);
-        assertNotEquals(this.b1, new Object());
+    void testEquals() {
+        assertFalse(B1.equals(null));
+        assertThat(B1, not(equalTo(new Object())));
+        assertThat(B1, equalTo(B1));
+        assertThat(B1, not(equalTo(B2)));
+        assertEquals(B2, B3);
+        assertNotEquals(B1, new Object());
     }
 
     @Test
-    public void testHashCode() {
-        final Set<Bandwidth> set = new HashSet<>();
+    void testHashCode() {
+        final var set = new HashSet<Bandwidth>();
 
-        set.add(this.b1);
+        set.add(B1);
         assertEquals(1, set.size());
 
-        set.add(this.b2);
+        set.add(B2);
         assertEquals(2, set.size());
 
-        set.add(this.b3);
+        set.add(B3);
         assertEquals(2, set.size());
 
-        set.add(this.b4);
+        set.add(B4);
         assertEquals(3, set.size());
     }
 }
index f247297a261e937e273627e7f3117f69c23660b1..61b81973423756b5c1154cf85cf822e49da75316 100644 (file)
@@ -5,28 +5,26 @@
  * 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 org.junit.Assert;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
-public class DefaultInstanceReferenceTest {
-
-    private static final InstanceIdentifier<NetworkTopology> IID
-            = InstanceIdentifier.builder(NetworkTopology.class).build();
+class DefaultInstanceReferenceTest {
+    private static final InstanceIdentifier<NetworkTopology> IID = InstanceIdentifier.create(NetworkTopology.class);
 
     @Test
-    public void testDefaultInstanceReference() {
-        final DefaultInstanceReference<NetworkTopology> defaultIID = new DefaultInstanceReference<>(IID);
-        Assert.assertEquals(IID, defaultIID.getInstanceIdentifier());
+    void testDefaultInstanceReference() {
+        final var defaultIID = new DefaultInstanceReference<>(IID);
+        assertEquals(IID, defaultIID.getInstanceIdentifier());
     }
 
-    @Test(expected = NullPointerException.class)
-    public void testNullReference() {
-        new DefaultInstanceReference<>(null);
+    @Test
+    void testNullReference() {
+        assertThrows(NullPointerException.class, () -> new DefaultInstanceReference<>(null));
     }
-
 }
index 199efdc6b108e87542a8b77ef99f332040b48690..fcbf954def02d0ba479badebdd9b3268767cafc3 100644 (file)
@@ -7,23 +7,22 @@
  */
 package org.opendaylight.protocol.concepts;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
 
-import org.junit.Test;
-import org.opendaylight.yangtools.concepts.Registration;
+import org.junit.jupiter.api.Test;
 
-public class HandlerRegistryTest {
+class HandlerRegistryTest {
     private static final String PARSER = "parser";
     private static final String SERIALIZER = "serializer";
     private static final int TYPE = 1;
 
     @Test
-    public void testHandlerRegistry() {
-        final HandlerRegistry<Object, String, String> registry = new HandlerRegistry<>();
-        final Registration parserReg = registry.registerParser(TYPE, PARSER);
-        final Registration serializerReg = registry.registerSerializer(Object.class, SERIALIZER);
+    void testHandlerRegistry() {
+        final var registry = new HandlerRegistry<Object, String, String>();
+        final var parserReg = registry.registerParser(TYPE, PARSER);
+        final var serializerReg = registry.registerSerializer(Object.class, SERIALIZER);
 
         assertNotNull(parserReg);
         assertNotNull(serializerReg);
index 738e847878b26eb9236fb5f1834cd1006eb7a435..77f1c8923c35fa727c30833df46967e00d470be5 100644 (file)
@@ -7,23 +7,24 @@
  */
 package org.opendaylight.protocol.concepts;
 
-import org.junit.Assert;
-import org.junit.Test;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IsoSystemIdentifier;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
-public class ISOSystemIdentifierTest {
+import org.junit.jupiter.api.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IsoSystemIdentifier;
 
-    @Test(expected = IllegalArgumentException.class)
-    public void testISOSystemIdentifier() {
-        final byte[] b = new byte[] { 10, 12, 127, 0, 9, 1, 1 };
-        new IsoSystemIdentifier(b);
+class ISOSystemIdentifierTest {
+    @Test
+    void testISOSystemIdentifier() {
+        final var ex = assertThrows(IllegalArgumentException.class,
+            () -> new IsoSystemIdentifier(new byte[] { 10, 12, 127, 0, 9, 1, 1 }));
+        assertEquals("Invalid length: 0a0c7f00090101, expected: [[6..6]].", ex.getMessage());
     }
 
     @Test
-    public void testGetBytes() {
-        final byte[] b = new byte[] { 10, 12, 127, 0, 9, 1 };
-        final IsoSystemIdentifier id = new IsoSystemIdentifier(b);
-        Assert.assertArrayEquals(new byte[] { 10, 12, 127, 0, 9, 1 }, id.getValue());
+    void testGetBytes() {
+        final var id = new IsoSystemIdentifier(new byte[] { 10, 12, 127, 0, 9, 1 });
+        assertArrayEquals(new byte[] { 10, 12, 127, 0, 9, 1 }, id.getValue());
     }
-
 }
index 8210ea03cf7f655d3852cf84f9497f924c53853f..704cc30f9a510301cb4964a047db15178f950de6 100644 (file)
@@ -7,22 +7,20 @@
  */
 package org.opendaylight.protocol.concepts;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
 
-import org.junit.Test;
-import org.opendaylight.yangtools.concepts.Registration;
-
-public class MultiRegistryTest {
+import org.junit.jupiter.api.Test;
 
+class MultiRegistryTest {
     @Test
-    public void testMultiRegistry() {
-        final MultiRegistry<Object, Integer> registry = new MultiRegistry<>();
-        final String first = "first";
-        final String second = "second";
-        final String third = "third";
+    void testMultiRegistry() {
+        final var registry = new MultiRegistry<Object, Integer>();
+        final var first = "first";
+        final var second = "second";
+        final var third = "third";
 
-        final Registration a = registry.register(first, 1);
+        final var a = registry.register(first, 1);
         registry.register(second, 2);
         registry.register(third, 3);
 
@@ -44,9 +42,9 @@ public class MultiRegistryTest {
     }
 
     @Test
-    public void testDifferentNumbers() {
-        final MultiRegistry<Object, Number> registry = new MultiRegistry<>();
-        final String first = "first";
+    void testDifferentNumbers() {
+        final var registry = new MultiRegistry<Object, Number>();
+        final var first = "first";
 
         registry.register(first, 1);
         assertEquals(1, registry.get("first"));
@@ -60,9 +58,9 @@ public class MultiRegistryTest {
 
     @Test
     public void testDifferentClasses() {
-        final MultiRegistry<Object, Object> registry = new MultiRegistry<>();
-        final String first = "first";
-        final String second = "second";
+        final var registry = new MultiRegistry<>();
+        final var first = "first";
+        final var second = "second";
 
         registry.register(first, 1);
         assertEquals(1, registry.get("first"));