use of com.google.inject.Provider in project roboguice by roboguice.
the class SpiBindingsTest method testProviderBinding.
public void testProviderBinding() {
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bind(String.class).toInstance("A");
}
});
Key<Provider<String>> providerOfStringKey = new Key<Provider<String>>() {
};
Binding<Provider<String>> binding = injector.getBinding(providerOfStringKey);
assertEquals(providerOfStringKey, binding.getKey());
checkBindingSource(binding);
assertTrue(binding instanceof ProviderBinding);
binding.acceptTargetVisitor(new FailingTargetVisitor<Provider<String>>() {
@Override
public Void visit(ProviderBinding<? extends Provider<String>> binding) {
assertEquals(Key.get(String.class), binding.getProvidedKey());
return null;
}
});
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class OverrideModuleTest method testOverrideScopeAnnotation.
public void testOverrideScopeAnnotation() {
final Scope scope = new Scope() {
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
throw new AssertionError("Should not be called");
}
};
final SingleUseScope replacementScope = new SingleUseScope();
Module original = new AbstractModule() {
@Override
protected void configure() {
bindScope(TestScopeAnnotation.class, scope);
bind(Date.class).in(TestScopeAnnotation.class);
}
};
Module replacements = new AbstractModule() {
@Override
protected void configure() {
bindScope(TestScopeAnnotation.class, replacementScope);
}
};
Injector injector = createInjector(Modules.override(original).with(replacements));
injector.getInstance(Date.class);
assertTrue(replacementScope.used);
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class InjectorSpiTest method testExistingBinding.
public void testExistingBinding() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Foo.class);
bind(Baz.class);
}
});
// Sanity check -- ensure we return the proper binding for all existing bindings.
for (Map.Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
assertSame(entry.getValue(), injector.getExistingBinding(entry.getKey()));
}
// Now run through specifics...
Binding<?> binding;
// 1) non-Provider Foo.class
binding = injector.getExistingBinding(Key.get(Foo.class));
assertNotNull(binding);
assertEquals(Foo.class, binding.getKey().getTypeLiteral().getRawType());
// 2) Provider<Foo> class (should already exist, because Baz @Injects it).
// the assertTrue is a bit stricter than necessary, but makes sure this works for pre-existing Provider bindings
assertTrue(injector.getAllBindings().containsKey(Key.get(new TypeLiteral<Provider<Foo>>() {
})));
binding = injector.getExistingBinding(Key.get(new TypeLiteral<Provider<Foo>>() {
}));
assertNotNull(binding);
assertEquals(Provider.class, binding.getKey().getTypeLiteral().getRawType());
assertEquals(Foo.class, ((Provider) binding.getProvider().get()).get().getClass());
// 3) non-Provider Baz.class
binding = injector.getExistingBinding(Key.get(Baz.class));
assertNotNull(binding);
assertEquals(Baz.class, binding.getKey().getTypeLiteral().getRawType());
// 4) Provider<Baz> class (should not already exist, because nothing used it yet).
// the assertFalse is a bit stricter than necessary, but makes sure this works for non-pre-existing Provider bindings
assertFalse(injector.getAllBindings().containsKey(Key.get(new TypeLiteral<Provider<Baz>>() {
})));
binding = injector.getExistingBinding(Key.get(new TypeLiteral<Provider<Baz>>() {
}));
assertNotNull(binding);
assertEquals(Provider.class, binding.getKey().getTypeLiteral().getRawType());
assertEquals(Baz.class, ((Provider) binding.getProvider().get()).get().getClass());
// 5) non-Provider Bar, doesn't exist.
assertNull(injector.getExistingBinding(Key.get(Bar.class)));
// 6) Provider Bar, doesn't exist.
assertNull(injector.getExistingBinding(Key.get(new TypeLiteral<Provider<Bar>>() {
})));
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class OverrideModuleTest method testFailsIfOverridenScopeInstanceHasBeenUsed.
public void testFailsIfOverridenScopeInstanceHasBeenUsed() {
final Scope scope = new Scope() {
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
return unscoped;
}
@Override
public String toString() {
return "ORIGINAL SCOPE";
}
};
final Module original = new AbstractModule() {
@Override
protected void configure() {
bindScope(TestScopeAnnotation.class, scope);
bind(Date.class).in(scope);
bind(String.class).in(scope);
}
};
Module originalWrapper = new AbstractModule() {
@Override
protected void configure() {
install(original);
}
};
Module replacements = new AbstractModule() {
@Override
protected void configure() {
bindScope(TestScopeAnnotation.class, new SingleUseScope());
}
};
try {
createInjector(Modules.override(originalWrapper).with(replacements));
fail("Exception expected");
} catch (CreationException e) {
assertContains(e.getMessage(), "1) The scope for @TestScopeAnnotation is bound directly and cannot be overridden.", "original binding at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "at ", replacements.getClass().getName() + ".configure(", asModuleChain(Modules.OverrideModule.class, replacements.getClass()));
}
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class ContextScope method scope.
public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) {
return new Provider<T>() {
public T get() {
synchronized (ContextScope.class) {
final Stack<WeakReference<Context>> stack = getContextStack();
// The context should never be finalized as long as the provider is still in memory
final Context context = stack.peek().get();
final Map<Key<?>, Object> objectsForScope = getScopedObjectMap(context);
if (objectsForScope == null)
// May want to consider throwing an exception here (if provider is used after onDestroy())
return null;
@SuppressWarnings({ "unchecked" }) T current = (T) objectsForScope.get(key);
if (current == null && !objectsForScope.containsKey(key)) {
current = unscoped.get();
objectsForScope.put(key, current);
}
return current;
}
}
};
}
Aggregations