Search in sources :

Example 41 with AbstractModule

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

the class OverrideModuleTest method testOverrideOfOverride.

public void testOverrideOfOverride() {
    Module original = new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("A1");
            bind(key2).toInstance("A2");
            bind(key3).toInstance("A3");
        }
    };
    Module replacements1 = new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("B1");
            bind(key2).toInstance("B2");
        }
    };
    Module overrides = Modules.override(original).with(replacements1);
    Module replacements2 = new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("C1");
            bind(key3).toInstance("C3");
        }
    };
    Injector injector = createInjector(Modules.override(overrides).with(replacements2));
    assertEquals("C1", injector.getInstance(String.class));
    assertEquals("B2", injector.getInstance(key2));
    assertEquals("C3", injector.getInstance(key3));
}
Also used : Guice.createInjector(com.google.inject.Guice.createInjector) Injector(com.google.inject.Injector) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 42 with AbstractModule

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

the class SpiBindingsTest method testToConstructorBinding.

public void testToConstructorBinding() {
    checkInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(D.class);
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> binding) {
            assertTrue(binding instanceof ConstructorBinding);
            checkBindingSource(binding);
            assertEquals(Key.get(D.class), binding.getKey());
            binding.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ConstructorBinding<? extends T> binding) {
                    Constructor<?> expected = D.class.getDeclaredConstructors()[0];
                    assertEquals(expected, binding.getConstructor().getMember());
                    assertEquals(ImmutableSet.<InjectionPoint>of(), binding.getInjectableMembers());
                    return null;
                }
            });
            return null;
        }
    });
}
Also used : AbstractModule(com.google.inject.AbstractModule)

Example 43 with AbstractModule

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

the class SpiBindingsTest method testExtensionSpi.

public void testExtensionSpi() {
    final AtomicBoolean visiting = new AtomicBoolean(false);
    final Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toProvider(new ProviderWithExtensionVisitor<String>() {

                @Override
                public <B, V> V acceptExtensionVisitor(BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) {
                    assertSame(this, binding.getUserSuppliedProvider());
                    // to process the binding as normal
                    if (visiting.get()) {
                        assertTrue("visitor: " + visitor, visitor instanceof FailingSpiTargetVisitor);
                        return (V) "visited";
                    } else {
                        return visitor.visit(binding);
                    }
                }

                @Override
                public String get() {
                    return "FooBar";
                }
            });
        }
    });
    visiting.set(true);
    // Check for Provider<String> binding -- that is still a ProviderBinding.
    Key<Provider<String>> providerOfStringKey = new Key<Provider<String>>() {
    };
    Binding<Provider<String>> providerBinding = injector.getBinding(providerOfStringKey);
    assertEquals(providerOfStringKey, providerBinding.getKey());
    checkBindingSource(providerBinding);
    assertTrue("binding: " + providerBinding, providerBinding instanceof ProviderBinding);
    providerBinding.acceptTargetVisitor(new FailingTargetVisitor<Provider<String>>() {

        @Override
        public Void visit(ProviderBinding<? extends Provider<String>> binding) {
            assertEquals(Key.get(String.class), binding.getProvidedKey());
            return null;
        }
    });
    // Check for String binding -- that one is ProviderInstanceBinding, and gets hooked
    Binding<String> binding = injector.getBinding(String.class);
    assertEquals(Key.get(String.class), binding.getKey());
    checkBindingSource(binding);
    assertTrue(binding instanceof ProviderInstanceBinding);
    assertEquals("visited", binding.acceptTargetVisitor(new FailingSpiTargetVisitor<String>()));
}
Also used : AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Injector(com.google.inject.Injector) Key(com.google.inject.Key)

Example 44 with AbstractModule

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

the class ToolStageInjectorTest method testToolStageDoesntInjectProviders.

public void testToolStageDoesntInjectProviders() {
    final Foo foo = new Foo();
    Guice.createInjector(Stage.TOOL, new AbstractModule() {

        @Override
        protected void configure() {
            requestStaticInjection(Foo.class);
            bind(Object.class).toProvider(foo);
        }
    });
    assertNull(Foo.s);
    assertNull(Foo.sm);
    assertNull(foo.f);
    assertNull(foo.m);
}
Also used : AbstractModule(com.google.inject.AbstractModule)

Example 45 with AbstractModule

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

the class ToolStageInjectorTest method testToolStageWarnsOfMissingObjectGraph.

public void testToolStageWarnsOfMissingObjectGraph() {
    final Bar bar = new Bar();
    try {
        Guice.createInjector(Stage.TOOL, new AbstractModule() {

            @Override
            protected void configure() {
                requestStaticInjection(Bar.class);
                requestInjection(bar);
            }
        });
        fail("expected exception");
    } catch (CreationException expected) {
        Asserts.assertContains(expected.toString(), "No implementation for java.util.Collection was bound.", "No implementation for java.util.Map was bound.", "No implementation for java.util.List was bound.", "No implementation for java.util.Set was bound.");
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Aggregations

AbstractModule (com.google.inject.AbstractModule)845 Injector (com.google.inject.Injector)525 Module (com.google.inject.Module)232 CreationException (com.google.inject.CreationException)162 Provider (com.google.inject.Provider)64 Key (com.google.inject.Key)55 ConfigModule (co.cask.cdap.common.guice.ConfigModule)54 AuthorizationEnforcementModule (co.cask.cdap.security.authorization.AuthorizationEnforcementModule)49 DataSetsModules (co.cask.cdap.data.runtime.DataSetsModules)46 PrivateModule (com.google.inject.PrivateModule)46 AuthenticationContextModules (co.cask.cdap.security.auth.context.AuthenticationContextModules)45 AuthorizationTestModule (co.cask.cdap.security.authorization.AuthorizationTestModule)43 TypeLiteral (com.google.inject.TypeLiteral)43 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)42 BeforeClass (org.junit.BeforeClass)38 Map (java.util.Map)37 CConfiguration (co.cask.cdap.common.conf.CConfiguration)36 UnsupportedUGIProvider (co.cask.cdap.security.impersonation.UnsupportedUGIProvider)36 NonCustomLocationUnitTestModule (co.cask.cdap.common.guice.NonCustomLocationUnitTestModule)35 DefaultOwnerAdmin (co.cask.cdap.security.impersonation.DefaultOwnerAdmin)35