Search in sources :

Example 1 with Guice.createInjector

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

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)

Example 2 with Guice.createInjector

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

the class OverrideModuleTest method testOverridesApplyOriginalScanners.

public void testOverridesApplyOriginalScanners() {
    Injector injector = Guice.createInjector(Modules.override(NamedMunger.module()).with(new AbstractModule() {

        @TestProvides
        @Named("test")
        String provideString() {
            return "foo";
        }
    }));
    assertNull(injector.getExistingBinding(Key.get(String.class, named("test"))));
    Binding<String> binding = injector.getBinding(Key.get(String.class, named("test-munged")));
    assertEquals("foo", binding.getProvider().get());
}
Also used : Guice.createInjector(com.google.inject.Guice.createInjector) Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 3 with Guice.createInjector

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

the class OverrideModuleTest method testOverrideModuleAndPrivateModule.

public void testOverrideModuleAndPrivateModule() {
    Module exposes5 = new PrivateModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(5);
            expose(Integer.class);
        }
    };
    Module binds15 = new AbstractModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(15);
        }
    };
    Injector injector = Guice.createInjector(Modules.override(exposes5).with(binds15));
    assertEquals(15, injector.getInstance(Integer.class).intValue());
    Injector reverse = Guice.createInjector(Modules.override(binds15).with(exposes5));
    assertEquals(5, reverse.getInstance(Integer.class).intValue());
}
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)

Example 4 with Guice.createInjector

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

the class OverrideModuleTest method testOverrideModuleAndPrivateModule.

public void testOverrideModuleAndPrivateModule() {
    Module exposes5 = new PrivateModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(5);
            expose(Integer.class);
        }
    };
    Module binds15 = new AbstractModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(15);
        }
    };
    Injector injector = Guice.createInjector(Modules.override(exposes5).with(binds15));
    assertEquals(15, injector.getInstance(Integer.class).intValue());
    Injector reverse = Guice.createInjector(Modules.override(binds15).with(exposes5));
    assertEquals(5, reverse.getInstance(Integer.class).intValue());
}
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)

Example 5 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)

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