IntegerType implements Type<IntegerHolder> { @Override public IntegerHolder convert(Object obj) { if (obj instanceof Long || obj instanceof Integer || obj instanceof Short || obj instanceof Byte) { Number n = (Number) obj; return new IntegerHolder(n.longValue()); } if (obj == null) { return new IntegerHolder(null); } if (obj instanceof String) { return decode((String) obj); } throw new IllegalArgumentException("conversion not supported for: " + obj.getClass()); }  @Override String encode(IntegerHolder value); @Override IntegerHolder decode(String text); @Override IntegerHolder convert(Object obj); @Override IntegerHolder[] newArray(int size); static final String TYPE_NAME; }
@Test public void testConvert() { Object[][] testCases = { {new Long(17), 17L}, {new Integer(50), 50L}, {(byte) 100, 100L}, {(short) 2000, 2000L}, {null, null}, {"", null}, {" ", null}, {"123", 123L}, {String.valueOf(Long.MIN_VALUE), Long.MIN_VALUE}, {String.valueOf(Long.MAX_VALUE), Long.MAX_VALUE} }; for (Object[] testCase : testCases) { Object obj = testCase[0]; Long expectedValue = (Long) testCase[1]; IntegerHolder holder = type.convert(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 { type.convert(obj); fail(); } catch (RuntimeException e) { assertTrue(e instanceof IllegalArgumentException || e instanceof NumberFormatException); } } }