use of com.google.inject.Binding in project guice by google.
the class MapBinderTest method testGetEntriesWithDuplicateKeys.
@SuppressWarnings("rawtypes")
public void testGetEntriesWithDuplicateKeys() {
// Set up the module
Module module = new AbstractModule() {
@Override
protected void configure() {
MapBinder<String, String> mapBinder = MapBinder.newMapBinder(binder(), String.class, String.class);
mapBinder.addBinding("A").toInstance("a1");
mapBinder.addBinding("A").toInstance("a2");
mapBinder.permitDuplicates();
}
};
// Get the MapBinderBinding
List<com.google.inject.spi.Element> elements = Elements.getElements(module);
MapBinderBinding<?> mapBinderBinding = getMapBinderBinding(elements);
// Execute the call to getEntries
List<Map.Entry<?, Binding<?>>> mapEntries = mapBinderBinding.getEntries(elements);
// Assert on the results
Map.Entry<?, Binding<?>> firstEntry = mapEntries.get(0);
assertEquals("A", firstEntry.getKey());
Binding<?> firstBinding = firstEntry.getValue();
assertEquals("a1", ((InstanceBinding) firstBinding).getInstance());
Map.Entry<?, Binding<?>> secondEntry = mapEntries.get(1);
assertEquals("A", secondEntry.getKey());
Binding<?> secondBinding = secondEntry.getValue();
assertEquals("a2", ((InstanceBinding) secondBinding).getInstance());
}
use of com.google.inject.Binding in project guice by google.
the class MapBinderTest method testTwoMapBindersAreDistinct.
public void testTwoMapBindersAreDistinct() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
MapBinder.newMapBinder(binder(), String.class, String.class).addBinding("A").toInstance("a");
MapBinder.newMapBinder(binder(), Integer.class, String.class).addBinding(1).toInstance("b");
}
});
Collector collector = new Collector();
Binding<Map<String, String>> map1 = injector.getBinding(Key.get(mapOfString));
map1.acceptTargetVisitor(collector);
assertNotNull(collector.mapbinding);
MapBinderBinding<?> map1Binding = collector.mapbinding;
Binding<Map<Integer, String>> map2 = injector.getBinding(Key.get(mapOfIntString));
map2.acceptTargetVisitor(collector);
assertNotNull(collector.mapbinding);
MapBinderBinding<?> map2Binding = collector.mapbinding;
List<Binding<String>> bindings = injector.findBindingsByType(stringType);
assertEquals("should have two elements: " + bindings, 2, bindings.size());
Binding<String> a = bindings.get(0);
Binding<String> b = bindings.get(1);
assertEquals("a", ((InstanceBinding<String>) a).getInstance());
assertEquals("b", ((InstanceBinding<String>) b).getInstance());
// Make sure the correct elements belong to their own sets.
assertTrue(map1Binding.containsElement(a));
assertFalse(map1Binding.containsElement(b));
assertFalse(map2Binding.containsElement(a));
assertTrue(map2Binding.containsElement(b));
}
use of com.google.inject.Binding in project guice by google.
the class MapBinderTest method testGetEntriesWithDuplicateValues.
@SuppressWarnings("rawtypes")
public void testGetEntriesWithDuplicateValues() {
// Set up the module
Module module = new AbstractModule() {
@Override
protected void configure() {
MapBinder<String, String> mapBinder = MapBinder.newMapBinder(binder(), String.class, String.class);
mapBinder.addBinding("A").toInstance("a");
mapBinder.addBinding("A").toInstance("a");
}
};
// Get the MapBinderBinding
List<com.google.inject.spi.Element> elements = Elements.getElements(module);
MapBinderBinding<?> mapBinderBinding = getMapBinderBinding(elements);
// Execute the call to getEntries
List<Map.Entry<?, Binding<?>>> mapEntries = mapBinderBinding.getEntries(elements);
// Assert on the results
Map.Entry<?, Binding<?>> firstEntry = mapEntries.get(0);
assertEquals("A", firstEntry.getKey());
Binding<?> firstBinding = firstEntry.getValue();
assertEquals("a", ((InstanceBinding) firstBinding).getInstance());
Map.Entry<?, Binding<?>> secondEntry = mapEntries.get(1);
assertEquals("A", secondEntry.getKey());
Binding<?> secondBinding = secondEntry.getValue();
assertEquals("a", ((InstanceBinding) secondBinding).getInstance());
}
use of com.google.inject.Binding in project guice by google.
the class OptionalBinderTest method testKeyHashCodesFixedAtInjectionTime.
/** Ensure key hash codes are fixed at injection time, not binding time. */
public void testKeyHashCodesFixedAtInjectionTime() {
Module m = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder<List<String>> b = OptionalBinder.newOptionalBinder(binder(), listOfStrings);
List<String> list = Lists.newArrayList();
b.setDefault().toInstance(list);
b.setBinding().toInstance(list);
list.add("A");
list.add("B");
}
};
Injector injector = Guice.createInjector(m);
for (Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
Key<?> bindingKey = entry.getKey();
Key<?> clonedKey;
if (bindingKey.getAnnotation() != null) {
clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotation());
} else if (bindingKey.getAnnotationType() != null) {
clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotationType());
} else {
clonedKey = Key.get(bindingKey.getTypeLiteral());
}
assertEquals(bindingKey, clonedKey);
assertEquals("Incorrect hashcode for " + bindingKey + " -> " + entry.getValue(), bindingKey.hashCode(), clonedKey.hashCode());
}
}
use of com.google.inject.Binding in project camel by apache.
the class Main method getCamelContextMap.
protected Map<String, CamelContext> getCamelContextMap() {
Map<String, CamelContext> answer = Maps.newHashMap();
if (injector != null) {
Set<Map.Entry<Key<?>, Binding<?>>> entries = injector.getBindings().entrySet();
for (Map.Entry<Key<?>, Binding<?>> entry : entries) {
Key<?> key = entry.getKey();
Class<?> keyType = Injectors.getKeyType(key);
if (keyType != null && CamelContext.class.isAssignableFrom(keyType)) {
Binding<?> binding = entry.getValue();
Object value = binding.getProvider().get();
if (value != null) {
CamelContext castValue = CamelContext.class.cast(value);
answer.put(key.toString(), castValue);
}
}
}
}
return answer;
}
Aggregations