forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetBindingTest.java
More file actions
339 lines (287 loc) · 10.5 KB
/
Copy pathSetBindingTest.java
File metadata and controls
339 lines (287 loc) · 10.5 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* 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.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import javax.inject.Inject;
import javax.inject.Named;
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 dagger.Provides.Type.SET;
import static dagger.Provides.Type.SET_VALUES;
import static java.util.Collections.emptySet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
@RunWith(JUnit4.class)
public final class SetBindingTest {
@Test public void multiValueBindings_SingleModule() {
class TestEntryPoint {
@Inject Set<String> strings;
}
@Module(injects = TestEntryPoint.class)
class TestModule {
@Provides(type=SET) String provideFirstString() { return "string1"; }
@Provides(type=SET) String provideSecondString() { return "string2"; }
}
TestEntryPoint ep = injectWithModule(new TestEntryPoint(), new TestModule());
assertEquals(set("string1", "string2"), ep.strings);
}
@Test public void multiValueBindings_MultiModule() {
class TestEntryPoint {
@Inject Set<String> strings;
}
@Module
class TestIncludesModule {
@Provides(type=SET) String provideSecondString() { return "string2"; }
}
@Module(injects = TestEntryPoint.class, includes = TestIncludesModule.class)
class TestModule {
@Provides(type=SET) String provideFirstString() { return "string1"; }
@Provides(type=SET_VALUES) Set<String> provideDefaultStrings() {
return emptySet();
}
}
TestEntryPoint ep = injectWithModule(new TestEntryPoint(),
new TestModule(), new TestIncludesModule());
assertEquals(set("string1", "string2"), ep.strings);
}
@Test public void multiValueBindings_MultiModule_NestedSet() {
class TestEntryPoint {
@Inject Set<Set<String>> stringses;
}
@Module
class TestIncludesModule {
@Provides(type=SET) Set<String> provideSecondStrings() { return set("string2"); }
}
@Module(injects = TestEntryPoint.class, includes = TestIncludesModule.class)
class TestModule {
@Provides(type=SET) Set<String> provideFirstStrings() { return set("string1"); }
@Provides(type=SET_VALUES) Set<Set<String>> provideDefaultStringeses() {
return set(set("string3"));
}
}
TestEntryPoint ep = injectWithModule(new TestEntryPoint(),
new TestModule(), new TestIncludesModule());
assertEquals(set(set("string1"),set("string2"), set("string3")), ep.stringses);
}
@Test public void multiValueBindings_WithSingletonAndDefaultValues() {
final AtomicInteger singletonCounter = new AtomicInteger(100);
final AtomicInteger defaultCounter = new AtomicInteger(200);
class TestEntryPoint {
@Inject Set<Integer> objects1;
@Inject Set<Integer> objects2;
}
@Module(injects = TestEntryPoint.class)
class TestModule {
@Provides(type=SET) @Singleton Integer a() { return singletonCounter.getAndIncrement(); }
@Provides(type=SET) Integer b() { return defaultCounter.getAndIncrement(); }
}
TestEntryPoint ep = injectWithModule(new TestEntryPoint(), new TestModule());
assertEquals(set(100, 200), ep.objects1);
assertEquals(set(100, 201), ep.objects2);
}
@Test public void multiValueBindings_WithSingletonsAcrossMultipleInjectableTypes() {
final AtomicInteger singletonCounter = new AtomicInteger(100);
final AtomicInteger defaultCounter = new AtomicInteger(200);
class TestEntryPoint1 {
@Inject Set<Integer> objects1;
}
class TestEntryPoint2 {
@Inject Set<Integer> objects2;
}
@Module(injects = { TestEntryPoint1.class, TestEntryPoint2.class })
class TestModule {
@Provides(type=SET) @Singleton Integer a() { return singletonCounter.getAndIncrement(); }
@Provides(type=SET) Integer b() { return defaultCounter.getAndIncrement(); }
}
ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule());
TestEntryPoint1 ep1 = graph.inject(new TestEntryPoint1());
TestEntryPoint2 ep2 = graph.inject(new TestEntryPoint2());
assertEquals(set(100, 200), ep1.objects1);
assertEquals(set(100, 201), ep2.objects2);
}
@Test public void multiValueBindings_WithQualifiers() {
class TestEntryPoint {
@Inject Set<String> strings;
@Inject @Named("foo") Set<String> fooStrings;
}
@Module(injects = TestEntryPoint.class)
class TestModule {
@Provides(type=SET_VALUES) Set<String> provideString1() {
return set("string1");
}
@Provides(type=SET) String provideString2() { return "string2"; }
@Provides(type=SET) @Named("foo") String provideString3() { return "string3"; }
@Provides(type=SET_VALUES) @Named("foo") Set<String> provideString4() {
return set("string4");
}
}
TestEntryPoint ep = injectWithModule(new TestEntryPoint(), new TestModule());
assertEquals(set("string1", "string2"), ep.strings);
assertEquals(set("string4", "string3"), ep.fooStrings);
}
// TODO(cgruber): Move this into an example project.
@Test public void sampleMultiBindingLogger() {
class TestEntryPoint {
@Inject Logger logger;
public void doStuff() {
Throwable t = new NullPointerException("Naughty Naughty");
this.logger.log("Logging an error", t);
}
}
final AtomicReference<String> logoutput = new AtomicReference<String>();
@Module
class LogModule {
@Provides(type=SET) LogSink outputtingLogSink() {
return new LogSink() {
@Override public void log(LogMessage message) {
StringWriter sw = new StringWriter();
message.error.printStackTrace(new PrintWriter(sw));
logoutput.set(message.message + "\n" + sw.getBuffer().toString());
}
};
}
}
@Module(injects = TestEntryPoint.class)
class TestModule {
@Provides(type=SET) LogSink nullLogger() {
return new LogSink() { @Override public void log(LogMessage message) {} };
}
}
TestEntryPoint ep = injectWithModule(new TestEntryPoint(),new TestModule(), new LogModule());
assertNull(logoutput.get());
ep.doStuff();
assertNotNull(logoutput.get());
assertThat(logoutput.get()).contains("Naughty Naughty");
assertThat(logoutput.get()).contains("NullPointerException");
}
@Test public void duplicateValuesContributed() {
class TestEntryPoint {
@Inject Set<String> strings;
}
@Module(injects = TestEntryPoint.class)
class TestModule {
@Provides(type=SET) String provideString1() { return "a"; }
@Provides(type=SET) String provideString2() { return "a"; }
@Provides(type=SET) String provideString3() { return "b"; }
}
TestEntryPoint ep = injectWithModule(new TestEntryPoint(), new TestModule());
assertThat(ep.strings).containsExactly("a", "b");
}
@Test public void validateSetBinding() {
class TestEntryPoint {
@Inject Set<String> strings;
}
@Module(injects = TestEntryPoint.class)
class TestModule {
@Provides(type=SET) String provideString1() { return "string1"; }
@Provides(type=SET) String provideString2() { return "string2"; }
}
ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule());
graph.validate();
}
@Test public void validateEmptySetBinding() {
class TestEntryPoint {
@Inject Set<String> strings;
}
@Module(injects = TestEntryPoint.class)
class TestModule {
@Provides(type=SET_VALUES) Set<String> provideDefault() {
return emptySet();
}
}
ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule());
graph.validate();
}
@Test public void validateLibraryModules() {
class TestEntryPoint {}
@Module(library = true)
class SetModule {
@Provides(type = SET)
public String provideString() {
return "";
}
}
@Module(injects = TestEntryPoint.class, includes = SetModule.class)
class TestModule {}
ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(),
new TestModule(), new SetModule());
graph.validate();
}
@Test public void validateLibraryModules_nonLibraryContributors() {
class TestEntryPoint {}
@Module(library = true)
class SetModule1 {
@Provides(type = SET)
public String provideString() {
return "a";
}
}
@Module
class SetModule2 {
@Provides(type = SET)
public String provideString() {
return "b";
}
}
@Module(injects = TestEntryPoint.class, includes = { SetModule1.class, SetModule2.class })
class TestModule {}
ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(),
new TestModule(), new SetModule1(), new SetModule2());
try {
graph.validate();
fail();
} catch (IllegalStateException expected) {}
}
static class Logger {
@Inject Set<LogSink> loggers;
public void log(String text, Throwable error) {
LogMessage m = new LogMessage(text, error);
for (LogSink sink : loggers) {
sink.log(m);
}
}
}
static class LogMessage {
public final String message;
public final Throwable error;
public LogMessage (String message, Throwable error) {
this.message = message;
this.error = error;
}
}
static interface LogSink {
void log(LogMessage message);
}
private <T> T injectWithModule(T ep, Object ... modules) {
return ObjectGraph.createWith(new TestingLoader(), modules).inject(ep);
}
private <T> Set<T> set(T... ts) {
return new LinkedHashSet<T>(Arrays.asList(ts));
}
}