use of com.google.inject.multibindings.MapBinder in project druid by druid-io.
the class PolyBindTest method testSanity.
@Test
public void testSanity() throws Exception {
setUp(new Module() {
@Override
public void configure(Binder binder) {
final MapBinder<String, Gogo> gogoBinder = PolyBind.optionBinder(binder, Key.get(Gogo.class));
gogoBinder.addBinding("a").to(GoA.class);
gogoBinder.addBinding("b").to(GoB.class);
final MapBinder<String, GogoSally> gogoSallyBinder = PolyBind.optionBinder(binder, Key.get(GogoSally.class));
gogoSallyBinder.addBinding("a").to(GoA.class);
gogoSallyBinder.addBinding("b").to(GoB.class);
PolyBind.createChoice(binder, "billy", Key.get(Gogo.class, Names.named("reverse")), Key.get(GoB.class));
final MapBinder<String, Gogo> annotatedGogoBinder = PolyBind.optionBinder(binder, Key.get(Gogo.class, Names.named("reverse")));
annotatedGogoBinder.addBinding("a").to(GoB.class);
annotatedGogoBinder.addBinding("b").to(GoA.class);
}
});
Assert.assertEquals("A", injector.getInstance(Gogo.class).go());
Assert.assertEquals("B", injector.getInstance(Key.get(Gogo.class, Names.named("reverse"))).go());
props.setProperty("billy", "b");
Assert.assertEquals("B", injector.getInstance(Gogo.class).go());
Assert.assertEquals("A", injector.getInstance(Key.get(Gogo.class, Names.named("reverse"))).go());
props.setProperty("billy", "a");
Assert.assertEquals("A", injector.getInstance(Gogo.class).go());
Assert.assertEquals("B", injector.getInstance(Key.get(Gogo.class, Names.named("reverse"))).go());
props.setProperty("billy", "b");
Assert.assertEquals("B", injector.getInstance(Gogo.class).go());
Assert.assertEquals("A", injector.getInstance(Key.get(Gogo.class, Names.named("reverse"))).go());
props.setProperty("billy", "c");
Assert.assertEquals("A", injector.getInstance(Gogo.class).go());
Assert.assertEquals("B", injector.getInstance(Key.get(Gogo.class, Names.named("reverse"))).go());
// test default property value
Assert.assertEquals("B", injector.getInstance(GogoSally.class).go());
props.setProperty("sally", "a");
Assert.assertEquals("A", injector.getInstance(GogoSally.class).go());
props.setProperty("sally", "b");
Assert.assertEquals("B", injector.getInstance(GogoSally.class).go());
props.setProperty("sally", "c");
try {
injector.getInstance(GogoSally.class).go();
// should never be reached
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof ProvisionException);
Assert.assertTrue(e.getMessage().contains("Unknown provider[c] of Key[type=io.druid.guice.PolyBindTest$GogoSally"));
}
}
use of com.google.inject.multibindings.MapBinder in project cdap by caskdata.
the class JobQueueDatasetTest method beforeClass.
@BeforeClass
public static void beforeClass() throws IOException, DatasetManagementException {
CConfiguration cConf = CConfiguration.create();
cConf.set(Constants.CFG_LOCAL_DATA_DIR, TEMP_FOLDER.newFolder().getAbsolutePath());
txManager = new TransactionManager(new Configuration());
txManager.startAndWait();
Injector injector = Guice.createInjector(new ConfigModule(cConf), new LocationRuntimeModule().getInMemoryModules(), new SystemDatasetRuntimeModule().getInMemoryModules(), new AbstractModule() {
@Override
protected void configure() {
// Add the app-fabric Dataset module, which is where the JobQueueDataset is defined
MapBinder<String, DatasetModule> datasetModuleBinder = MapBinder.newMapBinder(binder(), String.class, DatasetModule.class, Constants.Dataset.Manager.DefaultDatasetModules.class);
datasetModuleBinder.addBinding("app-fabric").toInstance(new AppFabricDatasetModule());
install(new FactoryModuleBuilder().implement(DatasetDefinitionRegistry.class, DefaultDatasetDefinitionRegistry.class).build(DatasetDefinitionRegistryFactory.class));
bind(DatasetFramework.class).to(InMemoryDatasetFramework.class);
bind(NamespaceQueryAdmin.class).to(InMemoryNamespaceClient.class).in(Scopes.SINGLETON);
}
});
DatasetFramework datasetFramework = injector.getInstance(DatasetFramework.class);
DatasetsUtil.createIfNotExists(datasetFramework, Schedulers.JOB_QUEUE_DATASET_ID, JobQueueDataset.class.getName(), DatasetProperties.EMPTY);
jobQueue = datasetFramework.getDataset(Schedulers.JOB_QUEUE_DATASET_ID, new HashMap<String, String>(), null);
txExecutor = new DynamicTransactionExecutorFactory(new InMemoryTxSystemClient(txManager)).createExecutor(Collections.<TransactionAware>singleton(jobQueue));
}
use of com.google.inject.multibindings.MapBinder in project guice by google.
the class MapBinderTest method testMultibinderDependenciesInToolStage.
/**
* Check that the dependencies are correct in the Tool Stage.
*/
public void testMultibinderDependenciesInToolStage() {
Injector injector = Guice.createInjector(Stage.TOOL, new AbstractModule() {
@Override
protected void configure() {
MapBinder<Integer, String> mapBinder = MapBinder.newMapBinder(binder(), Integer.class, String.class);
mapBinder.addBinding(1).toInstance("A");
mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b")));
bindConstant().annotatedWith(Names.named("b")).to("B");
}
});
Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {
});
HasDependencies withDependencies = (HasDependencies) binding;
Set<Dependency<?>> actualDependencies = withDependencies.getDependencies();
// We expect two dependencies, because the dependencies are annotated with
// Element, which has a uniqueId, it's difficult to directly compare them.
// Instead we will manually compare all the fields except the uniqueId
assertEquals(2, actualDependencies.size());
for (Dependency<?> dependency : actualDependencies) {
Key<?> key = dependency.getKey();
assertEquals(new TypeLiteral<String>() {
}, key.getTypeLiteral());
Annotation annotation = dependency.getKey().getAnnotation();
assertTrue(annotation instanceof Element);
Element element = (Element) annotation;
assertEquals("", element.setName());
assertEquals(Element.Type.MAPBINDER, element.type());
assertEquals("java.lang.Integer", element.keyType());
}
}
use of com.google.inject.multibindings.MapBinder in project guice by google.
the class MapBinderTest method testMapBinderMatching.
@Marker
public void testMapBinderMatching() throws Exception {
Method m = MapBinderTest.class.getDeclaredMethod("testMapBinderMatching");
assertNotNull(m);
final Annotation marker = m.getAnnotation(Marker.class);
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
MapBinder<Integer, Integer> mb1 = MapBinder.newMapBinder(binder(), Integer.class, Integer.class, Marker.class);
MapBinder<Integer, Integer> mb2 = MapBinder.newMapBinder(binder(), Integer.class, Integer.class, marker);
mb1.addBinding(1).toInstance(1);
mb2.addBinding(2).toInstance(2);
// This assures us that the two binders are equivalent, so we expect the instance added to
// each to have been added to one set.
assertEquals(mb1, mb2);
}
});
TypeLiteral<Map<Integer, Integer>> t = new TypeLiteral<Map<Integer, Integer>>() {
};
Map<Integer, Integer> s1 = injector.getInstance(Key.get(t, Marker.class));
Map<Integer, Integer> s2 = injector.getInstance(Key.get(t, marker));
// This assures us that the two sets are in fact equal. They may not be same set (as in Java
// object identical), but we shouldn't expect that, since probably Guice creates the set each
// time in case the elements are dependent on scope.
assertEquals(s1, s2);
// This ensures that MultiBinder is internally using the correct set name --
// making sure that instances of marker annotations have the same set name as
// MarkerAnnotation.class.
Map<Integer, Integer> expected = new HashMap<>();
expected.put(1, 1);
expected.put(2, 2);
assertEquals(expected, s1);
}
use of com.google.inject.multibindings.MapBinder in project cdap by caskdata.
the class DefaultPreviewManager method createPreviewInjector.
/**
* Create injector for the given application id.
*/
@VisibleForTesting
Injector createPreviewInjector(ApplicationId applicationId) throws IOException {
CConfiguration previewCConf = CConfiguration.copy(cConf);
java.nio.file.Path previewDirPath = Paths.get(cConf.get(Constants.CFG_LOCAL_DATA_DIR), "preview").toAbsolutePath();
Files.createDirectories(previewDirPath);
java.nio.file.Path previewDir = Files.createDirectories(Paths.get(previewDirPath.toAbsolutePath().toString(), applicationId.getApplication()));
previewCConf.set(Constants.CFG_LOCAL_DATA_DIR, previewDir.toString());
Configuration previewHConf = new Configuration(hConf);
previewHConf.set(Constants.CFG_LOCAL_DATA_DIR, previewDir.toString());
previewCConf.setIfUnset(Constants.CFG_DATA_LEVELDB_DIR, previewDir.toString());
previewCConf.setBoolean(Constants.Explore.EXPLORE_ENABLED, false);
return Guice.createInjector(new ConfigModule(previewCConf, previewHConf), new IOModule(), new AuthenticationContextModules().getMasterModule(), new SecurityModules().getStandaloneModules(), new PreviewSecureStoreModule(secureStore), new PreviewStreamAdminModule(streamAdmin), new PreviewDiscoveryRuntimeModule(discoveryService), new LocationRuntimeModule().getStandaloneModules(), new ConfigStoreModule().getStandaloneModule(), new PreviewRunnerModule(artifactRepository, artifactStore, authorizerInstantiator, authorizationEnforcer, privilegesManager, streamCoordinatorClient, preferencesStore), new ProgramRunnerRuntimeModule().getStandaloneModules(), new PreviewDataModules().getDataFabricModule(transactionManager), new PreviewDataModules().getDataSetsModule(datasetFramework), new DataSetServiceModules().getStandaloneModules(), new MetricsClientRuntimeModule().getStandaloneModules(), new LoggingModules().getStandaloneModules(), new NamespaceStoreModule().getStandaloneModules(), new MessagingServerRuntimeModule().getInMemoryModules(), new AbstractModule() {
@Override
protected void configure() {
// Bind system datasets defined in App-fabric.
// Have to do it here as public binding, instead of inside PreviewRunnerModule due to Guice 3
// doesn't support exporting multi-binder from private module
MapBinder<String, DatasetModule> datasetModuleBinder = MapBinder.newMapBinder(binder(), String.class, DatasetModule.class, Constants.Dataset.Manager.DefaultDatasetModules.class);
datasetModuleBinder.addBinding("app-fabric").toInstance(new AppFabricDatasetModule());
bind(ArtifactRepository.class).annotatedWith(Names.named(AppFabricServiceRuntimeModule.NOAUTH_ARTIFACT_REPO)).to(DefaultArtifactRepository.class).in(Scopes.SINGLETON);
}
@Provides
@Named(Constants.Service.MASTER_SERVICES_BIND_ADDRESS)
@SuppressWarnings("unused")
public InetAddress providesHostname(CConfiguration cConf) {
String address = cConf.get(Constants.Preview.ADDRESS);
return Networks.resolve(address, new InetSocketAddress("localhost", 0).getAddress());
}
});
}
Aggregations