Search in sources :

Example 11 with Guice.createInjector

use of com.google.inject.Guice.createInjector in project roboguice by roboguice.

the class OverrideModuleTest method testOverrideIsLazy.

public void testOverrideIsLazy() {
    final AtomicReference<String> value = new AtomicReference<String>("A");
    Module overridden = Modules.override(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).annotatedWith(named("original")).toInstance(value.get());
        }
    }).with(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).annotatedWith(named("override")).toInstance(value.get());
        }
    });
    // the value.get() call should be deferred until Guice.createInjector
    value.set("B");
    Injector injector = Guice.createInjector(overridden);
    assertEquals("B", injector.getInstance(Key.get(String.class, named("original"))));
    assertEquals("B", injector.getInstance(Key.get(String.class, named("override"))));
}
Also used : Guice.createInjector(com.google.inject.Guice.createInjector) Injector(com.google.inject.Injector) AtomicReference(java.util.concurrent.atomic.AtomicReference) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 12 with Guice.createInjector

use of com.google.inject.Guice.createInjector in project roboguice by roboguice.

the class OverrideModuleTest method testOverrideDeepExpose.

public void testOverrideDeepExpose() {
    final AtomicReference<Provider<Character>> charAProvider = new AtomicReference<Provider<Character>>();
    Module exposes5 = new PrivateModule() {

        @Override
        protected void configure() {
            install(new PrivateModule() {

                @Override
                protected void configure() {
                    bind(Integer.class).toInstance(5);
                    expose(Integer.class);
                    charAProvider.set(getProvider(Character.class));
                    bind(Character.class).toInstance('A');
                }
            });
            expose(Integer.class);
        }
    };
    Injector injector = Guice.createInjector(Modules.override(exposes5).with(EMPTY_MODULE));
    assertEquals(5, injector.getInstance(Integer.class).intValue());
    assertEquals('A', charAProvider.getAndSet(null).get().charValue());
    injector = Guice.createInjector(Modules.override(EMPTY_MODULE).with(exposes5));
    assertEquals(5, injector.getInstance(Integer.class).intValue());
    assertEquals('A', charAProvider.getAndSet(null).get().charValue());
    final AtomicReference<Provider<Character>> charBProvider = new AtomicReference<Provider<Character>>();
    Module binds15 = new AbstractModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(15);
            install(new PrivateModule() {

                @Override
                protected void configure() {
                    charBProvider.set(getProvider(Character.class));
                    bind(Character.class).toInstance('B');
                }
            });
        }
    };
    injector = Guice.createInjector(Modules.override(binds15).with(exposes5));
    assertEquals(5, injector.getInstance(Integer.class).intValue());
    assertEquals('A', charAProvider.getAndSet(null).get().charValue());
    assertEquals('B', charBProvider.getAndSet(null).get().charValue());
    injector = Guice.createInjector(Modules.override(exposes5).with(binds15));
    assertEquals(15, injector.getInstance(Integer.class).intValue());
    assertEquals('A', charAProvider.getAndSet(null).get().charValue());
    assertEquals('B', charBProvider.getAndSet(null).get().charValue());
}
Also used : Guice.createInjector(com.google.inject.Guice.createInjector) Injector(com.google.inject.Injector) AtomicReference(java.util.concurrent.atomic.AtomicReference) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) PrivateModule(com.google.inject.PrivateModule) Provider(com.google.inject.Provider) AbstractModule(com.google.inject.AbstractModule)

Example 13 with Guice.createInjector

use of com.google.inject.Guice.createInjector in project roboguice by roboguice.

the class OverrideModuleTest method testPrivateBindingOverride.

public void testPrivateBindingOverride() throws Exception {
    Injector inj = Guice.createInjector(Modules.override(new ExampleModule()).with(new AbstractModule() {

        @Override
        protected void configure() {
            bind(INPUT_KEY).toInstance(OVERRIDDEN_INPUT);
        }
    }));
    assertEquals(inj.getInstance(RESULT_KEY), OVERRIDDEN_RESULT);
}
Also used : Guice.createInjector(com.google.inject.Guice.createInjector) Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Aggregations

AbstractModule (com.google.inject.AbstractModule)13 Guice.createInjector (com.google.inject.Guice.createInjector)13 Injector (com.google.inject.Injector)13 Module (com.google.inject.Module)8 PrivateModule (com.google.inject.PrivateModule)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Provider (com.google.inject.Provider)2