use of com.google.inject.Key in project guice by google.
the class ProvidesIntoTest method testDoubleScannerIsIgnored.
public void testDoubleScannerIsIgnored() {
Injector injector = Guice.createInjector(MultibindingsScanner.asModule(), MultibindingsScanner.asModule(), new AbstractModule() {
@Override
protected void configure() {
}
@ProvidesIntoSet
String provideFoo() {
return "foo";
}
});
assertEquals(ImmutableSet.of("foo"), injector.getInstance(new Key<Set<String>>() {
}));
}
use of com.google.inject.Key in project guice by google.
the class SpiBindingsTest method testProviderBinding.
public void testProviderBinding() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
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.Key in project guice by google.
the class ProviderMethodsTest method testProviderMethods.
@SuppressWarnings("unchecked")
public void testProviderMethods() {
Injector injector = Guice.createInjector(this);
Bob bob = injector.getInstance(Bob.class);
assertEquals("A Bob", bob.getName());
Bob clone = injector.getInstance(Bob.class);
assertEquals("A Bob", clone.getName());
assertNotSame(bob, clone);
assertSame(bob.getDaughter(), clone.getDaughter());
Key soleBobKey = Key.get(Bob.class, Sole.class);
assertSame(injector.getInstance(soleBobKey), injector.getInstance(soleBobKey));
}
use of com.google.inject.Key in project roboguice by roboguice.
the class ProviderMethodsTest method testWildcardProviderMethods.
public void testWildcardProviderMethods() {
final List<String> strings = ImmutableList.of("A", "B", "C");
final List<Number> numbers = ImmutableList.<Number>of(1, 2, 3);
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
@SuppressWarnings("unchecked") Key<List<? super Integer>> listOfSupertypesOfInteger = (Key<List<? super Integer>>) Key.get(Types.listOf(Types.supertypeOf(Integer.class)));
bind(listOfSupertypesOfInteger).toInstance(numbers);
}
@Provides
public List<? extends CharSequence> provideCharSequences() {
return strings;
}
@Provides
public Class<?> provideType() {
return Float.class;
}
});
assertSame(strings, injector.getInstance(HasWildcardInjection.class).charSequences);
assertSame(numbers, injector.getInstance(HasWildcardInjection.class).numbers);
assertSame(Float.class, injector.getInstance(HasWildcardInjection.class).type);
}
use of com.google.inject.Key in project roboguice by roboguice.
the class ElementsTest method testNewPrivateBinder.
public void testNewPrivateBinder() {
final Key<Collection> collection = Key.get(Collection.class, SampleAnnotation.class);
final Key<ArrayList> arrayList = Key.get(ArrayList.class);
final ImmutableSet<Key<?>> collections = ImmutableSet.<Key<?>>of(arrayList, collection);
final Key<?> a = Key.get(String.class, Names.named("a"));
final Key<?> b = Key.get(String.class, Names.named("b"));
final ImmutableSet<Key<?>> ab = ImmutableSet.of(a, b);
checkModule(new AbstractModule() {
protected void configure() {
PrivateBinder one = binder().newPrivateBinder();
one.expose(ArrayList.class);
one.expose(Collection.class).annotatedWith(SampleAnnotation.class);
one.bind(List.class).to(ArrayList.class);
PrivateBinder two = binder().withSource("1 FooBar").newPrivateBinder().withSource("2 FooBar");
two.expose(String.class).annotatedWith(Names.named("a"));
two.expose(b);
two.bind(List.class).to(ArrayList.class);
}
}, new FailingElementVisitor() {
@Override
public Void visit(PrivateElements one) {
assertEquals(collections, one.getExposedKeys());
checkElements(one.getElements(), new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> binding) {
assertEquals(Key.get(List.class), binding.getKey());
return null;
}
});
return null;
}
}, new ExternalFailureVisitor() {
@Override
public Void visit(PrivateElements two) {
assertEquals(ab, two.getExposedKeys());
assertEquals("1 FooBar", two.getSource().toString());
checkElements(two.getElements(), new ExternalFailureVisitor() {
@Override
public <T> Void visit(Binding<T> binding) {
assertEquals("2 FooBar", binding.getSource().toString());
assertEquals(Key.get(List.class), binding.getKey());
return null;
}
});
return null;
}
});
}
Aggregations