use of com.google.inject.Guice.createInjector in project roboguice by roboguice.
the class OverrideModuleTest method testOverrideIsLazy.
public void testOverrideIsLazy() {
final AtomicReference<String> value = new AtomicReference<String>("A");
Module overridden = Modules.override(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).annotatedWith(named("original")).toInstance(value.get());
}
}).with(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).annotatedWith(named("override")).toInstance(value.get());
}
});
// the value.get() call should be deferred until Guice.createInjector
value.set("B");
Injector injector = Guice.createInjector(overridden);
assertEquals("B", injector.getInstance(Key.get(String.class, named("original"))));
assertEquals("B", injector.getInstance(Key.get(String.class, named("override"))));
}
use of com.google.inject.Guice.createInjector in project roboguice by roboguice.
the class OverrideModuleTest method testOverrideDeepExpose.
public void testOverrideDeepExpose() {
final AtomicReference<Provider<Character>> charAProvider = new AtomicReference<Provider<Character>>();
Module exposes5 = new PrivateModule() {
@Override
protected void configure() {
install(new PrivateModule() {
@Override
protected void configure() {
bind(Integer.class).toInstance(5);
expose(Integer.class);
charAProvider.set(getProvider(Character.class));
bind(Character.class).toInstance('A');
}
});
expose(Integer.class);
}
};
Injector injector = Guice.createInjector(Modules.override(exposes5).with(EMPTY_MODULE));
assertEquals(5, injector.getInstance(Integer.class).intValue());
assertEquals('A', charAProvider.getAndSet(null).get().charValue());
injector = Guice.createInjector(Modules.override(EMPTY_MODULE).with(exposes5));
assertEquals(5, injector.getInstance(Integer.class).intValue());
assertEquals('A', charAProvider.getAndSet(null).get().charValue());
final AtomicReference<Provider<Character>> charBProvider = new AtomicReference<Provider<Character>>();
Module binds15 = new AbstractModule() {
@Override
protected void configure() {
bind(Integer.class).toInstance(15);
install(new PrivateModule() {
@Override
protected void configure() {
charBProvider.set(getProvider(Character.class));
bind(Character.class).toInstance('B');
}
});
}
};
injector = Guice.createInjector(Modules.override(binds15).with(exposes5));
assertEquals(5, injector.getInstance(Integer.class).intValue());
assertEquals('A', charAProvider.getAndSet(null).get().charValue());
assertEquals('B', charBProvider.getAndSet(null).get().charValue());
injector = Guice.createInjector(Modules.override(exposes5).with(binds15));
assertEquals(15, injector.getInstance(Integer.class).intValue());
assertEquals('A', charAProvider.getAndSet(null).get().charValue());
assertEquals('B', charBProvider.getAndSet(null).get().charValue());
}
use of com.google.inject.Guice.createInjector in project roboguice by roboguice.
the class OverrideModuleTest method testPrivateBindingOverride.
public void testPrivateBindingOverride() throws Exception {
Injector inj = Guice.createInjector(Modules.override(new ExampleModule()).with(new AbstractModule() {
@Override
protected void configure() {
bind(INPUT_KEY).toInstance(OVERRIDDEN_INPUT);
}
}));
assertEquals(inj.getInstance(RESULT_KEY), OVERRIDDEN_RESULT);
}
Aggregations