aboutsummaryrefslogtreecommitdiff
path: root/factory/src/it/functional/src/test/java/com/google/auto/factory/DependencyInjectionIntegrationTest.java
blob: ebd83367567e3198ff1363a3f8451eb8f6896540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.google.auto.factory;

import static com.google.common.truth.Truth.assertThat;

import com.google.auto.factory.GenericFoo.DepE;
import com.google.auto.factory.GenericFoo.IntAndStringAccessor;
import com.google.common.collect.ImmutableList;
import com.google.inject.Guice;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import java.util.ArrayList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class DependencyInjectionIntegrationTest {

  private static final IntAndStringAccessor INT_AND_STRING_ACCESSOR = new IntAndStringAccessor() {};

  @Test public void daggerInjectedFactory() {
    FooFactory fooFactory = DaggerFactoryComponent.create().factory();
    Foo one = fooFactory.create("A");
    Foo two = fooFactory.create("B");
    assertThat(one.name()).isEqualTo("A");
    assertThat(one.dependency()).isNotNull();
    assertThat(one.dependencyProvider()).isNotNull();
    assertThat(one.dependencyProvider().get()).isInstanceOf(QualifiedDependencyImpl.class);
    assertThat(one.primitive()).isEqualTo(1);
    assertThat(one.qualifiedPrimitive()).isEqualTo(2);
    assertThat(two.name()).isEqualTo("B");
    assertThat(two.dependency()).isNotNull();
    assertThat(two.dependency()).isNotEqualTo(one.dependency());
    assertThat(two.dependencyProvider()).isNotNull();
    assertThat(two.dependencyProvider().get()).isInstanceOf(QualifiedDependencyImpl.class);
    assertThat(two.primitive()).isEqualTo(1);
    assertThat(two.qualifiedPrimitive()).isEqualTo(2);
  }

  @Test
  public void daggerInjectedGenericFactory() {
    GenericFooFactory<Number> genericFooFactory =
        DaggerFactoryComponent.create().generatedFactory();
    GenericFoo<Number, ImmutableList<Long>, String, DepE> three =
        genericFooFactory.create(ImmutableList.of(3L), INT_AND_STRING_ACCESSOR, DepE.VALUE_1);
    ArrayList<Double> intAndStringAccessorArrayList = new ArrayList<>();
    intAndStringAccessorArrayList.add(4.0);
    GenericFoo<Number, ArrayList<Double>, Long, DepE> four = genericFooFactory.create(
        intAndStringAccessorArrayList, INT_AND_STRING_ACCESSOR, DepE.VALUE_2);
    assertThat(three.getDepA()).isEqualTo(3);
    ImmutableList<Long> unusedLongList = three.getDepB();
    assertThat(three.getDepB()).containsExactly(3L);
    assertThat(three.getDepDIntAccessor()).isEqualTo(INT_AND_STRING_ACCESSOR);
    assertThat(three.getDepDStringAccessor()).isEqualTo(INT_AND_STRING_ACCESSOR);
    assertThat(three.passThrough("value")).isEqualTo("value");
    assertThat(three.getDepE()).isEqualTo(DepE.VALUE_1);
    assertThat(four.getDepA()).isEqualTo(3);
    ArrayList<Double> unusedDoubleList = four.getDepB();
    assertThat(four.getDepB()).isInstanceOf(ArrayList.class);
    assertThat(four.getDepB()).containsExactly(4.0);
    assertThat(four.getDepDIntAccessor()).isEqualTo(INT_AND_STRING_ACCESSOR);
    assertThat(four.getDepDStringAccessor()).isEqualTo(INT_AND_STRING_ACCESSOR);
    assertThat(four.passThrough(5L)).isEqualTo(5L);
    assertThat(four.getDepE()).isEqualTo(DepE.VALUE_2);
  }

  @Test public void guiceInjectedFactory() {
    FooFactory fooFactory = Guice.createInjector(new GuiceModule()).getInstance(FooFactory.class);
    Foo one = fooFactory.create("A");
    Foo two = fooFactory.create("B");
    assertThat(one.name()).isEqualTo("A");
    assertThat(one.dependency()).isNotNull();
    assertThat(one.dependencyProvider()).isNotNull();
    assertThat(one.dependencyProvider().get()).isInstanceOf(QualifiedDependencyImpl.class);
    assertThat(one.primitive()).isEqualTo(1);
    assertThat(one.qualifiedPrimitive()).isEqualTo(2);
    assertThat(two.name()).isEqualTo("B");
    assertThat(two.dependency()).isNotNull();
    assertThat(two.dependency()).isNotEqualTo(one.dependency());
    assertThat(two.dependencyProvider()).isNotNull();
    assertThat(two.dependencyProvider().get()).isInstanceOf(QualifiedDependencyImpl.class);
    assertThat(two.primitive()).isEqualTo(1);
    assertThat(two.qualifiedPrimitive()).isEqualTo(2);
  }

  @Test
  public void guiceInjectedGenericFactory() {
    GenericFooFactory<Number> genericFooFactory =
        Guice.createInjector(new GuiceModule())
            .getInstance(Key.get(new TypeLiteral<GenericFooFactory<Number>>() {}));
    GenericFoo<Number, ImmutableList<Long>, String, DepE> three =
        genericFooFactory.create(ImmutableList.of(3L), INT_AND_STRING_ACCESSOR, DepE.VALUE_1);
    ArrayList<Double> intAndStringAccessorArrayList = new ArrayList<>();
    intAndStringAccessorArrayList.add(4.0);
    GenericFoo<Number, ArrayList<Double>, Long, DepE> four = genericFooFactory.create(
        intAndStringAccessorArrayList, INT_AND_STRING_ACCESSOR, DepE.VALUE_2);
    assertThat(three.getDepA()).isEqualTo(3);
    ImmutableList<Long> unusedLongList = three.getDepB();
    assertThat(three.getDepB()).containsExactly(3L);
    assertThat(three.getDepDIntAccessor()).isEqualTo(INT_AND_STRING_ACCESSOR);
    assertThat(three.getDepDStringAccessor()).isEqualTo(INT_AND_STRING_ACCESSOR);
    assertThat(three.passThrough("value")).isEqualTo("value");
    assertThat(three.getDepE()).isEqualTo(DepE.VALUE_1);
    assertThat(four.getDepA()).isEqualTo(3);
    ArrayList<Double> unusedDoubleList = four.getDepB();
    assertThat(four.getDepB()).containsExactly(4.0);
    assertThat(four.getDepDIntAccessor()).isEqualTo(INT_AND_STRING_ACCESSOR);
    assertThat(four.getDepDStringAccessor()).isEqualTo(INT_AND_STRING_ACCESSOR);
    assertThat(four.passThrough(5L)).isEqualTo(5L);
    assertThat(four.getDepE()).isEqualTo(DepE.VALUE_2);
  }
}