Search in sources :

Example 51 with Provider

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

the class MultibinderTest method testMultibinderCanInjectCollectionOfProviders.

public void testMultibinderCanInjectCollectionOfProviders() {
    Module module = new AbstractModule() {

        protected void configure() {
            final Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class);
            multibinder.addBinding().toProvider(Providers.of("A"));
            multibinder.addBinding().toProvider(Providers.of("B"));
            multibinder.addBinding().toInstance("C");
        }
    };
    Injector injector = Guice.createInjector(module);
    injector.getInstance(Key.get(collectionOfProvidersOfStrings));
    Key<Collection<Provider<String>>> collectionKey = Key.get(collectionOfProvidersOfStrings);
    Collection<Provider<String>> providers = injector.getInstance(collectionKey);
    Collection<String> values = Lists.newArrayList();
    for (Provider<String> provider : providers) {
        values.add(provider.get());
    }
    Collection<String> expectedValues = ImmutableList.of("A", "B", "C");
    assertEquals(expectedValues, values);
}
Also used : Injector(com.google.inject.Injector) Collection(java.util.Collection) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 52 with Provider

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

the class MultibinderTest method testMultibindingProviderDependencies.

public void testMultibindingProviderDependencies() {
    final Annotation setAnn = Names.named("foo");
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, setAnn);
            multibinder.addBinding().toInstance("a");
            multibinder.addBinding().toInstance("b");
        }
    });
    HasDependencies providerBinding = (HasDependencies) injector.getBinding(new Key<Collection<Provider<String>>>(setAnn) {
    });
    HasDependencies setBinding = (HasDependencies) injector.getBinding(new Key<Set<String>>(setAnn) {
    });
    // sanity check the size
    assertEquals(setBinding.getDependencies().toString(), 2, setBinding.getDependencies().size());
    Set<Dependency<?>> expected = Sets.newHashSet();
    for (Dependency<?> dep : setBinding.getDependencies()) {
        Key key = dep.getKey();
        Dependency<?> providerDependency = Dependency.get(key.ofType(Types.providerOf(key.getTypeLiteral().getType())));
        expected.add(providerDependency);
    }
    assertEquals(expected, providerBinding.getDependencies());
}
Also used : Injector(com.google.inject.Injector) Dependency(com.google.inject.spi.Dependency) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 53 with Provider

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

the class MultibinderTest method testMultibinderSetShowsBothElementsIfToStringDifferent.

public void testMultibinderSetShowsBothElementsIfToStringDifferent() {
    // considers.
    class ValueType {

        int a;

        int b;

        ValueType(int a, int b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public boolean equals(Object obj) {
            return (obj instanceof ValueType) && (((ValueType) obj).a == a);
        }

        @Override
        public int hashCode() {
            return a;
        }

        @Override
        public String toString() {
            return String.format("ValueType(%d,%d)", a, b);
        }
    }
    Module module1 = new AbstractModule() {

        protected void configure() {
            final Multibinder<ValueType> multibinder = Multibinder.newSetBinder(binder(), ValueType.class);
            multibinder.addBinding().toProvider(Providers.of(new ValueType(1, 2)));
        }
    };
    Module module2 = new AbstractModule() {

        protected void configure() {
            final Multibinder<ValueType> multibinder = Multibinder.newSetBinder(binder(), ValueType.class);
            multibinder.addBinding().toInstance(new ValueType(1, 3));
        }
    };
    Injector injector = Guice.createInjector(module1, module2);
    TypeLiteral<ValueType> valueType = TypeLiteral.get(ValueType.class);
    TypeLiteral<Set<ValueType>> setOfValueType = new TypeLiteral<Set<ValueType>>() {
    };
    TypeLiteral<Collection<Provider<ValueType>>> collectionOfProvidersOfValueType = new TypeLiteral<Collection<Provider<ValueType>>>() {
    };
    try {
        injector.getInstance(Key.get(setOfValueType));
        fail();
    } catch (ProvisionException expected) {
        assertContains(expected.getMessage(), "1) Set injection failed due to multiple elements comparing equal:", "\"ValueType(1,2)\"", "bound at " + module1.getClass().getName(), "\"ValueType(1,3)\"", "bound at " + module2.getClass().getName());
    }
    // But we can still visit the module!
    assertSetVisitor(Key.get(setOfValueType), Key.get(collectionOfProvidersOfValueType), valueType, setOf(module1, module2), MODULE, false, 0, instance(new ValueType(1, 2)), instance(new ValueType(1, 3)));
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider) ProvisionException(com.google.inject.ProvisionException) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Collection(java.util.Collection) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule)

