use of com.google.inject.Key in project roboguice by roboguice.
the class CheckedProviderTest method testProvisionExceptionOnDependenciesOfCxtor.
public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(ProvisionExceptionFoo.class);
bindScope(BadScope.class, new Scope() {
@Override
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
return new Provider<T>() {
@Override
public T get() {
throw new OutOfScopeException("failure");
}
};
}
});
}
});
try {
injector.getInstance(Key.get(remoteProviderOfFoo)).get();
fail();
} catch (ProvisionException pe) {
assertEquals(2, pe.getErrorMessages().size());
List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(0).getMessage());
assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(1).getMessage());
}
}
use of com.google.inject.Key in project roboguice by roboguice.
the class ServletScopesTest method indexBindings.
private ImmutableMap<Key<?>, Binding<?>> indexBindings(Iterable<Element> elements) {
ImmutableMap.Builder<Key<?>, Binding<?>> builder = ImmutableMap.builder();
for (Element element : elements) {
if (element instanceof Binding) {
Binding<?> binding = (Binding<?>) element;
builder.put(binding.getKey(), binding);
} else if (element instanceof PrivateElements) {
PrivateElements privateElements = (PrivateElements) element;
Map<Key<?>, Binding<?>> privateBindings = indexBindings(privateElements.getElements());
for (Key<?> exposed : privateElements.getExposedKeys()) {
builder.put(exposed, privateBindings.get(exposed));
}
}
}
return builder.build();
}
use of com.google.inject.Key in project roboguice by roboguice.
the class TransferRequestIntegrationTest method testTransferNonHttpRequest_concurrentUseBlocks.
public void testTransferNonHttpRequest_concurrentUseBlocks() throws Exception {
Callable<Boolean> callable = new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
Future<Boolean> future = executor.submit(ServletScopes.transferRequest(FALSE_CALLABLE));
try {
return future.get(100, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
return true;
}
} finally {
executor.shutdownNow();
}
}
};
ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
assertTrue(ServletScopes.scopeRequest(callable, seedMap).call());
}
use of com.google.inject.Key in project roboguice by roboguice.
the class BoundFieldModuleTest method testBindingWithGenerics.
public void testBindingWithGenerics() {
final List<Integer> testIntList = Arrays.asList(new Integer[] { 1, 2, 3 });
final List<Boolean> testBoolList = Arrays.asList(new Boolean[] { true, true, false });
Object instance = new Object() {
@Bind
private List<Integer> anIntList = testIntList;
@Bind
private List<Boolean> aBoolList = testBoolList;
};
BoundFieldModule module = BoundFieldModule.of(instance);
Injector injector = Guice.createInjector(module);
assertEquals(testIntList, injector.getInstance(new Key<List<Integer>>() {
}));
assertEquals(testBoolList, injector.getInstance(new Key<List<Boolean>>() {
}));
}
use of com.google.inject.Key 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());
}
Aggregations