Search in sources :

Example 1 with Injector

use of com.google.inject.Injector in project druid by druid-io.

the class ConditionalMultibindTest method testMultiConditionalBind_cat_dog_non_continuous_syntax.

@Test
public void testMultiConditionalBind_cat_dog_non_continuous_syntax() {
    props.setProperty("animal.type", "pets");
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, Animal.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Cat.class);
            ConditionalMultibind.create(props, binder, Animal.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Dog.class);
        }
    });
    Set<Animal> animalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Animal>>() {
    }));
    Assert.assertEquals(2, animalSet.size());
    Assert.assertEquals(animalSet, ImmutableSet.<Animal>of(new Cat(), new Dog()));
}
Also used : Binder(com.google.inject.Binder) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Module(com.google.inject.Module) Test(org.junit.Test)

Example 2 with Injector

use of com.google.inject.Injector in project druid by druid-io.

the class ConditionalMultibindTest method testMultiConditionalBind_cat_dog.

@Test
public void testMultiConditionalBind_cat_dog() {
    props.setProperty("animal.type", "pets");
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, Animal.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Cat.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Dog.class);
        }
    });
    Set<Animal> animalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Animal>>() {
    }));
    Assert.assertEquals(2, animalSet.size());
    Assert.assertEquals(animalSet, ImmutableSet.<Animal>of(new Cat(), new Dog()));
}
Also used : Binder(com.google.inject.Binder) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Module(com.google.inject.Module) Test(org.junit.Test)

Example 3 with Injector

use of com.google.inject.Injector in project druid by druid-io.

the class ConditionalMultibindTest method testMultiConditionalBind_inject.

@Test
public void testMultiConditionalBind_inject() {
    props.setProperty("animal.type", "pets");
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, Animal.class).addBinding(Bird.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Cat.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Dog.class);
        }
    }, new Module() {

        @Override
        public void configure(Binder binder) {
            ConditionalMultibind.create(props, binder, Animal.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("not_match"), Tiger.class).addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Fish.class);
        }
    });
    PetShotAvails shop = new PetShotAvails();
    injector.injectMembers(shop);
    Assert.assertEquals(4, shop.animals.size());
    Assert.assertEquals(shop.animals, ImmutableSet.<Animal>of(new Bird(), new Cat(), new Dog(), new Fish()));
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) Module(com.google.inject.Module) Test(org.junit.Test)

Example 4 with Injector

use of com.google.inject.Injector in project druid by druid-io.

the class AuthenticationKerberosConfigTest method testserde.

@Test
public void testserde() {
    Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            binder.install(new PropertiesModule(Arrays.asList("test.runtime.properties")));
            binder.install(new ConfigModule());
            binder.install(new DruidGuiceExtensions());
            JsonConfigProvider.bind(binder, "druid.hadoop.security.kerberos", AuthenticationKerberosConfig.class);
        }

        @Provides
        @LazySingleton
        public ObjectMapper jsonMapper() {
            return new DefaultObjectMapper();
        }
    });
    Properties props = injector.getInstance(Properties.class);
    AuthenticationKerberosConfig config = injector.getInstance(AuthenticationKerberosConfig.class);
    Assert.assertEquals(props.getProperty("druid.hadoop.security.kerberos.principal"), config.getPrincipal());
    Assert.assertEquals(props.getProperty("druid.hadoop.security.kerberos.keytab"), config.getKeytab());
}
Also used : DruidGuiceExtensions(io.druid.guice.DruidGuiceExtensions) ConfigModule(io.druid.guice.ConfigModule) Provides(com.google.inject.Provides) Properties(java.util.Properties) Binder(com.google.inject.Binder) LazySingleton(io.druid.guice.LazySingleton) Injector(com.google.inject.Injector) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) Module(com.google.inject.Module) PropertiesModule(io.druid.guice.PropertiesModule) ConfigModule(io.druid.guice.ConfigModule) PropertiesModule(io.druid.guice.PropertiesModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Example 5 with Injector

use of com.google.inject.Injector in project druid by druid-io.

the class CaffeineCacheProviderWithConfig method testBasicInjection.

@Test
public void testBasicInjection() throws Exception {
    final CaffeineCacheConfig config = new CaffeineCacheConfig();
    Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.<Module>of(binder -> {
        binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/redis");
        binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
        binder.bind(CaffeineCacheConfig.class).toInstance(config);
        binder.bind(Cache.class).toProvider(CaffeineCacheProviderWithConfig.class).in(ManageLifecycle.class);
    }));
    final Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
    lifecycle.start();
    try {
        Cache cache = injector.getInstance(Cache.class);
        Assert.assertEquals(CaffeineCache.class, cache.getClass());
    } finally {
        lifecycle.stop();
    }
}
Also used : Module(com.google.inject.Module) Properties(java.util.Properties) Inject(com.google.inject.Inject) Test(org.junit.Test) Random(java.util.Random) Names(com.google.inject.name.Names) UUID(java.util.UUID) Field(java.lang.reflect.Field) Ints(com.google.common.primitives.Ints) GuiceInjectors(io.druid.guice.GuiceInjectors) ManageLifecycle(io.druid.guice.ManageLifecycle) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) Injector(com.google.inject.Injector) Initialization(io.druid.initialization.Initialization) JsonConfigurator(io.druid.guice.JsonConfigurator) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ForkJoinPool(java.util.concurrent.ForkJoinPool) JsonConfigProvider(io.druid.guice.JsonConfigProvider) Map(java.util.Map) Assert(org.junit.Assert) Method(java.lang.reflect.Method) Before(org.junit.Before) Injector(com.google.inject.Injector) ManageLifecycle(io.druid.guice.ManageLifecycle) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) ManageLifecycle(io.druid.guice.ManageLifecycle) Test(org.junit.Test)

Aggregations

Injector (com.google.inject.Injector)2159 AbstractModule (com.google.inject.AbstractModule)623 Test (org.junit.Test)513 Module (com.google.inject.Module)364 Test (org.testng.annotations.Test)131 Before (org.junit.Before)116 Binder (com.google.inject.Binder)114 Properties (java.util.Properties)110 Key (com.google.inject.Key)84 Map (java.util.Map)78 HttpServletRequest (javax.servlet.http.HttpServletRequest)78 Provider (com.google.inject.Provider)74 IOException (java.io.IOException)71 TypeLiteral (com.google.inject.TypeLiteral)70 Set (java.util.Set)64 BeforeClass (org.junit.BeforeClass)61 File (java.io.File)60 CConfiguration (co.cask.cdap.common.conf.CConfiguration)55 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)55 PrivateModule (com.google.inject.PrivateModule)55