CoerceUtil { public static <T> T coerce(Object value, Class<T> cls) { initializeCurrentClassLoaderIfNecessary(); if (value == null || cls == null || cls.isInstance(value)) { return (T) value; } try { return (T) ConvertUtils.convert(value, cls); } catch (ConversionException | InvalidAttributeException | IllegalArgumentException e) { throw new InvalidValueException(value, e.getMessage()); } }  static T coerce(Object value, Class<T> cls); static void register(Class<T> targetType, Serde<S, T> serde); static Serde<S, T> lookup(Class<T> targetType); static Map<Class<?>, Serde<?, ?>> getSerdes();  }
@Test public void testNoConversions() throws Exception { assertEquals(1, CoerceUtil.coerce(1, (Class<Object>) null), "coerce returns value if target class null"); assertEquals(null, CoerceUtil.coerce(null, Object.class), "coerce returns value if value is null"); assertEquals(1, (Object) CoerceUtil.coerce(1, int.class), "coerce returns value if value is assignable to target"); }
@Test public void testToEnumConversion() throws Exception { assertEquals(Seasons.SPRING, CoerceUtil.coerce(1, Seasons.class), "ToEnumConverter is called when target class is Enum"); }
@Test public void testToUUIDConversion() throws Exception { String uuidString = "11111111-2222-3333-4444-555555555555"; assertEquals( UUID.fromString(uuidString), CoerceUtil.coerce(uuidString, UUID.class), "ToUUIDConverter is called when target class is UUID"); }
@Test public void testBasicConversion() throws Exception { assertEquals("1", CoerceUtil.coerce(1, String.class), "coerce converts int to String"); assertEquals(Long.valueOf("1"), CoerceUtil.coerce("1", Long.class), "coerce converts String to Long"); assertEquals(1, (Object) CoerceUtil.coerce(1.0, int.class), "coerce converts float to int"); }
@Test public void testError() throws Exception { assertThrows(InvalidValueException.class, () -> CoerceUtil.coerce('A', Seasons.class)); }
@Test public void testMapConversion() { Map<String, Object> testMap = new LinkedHashMap<>(); testMap.put("field1", 1); testMap.put("field2", 2); TestClass testClass = new TestClass(1, 2); assertEquals(testClass, CoerceUtil.coerce(testMap, TestClass.class), "FromMapConverter is called when source class is a Map"); }
@Test public void testMapError() throws Exception { Map<String, Object> testMap = new LinkedHashMap<>(); testMap.put("foo", "bar"); testMap.put("baz", "qaz"); assertThrows(InvalidValueException.class, () -> CoerceUtil.coerce(testMap, TestClass.class)); }
@Test public void testConversionFailure() throws Exception { assertThrows(InvalidValueException.class, () -> CoerceUtil.coerce("a", Long.class)); }
@Test public void testNullConversion() throws Exception { assertNull(CoerceUtil.coerce(null, String.class)); }
@Test public void testStringToDate() throws Exception { Date date = CoerceUtil.coerce("1", Date.class); assertEquals(new Date(1), date); java.sql.Date date1 = CoerceUtil.coerce("1", java.sql.Date.class); assertEquals(new java.sql.Date(1), date1); Timestamp timestamp = CoerceUtil.coerce("1", Timestamp.class); assertEquals(new Timestamp(1), timestamp); Time time = CoerceUtil.coerce("1", Time.class); assertEquals(new Time(1), time); }
@Test public void testLongToDate() { Date dateLong = CoerceUtil.coerce(0L, Date.class); assertEquals(new Date(0), dateLong); java.sql.Date date1Long = CoerceUtil.coerce(0L, java.sql.Date.class); assertEquals(new java.sql.Date(0), date1Long); Timestamp timestampLong = CoerceUtil.coerce(0L, Timestamp.class); assertEquals(new Timestamp(0), timestampLong); Time timeLong = CoerceUtil.coerce(0L, Time.class); assertEquals(new Time(0), timeLong); }
@Test public void testIntToDate() throws Exception { Date date = CoerceUtil.coerce(0, Date.class); assertEquals(new Date(0), date); java.sql.Date date1 = CoerceUtil.coerce(0, java.sql.Date.class); assertEquals(new java.sql.Date(0), date1); Timestamp timestamp = CoerceUtil.coerce(0, Timestamp.class); assertEquals(new Timestamp(0), timestamp); Time time = CoerceUtil.coerce(0, Time.class); assertEquals(new Time(0), time); }
@Test public void testMultipleClassLoaders() { UUID uuid = UUID.randomUUID(); String uuidString = uuid.toString(); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); UUID coercedFromPrimaryClassLoader = CoerceUtil.coerce(uuidString, UUID.class); assertEquals(uuid, coercedFromPrimaryClassLoader); try { Thread.currentThread().setContextClassLoader(new SecondaryClassLoader()); UUID coercedFromSecondaryClassLoader = CoerceUtil.coerce(uuidString, UUID.class); assertEquals(uuid, coercedFromSecondaryClassLoader); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } }