Convert uint tests to assertThrows() 98/92698/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 2 Oct 2020 13:28:32 +0000 (15:28 +0200)
committerRobert Varga <nite@hq.sk>
Fri, 2 Oct 2020 14:06:37 +0000 (14:06 +0000)
We have a number of tests which can be consolidated, use assertThrows.
This flushes out a bad test case, which is corrected.

Change-Id: I3b0b760cb89a4dbd93ebe44af2ed917b0698fc31
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint16Test.java
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint32Test.java
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint64Test.java
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint8Test.java

index 0aca244e851f6571cdef8953f7df953c77ba25c3..17f7ec2976feeecdfb1982123881486ebeeda349 100644 (file)
@@ -13,6 +13,7 @@ import static org.hamcrest.Matchers.lessThan;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
@@ -100,9 +101,9 @@ public class Uint16Test {
         assertEquals(Uint64.valueOf(65535), Uint16.MAX_VALUE.toUint64());
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testToUint8() {
-        Uint16.MAX_VALUE.toUint8();
+        assertThrows(IllegalArgumentException.class, () -> Uint16.MAX_VALUE.toUint8());
     }
 
     @Test
@@ -121,38 +122,22 @@ public class Uint16Test {
         assertSame(source, read);
     }
 
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeByte() {
-        Uint16.valueOf((byte)-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeShort() {
-        Uint16.valueOf((short)-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeInt() {
-        Uint16.valueOf(-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeLong() {
-        Uint16.valueOf(-1L);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testBigInt() {
-        Uint16.valueOf(65536);
+    @Test
+    public void testNegativeValues() {
+        assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf((byte)-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf((short)-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(-1L));
     }
 
-    @Test(expected = IllegalArgumentException.class)
-    public void testBigLong() {
-        Uint16.valueOf(65536L);
+    @Test
+    public void testLargeValues() {
+        assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(65536));
+        assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(65536L));
     }
 
-    @Test(expected = NullPointerException.class)
+    @Test
     public void testNullValueOfString() {
-        Uint16.valueOf((String) null);
+        assertThrows(NullPointerException.class, () -> Uint16.valueOf((String) null));
     }
 }
index 2706dc0e842c09d09e017b8e1c7ec2fd03a32688..b1a122647a1f007c9ca31e23749614842a0817d0 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.common;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import com.google.common.primitives.UnsignedInteger;
@@ -101,14 +102,14 @@ public class Uint32Test {
         assertEquals(Uint64.valueOf(4294967295L), Uint32.MAX_VALUE.toUint64());
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testToUint8() {
-        Uint32.MAX_VALUE.toUint8();
+        assertThrows(IllegalArgumentException.class, () -> Uint32.MAX_VALUE.toUint8());
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testToUint16() {
-        Uint32.MAX_VALUE.toUint16();
+        assertThrows(IllegalArgumentException.class, () -> Uint32.MAX_VALUE.toUint16());
     }
 
     @Test
@@ -127,33 +128,21 @@ public class Uint32Test {
         assertSame(source, read);
     }
 
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeByte() {
-        Uint32.valueOf((byte)-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeShort() {
-        Uint32.valueOf((short)-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeInt() {
-        Uint32.valueOf(-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeLong() {
-        Uint32.valueOf(-1L);
+    @Test
+    public void testNegativeValues() {
+        assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((byte)-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((short)-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(-1L));
     }
 
-    @Test(expected = IllegalArgumentException.class)
-    public void testBigLong() {
-        Uint32.valueOf(4294967296L);
+    @Test
+    public void testLargeValues() {
+        assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(4294967296L));
     }
 
-    @Test(expected = NullPointerException.class)
+    @Test
     public void testNullValueOfString() {
-        Uint32.valueOf((String) null);
+        assertThrows(NullPointerException.class, () -> Uint32.valueOf((String) null));
     }
 }
index dfc5e84f33bd167c029dc62f5a807fc3a7f50617..521eb33690df0a449a620af4b7a279900081b836 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.common;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import com.google.common.primitives.UnsignedLong;
@@ -105,19 +106,19 @@ public class Uint64Test {
         assertEquals(Uint32.TEN, Uint64.TEN.toUint32());
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testToUint8() {
-        Uint64.MAX_VALUE.toUint8();
+        assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint8());
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testToUint16() {
-        Uint64.MAX_VALUE.toUint16();
+        assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint16());
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testToUint32() {
-        Uint64.MAX_VALUE.toUint32();
+        assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint32());
     }
 
     @Test
@@ -136,43 +137,24 @@ public class Uint64Test {
         assertSame(source, read);
     }
 
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeByte() {
-        Uint64.valueOf((byte)-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeShort() {
-        Uint64.valueOf((short)-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeInt() {
-        Uint64.valueOf(-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeLong() {
-        Uint64.valueOf(-1L);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeBigInteger() {
-        Uint64.valueOf(new BigInteger("-1"));
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testBigBigInteger() {
-        Uint64.valueOf(new BigInteger("0x10000000000000000"));
+    @Test
+    public void testNegativeValues() {
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf((byte)-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf((short)-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(-1L));
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(new BigInteger("-1")));
     }
 
-    @Test(expected = NullPointerException.class)
-    public void testNullValueOfString() {
-        Uint64.valueOf((String) null);
+    @Test
+    public void testLargeValues() {
+        final BigInteger big = new BigInteger("10000000000000000", 16);
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(big));
     }
 
-    @Test(expected = NullPointerException.class)
-    public void testNullValueOfBigInteger() {
-        Uint64.valueOf((BigInteger) null);
+    @Test
+    public void testNullValueOf() {
+        assertThrows(NullPointerException.class, () -> Uint64.valueOf((String) null));
+        assertThrows(NullPointerException.class, () -> Uint64.valueOf((BigInteger) null));
     }
 }
index a231e96a219e45046a803774bfb00badfffc4447..83c516ceeabb72220201a57d043b05fd7b55ea01 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.common;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
@@ -112,43 +113,23 @@ public class Uint8Test {
         assertSame(source, read);
     }
 
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeByte() {
-        Uint8.valueOf((byte)-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeShort() {
-        Uint8.valueOf((short)-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeInt() {
-        Uint8.valueOf(-1);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testNegativeLong() {
-        Uint8.valueOf(-1L);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testBigShort() {
-        Uint8.valueOf((short)256);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testBigInt() {
-        Uint8.valueOf(256);
+    @Test
+    public void testNegativeValues() {
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((byte)-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((short)-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(-1));
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(-1L));
     }
 
-    @Test(expected = IllegalArgumentException.class)
-    public void testBigLong() {
-        Uint8.valueOf(256L);
+    @Test
+    public void testLargeValues() {
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((short)256));
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(256));
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(256L));
     }
 
-    @Test(expected = NullPointerException.class)
+    @Test
     public void testNullValueOfString() {
-        Uint8.valueOf((String) null);
+        assertThrows(NullPointerException.class, () -> Uint8.valueOf((String) null));
     }
 }