use of com.google.inject.Injector in project guice by google.
the class SpiUtils method setInjectorTest.
@SuppressWarnings("unchecked")
private static <T> void setInjectorTest(Key<Set<T>> setKey, TypeLiteral<?> elementType, Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings, BindResult... results) {
Key<?> collectionOfProvidersKey = setKey.ofType(collectionOfProvidersOf(elementType));
Key<?> collectionOfJavaxProvidersKey = setKey.ofType(collectionOfJavaxProvidersOf(elementType));
Injector injector = Guice.createInjector(modules);
Visitor<Set<T>> visitor = new Visitor<Set<T>>();
Binding<Set<T>> binding = injector.getBinding(setKey);
MultibinderBinding<Set<T>> multibinder = (MultibinderBinding<Set<T>>) binding.acceptTargetVisitor(visitor);
assertNotNull(multibinder);
assertEquals(elementType, multibinder.getElementTypeLiteral());
assertEquals(allowDuplicates, multibinder.permitsDuplicates());
List<Binding<?>> elements = Lists.newArrayList(multibinder.getElements());
List<BindResult> bindResults = Lists.newArrayList(results);
assertEquals("wrong bind elements, expected: " + bindResults + ", but was: " + multibinder.getElements(), bindResults.size(), elements.size());
for (BindResult result : bindResults) {
Binding found = null;
for (Binding item : elements) {
if (matches(item, result)) {
found = item;
break;
}
}
if (found == null) {
fail("Could not find element: " + result + " in remaining elements: " + elements);
} else {
elements.remove(found);
}
}
if (!elements.isEmpty()) {
fail("Found all elements of: " + bindResults + ", but more were left over: " + elements);
}
Set<Binding> setOfElements = new HashSet<Binding>(multibinder.getElements());
Set<IndexedBinding> setOfIndexed = Sets.newHashSet();
Indexer indexer = new Indexer(injector);
for (Binding<?> oneBinding : setOfElements) {
setOfIndexed.add(oneBinding.acceptTargetVisitor(indexer));
}
List<Object> otherMultibinders = Lists.newArrayList();
List<Binding> otherContains = Lists.newArrayList();
boolean collectionOfProvidersMatch = false;
boolean collectionOfJavaxProvidersMatch = false;
for (Binding b : injector.getAllBindings().values()) {
boolean contains = multibinder.containsElement(b);
Key key = b.getKey();
Object visited = b.acceptTargetVisitor(visitor);
if (visited != null) {
if (visited.equals(multibinder)) {
assertTrue(contains);
} else {
otherMultibinders.add(visited);
}
} else if (setOfElements.contains(b)) {
assertTrue(contains);
} else if (key.equals(collectionOfProvidersKey)) {
assertTrue(contains);
collectionOfProvidersMatch = true;
} else if (key.equals(collectionOfJavaxProvidersKey)) {
assertTrue(contains);
collectionOfJavaxProvidersMatch = true;
} else if (contains) {
if (!indexer.isIndexable(b) || !setOfIndexed.contains(b.acceptTargetVisitor(indexer))) {
otherContains.add(b);
}
}
}
assertTrue(collectionOfProvidersMatch);
assertTrue(collectionOfJavaxProvidersMatch);
if (allowDuplicates) {
assertEquals("contained more than it should: " + otherContains, 1, otherContains.size());
} else {
assertTrue("contained more than it should: " + otherContains, otherContains.isEmpty());
}
assertEquals("other multibindings found: " + otherMultibinders, otherMultibindings, otherMultibinders.size());
}
use of com.google.inject.Injector in project camel by apache.
the class InjectorManager method closeInjectors.
protected void closeInjectors() throws CloseFailedException {
CloseErrors errors = new CloseErrorsImpl(this);
Set<Entry<Object, Injector>> entries = injectors.entrySet();
for (Entry<Object, Injector> entry : entries) {
Injector injector = entry.getValue();
Injectors.close(injector, errors);
}
injectors.clear();
errors.throwIfNecessary();
}
use of com.google.inject.Injector in project camel by apache.
the class InjectorManager method beforeTest.
public void beforeTest(Object test) throws Exception {
Preconditions.checkNotNull(test, "test");
Class<? extends Object> testType = test.getClass();
moduleType = getModuleForTestClass(testType);
Injector classInjector = injectors.get(moduleType);
if (classInjector == null) {
classInjector = createInjector(moduleType);
Preconditions.checkNotNull(classInjector, "classInjector");
injectors.put(moduleType, classInjector);
}
injectors.put(testType, classInjector);
classInjector.injectMembers(test);
}
use of com.google.inject.Injector in project camel by apache.
the class CollectionOfCustomRoutesTest method xtestGuice.
public void xtestGuice() throws Exception {
Injector injector = Guice.createInjector(new MyModule());
CamelContext camelContext = injector.getInstance(CamelContext.class);
List<Route> list = camelContext.getRoutes();
assertEquals("size of " + list, 2, list.size());
GuiceTest.assertCamelContextRunningThenCloseInjector(injector);
}
use of com.google.inject.Injector in project camel by apache.
the class ComponentFoundInRegistryTest method testGuice.
@Test
public void testGuice() throws Exception {
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.PROVIDER_URL, GuiceInitialContextFactory.class.getName());
env.put(Injectors.MODULE_CLASS_NAMES, MyModule.class.getName());
InitialContext context = new InitialContext(env);
Injector injector = (Injector) context.lookup(Injector.class.getName());
assertNotNull("Found injector", injector);
Object value = context.lookup("foo");
assertNotNull("Should have found a value for foo!", value);
CamelContext camelContext = injector.getInstance(CamelContext.class);
Component component = camelContext.getComponent("foo");
assertThat(component, is(MockComponent.class));
Endpoint endpoint = camelContext.getEndpoint("foo:cheese");
assertThat(endpoint, is(MockEndpoint.class));
}
Aggregations