Search in sources :

Example 36 with Provider

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

the class OptionalBinderTest method testOptionalBinderDependsOnJitBinding.

// A previous version of OptionalBinder would fail to find jit dependendencies that were created
// by other bindings
public void testOptionalBinderDependsOnJitBinding() {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), JitBinding.class);
        }
    };
    // Everything should be absent since nothing triggered discovery of the jit binding
    Injector injector = Guice.createInjector(module);
    assertFalse(injector.getInstance(optionalKey(JitBinding.class)).isPresent());
    assertNull(injector.getExistingBinding(Key.get(JitBinding.class)));
    // in this case, because jit bindings are allowed in this injector, the DependsOnJitBinding
    // binding will get initialized and create jit bindings for its dependency. The optionalbinder
    // should then pick it up
    module = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), JitBinding.class);
            bind(DependsOnJitBinding.class);
        }
    };
    injector = Guice.createInjector(module);
    assertTrue(injector.getInstance(optionalKey(JitBinding.class)).isPresent());
    assertNotNull(injector.getExistingBinding(Key.get(JitBinding.class)));
    // in this case, because the jit binding is discovered dynamically, the optionalbinder won't
    // find it.  In prior implementations of OptionalBinder this would depend on the exact
    // sequencing of the installation of OptionalBinder vs. these injection points that trigger
    // dynamic injection.  In the current implementation it will consistently not find it.
    module = new AbstractModule() {

        @Override
        protected void configure() {
            bind(Object.class).toProvider(new Provider<Object>() {

                @Inject
                void setter(Injector injector) {
                    injector.getInstance(JitBinding.class);
                }

                @Override
                public Object get() {
                    return null;
                }
            });
            OptionalBinder.newOptionalBinder(binder(), JitBinding.class);
        }
    };
    injector = Guice.createInjector(module);
    assertFalse(injector.getInstance(optionalKey(JitBinding.class)).isPresent());
    assertNotNull(injector.getExistingBinding(Key.get(JitBinding.class)));
}
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 37 with Provider

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

the class OptionalBinderTest method testOptionalIsAbsentByDefault.

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

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), String.class);
        }
    };
    Injector injector = Guice.createInjector(module);
    Optional<String> optional = injector.getInstance(Key.get(optionalOfString));
    assertFalse(optional.isPresent());
    Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString));
    assertFalse(optionalP.isPresent());
    Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString));
    assertFalse(optionalJxP.isPresent());
    assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, null, null, null);
    if (HAS_JAVA_OPTIONAL) {
        optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString)));
        assertFalse(optional.isPresent());
        optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString)));
        assertFalse(optionalP.isPresent());
        optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString)));
        assertFalse(optionalJxP.isPresent());
    }
}
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 38 with Provider

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

the class OptionalBinderTest method testSetBinding.

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

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toInstance("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 39 with Provider

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

the class OptionalBinderTest method testUsesUserBoundValueNullProvidersMakeAbsent.

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

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

        @Provides
        String provideString() {
            return null;
        }
    };
    Injector injector = Guice.createInjector(module);
    assertEquals(null, injector.getInstance(String.class));
    Optional<String> optional = injector.getInstance(Key.get(optionalOfString));
    assertFalse(optional.isPresent());
    Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString));
    assertEquals(null, optionalP.get().get());
    Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString));
    assertEquals(null, optionalJxP.get().get());
    assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, null, null, providerInstance(null));
    if (HAS_JAVA_OPTIONAL) {
        optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString)));
        assertFalse(optional.isPresent());
        optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString)));
        assertEquals(null, optionalP.get().get());
        optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString)));
        assertEquals(null, 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 40 with Provider

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

the class OptionalBinderTest method testLinkedToNullProvidersMakeAbsentValuesAndPresentProviders_actual.

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

        @Override
        protected void configure() {
            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(optionalJxP.get().get());
    assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, null, 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)

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