Search in sources :

Example 81 with Provider

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

the class OptionalBinderTest method testQualifiedAggregatesTogether.

public void testQualifiedAggregatesTogether() throws Exception {
    Module module1 = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), Key.get(String.class, Names.named("foo")));
        }
    };
    Module module2 = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), Key.get(String.class, Names.named("foo"))).setDefault().toInstance("a");
        }
    };
    Module module3 = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), Key.get(String.class, Names.named("foo"))).setBinding().toInstance("b");
        }
    };
    Injector injector = Guice.createInjector(module1, module2, module3);
    assertEquals("b", injector.getInstance(Key.get(String.class, Names.named("foo"))));
    Optional<String> optional = injector.getInstance(Key.get(optionalOfString, Names.named("foo")));
    assertTrue(optional.isPresent());
    assertEquals("b", optional.get());
    Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString, Names.named("foo")));
    assertTrue(optionalP.isPresent());
    assertEquals("b", optionalP.get().get());
    Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString, Names.named("foo")));
    assertTrue(optionalJxP.isPresent());
    assertEquals("b", optionalJxP.get().get());
    assertOptionalVisitor(Key.get(String.class, Names.named("foo")), setOf(module1, module2, module3), VisitType.BOTH, 0, instance("a"), instance("b"), null);
    if (HAS_JAVA_OPTIONAL) {
        optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString, Names.named("foo"))));
        assertTrue(optional.isPresent());
        assertEquals("b", optional.get());
        optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString, Names.named("foo"))));
        assertTrue(optionalP.isPresent());
        assertEquals("b", optionalP.get().get());
        optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString, Names.named("foo"))));
        assertTrue(optionalJxP.isPresent());
        assertEquals("b", optionalJxP.get().get());
    }
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 82 with Provider

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

the class OptionalBinderTest method testExactSameBindingCollapses_actual.

public void testExactSameBindingCollapses_actual() throws Exception {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toInstance(// using new String to ensure .equals is checked.
            new String("a"));
            OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toInstance(new String("a"));
        }
    };
    Injector injector = Guice.createInjector(module);
    assertEquals("a", injector.getInstance(String.class));
    Optional<String> optional = injector.getInstance(Key.get(optionalOfString));
    assertTrue(optional.isPresent());
    assertEquals("a", optional.get());
    Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString));
    assertTrue(optionalP.isPresent());
    assertEquals("a", optionalP.get().get());
    Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString));
    assertTrue(optionalJxP.isPresent());
    assertEquals("a", optionalJxP.get().get());
    assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, null, instance("a"), null);
    if (HAS_JAVA_OPTIONAL) {
        optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString)));
        assertTrue(optional.isPresent());
        assertEquals("a", optional.get());
        optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString)));
        assertTrue(optionalP.isPresent());
        assertEquals("a", optionalP.get().get());
        optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString)));
        assertTrue(optionalJxP.isPresent());
        assertEquals("a", optionalJxP.get().get());
    }
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 83 with Provider

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

the class OptionalBinderTest method testUsesUserBoundValue.

public void testUsesUserBoundValue() throws Exception {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), String.class);
        }

        @Provides
        String provideString() {
            return "foo";
        }
    };
    Injector injector = Guice.createInjector(module);
    assertEquals("foo", injector.getInstance(String.class));
    Optional<String> optional = injector.getInstance(Key.get(optionalOfString));
    assertEquals("foo", optional.get());
    Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString));
    assertEquals("foo", optionalP.get().get());
    Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString));
    assertEquals("foo", optionalJxP.get().get());
    assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, null, null, providerInstance("foo"));
    if (HAS_JAVA_OPTIONAL) {
        optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString)));
        assertEquals("foo", optional.get());
        optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString)));
        assertEquals("foo", optionalP.get().get());
        optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString)));
        assertEquals("foo", optionalJxP.get().get());
    }
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 84 with Provider

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

the class OptionalBinderTest method testLinkedToNullActualDoesntFallbackToDefault.

// TODO(sameb): Maybe change this?
public void testLinkedToNullActualDoesntFallbackToDefault() throws Exception {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("a");
            OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toProvider(Providers.<String>of(null));
        }
    };
    Injector injector = Guice.createInjector(module);
    assertNull(injector.getInstance(String.class));
    Optional<String> optional = injector.getInstance(Key.get(optionalOfString));
    assertFalse(optional.isPresent());
    Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString));
    assertTrue(optionalP.isPresent());
    assertNull(optionalP.get().get());
    Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString));
    assertTrue(optionalJxP.isPresent());
    assertNull(optionalP.get().get());
    assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, instance("a"), SpiUtils.<String>providerInstance(null), null);
    if (HAS_JAVA_OPTIONAL) {
        optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString)));
        assertFalse(optional.isPresent());
        optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString)));
        assertTrue(optionalP.isPresent());
        assertNull(optionalP.get().get());
        optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString)));
        assertTrue(optionalJxP.isPresent());
        assertNull(optionalJxP.get().get());
    }
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 85 with Provider

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

Provider (com.google.inject.Provider)133 Injector (com.google.inject.Injector)72 AbstractModule (com.google.inject.AbstractModule)63 Module (com.google.inject.Module)44 Key (com.google.inject.Key)17 TypeLiteral (com.google.inject.TypeLiteral)10 ArrayList (java.util.ArrayList)10 ProvisionException (com.google.inject.ProvisionException)9 Dependency (com.google.inject.spi.Dependency)9 Map (java.util.Map)8 Before (org.junit.Before)7 CurrentUser (com.google.gerrit.server.CurrentUser)6 RequestContext (com.google.gerrit.server.util.RequestContext)6 ThreadLocalRequestContext (com.google.gerrit.server.util.ThreadLocalRequestContext)6 InMemoryModule (com.google.gerrit.testutil.InMemoryModule)6 PrivateModule (com.google.inject.PrivateModule)6 Scope (com.google.inject.Scope)6 List (java.util.List)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Annotation (java.lang.annotation.Annotation)5