Search in sources :

Example 11 with MapBinder

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

the class PolyBindTest method testSanity.

@Test
public void testSanity() {
    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");
    try {
        Assert.assertEquals("A", injector.getInstance(Gogo.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=org.apache.druid.guice.PolyBindTest$Gogo"));
    }
    try {
        Assert.assertEquals("B", injector.getInstance(Key.get(Gogo.class, Names.named("reverse"))).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=org.apache.druid.guice.PolyBindTest$Gogo"));
    }
    // 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=org.apache.druid.guice.PolyBindTest$GogoSally"));
    }
}
Also used : Binder(com.google.inject.Binder) MapBinder(com.google.inject.multibindings.MapBinder) ProvisionException(com.google.inject.ProvisionException) MapBinder(com.google.inject.multibindings.MapBinder) Module(com.google.inject.Module) ProvisionException(com.google.inject.ProvisionException) Test(org.junit.Test)

Aggregations

MapBinder (com.google.inject.multibindings.MapBinder)11 AbstractModule (com.google.inject.AbstractModule)8 Injector (com.google.inject.Injector)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 Binder (com.google.inject.Binder)3 DatasetModule (co.cask.cdap.api.dataset.module.DatasetModule)2 CConfiguration (co.cask.cdap.common.conf.CConfiguration)2 ConfigModule (co.cask.cdap.common.guice.ConfigModule)2 LocationRuntimeModule (co.cask.cdap.common.guice.LocationRuntimeModule)2 AppFabricDatasetModule (co.cask.cdap.internal.app.AppFabricDatasetModule)2 BindingAnnotation (com.google.inject.BindingAnnotation)2 Provides (com.google.inject.Provides)2 Annotation (java.lang.annotation.Annotation)2 Set (java.util.Set)2 DatasetDefinitionRegistry (co.cask.cdap.api.dataset.module.DatasetDefinitionRegistry)1 ProgramRunnerRuntimeModule (co.cask.cdap.app.guice.ProgramRunnerRuntimeModule)1 PreviewRunnerModule (co.cask.cdap.app.preview.PreviewRunnerModule)1 IOModule (co.cask.cdap.common.guice.IOModule)1