Example 54 with Provider

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

the class MultibinderTest method testMultibinderCanInjectCollectionOfProvidersWithAnnotation.

public void testMultibinderCanInjectCollectionOfProvidersWithAnnotation() {
    final Annotation ann = Names.named("foo");
    Module module = new AbstractModule() {

        protected void configure() {
            final Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, ann);
            multibinder.addBinding().toProvider(Providers.of("A"));
            multibinder.addBinding().toProvider(Providers.of("B"));
            multibinder.addBinding().toInstance("C");
        }
    };
    Injector injector = Guice.createInjector(module);
    Key<Collection<Provider<String>>> collectionKey = Key.get(collectionOfProvidersOfStrings, ann);
    Collection<Provider<String>> providers = injector.getInstance(collectionKey);
    Collection<String> values = Lists.newArrayList();
    for (Provider<String> provider : providers) {
        values.add(provider.get());
    }
    Collection<String> expectedValues = ImmutableList.of("A", "B", "C");
    assertEquals(expectedValues, values);
}
Also used : Injector(com.google.inject.Injector) Collection(java.util.Collection) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 55 with Provider

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

the class OptionalBinderTest method testOptionalIsAppropriatelyLazy.

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

        int nextValue = 1;

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), Integer.class).setDefault().to(Key.get(Integer.class, Names.named("foo")));
        }

        @Provides
        @Named("foo")
        int provideInt() {
            return nextValue++;
        }
    };
    Injector injector = Guice.createInjector(module);
    Optional<Provider<Integer>> optionalP = injector.getInstance(Key.get(optionalOfProviderInteger));
    Optional<javax.inject.Provider<Integer>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderInteger));
    assertEquals(1, injector.getInstance(Integer.class).intValue());
    assertEquals(2, injector.getInstance(Integer.class).intValue());
    // Calling .get() on an Optional<Integer> multiple times will keep giving the same thing
    Optional<Integer> optional = injector.getInstance(Key.get(optionalOfInteger));
    assertEquals(3, optional.get().intValue());
    assertEquals(3, optional.get().intValue());
    // But getting another Optional<Integer> will give a new one.
    assertEquals(4, injector.getInstance(Key.get(optionalOfInteger)).get().intValue());
    // And the Optional<Provider> will return a provider that gives a new value each time.
    assertEquals(5, optionalP.get().get().intValue());
    assertEquals(6, optionalP.get().get().intValue());
    assertEquals(7, optionalJxP.get().get().intValue());
    assertEquals(8, optionalJxP.get().get().intValue());
    // and same rules with java.util.Optional
    if (HAS_JAVA_OPTIONAL) {
        optional = toOptional(injector.getInstance(Key.get(javaOptionalOfInteger)));
        assertEquals(9, optional.get().intValue());
        assertEquals(9, optional.get().intValue());
        optional = toOptional(injector.getInstance(Key.get(javaOptionalOfInteger)));
        assertEquals(10, optional.get().intValue());
        optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderInteger)));
        assertEquals(11, optionalP.get().get().intValue());
        assertEquals(12, optionalP.get().get().intValue());
        optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderInteger)));
        assertEquals(13, optionalJxP.get().get().intValue());
        assertEquals(14, optionalJxP.get().get().intValue());
    }
}
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