From 7e778f467eccb978529cc016287eaa6a83fb6991 Mon Sep 17 00:00:00 2001 From: Gabriel Vasconcelos Date: Wed, 12 Aug 2020 13:55:18 -0400 Subject: [PATCH 1/2] Creates cycle indirection scenarios for tests Creates `IndirectionCycle` with regular provided dependencies, `BindsIndirectionCycle` for bound dependencies indirection, and `MultibindProviderMapIndirectionCycle` for the scenario pinpointed in issue #175 and resolved by #198. --- .../com/example/BindsIndirectionCycle.java | 39 +++++++++++++++++ .../java/com/example/IndirectionCycle.java | 42 +++++++++++++++++++ ...ltibindingProviderMapIndirectionCycle.java | 34 +++++++++++++++ .../java/com/example/IntegrationTest.java | 29 +++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 integration-tests/src/main/java/com/example/BindsIndirectionCycle.java create mode 100644 integration-tests/src/main/java/com/example/IndirectionCycle.java create mode 100644 integration-tests/src/main/java/com/example/MultibindingProviderMapIndirectionCycle.java diff --git a/integration-tests/src/main/java/com/example/BindsIndirectionCycle.java b/integration-tests/src/main/java/com/example/BindsIndirectionCycle.java new file mode 100644 index 00000000..f87bb139 --- /dev/null +++ b/integration-tests/src/main/java/com/example/BindsIndirectionCycle.java @@ -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 providerObject; + @Inject public Lazy lazyObject; + @Inject public Provider> lazyProviderObject; + + @Inject + B(Provider providerObject) { + this.providerObject = providerObject; + } + } + + @Module + abstract class Module1 { + @Binds + abstract Object bindsA(A a); + } +} diff --git a/integration-tests/src/main/java/com/example/IndirectionCycle.java b/integration-tests/src/main/java/com/example/IndirectionCycle.java new file mode 100644 index 00000000..d639837a --- /dev/null +++ b/integration-tests/src/main/java/com/example/IndirectionCycle.java @@ -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 providerA; + @Inject public Lazy lazyA; + @Inject public Provider> lazyProviderA; + + @Inject + C(Provider providerA) { + this.providerA = providerA; + } + } +} diff --git a/integration-tests/src/main/java/com/example/MultibindingProviderMapIndirectionCycle.java b/integration-tests/src/main/java/com/example/MultibindingProviderMapIndirectionCycle.java new file mode 100644 index 00000000..75006a54 --- /dev/null +++ b/integration-tests/src/main/java/com/example/MultibindingProviderMapIndirectionCycle.java @@ -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> providerMap; + + @Inject + Factory(Map> providerMap) { + this.providerMap = providerMap; + } + } +} diff --git a/integration-tests/src/test/java/com/example/IntegrationTest.java b/integration-tests/src/test/java/com/example/IntegrationTest.java index 9b188809..25dd38bc 100644 --- a/integration-tests/src/test/java/com/example/IntegrationTest.java +++ b/integration-tests/src/test/java/com/example/IntegrationTest.java @@ -1586,6 +1586,35 @@ 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 multipleInterfacesRequestSameDependency() { String value = "my-value"; From 80de396a56e9108b34fd6fd7f04cc0bc9d65f57e Mon Sep 17 00:00:00 2001 From: Gabriel Vasconcelos Date: Thu, 20 Aug 2020 16:07:09 -0300 Subject: [PATCH 2/2] Adds scoped indirection cycle tests --- .../com/example/ScopedCycleIndirection.java | 76 +++++++++++++++++++ .../java/com/example/IntegrationTest.java | 26 +++++++ 2 files changed, 102 insertions(+) create mode 100644 integration-tests/src/main/java/com/example/ScopedCycleIndirection.java diff --git a/integration-tests/src/main/java/com/example/ScopedCycleIndirection.java b/integration-tests/src/main/java/com/example/ScopedCycleIndirection.java new file mode 100644 index 00000000..80eea9dc --- /dev/null +++ b/integration-tests/src/main/java/com/example/ScopedCycleIndirection.java @@ -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 provider; + @Inject public Lazy lazy; + @Inject public Provider> lazyProvider; + + @Inject + C(Provider 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> providerMap; + public final Provider provider; + + @Inject + D(Map> providerMap, Provider provider) { + this.providerMap = providerMap; + this.provider = provider; + } + } +} diff --git a/integration-tests/src/test/java/com/example/IntegrationTest.java b/integration-tests/src/test/java/com/example/IntegrationTest.java index 25dd38bc..1b2c7b7e 100644 --- a/integration-tests/src/test/java/com/example/IntegrationTest.java +++ b/integration-tests/src/test/java/com/example/IntegrationTest.java @@ -1615,6 +1615,32 @@ public void indirectionCycle() { 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";