Use assertThrows in yang-common 65/92565/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 21 Sep 2020 15:16:12 +0000 (17:16 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 21 Sep 2020 16:19:27 +0000 (16:19 +0000)
Migrate to from Test(expected=) to assertThrows(), which makes sonar
happier. It also improves accuracy of which call is actually tested.

Change-Id: I229af710b0946113a55d06134018d8554818e356
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Decimal64Test.java
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/RevisionTest.java
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
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/YangNamesTest.java
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/YangVersionTest.java

index 45b1234e99907975bc1eb48abf07e6bd7dcc0d31..a4963125777ac7a5d164fcaace3555cfad61e791 100644 (file)
@@ -9,81 +9,81 @@ package org.opendaylight.yangtools.yang.common;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import java.math.BigDecimal;
 import org.junit.Test;
 
 public class Decimal64Test {
-
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseEmpty() {
-        Decimal64.valueOf("");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(""));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseSingleIllegal() {
-        Decimal64.valueOf("a");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("a"));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseSingleHighIllegal() {
-        Decimal64.valueOf(":");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(":"));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseZeroIllegal() {
-        Decimal64.valueOf("0a");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0a"));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseZeroHighIllegal() {
-        Decimal64.valueOf("0:");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0:"));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseZeroPointIllegal() {
-        Decimal64.valueOf("0.a");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.a"));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseZeroPointHighIllegal() {
-        Decimal64.valueOf("0.:");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.:"));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParsePointIllegal() {
-        Decimal64.valueOf(".a");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(".a"));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseMinus() {
-        Decimal64.valueOf("-");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("-"));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParsePlus() {
-        Decimal64.valueOf("+");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("+"));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParsePeriod() {
-        Decimal64.valueOf(".");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("."));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseTwoPeriods() {
-        Decimal64.valueOf("..");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(".."));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseTrailingPeriod() {
-        Decimal64.valueOf("0.");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0."));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseMultiplePeriods() {
-        Decimal64.valueOf("0.1.");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.1."));
     }
 
     @Test
@@ -96,14 +96,14 @@ public class Decimal64Test {
         Decimal64.valueOf("0.12345678901234568");
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseTooLongString() {
-        Decimal64.valueOf("1234567890123456789");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("1234567890123456789"));
     }
 
-    @Test(expected = NumberFormatException.class)
+    @Test
     public void testParseTooLongDecimal() {
-        Decimal64.valueOf("0.123456789012345689");
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.123456789012345689"));
     }
 
     @Test
@@ -224,14 +224,16 @@ public class Decimal64Test {
         assertEquals(Byte.MAX_VALUE, Decimal64.valueOf(Byte.MAX_VALUE).byteValueExact());
     }
 
-    @Test(expected = ArithmeticException.class)
+    @Test
     public void testByteValueExactFrac() {
-        Decimal64.valueOf("1.1").byteValueExact();
+        final Decimal64 dec = Decimal64.valueOf("1.1");
+        assertThrows(ArithmeticException.class, () -> dec.byteValueExact());
     }
 
-    @Test(expected = ArithmeticException.class)
+    @Test
     public void testByteValueExactRange() {
-        Decimal64.valueOf(Byte.MAX_VALUE + 1).byteValueExact();
+        final Decimal64 dec = Decimal64.valueOf(Byte.MAX_VALUE + 1);
+        assertThrows(ArithmeticException.class, () -> dec.byteValueExact());
     }
 
     @Test
@@ -240,14 +242,16 @@ public class Decimal64Test {
         assertEquals(Short.MAX_VALUE, Decimal64.valueOf(Short.MAX_VALUE).shortValueExact());
     }
 
-    @Test(expected = ArithmeticException.class)
+    @Test
     public void testShortValueExactFrac() {
-        Decimal64.valueOf("1.1").shortValueExact();
+        final Decimal64 dec = Decimal64.valueOf("1.1");
+        assertThrows(ArithmeticException.class, () -> dec.shortValueExact());
     }
 
-    @Test(expected = ArithmeticException.class)
+    @Test
     public void testShortValueExactRange() {
-        Decimal64.valueOf(Short.MAX_VALUE + 1).shortValueExact();
+        final Decimal64 dec = Decimal64.valueOf(Short.MAX_VALUE + 1);
+        assertThrows(ArithmeticException.class, () -> dec.shortValueExact());
     }
 
     @Test
@@ -256,19 +260,22 @@ public class Decimal64Test {
         assertEquals(Integer.MAX_VALUE, Decimal64.valueOf(Integer.MAX_VALUE).intValueExact());
     }
 
-    @Test(expected = ArithmeticException.class)
+    @Test
     public void testIntValueExactFrac() {
-        Decimal64.valueOf("1.1").intValueExact();
+        final Decimal64 dec = Decimal64.valueOf("1.1");
+        assertThrows(ArithmeticException.class, () -> dec.intValueExact());
     }
 
-    @Test(expected = ArithmeticException.class)
+    @Test
     public void testIntValueExactRange() {
-        Decimal64.valueOf(Integer.MAX_VALUE + 1L).intValueExact();
+        final Decimal64 dec = Decimal64.valueOf(Integer.MAX_VALUE + 1L);
+        assertThrows(ArithmeticException.class, () -> dec.intValueExact());
     }
 
-    @Test(expected = ArithmeticException.class)
+    @Test
     public void testLongValueExactFrac() {
-        Decimal64.valueOf("1.1").longValueExact();
+        final Decimal64 dec = Decimal64.valueOf("1.1");
+        assertThrows(ArithmeticException.class, () -> dec.longValueExact());
     }
 
     private static void assertCanonicalVariants(final String str, final long intPart, final long fracPart,
index 2a433142b3edb03dfd2a98deba8b475b311e75c3..47c81cd7f5eb079331f352cba420eeeed6829f1d 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.common;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
@@ -26,34 +27,34 @@ public class RevisionTest {
         assertEquals("2017-12-25", Revision.of("2017-12-25").toString());
     }
 
-    @Test(expected = NullPointerException.class)
+    @Test
     public void testOfNull() {
-        Revision.of(null);
+        assertThrows(NullPointerException.class, () -> Revision.of(null));
     }
 
-    @Test(expected = DateTimeParseException.class)
+    @Test
     public void testOfEmpty() {
-        Revision.of("");
+        assertThrows(DateTimeParseException.class, () -> Revision.of(""));
     }
 
-    @Test(expected = DateTimeParseException.class)
+    @Test
     public void testOfInvalid() {
-        Revision.of("invalid");
+        assertThrows(DateTimeParseException.class, () -> Revision.of("invalid"));
     }
 
-    @Test(expected = DateTimeParseException.class)
+    @Test
     public void testOfInvalidDate1() {
-        Revision.of("2017-13-01");
+        assertThrows(DateTimeParseException.class, () -> Revision.of("2017-13-01"));
     }
 
-    @Test(expected = DateTimeParseException.class)
+    @Test
     public void testOfInvalidDate2() {
-        Revision.of("2017-12-00");
+        assertThrows(DateTimeParseException.class, () -> Revision.of("2017-12-00"));
     }
 
-    @Test(expected = DateTimeParseException.class)
+    @Test
     public void testOfInvalidDate3() {
-        Revision.of("2017-12-32");
+        assertThrows(DateTimeParseException.class, () -> Revision.of("2017-12-32"));
     }
 
     @Test
index 0aca244e851f6571cdef8953f7df953c77ba25c3..7a5c34e5fd739c0dc55b9d452db910e5373857b1 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,38 @@ public class Uint16Test {
         assertSame(source, read);
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeByte() {
-        Uint16.valueOf((byte)-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf((byte)-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeShort() {
-        Uint16.valueOf((short)-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf((short)-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeInt() {
-        Uint16.valueOf(-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeLong() {
-        Uint16.valueOf(-1L);
+        assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(-1L));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testBigInt() {
-        Uint16.valueOf(65536);
+        assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(65536));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testBigLong() {
-        Uint16.valueOf(65536L);
+        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..2860e5badf638df29f2ceb6d90bdab352621910a 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,33 @@ public class Uint32Test {
         assertSame(source, read);
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeByte() {
-        Uint32.valueOf((byte)-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((byte)-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeShort() {
-        Uint32.valueOf((short)-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((short)-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeInt() {
-        Uint32.valueOf(-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeLong() {
-        Uint32.valueOf(-1L);
+        assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(-1L));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testBigLong() {
-        Uint32.valueOf(4294967296L);
+        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..12f096379f93678232eafe901a3fa2bd9b7b0275 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,43 @@ public class Uint64Test {
         assertSame(source, read);
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeByte() {
-        Uint64.valueOf((byte)-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf((byte)-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeShort() {
-        Uint64.valueOf((short)-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf((short)-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeInt() {
-        Uint64.valueOf(-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeLong() {
-        Uint64.valueOf(-1L);
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(-1L));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeBigInteger() {
-        Uint64.valueOf(new BigInteger("-1"));
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(new BigInteger("-1")));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testBigBigInteger() {
-        Uint64.valueOf(new BigInteger("0x10000000000000000"));
+        assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(new BigInteger("0x10000000000000000")));
     }
 
-    @Test(expected = NullPointerException.class)
+    @Test
     public void testNullValueOfString() {
-        Uint64.valueOf((String) null);
+        assertThrows(NullPointerException.class, () -> Uint64.valueOf((String) null));
     }
 
-    @Test(expected = NullPointerException.class)
+    @Test
     public void testNullValueOfBigInteger() {
-        Uint64.valueOf((BigInteger) null);
+        assertThrows(NullPointerException.class, () -> Uint64.valueOf((BigInteger) null));
     }
 }
index a231e96a219e45046a803774bfb00badfffc4447..4f6cd6ef5546e3e855f389cd2bbb373f1913710b 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,43 @@ public class Uint8Test {
         assertSame(source, read);
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeByte() {
-        Uint8.valueOf((byte)-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((byte)-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeShort() {
-        Uint8.valueOf((short)-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((short)-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeInt() {
-        Uint8.valueOf(-1);
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(-1));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNegativeLong() {
-        Uint8.valueOf(-1L);
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(-1L));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testBigShort() {
-        Uint8.valueOf((short)256);
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((short)256));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testBigInt() {
-        Uint8.valueOf(256);
+        assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(256));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testBigLong() {
-        Uint8.valueOf(256L);
+        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));
     }
 }
index c957b61d1b52a0d79efd5773c5889ad1e0e78539..58edb3f0bc3309cd1eb881153ef5796a0674929c 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.common;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 
 import java.util.AbstractMap.SimpleImmutableEntry;
 import org.junit.Test;
@@ -20,8 +21,8 @@ public class YangNamesTest {
         assertEquals(new SimpleImmutableEntry<>("foo@bar", "baz"), YangNames.parseFilename("foo@bar@baz"));
     }
 
-    @Test(expected = NullPointerException.class)
+    @Test
     public void testParseFileNameNull() {
-        YangNames.parseFilename(null);
+        assertThrows(NullPointerException.class, () -> YangNames.parseFilename(null));
     }
 }
index dff3cfb1dc0dab703f58ea01c883b18ba677c491..f2c7b9f1f9fe4bde5712a28336b84c11e892c1de 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.common;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 
 import java.util.Optional;
 import org.junit.Test;
@@ -27,9 +28,9 @@ public class YangVersionTest {
         assertEquals(Optional.of(YangVersion.VERSION_1_1), YangVersion.parse("1.1"));
     }
 
-    @Test(expected = NullPointerException.class)
+    @Test
     public void testParseNull() {
-        YangVersion.parse(null);
+        assertThrows(NullPointerException.class, () -> YangVersion.parse(null));
     }
 
     @Test