Search in sources :

Example 1 with ConvertedConstantBinding

use of com.google.inject.spi.ConvertedConstantBinding in project guice by google.

the class TypeConversionTest method testCustomTypeConversion.

public void testCustomTypeConversion() throws CreationException {
    final Date result = new Date();
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            convertToTypes(Matchers.only(TypeLiteral.get(Date.class)), mockTypeConverter(result));
            bindConstant().annotatedWith(NumericValue.class).to("Today");
            bind(DateHolder.class);
        }
    });
    assertSame(result, injector.getInstance(DateHolder.class).date);
    Binding<Date> binding = injector.getBinding(Key.get(Date.class, NumericValue.class));
    assertTrue(binding instanceof ConvertedConstantBinding<?>);
    TypeConverterBinding converterBinding = ((ConvertedConstantBinding<?>) binding).getTypeConverterBinding();
    assertEquals("CustomConverter", converterBinding.getTypeConverter().toString());
    assertTrue(injector.getTypeConverterBindings().contains(converterBinding));
}
Also used : ConvertedConstantBinding(com.google.inject.spi.ConvertedConstantBinding) TypeConverterBinding(com.google.inject.spi.TypeConverterBinding) Date(java.util.Date)

Example 2 with ConvertedConstantBinding

use of com.google.inject.spi.ConvertedConstantBinding in project roboguice by roboguice.

the class TypeConversionTest method testCustomTypeConversion.

public void testCustomTypeConversion() throws CreationException {
    final Date result = new Date();
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            convertToTypes(Matchers.only(TypeLiteral.get(Date.class)), mockTypeConverter(result));
            bindConstant().annotatedWith(NumericValue.class).to("Today");
            bind(DateHolder.class);
        }
    });
    assertSame(result, injector.getInstance(DateHolder.class).date);
    Binding<Date> binding = injector.getBinding(Key.get(Date.class, NumericValue.class));
    assertTrue(binding instanceof ConvertedConstantBinding<?>);
    TypeConverterBinding converterBinding = ((ConvertedConstantBinding<?>) binding).getTypeConverterBinding();
    assertEquals("CustomConverter", converterBinding.getTypeConverter().toString());
    assertTrue(injector.getTypeConverterBindings().contains(converterBinding));
}
Also used : ConvertedConstantBinding(com.google.inject.spi.ConvertedConstantBinding) TypeConverterBinding(com.google.inject.spi.TypeConverterBinding) Date(java.util.Date)

Aggregations

ConvertedConstantBinding (com.google.inject.spi.ConvertedConstantBinding)2 TypeConverterBinding (com.google.inject.spi.TypeConverterBinding)2 Date (java.util.Date)2