Search in sources :

Example 91 with AbstractModule

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

the class FactoryProvider2Test method testDuplicateKeys.

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

            @Override
            protected void configure() {
                bind(DoubleToneCarFactory.class).toProvider(FactoryProvider.newFactory(DoubleToneCarFactory.class, Maxima.class));
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "A binding to " + Color.class.getName() + " annotated with @" + Assisted.class.getName() + "(value=paint) was already configured at");
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 92 with AbstractModule

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

the class FactoryProvider2Test method testFactoryUsesInjectedConstructor.

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

        @Override
        protected void configure() {
            bind(float.class).toInstance(140f);
            bind(SummerCarFactory.class).toProvider(FactoryProvider.newFactory(SummerCarFactory.class, Corvette.class));
        }
    });
    SummerCarFactory carFactory = injector.getInstance(SummerCarFactory.class);
    Corvette redCorvette = (Corvette) carFactory.create(Color.RED, false);
    assertEquals(Color.RED, redCorvette.color);
    assertEquals(140f, redCorvette.maxMph, 0.0f);
    assertFalse(redCorvette.isConvertable);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 93 with AbstractModule

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

the class FactoryProvider2Test method testAssistedFactoryForParameterizedType.

public void testAssistedFactoryForParameterizedType() {
    final TypeLiteral<InsuranceFactory<Mustang>> mustangInsuranceFactoryType = new TypeLiteral<InsuranceFactory<Mustang>>() {
    };
    final TypeLiteral<InsuranceFactory<Camaro>> camaroInsuranceFactoryType = new TypeLiteral<InsuranceFactory<Camaro>>() {
    };
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Double.class).annotatedWith(Names.named("lowLimit")).toInstance(50000.0d);
            bind(Double.class).annotatedWith(Names.named("highLimit")).toInstance(100000.0d);
            bind(mustangInsuranceFactoryType).toProvider(FactoryProvider.newFactory(mustangInsuranceFactoryType, TypeLiteral.get(MustangInsurance.class)));
            bind(camaroInsuranceFactoryType).toProvider(FactoryProvider.newFactory(camaroInsuranceFactoryType, TypeLiteral.get(CamaroInsurance.class)));
        }
    });
    InsuranceFactory<Mustang> mustangInsuranceFactory = injector.getInstance(Key.get(mustangInsuranceFactoryType));
    InsuranceFactory<Camaro> camaroInsuranceFactory = injector.getInstance(Key.get(camaroInsuranceFactoryType));
    Mustang mustang = new Mustang(5000d, Color.BLACK);
    MustangInsurance mustangPolicy = (MustangInsurance) mustangInsuranceFactory.create(mustang, 800.0d);
    assertEquals(800.0d, mustangPolicy.premium, 0.0);
    assertEquals(50000.0d, mustangPolicy.limit, 0.0);
    Camaro camaro = new Camaro(3000, 1967, Color.BLUE);
    CamaroInsurance camaroPolicy = (CamaroInsurance) camaroInsuranceFactory.create(camaro, 800.0d);
    assertEquals(800.0d, camaroPolicy.premium, 0.0);
    assertEquals(100000.0d, camaroPolicy.limit, 0.0);
}
Also used : AbstractModule(com.google.inject.AbstractModule) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector)

Example 94 with AbstractModule

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

the class ServletScopesTest method testIsRequestScopedNegative.

public void testIsRequestScopedNegative() {
    final Key<String> a = Key.get(String.class, named("A"));
    final Key<String> b = Key.get(String.class, named("B"));
    final Key<String> c = Key.get(String.class, named("C"));
    final Key<String> d = Key.get(String.class, named("D"));
    final Key<String> e = Key.get(String.class, named("E"));
    final Key<String> f = Key.get(String.class, named("F"));
    final Key<String> g = Key.get(String.class, named("G"));
    final Key<String> h = Key.get(String.class, named("H"));
    final Key<String> i = Key.get(String.class, named("I"));
    final Key<String> j = Key.get(String.class, named("J"));
    Module requestScopedBindings = new AbstractModule() {

        @Override
        protected void configure() {
            bind(a).to(b);
            bind(b).to(c);
            bind(c).toProvider(Providers.of("c")).in(Scopes.NO_SCOPE);
            bind(d).toInstance("d");
            bind(e).toProvider(Providers.of("e")).asEagerSingleton();
            bind(f).toProvider(Providers.of("f")).in(Scopes.SINGLETON);
            bind(g).toProvider(Providers.of("g")).in(Singleton.class);
            bind(h).toProvider(Providers.of("h")).in(CustomScoped.class);
            bindScope(CustomScoped.class, Scopes.NO_SCOPE);
            install(new PrivateModule() {

                @Override
                protected void configure() {
                    bind(i).toProvider(Providers.of("i")).in(CustomScoped.class);
                    expose(i);
                }
            });
        }

        @Provides
        @Named("J")
        @CustomScoped
        String provideJ() {
            return "j";
        }
    };
    // we know the module contains only bindings
    @SuppressWarnings("unchecked") List<Element> moduleBindings = Elements.getElements(requestScopedBindings);
    ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
    assertFalse(ServletScopes.isRequestScoped(map.get(a)));
    assertFalse(ServletScopes.isRequestScoped(map.get(b)));
    assertFalse(ServletScopes.isRequestScoped(map.get(c)));
    assertFalse(ServletScopes.isRequestScoped(map.get(d)));
    assertFalse(ServletScopes.isRequestScoped(map.get(e)));
    assertFalse(ServletScopes.isRequestScoped(map.get(f)));
    assertFalse(ServletScopes.isRequestScoped(map.get(g)));
    assertFalse(ServletScopes.isRequestScoped(map.get(h)));
    assertFalse(ServletScopes.isRequestScoped(map.get(i)));
    assertFalse(ServletScopes.isRequestScoped(map.get(j)));
    Injector injector = Guice.createInjector(requestScopedBindings);
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(a)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(b)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(c)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(d)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(e)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(f)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(g)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(h)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(i)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(j)));
}
Also used : Binding(com.google.inject.Binding) Element(com.google.inject.spi.Element) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) PrivateModule(com.google.inject.PrivateModule) Key(com.google.inject.Key)

Example 95 with AbstractModule

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

the class EdslTest method testExplicitBindingsWorksWithGuiceServlet.

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

        @Override
        protected void configure() {
            binder().requireExplicitBindings();
        }
    }, new ServletModule() {

        @Override
        protected void configureServlets() {
            bind(DummyServlet.class).in(Singleton.class);
            serve("/*").with(DummyServlet.class);
        }
    });
    assertNotNull(injector.getInstance(DummyServlet.class));
}
Also used : Injector(com.google.inject.Injector) Singleton(com.google.inject.Singleton) 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