Search in sources :

Example 36 with AbstractModule

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

the class OverrideModuleTest method testHashcodeNeverCalledOnProviderInstance.

public void testHashcodeNeverCalledOnProviderInstance() {
    final HashEqualsTester a = new HashEqualsTester();
    a.throwOnHashcode = true;
    a.equality = "test";
    final HashEqualsTester b = new HashEqualsTester();
    b.throwOnHashcode = true;
    b.equality = "test";
    Guice.createInjector(Modules.override(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class);
            bind(Object.class).toProvider(a);
            bind(Object.class).toProvider(b);
        }
    }).with());
}
Also used : AbstractModule(com.google.inject.AbstractModule)

Example 37 with AbstractModule

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

the class OverrideModuleTest method testOverrideScopeAnnotation.

public void testOverrideScopeAnnotation() {
    final Scope scope = new Scope() {

        @Override
        public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
            throw new AssertionError("Should not be called");
        }
    };
    final SingleUseScope replacementScope = new SingleUseScope();
    Module original = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, scope);
            bind(Date.class).in(TestScopeAnnotation.class);
        }
    };
    Module replacements = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, replacementScope);
        }
    };
    Injector injector = createInjector(Modules.override(original).with(replacements));
    injector.getInstance(Date.class);
    assertTrue(replacementScope.used);
}
Also used : Scope(com.google.inject.Scope) 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) Key(com.google.inject.Key) Date(java.util.Date) Provider(com.google.inject.Provider) AbstractModule(com.google.inject.AbstractModule)

Example 38 with AbstractModule

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

the class OverrideModuleTest method testOverridesApplyOriginalScanners.

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

        @Override
        protected void configure() {
        }

        @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 39 with AbstractModule

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

the class ProviderMethodsTest method testWildcardProviderMethods.

public void testWildcardProviderMethods() {
    final List<String> strings = ImmutableList.of("A", "B", "C");
    final List<Number> numbers = ImmutableList.<Number>of(1, 2, 3);
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            @SuppressWarnings("unchecked") Key<List<? super Integer>> listOfSupertypesOfInteger = (Key<List<? super Integer>>) Key.get(Types.listOf(Types.supertypeOf(Integer.class)));
            bind(listOfSupertypesOfInteger).toInstance(numbers);
        }

        @Provides
        public List<? extends CharSequence> provideCharSequences() {
            return strings;
        }

        @Provides
        public Class<?> provideType() {
            return Float.class;
        }
    });
    assertSame(strings, injector.getInstance(HasWildcardInjection.class).charSequences);
    assertSame(numbers, injector.getInstance(HasWildcardInjection.class).numbers);
    assertSame(Float.class, injector.getInstance(HasWildcardInjection.class).type);
}
Also used : Provides(com.google.inject.Provides) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Key(com.google.inject.Key)

Example 40 with AbstractModule

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

the class ProviderMethodsTest method testAutomaticProviderMethodsDoNotCauseDoubleBinding.

/**
   * If the user installs provider methods for the module manually, that shouldn't cause a double
   * binding of the provider methods' types.
   */
public void testAutomaticProviderMethodsDoNotCauseDoubleBinding() {
    Module installsSelf = new AbstractModule() {

        @Override
        protected void configure() {
            install(this);
            bind(Integer.class).toInstance(5);
        }

        @Provides
        public String provideString(Integer count) {
            return "A" + count;
        }
    };
    Injector injector = Guice.createInjector(installsSelf);
    assertEquals("A5", injector.getInstance(String.class));
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) ProviderMethodsModule(com.google.inject.internal.ProviderMethodsModule) AbstractModule(com.google.inject.AbstractModule) 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