use of com.google.inject.Binding in project roboguice by roboguice.
the class ExtensionSpiTest method testSpiOnElements.
public final void testSpiOnElements() throws Exception {
AssistedInjectSpiVisitor visitor = new AssistedInjectSpiVisitor();
Integer count = 0;
for (Element element : Elements.getElements(new Module())) {
if (element instanceof Binding) {
assertEquals(count++, ((Binding<?>) element).acceptTargetVisitor(visitor));
}
}
validateVisitor(visitor);
}
use of com.google.inject.Binding in project roboguice by roboguice.
the class FactoryModuleBuilderTest method testFactoryBindingDependencies.
public void testFactoryBindingDependencies() {
// validate dependencies work in all stages & as a raw element,
// and that dependencies work for methods, fields, constructors,
// and for @AssistedInject constructors too.
Module module = new AbstractModule() {
@Override
protected void configure() {
bind(Integer.class).toInstance(42);
bind(Double.class).toInstance(4.2d);
bind(Float.class).toInstance(4.2f);
bind(String.class).annotatedWith(named("dog")).toInstance("dog");
bind(String.class).annotatedWith(named("cat1")).toInstance("cat1");
bind(String.class).annotatedWith(named("cat2")).toInstance("cat2");
bind(String.class).annotatedWith(named("cat3")).toInstance("cat3");
bind(String.class).annotatedWith(named("arbitrary")).toInstance("fail!");
install(new FactoryModuleBuilder().implement(Animal.class, Dog.class).build(AnimalHouse.class));
}
};
Set<Key<?>> expectedKeys = ImmutableSet.<Key<?>>of(Key.get(Integer.class), Key.get(Double.class), Key.get(Float.class), Key.get(String.class, named("dog")), Key.get(String.class, named("cat1")), Key.get(String.class, named("cat2")), Key.get(String.class, named("cat3")));
Injector injector = Guice.createInjector(module);
validateDependencies(expectedKeys, injector.getBinding(AnimalHouse.class));
injector = Guice.createInjector(Stage.TOOL, module);
validateDependencies(expectedKeys, injector.getBinding(AnimalHouse.class));
List<Element> elements = Elements.getElements(module);
boolean found = false;
for (Element element : elements) {
if (element instanceof Binding) {
Binding<?> binding = (Binding<?>) element;
if (binding.getKey().equals(Key.get(AnimalHouse.class))) {
found = true;
validateDependencies(expectedKeys, binding);
break;
}
}
}
assertTrue(found);
}
use of com.google.inject.Binding 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.Binding in project roboguice by roboguice.
the class ModuleRewriterTest method testRewriteBindings.
public void testRewriteBindings() {
// create a module the binds String.class and CharSequence.class
Module module = new AbstractModule() {
protected void configure() {
bind(String.class).toInstance("Pizza");
bind(CharSequence.class).toInstance("Wine");
}
};
// record the elements from that module
List<Element> elements = Elements.getElements(module);
// create a rewriter that rewrites the binding to 'Wine' with a binding to 'Beer'
List<Element> rewritten = Lists.newArrayList();
for (Element element : elements) {
element = element.acceptVisitor(new DefaultElementVisitor<Element>() {
@Override
public <T> Element visit(Binding<T> binding) {
T target = binding.acceptTargetVisitor(Elements.<T>getInstanceVisitor());
if ("Wine".equals(target)) {
return null;
} else {
return binding;
}
}
});
if (element != null) {
rewritten.add(element);
}
}
// create a module from the original list of elements and the rewriter
Module rewrittenModule = Elements.getModule(rewritten);
// the wine binding is dropped
Injector injector = Guice.createInjector(rewrittenModule);
try {
injector.getInstance(CharSequence.class);
fail();
} catch (ConfigurationException expected) {
}
}
use of com.google.inject.Binding in project roboguice by roboguice.
the class SpiUtils method optionalModuleTest.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> void optionalModuleTest(Key<T> keyType, Iterable<? extends Module> modules, int expectedOtherOptionalBindings, BindResult<?> expectedDefault, BindResult<?> expectedActual, BindResult<?> expectedUserLinkedActual) {
if (expectedUserLinkedActual != null) {
assertNull("cannot have actual if expecting user binding", expectedActual);
assertNull("cannot have default if expecting user binding", expectedDefault);
}
Set<Element> elements = ImmutableSet.copyOf(Elements.getElements(modules));
Map<Key<?>, Binding<?>> indexed = index(elements);
Key<Optional<T>> optionalKey = keyType.ofType(OptionalBinder.optionalOf(keyType.getTypeLiteral()));
Key<?> javaOptionalKey = HAS_JAVA_OPTIONAL ? keyType.ofType(OptionalBinder.javaOptionalOf(keyType.getTypeLiteral())) : null;
Visitor visitor = new Visitor();
OptionalBinderBinding<Optional<T>> optionalBinder = null;
OptionalBinderBinding<?> javaOptionalBinder = null;
Key<?> defaultKey = null;
Key<?> actualKey = null;
Binding optionalBinding = indexed.get(optionalKey);
optionalBinder = (OptionalBinderBinding<Optional<T>>) optionalBinding.acceptTargetVisitor(visitor);
if (HAS_JAVA_OPTIONAL) {
Binding javaOptionalBinding = indexed.get(javaOptionalKey);
javaOptionalBinder = (OptionalBinderBinding) javaOptionalBinding.acceptTargetVisitor(visitor);
}
// Locate the defaultKey & actualKey
for (Element element : elements) {
if (optionalBinder.containsElement(element) && element instanceof Binding) {
Binding binding = (Binding) element;
if (isSourceEntry(binding, Source.DEFAULT)) {
defaultKey = binding.getKey();
} else if (isSourceEntry(binding, Source.ACTUAL)) {
actualKey = binding.getKey();
}
}
}
assertNotNull(optionalBinder);
if (HAS_JAVA_OPTIONAL) {
assertNotNull(javaOptionalBinder);
}
assertEquals(expectedDefault == null, defaultKey == null);
assertEquals(expectedActual == null, actualKey == null);
Key<Optional<javax.inject.Provider<T>>> optionalJavaxProviderKey = keyType.ofType(optionalOfJavaxProvider(keyType.getTypeLiteral()));
Key<?> javaOptionalJavaxProviderKey = HAS_JAVA_OPTIONAL ? keyType.ofType(javaOptionalOfJavaxProvider(keyType.getTypeLiteral())) : null;
Key<Optional<Provider<T>>> optionalProviderKey = keyType.ofType(optionalOfProvider(keyType.getTypeLiteral()));
Key<?> javaOptionalProviderKey = HAS_JAVA_OPTIONAL ? keyType.ofType(javaOptionalOfProvider(keyType.getTypeLiteral())) : null;
boolean keyMatch = false;
boolean optionalKeyMatch = false;
boolean javaOptionalKeyMatch = false;
boolean optionalJavaxProviderKeyMatch = false;
boolean javaOptionalJavaxProviderKeyMatch = false;
boolean optionalProviderKeyMatch = false;
boolean javaOptionalProviderKeyMatch = false;
boolean defaultMatch = false;
boolean actualMatch = false;
List<Object> otherOptionalElements = Lists.newArrayList();
List<Element> otherContains = Lists.newArrayList();
List<Element> nonContainedElements = Lists.newArrayList();
for (Element element : elements) {
boolean contains = optionalBinder.containsElement(element);
if (HAS_JAVA_OPTIONAL) {
assertEquals(contains, javaOptionalBinder.containsElement(element));
}
if (!contains) {
nonContainedElements.add(element);
}
Key key = null;
Binding b = null;
if (element instanceof Binding) {
b = (Binding) element;
key = b.getKey();
Object visited = b.acceptTargetVisitor(visitor);
if (visited instanceof OptionalBinderBinding) {
if (visited.equals(optionalBinder)) {
assertTrue(contains);
} else if (HAS_JAVA_OPTIONAL && visited.equals(javaOptionalBinder)) {
assertTrue(contains);
} else {
otherOptionalElements.add(visited);
}
}
} else if (element instanceof ProviderLookup) {
key = ((ProviderLookup) element).getKey();
}
if (key != null && key.equals(keyType)) {
// keyType might match because a user bound it
// (which is possible in a purely absent OptionalBinder)
assertEquals(expectedDefault != null || expectedActual != null, contains);
if (contains) {
keyMatch = true;
}
} else if (key != null && key.equals(optionalKey)) {
assertTrue(contains);
optionalKeyMatch = true;
} else if (key != null && key.equals(javaOptionalKey)) {
assertTrue(contains);
javaOptionalKeyMatch = true;
} else if (key != null && key.equals(optionalJavaxProviderKey)) {
assertTrue(contains);
optionalJavaxProviderKeyMatch = true;
} else if (key != null && key.equals(javaOptionalJavaxProviderKey)) {
assertTrue(contains);
javaOptionalJavaxProviderKeyMatch = true;
} else if (key != null && key.equals(optionalProviderKey)) {
assertTrue(contains);
optionalProviderKeyMatch = true;
} else if (key != null && key.equals(javaOptionalProviderKey)) {
assertTrue(contains);
javaOptionalProviderKeyMatch = true;
} else if (key != null && key.equals(defaultKey)) {
assertTrue(contains);
if (b != null) {
// otherwise it might just be a ProviderLookup into it
assertTrue("expected: " + expectedDefault + ", but was: " + b, matches(b, expectedDefault));
defaultMatch = true;
}
} else if (key != null && key.equals(actualKey)) {
assertTrue(contains);
if (b != null) {
// otherwise it might just be a ProviderLookup into it
assertTrue("expected: " + expectedActual + ", but was: " + b, matches(b, expectedActual));
actualMatch = true;
}
} else if (contains) {
otherContains.add(element);
}
}
// only expect a keymatch if either default or actual are set
assertEquals(expectedDefault != null || expectedActual != null, keyMatch);
assertTrue(optionalKeyMatch);
assertTrue(optionalJavaxProviderKeyMatch);
assertTrue(optionalProviderKeyMatch);
assertEquals(HAS_JAVA_OPTIONAL, javaOptionalKeyMatch);
assertEquals(HAS_JAVA_OPTIONAL, javaOptionalJavaxProviderKeyMatch);
assertEquals(HAS_JAVA_OPTIONAL, javaOptionalProviderKeyMatch);
assertEquals(expectedDefault != null, defaultMatch);
assertEquals(expectedActual != null, actualMatch);
assertEquals(otherContains.toString(), 0, otherContains.size());
assertEquals("other OptionalBindings found: " + otherOptionalElements, expectedOtherOptionalBindings, otherOptionalElements.size());
// Validate that we can construct an injector out of the remaining bindings.
Guice.createInjector(Elements.getModule(nonContainedElements));
}
Aggregations