TypeDecoder { public static ValueHolder convert(Type<?> type, Object obj) { try { return type.convert(obj); } catch (RuntimeException exception) { throw new TypeConversionException(exception, obj, type); } } private  TypeDecoder(); static ValueHolder decode(Type<?> type, String text); static ValueHolder convert(Type<?> type, Object obj); static ValueHolder[] convert(Type<?> type, Object[] array); static ValueHolder[] convert(Type<?> type, Collection<?> coll);  }
@Test public void testConvert() { Object[][] testCases = { {new Long(17), 17L}, {new Integer(50), 50L}, {null, null}, {"", null}, {" ", null}, {"123", 123L} }; for (Object[] testCase : testCases) { Object obj = testCase[0]; Long expectedValue = (Long) testCase[1]; ValueHolder holder = TypeDecoder.convert(type, obj); assertEquals(expectedValue, holder.getValue()); } }
@Test public void testConvert__illegalArgument() { Object[] illegals = {"1.1", "9aa", 0.1d, 0.1f, BigDecimal.ZERO}; for (Object obj : illegals) { try { TypeDecoder.convert(type, obj); fail(); } catch (TypeConversionException e) { } } }
@Test public void testConvert__array() { Object[][] arrays = { {1L, 2L, 3L}, {} }; Object[][] expectations = { new IntegerHolder[]{new IntegerHolder(1L), new IntegerHolder(2L), new IntegerHolder(3L)}, new IntegerHolder[]{} }; for (int i = 0; i < arrays.length; i++) { Object[] array = arrays[i]; IntegerHolder[] expected = (IntegerHolder[]) expectations[i]; ValueHolder[] result = TypeDecoder.convert(type, array); checkArrays(expected, result); } }
@Test public void testConvert__collection() { Object[] arrays = { Arrays.asList(1L, 2L, 3L), new ArrayList<Long>() }; Object[][] expectations = { new IntegerHolder[]{new IntegerHolder(1L), new IntegerHolder(2L), new IntegerHolder(3L)}, new IntegerHolder[]{} }; for (int i = 0; i < arrays.length; i++) { Collection<?> coll = (Collection<?>) arrays[i]; IntegerHolder[] expected = (IntegerHolder[]) expectations[i]; ValueHolder[] result = TypeDecoder.convert(type, coll); checkArrays(expected, result); } }