bug fix @DruidSecondaryModule plus unit test

This commit is contained in:
Slim Bouguerra 2015-03-26 10:23:53 -05:00
parent 9fd14cad4f
commit 1e6be7796e
2 changed files with 80 additions and 7 deletions

View File

@ -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

View File

@ -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<CustomEmitter> {
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;
}
}
}