DefaultConverters implements Converters { public <T> Converter<T> to(Class<T> clazz) { if (!existsFor(clazz)) { throw new VRaptorException("Unable to find converter for " + clazz.getName()); } return (Converter<T>) container.instanceFor(findConverterType(clazz)); } DefaultConverters(Container container); void register(Class<? extends Converter<?>> converterClass); Converter<T> to(Class<T> clazz); boolean existsFor(Class<?> type); boolean existsTwoWayFor(Class<?> type); TwoWayConverter<?> twoWayConverterFor(Class<?> type);  }
@Test public void shouldRegisterConvertersForAllDefaultTypes() { final HashMap<Class, Class<? extends Converter>> EXPECTED_CONVERTERS = new HashMap<Class, Class<? extends Converter>>() { { put(int.class, PrimitiveIntConverter.class); put(long.class, PrimitiveLongConverter.class); put(short.class, PrimitiveShortConverter.class); put(byte.class, PrimitiveByteConverter.class); put(double.class, PrimitiveDoubleConverter.class); put(float.class, PrimitiveFloatConverter.class); put(boolean.class, PrimitiveBooleanConverter.class); put(Integer.class, IntegerConverter.class); put(Long.class, LongConverter.class); put(Short.class, ShortConverter.class); put(Byte.class, ByteConverter.class); put(Double.class, DoubleConverter.class); put(Float.class, FloatConverter.class); put(Boolean.class, BooleanConverter.class); put(Calendar.class, LocaleBasedCalendarConverter.class); put(Date.class, LocaleBasedDateConverter.class); put(Enum.class, EnumConverter.class); } private static final long serialVersionUID = 8559316558416038474L; }; for (Class<? extends Converter> converterType : EXPECTED_CONVERTERS.values()) { Converter<?> expected = mock(converterType); when((Converter)container.instanceFor(converterType)).thenReturn(expected); } for (Entry<Class, Class<? extends Converter>> entry : EXPECTED_CONVERTERS.entrySet()) { Class<?> typeFor = entry.getKey(); Class<? extends Converter> converterType = entry.getValue(); Converter<?> converter = converters.to(typeFor); assertThat(converter, is(instanceOf(converterType))); } }
@Test(expected = VRaptorException.class) public void complainsIfNoConverterFound() { converters.to(DefaultConvertersTest.class); }