### Question:
NumberConverter implements ConditionalConverter<Object, Number> { public Number convert(MappingContext<Object, Number> context) { Object source = context.getSource(); if (source == null) return null; Class<?> destinationType = Primitives.wrapperFor(context.getDestinationType()); if (source instanceof Number) return numberFor((Number) source, destinationType); if (source instanceof Boolean) return numberFor(((Boolean) source).booleanValue() ? 1 : 0, destinationType); if (source instanceof Date && Long.class.equals(destinationType)) return Long.valueOf(((Date) source).getTime()); if (source instanceof Calendar && Long.class.equals(destinationType)) return Long.valueOf(((Calendar) source).getTime().getTime()); if (source instanceof XMLGregorianCalendar && Long.class.equals(destinationType)) return ((XMLGregorianCalendar) source).toGregorianCalendar().getTimeInMillis(); return numberFor(source.toString(), destinationType); }  Number convert(MappingContext<Object, Number> context); MatchResult match(Class<?> sourceType, Class<?> destinationType);  }### Answer:
@Test(expectedExceptions = MappingException.class, dataProvider = "numbersProvider") public void testStringToNumber(Number number) { Object[][] types = provideTypes(); for (int i = 0; i < types.length; i++) { Number result = (Number) convert(number.toString(), (Class<?>) types[i][0]); assertEquals(result.longValue(), number.longValue()); } for (int i = 0; i < types.length; i++) convert("12x", (Class<?>) types[i][0]); }
@Test(expectedExceptions = MappingException.class, dataProvider = "numbersProvider") public void shouldFailOnInvalidDestinationType(Number number) { convert(number, Object.class); }
@Test public void shouldConvertDateToLong() { Date dateValue = new Date(); assertEquals(new Long(dateValue.getTime()), convert(dateValue, Long.class)); }
@Test public void shouldConvertXmlGregorianCalendarToLong() throws DatatypeConfigurationException { XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar()); assertEquals(xmlGregorianCalendar.toGregorianCalendar().getTimeInMillis(), convert(xmlGregorianCalendar, Long.class)); }
@Test(expectedExceptions = MappingException.class) public void shouldThrowOnMapCalendarToInteger() { convert(Calendar.getInstance(), Integer.class); }
@Test(expectedExceptions = MappingException.class) public void shouldThrowOnMapDateToInteger() { convert(new Date(), Integer.class); }
@Test(expectedExceptions = MappingException.class) public void shouldThrowOnNotANumber() { convert("XXXX", Integer.class); }
@Test(dataProvider = "typesProvider") public void testBooleanToNumber(Class<?> type) { assertEquals(0, ((Number) convert(Boolean.FALSE, type)).intValue()); assertEquals(1, ((Number) convert(Boolean.TRUE, type)).intValue()); }
@Test(dataProvider = "typesProvider") public void testConvertNumber(Class<?> type) { Object[] number = { new Byte((byte) 7), new Short((short) 8), new Integer(9), new Long(10), new Float(11.1), new Double(12.2), new BigDecimal("17.2"), new BigInteger("33") }; for (int i = 0; i < number.length; i++) { Object val = convert(number[i], type); assertNotNull(val); assertTrue(type.isInstance(val)); } }