Search in sources :

Example 71 with AbstractModule

use of com.google.inject.AbstractModule in project guice by google.

the class FactoryModuleBuilderTest method testConfigureAnnotatedReturnValue.

public void testConfigureAnnotatedReturnValue() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            install(new FactoryModuleBuilder().implement(Car.class, Names.named("german"), Beetle.class).implement(Car.class, Names.named("american"), Mustang.class).build(AnnotatedVersatileCarFactory.class));
        }
    });
    AnnotatedVersatileCarFactory factory = injector.getInstance(AnnotatedVersatileCarFactory.class);
    assertTrue(factory.getGermanCar(Color.GRAY) instanceof Beetle);
    assertTrue(factory.getAmericanCar(Color.BLACK) instanceof Mustang);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 72 with AbstractModule

use of com.google.inject.AbstractModule in project guice by google.

the class FactoryModuleBuilderTest method testExplicitForwardingAssistedBindingFailsWithInterface.

public void testExplicitForwardingAssistedBindingFailsWithInterface() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(Volkswagen.class).to(Golf.class);
                install(new FactoryModuleBuilder().implement(Car.class, Volkswagen.class).build(ColoredCarFactory.class));
            }
        });
        fail();
    } catch (CreationException ce) {
        assertContains(ce.getMessage(), "1) " + Volkswagen.class.getName() + " is an interface, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + Volkswagen.class.getName(), "while locating " + Car.class.getName(), "at " + ColoredCarFactory.class.getName() + ".create(");
        assertEquals(1, ce.getErrorMessages().size());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 73 with AbstractModule

use of com.google.inject.AbstractModule in project guice by google.

the class FactoryModuleBuilderTest method testAnnotatedAndParentBoundReturnValue.

public void testAnnotatedAndParentBoundReturnValue() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Car.class).to(Golf.class);
            bind(Integer.class).toInstance(911);
            bind(Double.class).toInstance(5.0d);
            install(new FactoryModuleBuilder().implement(Car.class, Names.named("german"), Beetle.class).implement(Car.class, Names.named("american"), Mustang.class).build(AnnotatedVersatileCarFactory.class));
        }
    });
    AnnotatedVersatileCarFactory factory = injector.getInstance(AnnotatedVersatileCarFactory.class);
    assertTrue(factory.getGermanCar(Color.BLACK) instanceof Beetle);
    assertTrue(injector.getInstance(Car.class) instanceof Golf);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 74 with AbstractModule

use of com.google.inject.AbstractModule in project guice by google.

the class FactoryModuleBuilderTest method testParentBoundReturnValue.

public void testParentBoundReturnValue() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Car.class).to(Golf.class);
            bind(Double.class).toInstance(5.0d);
            install(new FactoryModuleBuilder().implement(Car.class, Mustang.class).build(ColoredCarFactory.class));
        }
    });
    ColoredCarFactory factory = injector.getInstance(ColoredCarFactory.class);
    assertTrue(factory.create(Color.RED) instanceof Mustang);
    assertTrue(injector.getInstance(Car.class) instanceof Golf);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 75 with AbstractModule

use of com.google.inject.AbstractModule in project guice by google.

the class FactoryModuleBuilderTest method testNoBindingAssistedInject.

public void testNoBindingAssistedInject() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            install(new FactoryModuleBuilder().build(MustangFactory.class));
        }
    });
    MustangFactory factory = injector.getInstance(MustangFactory.class);
    Mustang mustang = factory.create(Color.BLUE);
    assertEquals(Color.BLUE, mustang.color);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Aggregations

AbstractModule (com.google.inject.AbstractModule)845 Injector (com.google.inject.Injector)525 Module (com.google.inject.Module)232 CreationException (com.google.inject.CreationException)162 Provider (com.google.inject.Provider)64 Key (com.google.inject.Key)55 ConfigModule (co.cask.cdap.common.guice.ConfigModule)54 AuthorizationEnforcementModule (co.cask.cdap.security.authorization.AuthorizationEnforcementModule)49 DataSetsModules (co.cask.cdap.data.runtime.DataSetsModules)46 PrivateModule (com.google.inject.PrivateModule)46 AuthenticationContextModules (co.cask.cdap.security.auth.context.AuthenticationContextModules)45 AuthorizationTestModule (co.cask.cdap.security.authorization.AuthorizationTestModule)43 TypeLiteral (com.google.inject.TypeLiteral)43 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)42 BeforeClass (org.junit.BeforeClass)38 Map (java.util.Map)37 CConfiguration (co.cask.cdap.common.conf.CConfiguration)36 UnsupportedUGIProvider (co.cask.cdap.security.impersonation.UnsupportedUGIProvider)36 NonCustomLocationUnitTestModule (co.cask.cdap.common.guice.NonCustomLocationUnitTestModule)35 DefaultOwnerAdmin (co.cask.cdap.security.impersonation.DefaultOwnerAdmin)35