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.types.dft.*;
23 import org.junit.Before;
24 import org.junit.Test;
25
26 import static org.hamcrest.Matchers.*;
27 import static org.junit.Assert.assertThat;
28
29
30
31
32
33
34 public class DefaultBindingsTest {
35 private InjectorBuilder b;
36
37
38 @Before
39 public void setup() {
40 b = InjectorBuilder.create(DefaultBindingsTest.class.getClassLoader());
41 }
42
43 @Test
44 public void testDefaultImplementation() throws InjectionException {
45 Injector inj = b.build();
46 IDftImpl a = inj.getInstance(IDftImpl.class);
47 assertThat(a, notNullValue());
48 assertThat(a, instanceOf(CDftImplA.class));
49 }
50
51 @Test
52 public void testOverrideDefaultImplementation() throws InjectionException {
53 b.bind(IDftImpl.class).to(CDftImplB.class);
54 Injector inj = b.build();
55 IDftImpl a = inj.getInstance(IDftImpl.class);
56 assertThat(a, notNullValue());
57 assertThat(a, instanceOf(CDftImplB.class));
58 }
59
60 @Test
61 public void testDefaultProvider() throws InjectionException {
62 Injector inj = b.build();
63 IDftProvider a = inj.getInstance(IDftProvider.class);
64 assertThat(a, notNullValue());
65 assertThat(a, instanceOf(PDftProvider.Impl.class));
66 }
67
68 @Test
69 public void testOverrideDefaultProvider() throws InjectionException {
70 b.bind(IDftProvider.class).to(CDftProvider.class);
71 Injector inj = b.build();
72 IDftProvider a = inj.getInstance(IDftProvider.class);
73 assertThat(a, notNullValue());
74 assertThat(a, instanceOf(CDftProvider.class));
75 }
76
77 @Test
78 public void testPropDefaultImplementation() throws InjectionException {
79 Injector inj = b.build();
80 IPropDftImpl a = inj.getInstance(IPropDftImpl.class);
81 assertThat(a, notNullValue());
82 assertThat(a, instanceOf(CPropDftImplA.class));
83 }
84
85 @Test
86 public void testPropOverrideDefaultImplementation() throws InjectionException {
87 b.bind(IPropDftImpl.class).to(CPropDftImplB.class);
88 Injector inj = b.build();
89 IPropDftImpl a = inj.getInstance(IPropDftImpl.class);
90 assertThat(a, notNullValue());
91 assertThat(a, instanceOf(CPropDftImplB.class));
92 }
93
94 @Test
95 public void testPropDefaultProvider() throws InjectionException {
96 Injector inj = b.build();
97 IPropDftProvider a = inj.getInstance(IPropDftProvider.class);
98 assertThat(a, notNullValue());
99 assertThat(a, instanceOf(PPropDftProvider.Impl.class));
100 }
101
102 @Test
103 public void testPropOverrideDefaultProvider() throws InjectionException {
104 b.bind(IPropDftProvider.class).to(CPropDftProvider.class);
105 Injector inj = b.build();
106 IPropDftProvider a = inj.getInstance(IPropDftProvider.class);
107 assertThat(a, notNullValue());
108 assertThat(a, instanceOf(CPropDftProvider.class));
109 }
110
111 @Test
112 public void testPropImplDoubleDepCache() throws InjectionException {
113 Injector inj = b.build();
114 CPropImplDoubleDep obj = inj.getInstance(CPropImplDoubleDep.class);
115 assertThat(obj.right, sameInstance(obj.left));
116 }
117
118 @Test
119 public void testPropImplDoubleDepUncache() throws InjectionException {
120 b.setDefaultCachePolicy(CachePolicy.NEW_INSTANCE);
121 Injector inj = b.build();
122 CPropImplDoubleDep obj = inj.getInstance(CPropImplDoubleDep.class);
123 assertThat(obj.right, not(sameInstance(obj.left)));
124 }
125
126 @Test
127 public void testPropImplDoubleDepPropUncache() throws InjectionException {
128 Injector inj = b.build();
129 CPropImplDoubleDepNoCache obj = inj.getInstance(CPropImplDoubleDepNoCache.class);
130 assertThat(obj.right, not(sameInstance(obj.left)));
131 }
132
133 @Test
134 public void testDftImplDoubleDepCache() throws InjectionException {
135 Injector inj = b.build();
136 CDoubleDep obj = inj.getInstance(CDoubleDep.class);
137 assertThat(obj.right, sameInstance(obj.left));
138 }
139
140 @Test
141 public void testDftImplDoubleDepUncache() throws InjectionException {
142 b.setDefaultCachePolicy(CachePolicy.NEW_INSTANCE);
143 Injector inj = b.build();
144 CDoubleDep obj = inj.getInstance(CDoubleDep.class);
145 assertThat(obj.right, not(sameInstance(obj.left)));
146 }
147
148 @Test
149 public void testDftImplDoubleDepPropUncache() throws InjectionException {
150 Injector inj = b.build();
151 CDoubleDepNoCache obj = inj.getInstance(CDoubleDepNoCache.class);
152 assertThat(obj.right, not(sameInstance(obj.left)));
153 }
154
155
156
157
158 @Test
159 public void testPreemptDefaultImplementation() throws InjectionException {
160 Injector inj = b.build();
161 IPreemptDftImpl a = inj.getInstance(IPreemptDftImpl.class);
162 assertThat(a, notNullValue());
163 assertThat(a, instanceOf(CPreemptDftImplA.class));
164 }
165 }