Search in sources :

Example 36 with PrivateModule

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

the class ModuleAnnotatedMethodScannerTest method privateModuleWithinPrivateModule_parentScannerInheritedIfInstalledAfter.

@Test
public void privateModuleWithinPrivateModule_parentScannerInheritedIfInstalledAfter() {
    Injector injector = Guice.createInjector(new PrivateModule() {

        @Override
        protected void configure() {
            expose(Key.get(String.class, named("foo-munged")));
            install(new PrivateModule() {

                @Override
                protected void configure() {
                }

                @Exposed
                @TestProvides
                @Named("foo")
                String foo() {
                    return "foo";
                }
            });
        }
    }, scannerModule(new NamedMunger()));
    assertMungedBinding(injector, String.class, "foo", "foo");
}
Also used : Injector(com.google.inject.Injector) PrivateModule(com.google.inject.PrivateModule) Test(org.junit.Test)

Example 37 with PrivateModule

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

the class ModuleAnnotatedMethodScannerTest method privateModuleWithinPrivateModule.

@Test
public void privateModuleWithinPrivateModule() {
    Injector injector = Guice.createInjector(scannerModule(new NamedMunger()), new PrivateModule() {

        @Override
        protected void configure() {
            expose(Key.get(String.class, named("foo-munged")));
            install(new PrivateModule() {

                @Override
                protected void configure() {
                }

                @Exposed
                @TestProvides
                @Named("foo")
                String foo() {
                    return "foo";
                }
            });
        }
    });
    assertMungedBinding(injector, String.class, "foo", "foo");
}
Also used : Injector(com.google.inject.Injector) PrivateModule(com.google.inject.PrivateModule) Test(org.junit.Test)

Example 38 with PrivateModule

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

the class ModuleAnnotatedMethodScannerTest method privateModuleInheritScanner_usingPrivateModule.

@Test
public void privateModuleInheritScanner_usingPrivateModule() {
    Injector injector = Guice.createInjector(scannerModule(new NamedMunger()), new PrivateModule() {

        @Override
        protected void configure() {
        }

        @Exposed
        @TestProvides
        @Named("foo")
        String foo() {
            return "foo";
        }
    });
    assertMungedBinding(injector, String.class, "foo", "foo");
}
Also used : Exposed(com.google.inject.Exposed) Named(com.google.inject.name.Named) Injector(com.google.inject.Injector) PrivateModule(com.google.inject.PrivateModule) Test(org.junit.Test)

Example 39 with PrivateModule

use of com.google.inject.PrivateModule 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)

Aggregations

PrivateModule (com.google.inject.PrivateModule)39 Injector (com.google.inject.Injector)26 AbstractModule (com.google.inject.AbstractModule)21 Module (com.google.inject.Module)11 FactoryModuleBuilder (com.google.inject.assistedinject.FactoryModuleBuilder)10 Test (org.junit.Test)9 Named (com.google.inject.name.Named)8 Guice.createInjector (com.google.inject.Guice.createInjector)6 DatasetDefinitionRegistry (co.cask.cdap.api.dataset.module.DatasetDefinitionRegistry)5 ConfigModule (co.cask.cdap.common.guice.ConfigModule)5 DefaultDatasetDefinitionRegistry (co.cask.cdap.data2.dataset2.DefaultDatasetDefinitionRegistry)5 Exposed (com.google.inject.Exposed)5 Key (com.google.inject.Key)5 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)4 Binding (com.google.inject.Binding)4 Singleton (com.google.inject.Singleton)4 Element (com.google.inject.spi.Element)4 CConfiguration (co.cask.cdap.common.conf.CConfiguration)3 AuthorizationDatasetTypeService (co.cask.cdap.data2.datafabric.dataset.service.AuthorizationDatasetTypeService)3 DatasetService (co.cask.cdap.data2.datafabric.dataset.service.DatasetService)3