From 1e6be7796e5bd169cab52fc8cbb1613cb14a4164 Mon Sep 17 00:00:00 2001 From: Slim Bouguerra Date: Thu, 26 Mar 2015 10:23:53 -0500 Subject: [PATCH] bug fix @DruidSecondaryModule plus unit test --- .../io/druid/guice/DruidSecondaryModule.java | 10 +-- .../io/druid/guice/GuiceInjectorsTest.java | 77 +++++++++++++++++++ 2 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 processing/src/test/java/io/druid/guice/GuiceInjectorsTest.java diff --git a/common/src/main/java/io/druid/guice/DruidSecondaryModule.java b/common/src/main/java/io/druid/guice/DruidSecondaryModule.java index b197bfbdc74..d1e4c6e4aea 100644 --- a/common/src/main/java/io/druid/guice/DruidSecondaryModule.java +++ b/common/src/main/java/io/druid/guice/DruidSecondaryModule.java @@ -43,7 +43,6 @@ public class DruidSecondaryModule implements Module private final ObjectMapper jsonMapper; private final ObjectMapper smileMapper; private final Validator validator; - private final JsonConfigurator jsonConfigurator; @Inject public DruidSecondaryModule( @@ -51,8 +50,7 @@ public class DruidSecondaryModule implements Module ConfigurationObjectFactory factory, @Json ObjectMapper jsonMapper, @Smile ObjectMapper smileMapper, - Validator validator, - JsonConfigurator jsonConfigurator + Validator validator ) { this.properties = properties; @@ -60,7 +58,6 @@ public class DruidSecondaryModule implements Module this.jsonMapper = jsonMapper; this.smileMapper = smileMapper; this.validator = validator; - this.jsonConfigurator = jsonConfigurator; } @Override @@ -69,10 +66,9 @@ public class DruidSecondaryModule implements Module binder.install(new DruidGuiceExtensions()); binder.bind(Properties.class).toInstance(properties); binder.bind(ConfigurationObjectFactory.class).toInstance(factory); - // make objectMapper eager to ensure jackson gets setup with guice injection for JsonConfigurator - binder.bind(ObjectMapper.class).to(Key.get(ObjectMapper.class, Json.class)).asEagerSingleton(); + binder.bind(ObjectMapper.class).to(Key.get(ObjectMapper.class, Json.class)); binder.bind(Validator.class).toInstance(validator); - binder.bind(JsonConfigurator.class).toInstance(jsonConfigurator); + binder.bind(JsonConfigurator.class); } @Provides @LazySingleton @Json diff --git a/processing/src/test/java/io/druid/guice/GuiceInjectorsTest.java b/processing/src/test/java/io/druid/guice/GuiceInjectorsTest.java new file mode 100644 index 00000000000..aaa89e65b98 --- /dev/null +++ b/processing/src/test/java/io/druid/guice/GuiceInjectorsTest.java @@ -0,0 +1,77 @@ +package io.druid.guice; + +import com.fasterxml.jackson.annotation.JacksonInject; +import com.google.inject.Binder; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Module; +import com.google.inject.Provider; +import org.junit.Assert; +import org.junit.Test; + +public class GuiceInjectorsTest +{ + @Test public void testSanity() + { + Injector stageOne = GuiceInjectors.makeStartupInjector(); + + Module module = stageOne.getInstance(DruidSecondaryModule.class); + + Injector stageTwo = Guice.createInjector(module, new Module() + { + @Override public void configure(Binder binder) + { + binder.bind(String.class).toInstance("Expected String"); + JsonConfigProvider.bind(binder, "druid.emitter.", Emitter.class); + binder.bind(CustomEmitter.class).toProvider(new CustomEmitterFactory()); + } + }); + + + CustomEmitter customEmitter = stageTwo.getInstance(CustomEmitter.class); + + Assert.assertEquals("Expected String", customEmitter.getOtherValue()); + } + + private static class Emitter { + + @JacksonInject + private String value; + + public String getValue() + { + return value; + } + } + + private class CustomEmitterFactory implements Provider { + + private Emitter emitter; + private Injector injector; + + @com.google.inject.Inject + public void configure(Injector injector) { + this.injector = injector; + emitter = injector.getInstance(Emitter.class); + } + + @Override public CustomEmitter get() + { + return new CustomEmitter(emitter); + } + } + + private static class CustomEmitter + { + public String getOtherValue() + { + return emitter.getValue(); + } + + private Emitter emitter; + + public CustomEmitter(Emitter emitter){ + this.emitter = emitter; + } + } +}