Search in sources :

Example 81 with AbstractModule

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

the class FactoryProviderTest method testDuplicateAssistedFactoryBinding.

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

        @Override
        protected void configure() {
            bind(Double.class).toInstance(5.0d);
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
        }
    });
    ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
    Mustang blueMustang = (Mustang) carFactory.create(Color.BLUE);
    assertEquals(Color.BLUE, blueMustang.color);
    assertEquals(5.0d, blueMustang.engineSize, 0.0);
    Mustang redMustang = (Mustang) carFactory.create(Color.RED);
    assertEquals(Color.RED, redMustang.color);
    assertEquals(5.0d, redMustang.engineSize, 0.0);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 82 with AbstractModule

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

the class ManyConstructorsTest method testDifferentOrderParameters.

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

        @Override
        protected void configure() {
            install(new FactoryModuleBuilder().build(OtherFactory.class));
        }
    });
    OtherFactory factory = injector.getInstance(OtherFactory.class);
    Foo noIndex = factory.create("no index");
    assertEquals("no index", noIndex.name);
    assertNull(noIndex.index);
    Foo index = factory.create(1, "index");
    assertEquals("index", index.name);
    assertEquals(1, index.index.intValue());
    Foo index2 = factory.create("index", 2);
    assertEquals("index", index2.name);
    assertEquals(2, index2.index.intValue());
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 83 with AbstractModule

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

the class ManyConstructorsTest method testInterfaceToImpl.

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

        @Override
        protected void configure() {
            install(new FactoryModuleBuilder().implement(Bar.class, Foo.class).build(BarFactory.class));
        }
    });
    BarFactory factory = injector.getInstance(BarFactory.class);
    Bar noIndex = factory.create("no index");
    assertEquals("no index", noIndex.getName());
    assertNull(noIndex.getIndex());
    Bar index = factory.create("index", 1);
    assertEquals("index", index.getName());
    assertEquals(1, index.getIndex().intValue());
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 84 with AbstractModule

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

the class ManyConstructorsTest method testDependenciesAndOtherAnnotations.

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

        @Override
        protected void configure() {
            install(new FactoryModuleBuilder().build(FamilyFarmFactory.class));
        }
    });
    FamilyFarmFactory factory = injector.getInstance(FamilyFarmFactory.class);
    Farm pops = factory.popsFarm("Pop");
    assertEquals("Pop", pops.pop);
    assertEquals(null, pops.mom);
    Farm moms = factory.momsFarm("Mom");
    assertEquals(null, moms.pop);
    assertEquals("Mom", moms.mom);
    Farm momAndPop = factory.momAndPopsFarm("Mom", "Pop");
    assertEquals("Pop", momAndPop.pop);
    assertEquals("Mom", momAndPop.mom);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 85 with AbstractModule

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

the class ManyConstructorsTest method testUsingOneConstructor.

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

        @Override
        protected void configure() {
            install(new FactoryModuleBuilder().build(SimpleFactory.class));
        }
    });
    SimpleFactory factory = injector.getInstance(SimpleFactory.class);
    Foo noIndex = factory.create("no index");
    assertEquals("no index", noIndex.name);
    assertNull(noIndex.index);
    injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            install(new FactoryModuleBuilder().build(SimpleFactory2.class));
        }
    });
    SimpleFactory2 factory2 = injector.getInstance(SimpleFactory2.class);
    Foo index = factory2.create("index", 1);
    assertEquals("index", index.name);
    assertEquals(1, index.index.intValue());
}
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