### Question:
Variant { public static <S, D> D convert(S val, Class<D> destType) { Variant v = new Variant(val); return v.asType(destType); } Variant(); <T> Variant(T src);  Variant(Cell src);  Variant(Cell src, Locale locale); void clear(); boolean isEmpty(); @SuppressWarnings("squid:S3776") final void setValue(T val); Long toLong(); Double toDouble(); @Override String toString(); Date toDate(); Boolean toBoolean(); boolean isStringTruthy(String s); @SuppressWarnings("squid:S3776") T asType(Class<T> type); static D convert(S val, Class<D> destType);  }### Answer:
@Test public void numericConversion() { assert (Variant.convert("12345", Integer.class) instanceof Integer); assertEquals(12345L, (long) Variant.convert("12345", Integer.class)); assert (Variant.convert("12345", Short.class) instanceof Short); assertEquals(12345L, (long) Variant.convert("12345", Short.class)); assert (Variant.convert("12345.123", Long.class) instanceof Long); assertEquals(12345L, (long) Variant.convert("12345", Long.class)); assert (Variant.convert("12345.123", Double.class) instanceof Double); assertEquals(12345.123, Variant.convert("12345.123", Double.class), 0.0000001); assert (Variant.convert("12345.123", Float.class) instanceof Float); assertEquals(12345.123, Variant.convert("12345.123", Float.class), 0.001); assert (Variant.convert(1, Boolean.class)); assertFalse(Variant.convert("0", Boolean.class)); assertEquals(1L, (long) Variant.convert(true, Long.class)); }
@Test public void booleanConversion() { assert (Variant.convert("1", Boolean.class)); assertFalse(Variant.convert("0", Boolean.class)); assert (Variant.convert(1.1234, Boolean.class)); assertFalse(Variant.convert(0.0, Boolean.class)); assert (Variant.convert("true", Boolean.class)); assertFalse(Variant.convert("FaLsE", Boolean.class)); assert (Variant.convert("tRuE", Boolean.class)); assert (Variant.convert("y", Boolean.class)); assertFalse(Variant.convert("n", Boolean.class)); }
@Test public void calendarConversion() { long now = System.currentTimeMillis(); Date nowDate = new Date(now); Instant nowInstant = nowDate.toInstant(); assertEquals(nowDate, Variant.convert(now, Date.class)); assertEquals(nowInstant, Variant.convert(now, Instant.class)); assertEquals(nowInstant, Variant.convert(nowDate, Instant.class)); String nowStringShort = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.LONG, Locale.getDefault()).format(nowDate); String nowStringLong = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.LONG, SimpleDateFormat.LONG, Locale.getDefault()).format(nowDate); assertNotNull(Variant.convert(nowStringLong, Date.class).getTime()); assertNotNull(Variant.convert(nowStringShort, Date.class).getTime()); assertNotNull(Variant.convert("12:00 AM", Date.class).getTime()); }
@Test public void databaseDateFormatConversion() { Calendar cal = Variant.convert("2016-02-12T14:47:41.922-05:00", Calendar.class); assertNotNull(cal); assertEquals(2016L, cal.get(Calendar.YEAR)); }
@Test public void unsupportedTypes() { assertNull(Variant.convert("Known type", Exception.class)); assertNull(Variant.convert("Known type", VariantTest.class)); }