forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionTest.java
More file actions
113 lines (97 loc) · 4.02 KB
/
Copy pathExtensionTest.java
File metadata and controls
113 lines (97 loc) · 4.02 KB
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
113
/*
* Copyright (C) 2012 Google Inc.
* Copyright (C) 2012 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dagger;
import dagger.internal.TestingLoader;
import java.util.Arrays;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNotNull;
@RunWith(JUnit4.class)
public final class ExtensionTest {
@Singleton
static class A {
@Inject A() {}
}
static class B {
@Inject A a;
}
@Singleton
static class C {
@Inject A a;
@Inject B b;
}
static class D {
@Inject A a;
@Inject B b;
@Inject C c;
}
@Module(injects = { A.class, B.class }) static class RootModule { }
@Module(addsTo = RootModule.class, injects = { C.class, D.class })
static class ExtensionModule { }
@Test public void basicExtension() {
assertNotNull(ObjectGraph.createWith(new TestingLoader(), new RootModule())
.plus(new ExtensionModule()));
}
@Test public void basicInjection() {
ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule());
assertThat(root.get(A.class)).isNotNull();
assertThat(root.get(A.class)).isSameAs(root.get(A.class)); // Present and Singleton.
assertThat(root.get(B.class)).isNotSameAs(root.get(B.class)); // Not singleton.
assertFailInjectNotRegistered(root, C.class); // Not declared in RootModule.
assertFailInjectNotRegistered(root, D.class); // Not declared in RootModule.
// Extension graph behaves as the root graph would for root-ish things.
ObjectGraph extension = root.plus(new ExtensionModule());
assertThat(root.get(A.class)).isSameAs(extension.get(A.class));
assertThat(root.get(B.class)).isNotSameAs(extension.get(B.class));
assertThat(root.get(B.class).a).isSameAs(extension.get(B.class).a);
assertThat(extension.get(C.class).a).isNotNull();
assertThat(extension.get(D.class).c).isNotNull();
}
@Test public void scopedGraphs() {
ObjectGraph app = ObjectGraph.createWith(new TestingLoader(), new RootModule());
assertThat(app.get(A.class)).isNotNull();
assertThat(app.get(A.class)).isSameAs(app.get(A.class));
assertThat(app.get(B.class)).isNotSameAs(app.get(B.class));
assertFailInjectNotRegistered(app, C.class);
assertFailInjectNotRegistered(app, D.class);
ObjectGraph request1 = app.plus(new ExtensionModule());
ObjectGraph request2 = app.plus(new ExtensionModule());
for (ObjectGraph request : Arrays.asList(request1, request2)) {
assertThat(request.get(A.class)).isNotNull();
assertThat(request.get(A.class)).isSameAs(request.get(A.class));
assertThat(request.get(B.class)).isNotSameAs(request.get(B.class));
assertThat(request.get(C.class)).isNotNull();
assertThat(request.get(C.class)).isSameAs(request.get(C.class));
assertThat(request.get(D.class)).isNotSameAs(request.get(D.class));
}
// Singletons are one-per-graph-instance where they are declared.
assertThat(request1.get(C.class)).isNotSameAs(request2.get(C.class));
// Singletons that come from common roots should be one-per-common-graph-instance.
assertThat(request1.get(C.class).a).isSameAs(request2.get(C.class).a);
}
private void assertFailInjectNotRegistered(ObjectGraph graph, Class<?> clazz) {
try {
assertThat(graph.get(clazz)).isNull();
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("No inject");
}
}
}