Search in sources :

Example 51 with Provider

use of javax.inject.Provider in project dagger by square.

the class InjectionOfLazyTest method sideBySideLazyVsProvider.

@Test
public void sideBySideLazyVsProvider() {
    final AtomicInteger counter = new AtomicInteger();
    class TestEntryPoint {

        @Inject
        Provider<Integer> providerOfInteger;

        @Inject
        Lazy<Integer> lazyInteger;
    }
    @Module(injects = TestEntryPoint.class)
    class TestModule {

        @Provides
        Integer provideInteger() {
            return counter.incrementAndGet();
        }
    }
    TestEntryPoint ep = injectWithModule(new TestEntryPoint(), new TestModule());
    assertEquals(0, counter.get());
    assertEquals(0, counter.get());
    assertEquals(1, ep.lazyInteger.get().intValue());
    assertEquals(1, counter.get());
    // fresh instance
    assertEquals(2, ep.providerOfInteger.get().intValue());
    // still the same instance
    assertEquals(1, ep.lazyInteger.get().intValue());
    assertEquals(2, counter.get());
    // fresh instance
    assertEquals(3, ep.providerOfInteger.get().intValue());
    // still the same instance.
    assertEquals(1, ep.lazyInteger.get().intValue());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Provider(javax.inject.Provider) Test(org.junit.Test)

Example 52 with Provider

use of javax.inject.Provider in project dagger by square.

the class InjectionOfLazyTest method providerOfLazyOfSomething.

@Test
public void providerOfLazyOfSomething() {
    final AtomicInteger counter = new AtomicInteger();
    class TestEntryPoint {

        @Inject
        Provider<Lazy<Integer>> providerOfLazyInteger;
    }
    @Module(injects = TestEntryPoint.class)
    class TestModule {

        @Provides
        Integer provideInteger() {
            return counter.incrementAndGet();
        }
    }
    TestEntryPoint ep = injectWithModule(new TestEntryPoint(), new TestModule());
    assertEquals(0, counter.get());
    Lazy<Integer> i = ep.providerOfLazyInteger.get();
    assertEquals(1, i.get().intValue());
    assertEquals(1, counter.get());
    assertEquals(1, i.get().intValue());
    Lazy<Integer> j = ep.providerOfLazyInteger.get();
    assertEquals(2, j.get().intValue());
    assertEquals(2, counter.get());
    assertEquals(1, i.get().intValue());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Provider(javax.inject.Provider) Test(org.junit.Test)

Example 53 with Provider

use of javax.inject.Provider in project dagger by square.

the class MembersInjectorTest method instanceInjectionOfMembersOnlyType.

@Test
public void instanceInjectionOfMembersOnlyType() {
    class TestEntryPoint {

        @Inject
        Provider<Unconstructable> provider;
    }
    @Module(injects = TestEntryPoint.class)
    class TestModule {
    }
    ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule());
    try {
        graph.get(TestEntryPoint.class);
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : TestingLoader(dagger.internal.TestingLoader) Provider(javax.inject.Provider) Test(org.junit.Test)

Aggregations

Provider (javax.inject.Provider)53 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)23 Test (org.junit.Test)15 ComponentManager (org.xwiki.component.manager.ComponentManager)15 Before (org.junit.Before)11 HashMap (java.util.HashMap)9 File (java.io.File)7 FilesystemExportContext (org.xwiki.url.filesystem.FilesystemExportContext)7 ArrayList (java.util.ArrayList)6 XWikiContext (com.xpn.xwiki.XWikiContext)5 Map (java.util.Map)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 Maps (com.google.common.collect.Maps)4 Type (java.lang.reflect.Type)4 Query (org.graylog.plugins.views.search.Query)4 QueryResult (org.graylog.plugins.views.search.QueryResult)4 SearchJob (org.graylog.plugins.views.search.SearchJob)4 SearchType (org.graylog.plugins.views.search.SearchType)4 IndexLookup (org.graylog.plugins.views.search.elasticsearch.IndexLookup)4 QueryStringDecorators (org.graylog.plugins.views.search.elasticsearch.QueryStringDecorators)4