Skip to content

Commit

Permalink
Added more tests. 631/688
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Mar 19, 2024
1 parent cd8933e commit ab7f962
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/cedarsoftware/util/convert/UUIDConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([email protected])
Expand Down Expand Up @@ -33,5 +37,12 @@ static BigInteger toBigInteger(Object from, Converter converter) {
String hex = from.toString().replace("-", "");
return new BigInteger(hex, 16);
}

static Map<String, Object> toMap(Object from, Converter converter) {
UUID uuid = (UUID) from;
Map<String, Object> target = new CompactLinkedMap<>();
target.put(MapConversions.UUID, uuid.toString());
return target;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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},
});
}

/**
Expand Down Expand Up @@ -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>) () -> {
Calendar cal = Calendar.getInstance(TOKYO_TZ);
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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>) () -> {
Calendar cal = Calendar.getInstance(TOKYO_TZ);
Expand Down

0 comments on commit ab7f962

Please sign in to comment.