From fecdb363f419bb6da62753739a1456db223e12eb Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Wed, 12 Aug 2020 13:55:18 -0400 Subject: [PATCH] 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. --- gradle.properties | 2 +- .../com/example/BindsIndirectionCycle.java | 39 +++++++++++++++++ .../java/com/example/IndirectionCycle.java | 42 +++++++++++++++++++ ...ltibindingProviderMapIndirectionCycle.java | 34 +++++++++++++++ .../java/com/example/IntegrationTest.java | 29 +++++++++++++ 5 files changed, 145 insertions(+), 1 deletion(-) 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/gradle.properties b/gradle.properties index a0aa1c33..8597fa12 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ GROUP=com.jakewharton.dagger -VERSION_NAME=0.3.0 +VERSION_NAME=0.4.0-SNAPSHOT POM_DESCRIPTION=Reflection-based Dagger implementation 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";