use of com.google.inject.spi.TypeEncounter in project roboguice by roboguice.
the class TypeListenerTest method testTypesWithNoInjectableMembersAreNotified.
/**
* We had a bug where we weren't notifying of types encountered for member injection when those
* types had no members to be injected. Constructed types are always injected because they always
* have at least one injection point: the class constructor.
*/
public void testTypesWithNoInjectableMembersAreNotified() {
final AtomicInteger notificationCount = new AtomicInteger();
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(onlyAbcd, new TypeListener() {
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
notificationCount.incrementAndGet();
}
});
bind(C.class).toInstance(new C());
}
});
assertEquals(1, notificationCount.get());
}
use of com.google.inject.spi.TypeEncounter in project guice by google.
the class TypeListenerTest method testTypesWithNoInjectableMembersAreNotified.
/**
* We had a bug where we weren't notifying of types encountered for member injection when those
* types had no members to be injected. Constructed types are always injected because they always
* have at least one injection point: the class constructor.
*/
public void testTypesWithNoInjectableMembersAreNotified() {
final AtomicInteger notificationCount = new AtomicInteger();
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(onlyAbcd, new TypeListener() {
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
notificationCount.incrementAndGet();
}
});
bind(C.class).toInstance(new C());
}
});
assertEquals(1, notificationCount.get());
}
use of com.google.inject.spi.TypeEncounter in project guice by google.
the class TypeListenerTest method testAddErrors.
public void testAddErrors() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
requestInjection(new Object());
bindListener(Matchers.any(), new TypeListener() {
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
encounter.addError("There was an error on %s", type);
encounter.addError(new IllegalArgumentException("whoops!"));
encounter.addError(new Message("And another problem"));
encounter.addError(new IllegalStateException());
}
});
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "1) There was an error on java.lang.Object", "2) An exception was caught and reported. Message: whoops!", "3) And another problem", "4) An exception was caught and reported. Message: null", "4 errors");
}
}
use of com.google.inject.spi.TypeEncounter in project guice by google.
the class TypeListenerTest method testTypeListenersAreFired.
public void testTypeListenersAreFired() {
final AtomicInteger firedCount = new AtomicInteger();
final TypeListener typeListener = new TypeListener() {
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
assertEquals(new TypeLiteral<A>() {
}, type);
firedCount.incrementAndGet();
}
};
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(onlyAbcd, typeListener);
bind(A.class);
}
});
assertEquals(1, firedCount.get());
}
use of com.google.inject.spi.TypeEncounter in project guice by google.
the class TypeListenerTest method testConstructedTypeListenerIsTheSameAsMembersInjectorListener.
public void testConstructedTypeListenerIsTheSameAsMembersInjectorListener() {
final AtomicInteger typeEncounters = new AtomicInteger();
final AtomicInteger injections = new AtomicInteger();
final InjectionListener<A> listener = new InjectionListener<A>() {
@Override
public void afterInjection(A injectee) {
injections.incrementAndGet();
assertNotNull(injectee.injector);
}
};
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindListener(onlyAbcd, new TypeListener() {
@Override
@SuppressWarnings("unchecked")
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
typeEncounters.incrementAndGet();
encounter.register((InjectionListener) listener);
}
});
bind(A.class);
getMembersInjector(A.class);
}
});
// creating the injector shouldn't trigger injections
assertEquals(0, injections.getAndSet(0));
// constructing an A should trigger an injection
injector.getInstance(A.class);
assertEquals(1, injections.getAndSet(0));
// injecting an A should trigger an injection
injector.injectMembers(new A());
assertEquals(1, injections.getAndSet(0));
// getting a provider shouldn't
Provider<A> aProvider = injector.getProvider(A.class);
MembersInjector<A> aMembersInjector = injector.getMembersInjector(A.class);
assertEquals(0, injections.getAndSet(0));
// exercise the provider
aProvider.get();
aProvider.get();
assertEquals(2, injections.getAndSet(0));
// exercise the members injector
aMembersInjector.injectMembers(new A());
aMembersInjector.injectMembers(new A());
assertEquals(2, injections.getAndSet(0));
// we should only have encountered one type
assertEquals(1, typeEncounters.getAndSet(0));
}
Aggregations