1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.grouplens.grapht;
21
22 import org.grouplens.grapht.annotation.DefaultNull;
23 import org.junit.Test;
24
25 import javax.annotation.Nullable;
26 import javax.inject.Inject;
27 import javax.inject.Qualifier;
28 import java.lang.annotation.*;
29
30 import static org.hamcrest.Matchers.*;
31 import static org.junit.Assert.assertThat;
32
33
34
35
36
37
38 public class QualifiedDefaultNullTest {
39 @Test
40 public void testQualifiedDefaultNull() throws InjectionException {
41 InjectorBuilder bld = InjectorBuilder.create();
42 Injector inj = bld.build();
43
44
45 MainC main = inj.getInstance(MainC.class);
46 assertThat(main, notNullValue());
47 assertThat(main.dependency, nullValue());
48 }
49
50 @Test
51 public void testQualifiedDefaultNullWithBinding() throws InjectionException {
52 InjectorBuilder bld = InjectorBuilder.create();
53 bld.bind(TestQ.class, DepC.class)
54 .to(DepC.class);
55 Injector inj = bld.build();
56
57
58 MainC main = inj.getInstance(MainC.class);
59 assertThat(main, notNullValue());
60 assertThat(main.dependency,
61 allOf(notNullValue(),
62 instanceOf(DepC.class)));
63 }
64
65
66
67
68 @Qualifier
69 @Retention(RetentionPolicy.RUNTIME)
70 @Target(ElementType.PARAMETER)
71 @DefaultNull
72 @Documented
73 public static @interface TestQ {}
74
75
76
77
78 public static class MainC {
79 private final DepC dependency;
80
81 @Inject
82 public MainC(@Nullable @TestQ DepC dep) {
83 dependency = dep;
84 }
85 }
86
87 public static class DepC {
88 @Inject
89 public DepC() {}
90 }
91 }