Search in sources :

Example 1 with TypeEncounter

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Example 2 with TypeEncounter

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Example 3 with TypeEncounter

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");
    }
}
Also used : Message(com.google.inject.spi.Message) TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Example 4 with TypeEncounter

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Example 5 with TypeEncounter

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));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeListener(com.google.inject.spi.TypeListener) InjectionListener(com.google.inject.spi.InjectionListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Aggregations

TypeEncounter (com.google.inject.spi.TypeEncounter)10 TypeListener (com.google.inject.spi.TypeListener)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 InjectionListener (com.google.inject.spi.InjectionListener)2 Message (com.google.inject.spi.Message)2