Search in sources :

Example 6 with Guice.createInjector

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

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 7 with Guice.createInjector

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

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 8 with Guice.createInjector

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

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)

Example 9 with Guice.createInjector

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

the class OverrideModuleTest method testExposedBindingOverride.

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

        @Override
        protected void configure() {
            bind(RESULT_KEY).toInstance(OVERRIDDEN_RESULT);
        }
    }));
    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)

Example 10 with Guice.createInjector

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

the class OverrideModuleTest method testOverridePrivateModuleOverPrivateModule.

public void testOverridePrivateModuleOverPrivateModule() {
    Module exposes5and6 = new AbstractModule() {

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

                @Override
                protected void configure() {
                    bind(Integer.class).toInstance(5);
                    expose(Integer.class);
                    bind(Character.class).toInstance('E');
                }
            });
            install(new PrivateModule() {

                @Override
                protected void configure() {
                    bind(Long.class).toInstance(6L);
                    expose(Long.class);
                    bind(Character.class).toInstance('F');
                }
            });
        }
    };
    AbstractModule exposes15 = new AbstractModule() {

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

                @Override
                protected void configure() {
                    bind(Integer.class).toInstance(15);
                    expose(Integer.class);
                    bind(Character.class).toInstance('G');
                }
            });
            install(new PrivateModule() {

                @Override
                protected void configure() {
                    bind(Character.class).toInstance('H');
                }
            });
        }
    };
    // override forwards
    Injector injector = Guice.createInjector(Modules.override(exposes5and6).with(exposes15));
    assertEquals(15, injector.getInstance(Integer.class).intValue());
    assertEquals(6L, injector.getInstance(Long.class).longValue());
    // and in reverse order
    Injector reverse = Guice.createInjector(Modules.override(exposes15).with(exposes5and6));
    assertEquals(5, reverse.getInstance(Integer.class).intValue());
    assertEquals(6L, reverse.getInstance(Long.class).longValue());
}
Also used : Guice.createInjector(com.google.inject.Guice.createInjector) 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) 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