Search in sources :

Example 96 with AbstractModule

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

the class BinderTest method testBindingToProvider.

public void testBindingToProvider() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(new TypeLiteral<Provider<String>>() {
                }).toInstance(Providers.of("A"));
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) Binding to Provider is not allowed.", "at " + BinderTest.class.getName(), getDeclaringSourcePart(getClass()));
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 97 with AbstractModule

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

the class BinderTest method testToStringOnBinderApi.

public void testToStringOnBinderApi() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            public void configure() {
                assertEquals("Binder", binder().toString());
                assertEquals("Provider<java.lang.Integer>", getProvider(Integer.class).toString());
                assertEquals("Provider<java.util.List<java.lang.String>>", getProvider(Key.get(new TypeLiteral<List<String>>() {
                })).toString());
                assertEquals("BindingBuilder<java.lang.Integer>", bind(Integer.class).toString());
                assertEquals("BindingBuilder<java.lang.Integer>", bind(Integer.class).annotatedWith(Names.named("a")).toString());
                assertEquals("ConstantBindingBuilder", bindConstant().toString());
                assertEquals("ConstantBindingBuilder", bindConstant().annotatedWith(Names.named("b")).toString());
                assertEquals("AnnotatedElementBuilder", binder().newPrivateBinder().expose(Integer.class).toString());
            }
        });
        fail();
    } catch (CreationException ignored) {
    }
}
Also used : List(java.util.List) CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 98 with AbstractModule

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

the class BinderTest method testUntargettedBinding.

/**
   * Untargetted bindings should follow @ImplementedBy and @ProvidedBy
   * annotations if they exist. Otherwise the class should be constructed
   * directly.
   */
public void testUntargettedBinding() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(HasProvidedBy1.class);
            bind(HasImplementedBy1.class);
            bind(HasProvidedBy2.class);
            bind(HasImplementedBy2.class);
            bind(JustAClass.class);
        }
    });
    assertNotNull(injector.getInstance(HasProvidedBy1.class));
    assertNotNull(injector.getInstance(HasImplementedBy1.class));
    assertNotSame(HasProvidedBy2.class, injector.getInstance(HasProvidedBy2.class).getClass());
    assertSame(ExtendsHasImplementedBy2.class, injector.getInstance(HasImplementedBy2.class).getClass());
    assertSame(JustAClass.class, injector.getInstance(JustAClass.class).getClass());
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 99 with AbstractModule

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

the class NamesTest method testBindPropertiesUsingMap.

public void testBindPropertiesUsingMap() {
    final Map<String, String> properties = ImmutableMap.of("SanJose", "Sharks", "Edmonton", "Oilers");
    Injector injector = Guice.createInjector(new AbstractModule() {

        protected void configure() {
            Names.bindProperties(binder(), properties);
        }
    });
    assertEquals("Sharks", injector.getInstance(Key.get(String.class, Names.named("SanJose"))));
    assertEquals("Oilers", injector.getInstance(Key.get(String.class, Names.named("Edmonton"))));
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 100 with AbstractModule

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

the class NamesTest method testBindPropertiesUsingProperties.

public void testBindPropertiesUsingProperties() {
    final Properties teams = new Properties();
    teams.setProperty("SanJose", "Sharks");
    teams.setProperty("Edmonton", "Oilers");
    Injector injector = Guice.createInjector(new AbstractModule() {

        protected void configure() {
            Names.bindProperties(binder(), teams);
        }
    });
    assertEquals("Sharks", injector.getInstance(Key.get(String.class, Names.named("SanJose"))));
    assertEquals("Oilers", injector.getInstance(Key.get(String.class, Names.named("Edmonton"))));
}
Also used : Injector(com.google.inject.Injector) Properties(java.util.Properties) AbstractModule(com.google.inject.AbstractModule)

Aggregations

AbstractModule (com.google.inject.AbstractModule)840 Injector (com.google.inject.Injector)522 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 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)42 TypeLiteral (com.google.inject.TypeLiteral)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