Search in sources :

Example 91 with Provider

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

the class ElementsTest method testBindToProvider.

public void testBindToProvider() {
    final Provider<String> aProvider = new Provider<String>() {

        @Override
        public String get() {
            return "A";
        }
    };
    final javax.inject.Provider<Integer> intJavaxProvider = new javax.inject.Provider<Integer>() {

        @Override
        public Integer get() {
            return 42;
        }
    };
    final javax.inject.Provider<Double> doubleJavaxProvider = new javax.inject.Provider<Double>() {

        @javax.inject.Inject
        String string;

        @Override
        public Double get() {
            return 42.42;
        }
    };
    checkModule(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toProvider(aProvider);
            bind(Integer.class).toProvider(intJavaxProvider);
            bind(Double.class).toProvider(doubleJavaxProvider);
            bind(List.class).toProvider(ListProvider.class);
            bind(Collection.class).toProvider(Key.get(ListProvider.class));
            bind(Iterable.class).toProvider(new TypeLiteral<TProvider<List>>() {
            });
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderInstanceBinding);
            assertEquals(Key.get(String.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderInstanceBinding<? extends T> binding) {
                    assertSame(aProvider, binding.getUserSuppliedProvider());
                    assertSame(aProvider, binding.getProviderInstance());
                    return null;
                }
            });
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderInstanceBinding);
            assertEquals(Key.get(Integer.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderInstanceBinding<? extends T> binding) {
                    assertSame(intJavaxProvider, binding.getUserSuppliedProvider());
                    assertEquals(42, binding.getProviderInstance().get());
                    // we don't wrap this w/ dependencies if there were none.
                    assertFalse(binding.getProviderInstance() instanceof HasDependencies);
                    return null;
                }
            });
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderInstanceBinding);
            assertEquals(Key.get(Double.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderInstanceBinding<? extends T> binding) {
                    assertSame(doubleJavaxProvider, binding.getUserSuppliedProvider());
                    assertEquals(42.42, binding.getProviderInstance().get());
                    // we do wrap it with dependencies if there were some.
                    assertTrue(binding.getProviderInstance() instanceof HasDependencies);
                    Set<Dependency<?>> deps = ((HasDependencies) binding.getProviderInstance()).getDependencies();
                    assertEquals(1, deps.size());
                    assertEquals(String.class, deps.iterator().next().getKey().getTypeLiteral().getRawType());
                    return null;
                }
            });
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderKeyBinding);
            assertEquals(Key.get(List.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderKeyBinding<? extends T> binding) {
                    assertEquals(Key.get(ListProvider.class), binding.getProviderKey());
                    return null;
                }
            });
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderKeyBinding);
            assertEquals(Key.get(Collection.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderKeyBinding<? extends T> binding) {
                    assertEquals(Key.get(ListProvider.class), binding.getProviderKey());
                    return null;
                }
            });
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderKeyBinding);
            assertEquals(Key.get(Iterable.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderKeyBinding<? extends T> binding) {
                    assertEquals(new Key<TProvider<List>>() {
                    }, binding.getProviderKey());
                    return null;
                }
            });
            return null;
        }
    });
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) ArrayList(java.util.ArrayList) List(java.util.List) Provider(com.google.inject.Provider) AbstractModule(com.google.inject.AbstractModule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 92 with Provider

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

the class OptionalBinderTest method testSetDefault.

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

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().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, 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)

Example 93 with Provider

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

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)

Example 94 with Provider

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

the class SpiBindingsTest method testProviderBinding.

public void testProviderBinding() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("A");
        }
    });
    Key<Provider<String>> providerOfStringKey = new Key<Provider<String>>() {
    };
    Binding<Provider<String>> binding = injector.getBinding(providerOfStringKey);
    assertEquals(providerOfStringKey, binding.getKey());
    checkBindingSource(binding);
    assertTrue(binding instanceof ProviderBinding);
    binding.acceptTargetVisitor(new FailingTargetVisitor<Provider<String>>() {

        @Override
        public Void visit(ProviderBinding<? extends Provider<String>> binding) {
            assertEquals(Key.get(String.class), binding.getProvidedKey());
            return null;
        }
    });
}
Also used : Injector(com.google.inject.Injector) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 95 with Provider

use of com.google.inject.Provider in project ninja by ninjaframework.

the class ReverseRouterTest method before.

@Before
@SuppressWarnings("Convert2Lambda")
public void before() {
    this.ninjaProperties = mock(NinjaProperties.class);
    this.injector = mock(Injector.class);
    this.testControllerProvider = mock(Provider.class);
    this.ninjaBaseDirectoryResolver = new NinjaBaseDirectoryResolver(ninjaProperties);
    when(testControllerProvider.get()).thenReturn(new TestController());
    when(injector.getProvider(TestController.class)).thenReturn(testControllerProvider);
    when(injector.getInstance(ParamParsers.class)).thenReturn(new ParamParsers(Collections.emptySet()));
    Provider<RouteBuilderImpl> routeBuilderImplProvider = mock(Provider.class);
    when(routeBuilderImplProvider.get()).thenAnswer((invocation) -> new RouteBuilderImpl(ninjaProperties, ninjaBaseDirectoryResolver));
    router = new RouterImpl(injector, ninjaProperties, routeBuilderImplProvider);
    reverseRouter = new ReverseRouter(ninjaProperties, router);
    router.GET().route("/home").with(TestController::home);
    router.GET().route("/user/{email}/{id: .*}").with(TestController::user);
    router.GET().route("/u{userId: .*}/entries/{entryId: .*}").with(TestController::entry);
    // second route to index should not break reverse routing matching the first
    router.GET().route("/home/index").with(TestController::index);
    router.compileRoutes();
}
Also used : NinjaBaseDirectoryResolver(ninja.utils.NinjaBaseDirectoryResolver) ParamParsers(ninja.params.ParamParsers) Injector(com.google.inject.Injector) NinjaProperties(ninja.utils.NinjaProperties) Provider(com.google.inject.Provider) Before(org.junit.Before)

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