Search in sources :

Example 16 with Multibinder

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

the class InitializationTest method testCreateInjectorWithNodeRoleFilterUsingInject_moduleLoaded.

@Test
public void testCreateInjectorWithNodeRoleFilterUsingInject_moduleLoaded() {
    final Set<NodeRole> nodeRoles = ImmutableSet.of(new NodeRole("role1"), new NodeRole("druid"));
    final DruidNode expected = new DruidNode("test-inject", null, false, null, null, true, false);
    Injector startupInjector = GuiceInjectors.makeStartupInjectorWithModules(ImmutableList.of(binder -> {
        Multibinder<NodeRole> selfBinder = Multibinder.newSetBinder(binder, NodeRole.class, Self.class);
        nodeRoles.forEach(nodeRole -> selfBinder.addBinding().toInstance(nodeRole));
    }));
    Injector injector = Initialization.makeInjectorWithModules(nodeRoles, startupInjector, ImmutableList.of((com.google.inject.Module) binder -> JsonConfigProvider.bindInstance(binder, Key.get(DruidNode.class, Self.class), expected), new NodeRolesInjectTestModule()));
    Assert.assertNotNull(injector);
    Assert.assertEquals(expected, injector.getInstance(Key.get(DruidNode.class, Self.class)));
    Assert.assertEquals("I am Druid", injector.getInstance(Key.get(String.class, Names.named("emperor"))));
}
Also used : Module(com.fasterxml.jackson.databind.Module) MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) Self(org.apache.druid.guice.annotations.Self) URL(java.net.URL) Inject(com.google.inject.Inject) Key(com.google.inject.Key) Collections2(com.google.common.collect.Collections2) HashSet(java.util.HashSet) URLClassLoader(java.net.URLClassLoader) ImmutableList(com.google.common.collect.ImmutableList) Binder(com.google.inject.Binder) ExtensionsConfig(org.apache.druid.guice.ExtensionsConfig) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) JsonConfigProvider(org.apache.druid.guice.JsonConfigProvider) LoadScope(org.apache.druid.guice.annotations.LoadScope) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConfigurationException(com.google.inject.ConfigurationException) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) Test(org.junit.Test) IOException(java.io.IOException) Names(com.google.inject.name.Names) Multibinder(com.google.inject.multibindings.Multibinder) Sets(com.google.common.collect.Sets) GuiceInjectors(org.apache.druid.guice.GuiceInjectors) File(java.io.File) Injector(com.google.inject.Injector) List(java.util.List) Rule(org.junit.Rule) DruidNode(org.apache.druid.server.DruidNode) NodeRole(org.apache.druid.discovery.NodeRole) Assert(org.junit.Assert) FixMethodOrder(org.junit.FixMethodOrder) Comparator(java.util.Comparator) TemporaryFolder(org.junit.rules.TemporaryFolder) NodeRole(org.apache.druid.discovery.NodeRole) Multibinder(com.google.inject.multibindings.Multibinder) Injector(com.google.inject.Injector) DruidNode(org.apache.druid.server.DruidNode) Self(org.apache.druid.guice.annotations.Self) Module(com.fasterxml.jackson.databind.Module) Test(org.junit.Test)

Example 17 with Multibinder

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

the class InitializationTest method testCreateInjectorWithNodeRoleFilterUsingInject_moduleNotLoaded.

@Test
public void testCreateInjectorWithNodeRoleFilterUsingInject_moduleNotLoaded() {
    final Set<NodeRole> nodeRoles = ImmutableSet.of(new NodeRole("role1"), new NodeRole("role2"));
    final DruidNode expected = new DruidNode("test-inject", null, false, null, null, true, false);
    Injector startupInjector = GuiceInjectors.makeStartupInjectorWithModules(ImmutableList.of(binder -> {
        Multibinder<NodeRole> selfBinder = Multibinder.newSetBinder(binder, NodeRole.class, Self.class);
        nodeRoles.forEach(nodeRole -> selfBinder.addBinding().toInstance(nodeRole));
    }));
    Injector injector = Initialization.makeInjectorWithModules(nodeRoles, startupInjector, ImmutableList.of((com.google.inject.Module) binder -> JsonConfigProvider.bindInstance(binder, Key.get(DruidNode.class, Self.class), expected), new NodeRolesInjectTestModule()));
    Assert.assertNotNull(injector);
    Assert.assertEquals(expected, injector.getInstance(Key.get(DruidNode.class, Self.class)));
    Assert.assertThrows("Guice configuration errors", ConfigurationException.class, () -> injector.getInstance(Key.get(String.class, Names.named("emperor"))));
}
Also used : Module(com.fasterxml.jackson.databind.Module) MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) Self(org.apache.druid.guice.annotations.Self) URL(java.net.URL) Inject(com.google.inject.Inject) Key(com.google.inject.Key) Collections2(com.google.common.collect.Collections2) HashSet(java.util.HashSet) URLClassLoader(java.net.URLClassLoader) ImmutableList(com.google.common.collect.ImmutableList) Binder(com.google.inject.Binder) ExtensionsConfig(org.apache.druid.guice.ExtensionsConfig) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) JsonConfigProvider(org.apache.druid.guice.JsonConfigProvider) LoadScope(org.apache.druid.guice.annotations.LoadScope) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConfigurationException(com.google.inject.ConfigurationException) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) Test(org.junit.Test) IOException(java.io.IOException) Names(com.google.inject.name.Names) Multibinder(com.google.inject.multibindings.Multibinder) Sets(com.google.common.collect.Sets) GuiceInjectors(org.apache.druid.guice.GuiceInjectors) File(java.io.File) Injector(com.google.inject.Injector) List(java.util.List) Rule(org.junit.Rule) DruidNode(org.apache.druid.server.DruidNode) NodeRole(org.apache.druid.discovery.NodeRole) Assert(org.junit.Assert) FixMethodOrder(org.junit.FixMethodOrder) Comparator(java.util.Comparator) TemporaryFolder(org.junit.rules.TemporaryFolder) NodeRole(org.apache.druid.discovery.NodeRole) Multibinder(com.google.inject.multibindings.Multibinder) Injector(com.google.inject.Injector) DruidNode(org.apache.druid.server.DruidNode) Self(org.apache.druid.guice.annotations.Self) Module(com.fasterxml.jackson.databind.Module) Test(org.junit.Test)

Aggregations

Multibinder (com.google.inject.multibindings.Multibinder)17 AbstractModule (com.google.inject.AbstractModule)14 Injector (com.google.inject.Injector)10 ImmutableSet (com.google.common.collect.ImmutableSet)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 HttpHandler (co.cask.http.HttpHandler)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Singleton (com.google.inject.Singleton)3 DatasetDefinitionRegistry (co.cask.cdap.api.dataset.module.DatasetDefinitionRegistry)2 ConfigModule (co.cask.cdap.common.guice.ConfigModule)2 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)2 IOModule (co.cask.cdap.common.guice.IOModule)2 NonCustomLocationUnitTestModule (co.cask.cdap.common.guice.NonCustomLocationUnitTestModule)2 DataFabricModules (co.cask.cdap.data.runtime.DataFabricModules)2 DataSetServiceModules (co.cask.cdap.data.runtime.DataSetServiceModules)2 DataSetsModules (co.cask.cdap.data.runtime.DataSetsModules)2 StreamAdminModules (co.cask.cdap.data.stream.StreamAdminModules)2 StreamViewHttpHandler (co.cask.cdap.data.stream.StreamViewHttpHandler)2 StreamFetchHandler (co.cask.cdap.data.stream.service.StreamFetchHandler)2