Skip to content

Commit

Permalink
Creates cycle indirection scenarios for tests
Browse files Browse the repository at this point in the history
Creates `IndirectionCycle` with regular provided dependencies,
`BindsIndirectionCycle` for bound dependencies indirection, and
`MultibindProviderMapIndirectionCycle` for the scenario pinpointed in
issue #175 and resolved by #198.
  • Loading branch information
JakeWharton authored and gcvasconcelos-picpay committed Aug 20, 2020
1 parent 0a47d59 commit fecdb36
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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

Expand Down
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;
}
}
}
29 changes: 29 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,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";
Expand Down

0 comments on commit fecdb36

Please sign in to comment.