Search in sources :

Example 26 with Provider

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

the class BoundFieldModuleTest method testRawProviderCannotBeBound.

public void testRawProviderCannotBeBound() {
    final Integer testValue = 1024;
    Object instance = new Object() {

        @Bind
        private Provider anIntProvider = new Provider() {

            @Override
            public Object get() {
                return testValue;
            }
        };
    };
    BoundFieldModule module = BoundFieldModule.of(instance);
    try {
        Guice.createInjector(module);
        fail();
    } catch (CreationException e) {
        assertContains(e.getMessage(), "Non parameterized Provider fields must have an " + "explicit binding class via @Bind(to = Foo.class)");
    }
}
Also used : CreationException(com.google.inject.CreationException) Provider(com.google.inject.Provider)

Example 27 with Provider

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

the class BoundFieldModuleTest method testIncrementingProvider.

public void testIncrementingProvider() {
    final Integer testBaseValue = 1024;
    Object instance = new Object() {

        @Bind
        private Provider<Integer> anIntProvider = new Provider<Integer>() {

            private int value = testBaseValue;

            @Override
            public Integer get() {
                return value++;
            }
        };
    };
    BoundFieldModule module = BoundFieldModule.of(instance);
    Injector injector = Guice.createInjector(module);
    assertEquals(testBaseValue, injector.getInstance(Integer.class));
    assertEquals((Integer) (testBaseValue + 1), injector.getInstance(Integer.class));
    assertEquals((Integer) (testBaseValue + 2), injector.getInstance(Integer.class));
}
Also used : Injector(com.google.inject.Injector) Provider(com.google.inject.Provider)

Example 28 with Provider

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

the class BoundFieldModuleTest method testBindingProviderWithProviderSubclassValue.

public void testBindingProviderWithProviderSubclassValue() {
    final Integer testValue = 1024;
    Object instance = new Object() {

        @Bind
        private Provider<Integer> anIntProvider = new IntegerProvider(testValue);
    };
    BoundFieldModule module = BoundFieldModule.of(instance);
    Injector injector = Guice.createInjector(module);
    assertEquals(testValue, injector.getInstance(Integer.class));
}
Also used : Injector(com.google.inject.Injector) Provider(com.google.inject.Provider)

Example 29 with Provider

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

the class BoundFieldModuleTest method testRawProviderCanBindToIncorrectType.

public void testRawProviderCanBindToIncorrectType() {
    final Integer testValue = 1024;
    Object instance = new Object() {

        @Bind(to = String.class)
        private Provider anIntProvider = new Provider() {

            @Override
            public Object get() {
                return testValue;
            }
        };
    };
    BoundFieldModule module = BoundFieldModule.of(instance);
    Injector injector = Guice.createInjector(module);
    assertEquals(testValue, injector.getInstance(String.class));
}
Also used : Injector(com.google.inject.Injector) Provider(com.google.inject.Provider)

Example 30 with Provider

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

the class OptionalBinderTest method testExactSameBindingCollapses_defaults.

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

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance(// using new String to ensure .equals is checked.
            new String("a"));
            OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().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, instance("a"), null, 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)

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