From 0fa5e1bab665c4b83963c7ea18be74372cb67435 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 8 Oct 2024 11:24:37 +0200 Subject: [PATCH] Update tests We are about to switch serialization proxies, update tests to show how the format changes. Change-Id: I6b448891519a374dc53a24fa48f1455b3c6078dc Signed-off-by: Robert Varga --- .../yangtools/yang/common/Decimal64Test.java | 165 ++++++++++-------- .../yangtools/yang/common/EmptyTest.java | 27 +-- .../yangtools/yang/common/Uint16Test.java | 65 +++---- .../yangtools/yang/common/Uint32Test.java | 66 +++---- .../yangtools/yang/common/Uint64Test.java | 69 ++++---- .../yangtools/yang/common/Uint8Test.java | 61 ++++--- 6 files changed, 246 insertions(+), 207 deletions(-) diff --git a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Decimal64Test.java b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Decimal64Test.java index 9b38a85c67..9cca66aeec 100644 --- a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Decimal64Test.java +++ b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Decimal64Test.java @@ -7,118 +7,122 @@ */ package org.opendaylight.yangtools.yang.common; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.math.BigDecimal; import java.math.RoundingMode; +import java.util.HexFormat; import org.junit.jupiter.api.Test; -public class Decimal64Test { +class Decimal64Test { @Test - public void testParseEmpty() { + void testParseEmpty() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("")); } @Test - public void testParseSingleIllegal() { + void testParseSingleIllegal() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("a")); } @Test - public void testParseSingleHighIllegal() { + void testParseSingleHighIllegal() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(":")); } @Test - public void testParseZeroIllegal() { + void testParseZeroIllegal() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0a")); } @Test - public void testParseZeroHighIllegal() { + void testParseZeroHighIllegal() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0:")); } @Test - public void testParseZeroPointIllegal() { + void testParseZeroPointIllegal() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.a")); } @Test - public void testParseZeroPointHighIllegal() { + void testParseZeroPointHighIllegal() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.:")); } @Test - public void testParsePointIllegal() { + void testParsePointIllegal() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(".a")); } @Test - public void testParseMinus() { + void testParseMinus() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("-")); } @Test - public void testParsePlus() { + void testParsePlus() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("+")); } @Test - public void testParsePeriod() { + void testParsePeriod() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf(".")); } @Test - public void testParseTwoPeriods() { + void testParseTwoPeriods() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("..")); } @Test - public void testParseTrailingPeriod() { + void testParseTrailingPeriod() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.")); } @Test - public void testParseMultiplePeriods() { + void testParseMultiplePeriods() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.1.")); } @Test - public void testParseLongString() { - Decimal64.valueOf("123456789012345678"); + void testParseLongString() { + assertEquals(Decimal64.of(1, 1234567890123456780L), Decimal64.valueOf("123456789012345678")); } @Test - public void testParseLongDecimal() { - Decimal64.valueOf("0.12345678901234568"); + void testParseLongDecimal() { + assertEquals(Decimal64.of(18, 12345678901234568L), Decimal64.valueOf("0.12345678901234568")); } @Test - public void testFractionLimits() { - Decimal64.valueOf("922337203685477580.7"); - Decimal64.valueOf("9.223372036854775807"); + void testFractionLimits() { + assertEquals(Decimal64.of(1, 9223372036854775807L), Decimal64.valueOf("922337203685477580.7")); + assertEquals(Decimal64.of(18, 9223372036854775807L), Decimal64.valueOf("9.223372036854775807")); assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("922337203685477580.71")); assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("9.2233720368547758071")); } @Test - public void testParseTooLongString() { + void testParseTooLongString() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("1234567890123456789")); } @Test - public void testParseTooLongDecimal() { + void testParseTooLongDecimal() { assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.1234567890123456789")); } @Test - public void testParse() { + void testParse() { assertParsedVariants("0", 0, 0, 1); assertParsedVariants("0.00", 0, 0, 1); assertParsedVariants("00.0", 0, 0, 1); @@ -128,7 +132,7 @@ public class Decimal64Test { assertParsedVariants("10.0900900", 10, 9009, 5); assertParsedVariants("0002210.09", 2210, 9, 2); - Decimal64 parsed = assertParsedString("0.0", 0, 0, 1, false); + var parsed = assertParsedString("0.0", 0, 0, 1, false); parsed = assertParsedString("+0.0", 0, 0, 1, false); assertEquals("0.0", parsed.toString()); assertEquals("0.0", parsed.toString()); @@ -140,12 +144,12 @@ public class Decimal64Test { } @Test - public void testCompare() { - final Decimal64 one = Decimal64.valueOf("1"); - final Decimal64 two = Decimal64.valueOf("2"); - final Decimal64 three = Decimal64.valueOf("3"); - final Decimal64 negOne = Decimal64.valueOf("-1"); - final Decimal64 anotherOne = Decimal64.valueOf("1"); + void testCompare() { + final var one = Decimal64.valueOf("1"); + final var two = Decimal64.valueOf("2"); + final var three = Decimal64.valueOf("3"); + final var negOne = Decimal64.valueOf("-1"); + final var anotherOne = Decimal64.valueOf("1"); assertEquals(0, one.compareTo(one)); assertEquals(0, one.compareTo(anotherOne)); @@ -160,10 +164,10 @@ public class Decimal64Test { } @Test - public void testEquals() { - final Decimal64 one = Decimal64.valueOf("1"); - final Decimal64 two = Decimal64.valueOf("2"); - final Decimal64 anotherOne = Decimal64.valueOf("1"); + void testEquals() { + final var one = Decimal64.valueOf("1"); + final var two = Decimal64.valueOf("2"); + final var anotherOne = Decimal64.valueOf("1"); assertTrue(one.equals(one)); assertTrue(one.equals(anotherOne)); @@ -175,7 +179,7 @@ public class Decimal64Test { } @Test - public void testConversions() { + void testConversions() { assertEquals(new BigDecimal("0.12"), Decimal64.valueOf("0.12").decimalValue()); assertEquals(new BigDecimal("-0.12"), Decimal64.valueOf("-0.12").decimalValue()); assertEquals(new BigDecimal("0.12"), Decimal64.valueOf("+0.12").decimalValue()); @@ -193,7 +197,7 @@ public class Decimal64Test { } @Test - public void testFactories() { + void testFactories() { assertEquals("0.0", Decimal64.valueOf(1, (byte) 0).toString()); assertEquals("1.0", Decimal64.valueOf(1, (byte) 1).toString()); assertEquals("-1.0", Decimal64.valueOf(1, (byte) -1).toString()); @@ -224,12 +228,12 @@ public class Decimal64Test { } @Test - public void testLeadingZeroToString() { + void testLeadingZeroToString() { assertEquals("-0.63", Decimal64.valueOf("-0.63").toString()); } @Test - public void testFractionPartToString() { + void testFractionPartToString() { assertEquals("0.3", Decimal64.valueOf("0.3").toString()); assertEquals("0.03", Decimal64.valueOf("0.03").toString()); assertEquals("0.003", Decimal64.valueOf("0.003").toString()); @@ -239,7 +243,7 @@ public class Decimal64Test { } @Test - public void testScalingToString() { + void testScalingToString() { assertEquals("30.0", Decimal64.of(1, 300).toString()); assertEquals("3.0", Decimal64.of(2, 300).toString()); assertEquals("0.3", Decimal64.of(3, 300).toString()); @@ -247,7 +251,7 @@ public class Decimal64Test { } @Test - public void testBoundaries() { + void testBoundaries() { assertEquals(-128L, Decimal64.valueOf(1, Byte.MIN_VALUE).longValue()); assertEquals(127L, Decimal64.valueOf(1, Byte.MAX_VALUE).longValue()); assertEquals(-32768L, Decimal64.valueOf(2, Short.MIN_VALUE).longValue()); @@ -257,81 +261,81 @@ public class Decimal64Test { } @Test - public void testByteValueExact() { + void testByteValueExact() { assertEquals(Byte.MIN_VALUE, Decimal64.valueOf(1, Byte.MIN_VALUE).byteValueExact()); assertEquals(Byte.MAX_VALUE, Decimal64.valueOf(1, Byte.MAX_VALUE).byteValueExact()); } @Test - public void testByteValueExactFrac() { - final Decimal64 dec = Decimal64.valueOf("1.1"); + void testByteValueExactFrac() { + final var dec = Decimal64.valueOf("1.1"); assertThrows(ArithmeticException.class, () -> dec.byteValueExact()); } @Test - public void testByteValueExactRange() { - final Decimal64 dec = Decimal64.valueOf(1, Byte.MAX_VALUE + 1); + void testByteValueExactRange() { + final var dec = Decimal64.valueOf(1, Byte.MAX_VALUE + 1); assertThrows(ArithmeticException.class, () -> dec.byteValueExact()); } @Test - public void testShortValueExact() { + void testShortValueExact() { assertEquals(Short.MIN_VALUE, Decimal64.valueOf(1, Short.MIN_VALUE).shortValueExact()); assertEquals(Short.MAX_VALUE, Decimal64.valueOf(1, Short.MAX_VALUE).shortValueExact()); } @Test - public void testShortValueExactFrac() { - final Decimal64 dec = Decimal64.valueOf("1.1"); + void testShortValueExactFrac() { + final var dec = Decimal64.valueOf("1.1"); assertThrows(ArithmeticException.class, () -> dec.shortValueExact()); } @Test - public void testShortValueExactRange() { - final Decimal64 dec = Decimal64.valueOf(1, Short.MAX_VALUE + 1); + void testShortValueExactRange() { + final var dec = Decimal64.valueOf(1, Short.MAX_VALUE + 1); assertThrows(ArithmeticException.class, () -> dec.shortValueExact()); } @Test - public void testIntValueExact() { + void testIntValueExact() { assertEquals(Integer.MIN_VALUE, Decimal64.valueOf(1, Integer.MIN_VALUE).intValueExact()); assertEquals(Integer.MAX_VALUE, Decimal64.valueOf(1, Integer.MAX_VALUE).intValueExact()); } @Test - public void testIntValueExactFrac() { - final Decimal64 dec = Decimal64.valueOf("1.1"); + void testIntValueExactFrac() { + final var dec = Decimal64.valueOf("1.1"); assertThrows(ArithmeticException.class, () -> dec.intValueExact()); } @Test - public void testIntValueExactRange() { - final Decimal64 dec = Decimal64.valueOf(1, Integer.MAX_VALUE + 1L); + void testIntValueExactRange() { + final var dec = Decimal64.valueOf(1, Integer.MAX_VALUE + 1L); assertThrows(ArithmeticException.class, () -> dec.intValueExact()); } @Test - public void testLongValueExactFrac() { - final Decimal64 dec = Decimal64.valueOf("1.1"); + void testLongValueExactFrac() { + final var dec = Decimal64.valueOf("1.1"); assertThrows(ArithmeticException.class, () -> dec.longValueExact()); } @Test - public void testLongValueOfBits() { - final Decimal64 dec = Decimal64.valueOf(2, 25552555555L); + void testLongValueOfBits() { + final var dec = Decimal64.valueOf(2, 25552555555L); assertEquals(2, dec.scale()); assertEquals(2555255555500L, dec.unscaledValue()); } @Test - public void testLongValueOfNegativeBits() { - final Decimal64 dec = Decimal64.valueOf(2, -25552555555L); + void testLongValueOfNegativeBits() { + final var dec = Decimal64.valueOf(2, -25552555555L); assertEquals(2, dec.scale()); assertEquals(-2555255555500L, dec.unscaledValue()); } @Test - public void testByteRange() { + void testByteRange() { for (int i = 1; i <= 16; ++i) { assertEquals(Byte.MIN_VALUE, Decimal64.valueOf(i, Byte.MIN_VALUE).byteValueExact()); assertEquals(Byte.MAX_VALUE, Decimal64.valueOf(i, Byte.MAX_VALUE).byteValueExact()); @@ -344,7 +348,7 @@ public class Decimal64Test { } @Test - public void testShortRange() { + void testShortRange() { for (int i = 1; i <= 14; ++i) { assertEquals(Short.MIN_VALUE, Decimal64.valueOf(i, Short.MIN_VALUE).shortValueExact()); assertEquals(Short.MAX_VALUE, Decimal64.valueOf(i, Short.MAX_VALUE).shortValueExact()); @@ -357,7 +361,7 @@ public class Decimal64Test { } @Test - public void testIntRange() { + void testIntRange() { for (int i = 1; i <= 9; ++i) { assertEquals(Integer.MIN_VALUE, Decimal64.valueOf(i, Integer.MIN_VALUE).intValueExact()); assertEquals(Integer.MAX_VALUE, Decimal64.valueOf(i, Integer.MAX_VALUE).intValueExact()); @@ -370,7 +374,7 @@ public class Decimal64Test { } @Test - public void testLongRange() { + void testLongRange() { for (int i = 1; i <= 18; ++i) { int scale = i; assertThrows(IllegalArgumentException.class, () -> Decimal64.valueOf(scale, Long.MIN_VALUE)); @@ -378,12 +382,31 @@ public class Decimal64Test { } } + @Test + void testSerialization() throws Exception { + final var source = Decimal64.valueOf("-0.63"); + final var bos = new ByteArrayOutputStream(); + try (var oos = new ObjectOutputStream(bos)) { + oos.writeObject(source); + } + + final var bytes = bos.toByteArray(); + assertEquals(""" + aced0005737200306f72672e6f70656e6461796c696768742e79616e67746f6f6c732e79616e672e636f6d6d6f6e2e446563696d616\ + c363400000000000000010200024200066f66667365744a000576616c7565787200106a6176612e6c616e672e4e756d62657286ac95\ + 1d0b94e08b020000787001ffffffffffffffc1""", HexFormat.of().formatHex(bytes)); + + try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { + assertEquals(source, ois.readObject()); + } + } + private static void assertCanonicalVariants(final String str, final long intPart, final long fracPart, final int digits) { assertCanonicalString(str, intPart, fracPart, digits, false); assertCanonicalString("-" + str, intPart, fracPart, digits, true); - final Decimal64 parsed = assertParsedString("+" + str, intPart, fracPart, digits, false); + final var parsed = assertParsedString("+" + str, intPart, fracPart, digits, false); assertEquals(str, parsed.toString()); } @@ -396,13 +419,13 @@ public class Decimal64Test { private static void assertCanonicalString(final String str, final long intPart, final long fracPart, final int digits, final boolean negative) { - final Decimal64 parsed = assertParsedString(str, intPart, fracPart, digits, negative); + final var parsed = assertParsedString(str, intPart, fracPart, digits, negative); assertEquals(str, parsed.toString()); } private static Decimal64 assertParsedString(final String str, final long intPart, final long fracPart, final int digits, final boolean negative) { - final Decimal64 parsed = Decimal64.valueOf(str); + final var parsed = Decimal64.valueOf(str); assertEquals(new Decimal64((byte) digits, intPart, fracPart, negative), parsed); return parsed; } diff --git a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/EmptyTest.java b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/EmptyTest.java index 7c1f9a3e9e..f1880e48de 100644 --- a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/EmptyTest.java +++ b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/EmptyTest.java @@ -13,35 +13,36 @@ import static org.junit.jupiter.api.Assertions.assertSame; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.util.HexFormat; import org.junit.jupiter.api.Test; -public class EmptyTest { - +class EmptyTest { @Test - public void testInstanceNotNull() { + void testInstanceNotNull() { assertNotNull(Empty.value()); } @Test - public void testToString() { + void testToString() { assertEquals("empty", Empty.value().toString()); } @Test - public void testSerialization() throws IOException, ClassNotFoundException { - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { + void testSerialization() throws Exception { + final var bos = new ByteArrayOutputStream(); + try (var oos = new ObjectOutputStream(bos)) { oos.writeObject(Empty.value()); } - final Object read; - try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) { - read = ois.readObject(); - } + final var bytes = bos.toByteArray(); + assertEquals(""" + aced00057372002c6f72672e6f70656e6461796c696768742e79616e67746f6f6c732e79616e672e636f6d6d6f6e2e456d707479000\ + 00000000000010200007870""", HexFormat.of().formatHex(bytes)); - assertSame(Empty.value(), read); + try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { + assertSame(Empty.value(), ois.readObject()); + } } } diff --git a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint16Test.java b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint16Test.java index 420276f22d..23aa959185 100644 --- a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint16Test.java +++ b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint16Test.java @@ -18,14 +18,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.util.HexFormat; import org.junit.jupiter.api.Test; -public class Uint16Test { +class Uint16Test { @Test - public void testValueOf() { + void testValueOf() { assertEquals(127, Uint16.valueOf(Byte.MAX_VALUE).byteValue()); assertEquals(32767, Uint16.valueOf(Short.MAX_VALUE).shortValue()); assertEquals(65535, Uint16.valueOf(65535).intValue()); @@ -34,7 +34,7 @@ public class Uint16Test { } @Test - public void testSaturatedOf() { + void testSaturatedOf() { assertEquals(127, Uint16.saturatedOf((byte) 127).byteValue()); assertEquals(127, Uint16.saturatedOf((short) 127).byteValue()); assertEquals(127, Uint16.saturatedOf(127).byteValue()); @@ -46,10 +46,10 @@ public class Uint16Test { } @Test - public void testCompareTo() { - final Uint16 five = Uint16.valueOf(5); - final Uint16 zero = Uint16.valueOf(0); - final Uint16 max = Uint16.valueOf(65535); + void testCompareTo() { + final var five = Uint16.valueOf(5); + final var zero = Uint16.valueOf(0); + final var max = Uint16.valueOf(65535); assertEquals(0, zero.compareTo(zero)); assertThat(zero.compareTo(five), lessThan(0)); @@ -65,12 +65,12 @@ public class Uint16Test { } @Test - public void testEquals() { - final Uint16 five = Uint16.valueOf(5); - final Uint16 zero = Uint16.valueOf(0); - final Uint16 max = Uint16.valueOf(65535); + void testEquals() { + final var five = Uint16.valueOf(5); + final var zero = Uint16.valueOf(0); + final var max = Uint16.valueOf(65535); - final Uint16 test = new Uint16(five); + final var test = new Uint16(five); assertFalse(test.equals(zero)); assertFalse(test.equals(new Object())); assertFalse(test.equals(max)); @@ -80,7 +80,7 @@ public class Uint16Test { } @Test - public void testToString() { + void testToString() { assertEquals("0", Uint16.valueOf(0).toString()); assertEquals("32767", Uint16.valueOf(32767).toString()); assertEquals("32768", Uint16.valueOf(32768).toString()); @@ -88,22 +88,22 @@ public class Uint16Test { } @Test - public void testHashCode() { + void testHashCode() { assertEquals(Short.hashCode((short)-63), Uint16.fromShortBits((short)-63).hashCode()); } @Test - public void testFloatValue() { + void testFloatValue() { assertEquals(0, Uint16.valueOf(0).floatValue(), 0); } @Test - public void testDoubleValue() { + void testDoubleValue() { assertEquals(0, Uint16.valueOf(0).doubleValue(), 0); } @Test - public void testConversions() { + void testConversions() { assertSame(Uint16.valueOf(5), Uint16.valueOf(Uint8.valueOf(5))); assertSame(Uint16.valueOf(10), Uint16.valueOf(Uint32.valueOf(10))); assertSame(Uint16.valueOf(20), Uint16.valueOf(Uint64.valueOf(20))); @@ -114,28 +114,31 @@ public class Uint16Test { } @Test - public void testToUint8() { + void testToUint8() { assertThrows(IllegalArgumentException.class, () -> Uint16.MAX_VALUE.toUint8()); } @Test - public void testSerialization() throws IOException, ClassNotFoundException { - final Uint16 source = Uint16.valueOf(255); - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { + void testSerialization() throws Exception { + final var source = Uint16.valueOf(255); + final var bos = new ByteArrayOutputStream(); + try (var oos = new ObjectOutputStream(bos)) { oos.writeObject(source); } - final Object read; - try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) { - read = ois.readObject(); - } + final var bytes = bos.toByteArray(); + assertEquals(""" + aced00057372002d6f72672e6f70656e6461796c696768742e79616e67746f6f6c732e79616e672e636f6d6d6f6e2e55696e7431360\ + 00000000000000102000153000576616c7565787200106a6176612e6c616e672e4e756d62657286ac951d0b94e08b020000787000ff\ + """, HexFormat.of().formatHex(bytes)); - assertSame(source, read); + try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { + assertSame(source, ois.readObject()); + } } @Test - public void testNegativeValues() { + void testNegativeValues() { assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf((byte)-1)); assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf((short)-1)); assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(-1)); @@ -148,7 +151,7 @@ public class Uint16Test { } @Test - public void testLargeValues() { + void testLargeValues() { assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(65536)); assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(65536L)); @@ -157,7 +160,7 @@ public class Uint16Test { } @Test - public void testNullValueOfString() { + void testNullValueOfString() { assertThrows(NullPointerException.class, () -> Uint16.valueOf((String) null)); } } diff --git a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint32Test.java b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint32Test.java index 0dc1dd5787..ca6f1f97f3 100644 --- a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint32Test.java +++ b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint32Test.java @@ -16,14 +16,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import com.google.common.primitives.UnsignedInteger; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.util.HexFormat; import org.junit.jupiter.api.Test; -public class Uint32Test { +class Uint32Test { @Test - public void testValueOf() { + void testValueOf() { assertEquals(127, Uint32.valueOf(Byte.MAX_VALUE).byteValue()); assertEquals(32767, Uint32.valueOf(Short.MAX_VALUE).shortValue()); assertEquals(2147483647, Uint32.valueOf(Integer.MAX_VALUE).intValue()); @@ -32,7 +32,7 @@ public class Uint32Test { } @Test - public void testSaturatedOf() { + void testSaturatedOf() { assertEquals(127, Uint32.saturatedOf((byte) 127).byteValue()); assertEquals(127, Uint32.saturatedOf((short) 127).byteValue()); assertEquals(127, Uint32.saturatedOf(127).byteValue()); @@ -44,10 +44,10 @@ public class Uint32Test { } @Test - public void testCompareTo() { - final Uint32 five = Uint32.valueOf(5); - final Uint32 zero = Uint32.valueOf(0); - final Uint32 max = Uint32.valueOf(4294967295L); + void testCompareTo() { + final var five = Uint32.valueOf(5); + final var zero = Uint32.valueOf(0); + final var max = Uint32.valueOf(4294967295L); assertEquals(0, zero.compareTo(zero)); assertEquals(-1, zero.compareTo(five)); @@ -63,12 +63,12 @@ public class Uint32Test { } @Test - public void testEquals() { - final Uint32 five = Uint32.valueOf(5); - final Uint32 zero = Uint32.valueOf(0); - final Uint32 max = Uint32.valueOf(4294967295L); + void testEquals() { + final var five = Uint32.valueOf(5); + final var zero = Uint32.valueOf(0); + final var max = Uint32.valueOf(4294967295L); - final Uint32 test = new Uint32(five); + final var test = new Uint32(five); assertFalse(test.equals(zero)); assertFalse(test.equals(new Object())); assertFalse(test.equals(max)); @@ -78,7 +78,7 @@ public class Uint32Test { } @Test - public void testToString() { + void testToString() { assertEquals("0", Uint32.valueOf(0).toString()); assertEquals("2147483647", Uint32.valueOf(2147483647L).toString()); assertEquals("2147483648", Uint32.valueOf(2147483648L).toString()); @@ -86,22 +86,22 @@ public class Uint32Test { } @Test - public void testHashCode() { + void testHashCode() { assertEquals(Integer.hashCode(-63), Uint32.fromIntBits(-63).hashCode()); } @Test - public void testFloatValue() { + void testFloatValue() { assertEquals(0, Uint32.valueOf(0).floatValue(), 0); } @Test - public void testDoubleValue() { + void testDoubleValue() { assertEquals(0, Uint32.valueOf(0).doubleValue(), 0); } @Test - public void testConversions() { + void testConversions() { assertSame(Uint32.valueOf(5), Uint32.valueOf(Uint8.valueOf(5))); assertSame(Uint32.valueOf(10), Uint32.valueOf(Uint16.valueOf(10))); assertSame(Uint32.valueOf(20), Uint32.valueOf(Uint64.valueOf(20))); @@ -115,33 +115,37 @@ public class Uint32Test { } @Test - public void testToUint8() { + void testToUint8() { assertThrows(IllegalArgumentException.class, () -> Uint32.MAX_VALUE.toUint8()); } @Test - public void testToUint16() { + void testToUint16() { assertThrows(IllegalArgumentException.class, () -> Uint32.MAX_VALUE.toUint16()); } @Test - public void testSerialization() throws IOException, ClassNotFoundException { - final Uint32 source = Uint32.valueOf(255); - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { + void testSerialization() throws Exception { + final var source = Uint32.valueOf(255); + final var bos = new ByteArrayOutputStream(); + try (var oos = new ObjectOutputStream(bos)) { oos.writeObject(source); } - final Object read; - try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) { - read = ois.readObject(); + final var bytes = bos.toByteArray(); + assertEquals(""" + aced00057372002d6f72672e6f70656e6461796c696768742e79616e67746f6f6c732e79616e672e636f6d6d6f6e2e55696e7433320\ + 00000000000000102000149000576616c7565787200106a6176612e6c616e672e4e756d62657286ac951d0b94e08b02000078700000\ + 00ff""", HexFormat.of().formatHex(bytes)); + + try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { + assertSame(source, ois.readObject()); } - assertSame(source, read); } @Test - public void testNegativeValues() { + void testNegativeValues() { assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((byte)-1)); assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((short)-1)); assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(-1)); @@ -154,14 +158,14 @@ public class Uint32Test { } @Test - public void testLargeValues() { + void testLargeValues() { assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(4294967296L)); assertEquals(Uint32.MAX_VALUE, Uint32.saturatedOf(4294967296L)); } @Test - public void testNullValueOfString() { + void testNullValueOfString() { assertThrows(NullPointerException.class, () -> Uint32.valueOf((String) null)); } } diff --git a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint64Test.java b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint64Test.java index 5ba683c22f..54cd150552 100644 --- a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint64Test.java +++ b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint64Test.java @@ -16,15 +16,15 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import com.google.common.primitives.UnsignedLong; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.math.BigInteger; +import java.util.HexFormat; import org.junit.jupiter.api.Test; -public class Uint64Test { +class Uint64Test { @Test - public void testValueOf() { + void testValueOf() { assertEquals(127, Uint64.valueOf(Byte.MAX_VALUE).byteValue()); assertEquals(32767, Uint64.valueOf(Short.MAX_VALUE).shortValue()); assertEquals(2147483647, Uint64.valueOf(Integer.MAX_VALUE).intValue()); @@ -35,7 +35,7 @@ public class Uint64Test { } @Test - public void testSaturatedOf() { + void testSaturatedOf() { assertEquals(127, Uint64.saturatedOf((byte) 127).byteValue()); assertEquals(127, Uint64.saturatedOf((short) 127).byteValue()); assertEquals(127, Uint64.saturatedOf(127).byteValue()); @@ -48,10 +48,10 @@ public class Uint64Test { } @Test - public void testCompareTo() { - final Uint64 five = Uint64.valueOf(5); - final Uint64 zero = Uint64.valueOf(0); - final Uint64 max = Uint64.valueOf(4294967295L); + void testCompareTo() { + final var five = Uint64.valueOf(5); + final var zero = Uint64.valueOf(0); + final var max = Uint64.valueOf(4294967295L); assertEquals(0, zero.compareTo(zero)); assertEquals(-1, zero.compareTo(five)); @@ -67,12 +67,12 @@ public class Uint64Test { } @Test - public void testEquals() { - final Uint64 five = Uint64.valueOf(5); - final Uint64 zero = Uint64.valueOf(0); - final Uint64 max = Uint64.valueOf(4294967295L); + void testEquals() { + final var five = Uint64.valueOf(5); + final var zero = Uint64.valueOf(0); + final var max = Uint64.valueOf(4294967295L); - final Uint64 test = new Uint64(five); + final var test = new Uint64(five); assertFalse(test.equals(zero)); assertFalse(test.equals(new Object())); assertFalse(test.equals(max)); @@ -82,7 +82,7 @@ public class Uint64Test { } @Test - public void testToString() { + void testToString() { assertEquals("0", Uint64.valueOf(0).toString()); assertEquals("2147483647", Uint64.valueOf(2147483647L).toString()); assertEquals("2147483648", Uint64.valueOf(2147483648L).toString()); @@ -90,22 +90,22 @@ public class Uint64Test { } @Test - public void testHashCode() { + void testHashCode() { assertEquals(Long.hashCode(-63), Uint64.fromLongBits(-63L).hashCode()); } @Test - public void testFloatValue() { + void testFloatValue() { assertEquals(0, Uint64.valueOf(0).floatValue(), 0); } @Test - public void testDoubleValue() { + void testDoubleValue() { assertEquals(0, Uint64.valueOf(0).doubleValue(), 0); } @Test - public void testConversions() { + void testConversions() { assertSame(Uint64.valueOf(5), Uint64.valueOf(Uint8.valueOf(5))); assertSame(Uint64.valueOf(10), Uint64.valueOf(Uint16.valueOf(10))); assertSame(Uint64.valueOf(20), Uint64.valueOf(Uint32.valueOf(20))); @@ -120,38 +120,41 @@ public class Uint64Test { } @Test - public void testToUint8() { + void testToUint8() { assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint8()); } @Test - public void testToUint16() { + void testToUint16() { assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint16()); } @Test - public void testToUint32() { + void testToUint32() { assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint32()); } @Test - public void testSerialization() throws IOException, ClassNotFoundException { - final Uint64 source = Uint64.valueOf(255); - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { + void testSerialization() throws Exception { + final var source = Uint64.valueOf(255); + final var bos = new ByteArrayOutputStream(); + try (var oos = new ObjectOutputStream(bos)) { oos.writeObject(source); } - final Object read; - try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) { - read = ois.readObject(); - } + final var bytes = bos.toByteArray(); + assertEquals(""" + aced00057372002d6f72672e6f70656e6461796c696768742e79616e67746f6f6c732e79616e672e636f6d6d6f6e2e55696e7436340\ + 0000000000000010200014a000576616c7565787200106a6176612e6c616e672e4e756d62657286ac951d0b94e08b02000078700000\ + 0000000000ff""", HexFormat.of().formatHex(bytes)); - assertSame(source, read); + try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { + assertSame(source, ois.readObject()); + } } @Test - public void testNegativeValues() { + void testNegativeValues() { assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf((byte)-1)); assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf((short)-1)); assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(-1)); @@ -166,7 +169,7 @@ public class Uint64Test { } @Test - public void testLargeValues() { + void testLargeValues() { final BigInteger big = new BigInteger("10000000000000000", 16); assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(big)); @@ -174,7 +177,7 @@ public class Uint64Test { } @Test - public void testNullValueOf() { + void testNullValueOf() { assertThrows(NullPointerException.class, () -> Uint64.valueOf((String) null)); assertThrows(NullPointerException.class, () -> Uint64.valueOf((BigInteger) null)); } diff --git a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint8Test.java b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint8Test.java index 547f2d91d4..22fb8ef237 100644 --- a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint8Test.java +++ b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint8Test.java @@ -18,11 +18,12 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.util.HexFormat; import org.junit.jupiter.api.Test; -public class Uint8Test { +class Uint8Test { @Test - public void testValueOf() { + void testValueOf() { assertEquals(127, Uint8.valueOf(Byte.MAX_VALUE).byteValue()); assertEquals(255, Uint8.valueOf(255).intValue()); assertEquals(255L, Uint8.valueOf(255L).longValue()); @@ -30,7 +31,7 @@ public class Uint8Test { } @Test - public void testSaturatedOf() { + void testSaturatedOf() { assertEquals(127, Uint8.saturatedOf((byte) 127).byteValue()); assertEquals(127, Uint8.saturatedOf((short) 127).byteValue()); assertEquals(127, Uint8.saturatedOf(127).byteValue()); @@ -42,10 +43,10 @@ public class Uint8Test { } @Test - public void testCompareTo() { - final Uint8 five = Uint8.valueOf(5); - final Uint8 zero = Uint8.valueOf(0); - final Uint8 max = Uint8.valueOf(255); + void testCompareTo() { + final var five = Uint8.valueOf(5); + final var zero = Uint8.valueOf(0); + final var max = Uint8.valueOf(255); assertEquals(0, zero.compareTo(zero)); assertEquals(-5, zero.compareTo(five)); @@ -61,12 +62,12 @@ public class Uint8Test { } @Test - public void testEquals() { - final Uint8 five = Uint8.valueOf(5); - final Uint8 zero = Uint8.valueOf(0); - final Uint8 max = Uint8.valueOf(255); + void testEquals() { + final var five = Uint8.valueOf(5); + final var zero = Uint8.valueOf(0); + final var max = Uint8.valueOf(255); - final Uint8 test = new Uint8(five); + final var test = new Uint8(five); assertFalse(test.equals(zero)); assertFalse(test.equals(new Object())); assertFalse(test.equals(max)); @@ -76,7 +77,7 @@ public class Uint8Test { } @Test - public void testToString() { + void testToString() { assertEquals("0", Uint8.valueOf(0).toString()); assertEquals("127", Uint8.valueOf(127).toString()); assertEquals("128", Uint8.valueOf(128).toString()); @@ -84,22 +85,22 @@ public class Uint8Test { } @Test - public void testHashCode() { + void testHashCode() { assertEquals(Byte.hashCode((byte)-63), Uint8.fromByteBits((byte)-63).hashCode()); } @Test - public void testFloatValue() { + void testFloatValue() { assertEquals(0, Uint8.valueOf(0).floatValue(), 0); } @Test - public void testDoubleValue() { + void testDoubleValue() { assertEquals(0, Uint8.valueOf(0).doubleValue(), 0); } @Test - public void testConversions() { + void testConversions() { assertSame(Uint8.valueOf(5), Uint8.valueOf(Uint16.valueOf(5))); assertSame(Uint8.valueOf(10), Uint8.valueOf(Uint32.valueOf(10))); assertSame(Uint8.valueOf(20), Uint8.valueOf(Uint64.valueOf(20))); @@ -110,23 +111,27 @@ public class Uint8Test { } @Test - public void testSerialization() throws IOException, ClassNotFoundException { - final Uint8 source = Uint8.valueOf(255); - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { + void testSerialization() throws IOException, ClassNotFoundException { + final var source = Uint8.valueOf(255); + final var bos = new ByteArrayOutputStream(); + try (var oos = new ObjectOutputStream(bos)) { oos.writeObject(source); } - final Object read; - try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) { - read = ois.readObject(); + final var bytes = bos.toByteArray(); + assertEquals(""" + aced00057372002c6f72672e6f70656e6461796c696768742e79616e67746f6f6c732e79616e672e636f6d6d6f6e2e55696e7438000\ + 000000000000102000142000576616c7565787200106a6176612e6c616e672e4e756d62657286ac951d0b94e08b0200007870ff""", + HexFormat.of().formatHex(bytes)); + + try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { + assertSame(source, ois.readObject()); } - assertSame(source, read); } @Test - public void testNegativeValues() { + void testNegativeValues() { assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((byte)-1)); assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((short)-1)); assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(-1)); @@ -139,7 +144,7 @@ public class Uint8Test { } @Test - public void testLargeValues() { + void testLargeValues() { assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((short)256)); assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(256)); assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(256L)); @@ -150,7 +155,7 @@ public class Uint8Test { } @Test - public void testNullValueOfString() { + void testNullValueOfString() { assertThrows(NullPointerException.class, () -> Uint8.valueOf((String) null)); } } -- 2.36.6