From ab7f962460afedf6aaf37c7041f818f80c5f5f72 Mon Sep 17 00:00:00 2001 From: John DeRegnaucourt Date: Tue, 19 Mar 2024 12:33:23 -0400 Subject: [PATCH] Added more tests. 631/688 --- .../cedarsoftware/util/convert/Converter.java | 2 +- .../util/convert/MapConversions.java | 7 +- .../util/convert/StringConversions.java | 7 +- .../util/convert/UUIDConversions.java | 11 +++ .../util/convert/ConverterEverythingTest.java | 91 +++++++++++-------- 5 files changed, 79 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/cedarsoftware/util/convert/Converter.java b/src/main/java/com/cedarsoftware/util/convert/Converter.java index d977381c..d3f837e8 100644 --- a/src/main/java/com/cedarsoftware/util/convert/Converter.java +++ b/src/main/java/com/cedarsoftware/util/convert/Converter.java @@ -908,7 +908,7 @@ private static void buildFactoryConversions() { CONVERSION_DB.put(pair(ZoneId.class, Map.class), ZoneIdConversions::toMap); CONVERSION_DB.put(pair(ZoneOffset.class, Map.class), ZoneOffsetConversions::toMap); CONVERSION_DB.put(pair(Class.class, Map.class), MapConversions::initMap); - CONVERSION_DB.put(pair(UUID.class, Map.class), MapConversions::initMap); + CONVERSION_DB.put(pair(UUID.class, Map.class), UUIDConversions::toMap); CONVERSION_DB.put(pair(Calendar.class, Map.class), CalendarConversions::toMap); CONVERSION_DB.put(pair(Number.class, Map.class), MapConversions::initMap); CONVERSION_DB.put(pair(Map.class, Map.class), UNSUPPORTED); diff --git a/src/main/java/com/cedarsoftware/util/convert/MapConversions.java b/src/main/java/com/cedarsoftware/util/convert/MapConversions.java index ba9ed5ad..3eff4c1f 100644 --- a/src/main/java/com/cedarsoftware/util/convert/MapConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/MapConversions.java @@ -81,6 +81,7 @@ final class MapConversions { static final String VARIANT = "variant"; static final String URI_KEY = "URI"; static final String URL_KEY = "URL"; + static final String UUID = "UUID"; private MapConversions() {} @@ -89,13 +90,17 @@ private MapConversions() {} static Object toUUID(Object from, Converter converter) { Map map = (Map) from; + if (map.containsKey(MapConversions.UUID)) { + return converter.convert(map.get(UUID), UUID.class); + } + if (map.containsKey(MOST_SIG_BITS) && map.containsKey(LEAST_SIG_BITS)) { long most = converter.convert(map.get(MOST_SIG_BITS), long.class); long least = converter.convert(map.get(LEAST_SIG_BITS), long.class); return new UUID(most, least); } - return fromMap(from, converter, UUID.class, MOST_SIG_BITS, LEAST_SIG_BITS); + return fromMap(from, converter, UUID.class, UUID, MOST_SIG_BITS, LEAST_SIG_BITS); } static Byte toByte(Object from, Converter converter) { diff --git a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java index 7c4cc0a0..297e326d 100644 --- a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java @@ -274,7 +274,12 @@ static String enumToString(Object from, Converter converter) { } static UUID toUUID(Object from, Converter converter) { - return UUID.fromString(((String) from).trim()); + String s = (String) from; + try { + return UUID.fromString(s); + } catch (Exception e) { + throw new IllegalArgumentException("Unable to convert '" + s + "' to UUID", e); + } } static Duration toDuration(Object from, Converter converter) { diff --git a/src/main/java/com/cedarsoftware/util/convert/UUIDConversions.java b/src/main/java/com/cedarsoftware/util/convert/UUIDConversions.java index f5630a97..c0b355d8 100644 --- a/src/main/java/com/cedarsoftware/util/convert/UUIDConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/UUIDConversions.java @@ -2,6 +2,10 @@ import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Map; +import java.util.UUID; + +import com.cedarsoftware.util.CompactLinkedMap; /** * @author John DeRegnaucourt (jdereg@gmail.com) @@ -33,5 +37,12 @@ static BigInteger toBigInteger(Object from, Converter converter) { String hex = from.toString().replace("-", ""); return new BigInteger(hex, 16); } + + static Map toMap(Object from, Converter converter) { + UUID uuid = (UUID) from; + Map target = new CompactLinkedMap<>(); + target.put(MapConversions.UUID, uuid.toString()); + return target; + } } diff --git a/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java b/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java index 2b3e4d2e..2872e98d 100644 --- a/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java +++ b/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java @@ -188,6 +188,61 @@ public ZoneId getZoneId() { loadTimeZoneTests(); loadUriTests(); loadUrlTests(); + loadUuidTests(); + } + + /** + * UUID + */ + private static void loadUuidTests() { + TEST_DB.put(pair(Void.class, UUID.class), new Object[][]{ + {null, null} + }); + TEST_DB.put(pair(UUID.class, UUID.class), new Object[][]{ + {UUID.fromString("f0000000-0000-0000-0000-000000000001"), UUID.fromString("f0000000-0000-0000-0000-000000000001")}, + }); + TEST_DB.put(pair(Map.class, UUID.class), new Object[][]{ + {mapOf("UUID", "f0000000-0000-0000-0000-000000000001"), UUID.fromString("f0000000-0000-0000-0000-000000000001"), true}, + {mapOf("UUID", "f0000000-0000-0000-0000-00000000000x"), new IllegalArgumentException("Unable to convert 'f0000000-0000-0000-0000-00000000000x' to UUID")}, + }); + TEST_DB.put(pair(String.class, UUID.class), new Object[][]{ + {"f0000000-0000-0000-0000-000000000001", UUID.fromString("f0000000-0000-0000-0000-000000000001"), true}, + {"f0000000-0000-0000-0000-00000000000x", new IllegalArgumentException("Unable to convert 'f0000000-0000-0000-0000-00000000000x' to UUID")}, + {"00000000-0000-0000-0000-000000000000", new UUID(0L, 0L), true}, + {"00000000-0000-0001-0000-000000000001", new UUID(1L, 1L), true}, + {"7fffffff-ffff-ffff-7fff-ffffffffffff", new UUID(Long.MAX_VALUE, Long.MAX_VALUE), true}, + {"80000000-0000-0000-8000-000000000000", new UUID(Long.MIN_VALUE, Long.MIN_VALUE), true}, + }); + TEST_DB.put(pair(BigDecimal.class, UUID.class), new Object[][]{ + {BigDecimal.ZERO, new UUID(0L, 0L), true}, + {new BigDecimal("18446744073709551617"), new UUID(1L, 1L), true}, + {new BigDecimal("170141183460469231722463931679029329919"), new UUID(Long.MAX_VALUE, Long.MAX_VALUE), true}, + {BigDecimal.ZERO, UUID.fromString("00000000-0000-0000-0000-000000000000"), true}, + {BigDecimal.valueOf(1), UUID.fromString("00000000-0000-0000-0000-000000000001"), true}, + {new BigDecimal("18446744073709551617"), UUID.fromString("00000000-0000-0001-0000-000000000001"), true}, + {new BigDecimal("340282366920938463463374607431768211455"), UUID.fromString("ffffffff-ffff-ffff-ffff-ffffffffffff"), true}, + {new BigDecimal("340282366920938463463374607431768211454"), UUID.fromString("ffffffff-ffff-ffff-ffff-fffffffffffe"), true}, + {new BigDecimal("319014718988379809496913694467282698240"), UUID.fromString("f0000000-0000-0000-0000-000000000000"), true}, + {new BigDecimal("319014718988379809496913694467282698241"), UUID.fromString("f0000000-0000-0000-0000-000000000001"), true}, + {new BigDecimal("170141183460469231731687303715884105726"), UUID.fromString("7fffffff-ffff-ffff-ffff-fffffffffffe"), true}, + {new BigDecimal("170141183460469231731687303715884105727"), UUID.fromString("7fffffff-ffff-ffff-ffff-ffffffffffff"), true}, + {new BigDecimal("170141183460469231731687303715884105728"), UUID.fromString("80000000-0000-0000-0000-000000000000"), true}, + }); + TEST_DB.put(pair(BigInteger.class, UUID.class), new Object[][]{ + {BigInteger.ZERO, new UUID(0L, 0L), true}, + {new BigInteger("18446744073709551617"), new UUID(1L, 1L), true}, + {new BigInteger("170141183460469231722463931679029329919"), new UUID(Long.MAX_VALUE, Long.MAX_VALUE), true}, + {BigInteger.ZERO, UUID.fromString("00000000-0000-0000-0000-000000000000"), true}, + {BigInteger.valueOf(1), UUID.fromString("00000000-0000-0000-0000-000000000001"), true}, + {new BigInteger("18446744073709551617"), UUID.fromString("00000000-0000-0001-0000-000000000001"), true}, + {new BigInteger("340282366920938463463374607431768211455"), UUID.fromString("ffffffff-ffff-ffff-ffff-ffffffffffff"), true}, + {new BigInteger("340282366920938463463374607431768211454"), UUID.fromString("ffffffff-ffff-ffff-ffff-fffffffffffe"), true}, + {new BigInteger("319014718988379809496913694467282698240"), UUID.fromString("f0000000-0000-0000-0000-000000000000"), true}, + {new BigInteger("319014718988379809496913694467282698241"), UUID.fromString("f0000000-0000-0000-0000-000000000001"), true}, + {new BigInteger("170141183460469231731687303715884105726"), UUID.fromString("7fffffff-ffff-ffff-ffff-fffffffffffe"), true}, + {new BigInteger("170141183460469231731687303715884105727"), UUID.fromString("7fffffff-ffff-ffff-ffff-ffffffffffff"), true}, + {new BigInteger("170141183460469231731687303715884105728"), UUID.fromString("80000000-0000-0000-0000-000000000000"), true}, + }); } /** @@ -741,12 +796,6 @@ private static void loadStringTests() { {ZonedDateTime.parse("2024-02-14T19:20:00-05:00"), "2024-02-14T19:20:00-05:00"}, {ZonedDateTime.parse("2024-02-14T19:20:00+05:00"), "2024-02-14T19:20:00+05:00"}, }); - TEST_DB.put(pair(UUID.class, String.class), new Object[][]{ - {new UUID(0L, 0L), "00000000-0000-0000-0000-000000000000", true}, - {new UUID(1L, 1L), "00000000-0000-0001-0000-000000000001", true}, - {new UUID(Long.MAX_VALUE, Long.MAX_VALUE), "7fffffff-ffff-ffff-7fff-ffffffffffff", true}, - {new UUID(Long.MIN_VALUE, Long.MIN_VALUE), "80000000-0000-0000-8000-000000000000", true}, - }); TEST_DB.put(pair(Calendar.class, String.class), new Object[][]{ {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); @@ -1913,21 +1962,6 @@ private static void loadBigDecimalTests() { {Instant.parse("1970-01-02T00:00:00Z"), new BigDecimal("86400"), true}, {Instant.parse("1970-01-02T00:00:00.000000001Z"), new BigDecimal("86400.000000001"), true}, }); - TEST_DB.put(pair(UUID.class, BigDecimal.class), new Object[][]{ - {new UUID(0L, 0L), BigDecimal.ZERO, true}, - {new UUID(1L, 1L), new BigDecimal("18446744073709551617"), true}, - {new UUID(Long.MAX_VALUE, Long.MAX_VALUE), new BigDecimal("170141183460469231722463931679029329919"), true}, - {UUID.fromString("00000000-0000-0000-0000-000000000000"), BigDecimal.ZERO, true}, - {UUID.fromString("00000000-0000-0000-0000-000000000001"), BigDecimal.valueOf(1), true}, - {UUID.fromString("00000000-0000-0001-0000-000000000001"), new BigDecimal("18446744073709551617"), true}, - {UUID.fromString("ffffffff-ffff-ffff-ffff-ffffffffffff"), new BigDecimal("340282366920938463463374607431768211455"), true}, - {UUID.fromString("ffffffff-ffff-ffff-ffff-fffffffffffe"), new BigDecimal("340282366920938463463374607431768211454"), true}, - {UUID.fromString("f0000000-0000-0000-0000-000000000000"), new BigDecimal("319014718988379809496913694467282698240"), true}, - {UUID.fromString("f0000000-0000-0000-0000-000000000001"), new BigDecimal("319014718988379809496913694467282698241"), true}, - {UUID.fromString("7fffffff-ffff-ffff-ffff-fffffffffffe"), new BigDecimal("170141183460469231731687303715884105726"), true}, - {UUID.fromString("7fffffff-ffff-ffff-ffff-ffffffffffff"), new BigDecimal("170141183460469231731687303715884105727"), true}, - {UUID.fromString("80000000-0000-0000-0000-000000000000"), new BigDecimal("170141183460469231731687303715884105728"), true}, - }); TEST_DB.put(pair(Map.class, BigDecimal.class), new Object[][]{ {mapOf("_v", "0"), BigDecimal.ZERO}, {mapOf("_v", BigDecimal.valueOf(0)), BigDecimal.ZERO, true}, @@ -2041,21 +2075,6 @@ private static void loadBigIntegerTests() { {zdt("1970-01-01T00:00:00Z").toLocalDateTime(), BigInteger.ZERO, true}, {zdt("1970-01-01T00:00:00.000000001Z").toLocalDateTime(), new BigInteger("1"), true}, }); - TEST_DB.put(pair(UUID.class, BigInteger.class), new Object[][]{ - {new UUID(0L, 0L), BigInteger.ZERO, true}, - {new UUID(1L, 1L), new BigInteger("18446744073709551617"), true}, - {new UUID(Long.MAX_VALUE, Long.MAX_VALUE), new BigInteger("170141183460469231722463931679029329919"), true}, - {UUID.fromString("00000000-0000-0000-0000-000000000000"), BigInteger.ZERO, true}, - {UUID.fromString("00000000-0000-0000-0000-000000000001"), BigInteger.valueOf(1), true}, - {UUID.fromString("00000000-0000-0001-0000-000000000001"), new BigInteger("18446744073709551617"), true}, - {UUID.fromString("ffffffff-ffff-ffff-ffff-ffffffffffff"), new BigInteger("340282366920938463463374607431768211455"), true}, - {UUID.fromString("ffffffff-ffff-ffff-ffff-fffffffffffe"), new BigInteger("340282366920938463463374607431768211454"), true}, - {UUID.fromString("f0000000-0000-0000-0000-000000000000"), new BigInteger("319014718988379809496913694467282698240"), true}, - {UUID.fromString("f0000000-0000-0000-0000-000000000001"), new BigInteger("319014718988379809496913694467282698241"), true}, - {UUID.fromString("7fffffff-ffff-ffff-ffff-fffffffffffe"), new BigInteger("170141183460469231731687303715884105726"), true}, - {UUID.fromString("7fffffff-ffff-ffff-ffff-ffffffffffff"), new BigInteger("170141183460469231731687303715884105727"), true}, - {UUID.fromString("80000000-0000-0000-0000-000000000000"), new BigInteger("170141183460469231731687303715884105728"), true}, - }); TEST_DB.put(pair(Calendar.class, BigInteger.class), new Object[][]{ {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ);