Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creates cycle indirection scenarios for tests #200

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example;

import dagger.Binds;
import dagger.Component;
import dagger.Lazy;
import dagger.Module;
import javax.inject.Inject;
import javax.inject.Provider;

@Component(modules = BindsIndirectionCycle.Module1.class)
public interface BindsIndirectionCycle {
B b();

class A {
public final B b;

@Inject
A(B b) {
this.b = b;
}
}

class B {
public final Provider<Object> providerObject;
@Inject public Lazy<Object> lazyObject;
@Inject public Provider<Lazy<Object>> lazyProviderObject;

@Inject
B(Provider<Object> providerObject) {
this.providerObject = providerObject;
}
}

@Module
abstract class Module1 {
@Binds
abstract Object bindsA(A a);
}
}
42 changes: 42 additions & 0 deletions integration-tests/src/main/java/com/example/IndirectionCycle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example;

import dagger.Component;
import dagger.Lazy;
import javax.inject.Inject;
import javax.inject.Provider;

@Component
public interface IndirectionCycle {
A a();

C c();

class A {
public final B b;

@Inject
A(B b) {
this.b = b;
}
}

class B {
public final C c;

@Inject
B(C c) {
this.c = c;
}
}

class C {
public final Provider<A> providerA;
@Inject public Lazy<A> lazyA;
@Inject public Provider<Lazy<A>> lazyProviderA;

@Inject
C(Provider<A> providerA) {
this.providerA = providerA;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example;

import dagger.Component;
import dagger.Module;
import dagger.Provides;
import dagger.multibindings.IntoMap;
import dagger.multibindings.StringKey;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Provider;

@Component(modules = MultibindingProviderMapIndirectionCycle.Module1.class)
public interface MultibindingProviderMapIndirectionCycle {
Factory factory();

@Module
abstract class Module1 {
@Provides
@IntoMap
@StringKey("1")
static Long one(Factory factory) {
return 1L;
}
}

class Factory {
public final Map<String, Provider<Long>> providerMap;

@Inject
Factory(Map<String, Provider<Long>> providerMap) {
this.providerMap = providerMap;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.example;

import dagger.Binds;
import dagger.Component;
import dagger.Lazy;
import dagger.Module;
import dagger.Provides;
import dagger.multibindings.IntoMap;
import dagger.multibindings.StringKey;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

@Singleton
@Component(modules = ScopedCycleIndirection.Module1.class)
public interface ScopedCycleIndirection {
String s();

C c();

D d();

@Singleton
class B {
public final C c;

@Inject
B(C c) {
this.c = c;
}
}

@Singleton
class C {
public final Provider<String> provider;
@Inject public Lazy<String> lazy;
@Inject public Provider<Lazy<String>> lazyProvider;

@Inject
C(Provider<String> provider) {
this.provider = provider;
}
}

@Module
abstract class Module1 {
@Singleton
@Provides
static String s(B b, D d) {
return "a";
}

@Singleton
@Binds
@IntoMap
@StringKey("a")
abstract Object bindsIntoMap(String s);

@Singleton
@Binds
abstract Object binds(String s);
}

@Singleton
class D {
public final Map<String, Provider<Object>> providerMap;
public final Provider<Object> provider;

@Inject
D(Map<String, Provider<Object>> providerMap, Provider<Object> provider) {
this.providerMap = providerMap;
this.provider = provider;
}
}
}
55 changes: 55 additions & 0 deletions integration-tests/src/test/java/com/example/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,61 @@ public void nestedDependencyInterfaceTest() {
assertThat(result).isSameInstanceAs(value);
}

@Test
public void multibindingProviderMapIndirectionCycle() {
MultibindingProviderMapIndirectionCycle.Factory factory =
backend.create(MultibindingProviderMapIndirectionCycle.class).factory();
assertThat(factory).isNotNull();
assertThat(factory.providerMap.get("1")).isNotNull();
assertThat(factory.providerMap.get("1").get()).isEqualTo(1L);
}

@Test
public void bindsIndirectionCycle() {
BindsIndirectionCycle.B b = backend.create(BindsIndirectionCycle.class).b();
assertThat(b).isNotNull();
assertThat(b.providerObject.get()).isNotNull();
assertThat(b.lazyObject.get()).isNotNull();
assertThat(b.lazyProviderObject.get()).isNotNull();
}

@Test
public void indirectionCycle() {
IndirectionCycle component = backend.create(IndirectionCycle.class);
IndirectionCycle.A a = component.a();
IndirectionCycle.C c = component.c();
assertThat(a).isNotNull();
assertThat(c.providerA.get()).isNotNull();
assertThat(c.lazyA.get()).isNotNull();
assertThat(c.lazyProviderA.get()).isNotNull();
}

@Test
public void scopedMultibindingProviderMapIndirectionCycle() {
ScopedCycleIndirection.D d = backend.create(ScopedCycleIndirection.class).d();
assertThat(d).isNotNull();
assertThat(d.providerMap.get("a")).isNotNull();
assertThat(d.providerMap.get("a").get()).isEqualTo("a");
}

@Test
public void scopedBindsIndirectionCycle() {
ScopedCycleIndirection.D d = backend.create(ScopedCycleIndirection.class).d();
assertThat(d).isNotNull();
assertThat(d.provider.get()).isEqualTo("a");
}

@Test
public void scopedIndirectionCycle() {
ScopedCycleIndirection component = backend.create(ScopedCycleIndirection.class);
String s = component.s();
ScopedCycleIndirection.C c = component.c();
assertThat(s).isEqualTo("a");
assertThat(c.provider.get()).isEqualTo("a");
assertThat(c.lazy.get()).isEqualTo("a");
assertThat(c.lazyProvider.get().get()).isEqualTo("a");
}

@Test
public void multipleInterfacesRequestSameDependency() {
String value = "my-value";
Expand Down