1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package jp.liq.container.example;
17
18 import static org.junit.Assert.*;
19 import jp.liq.container.Container;
20 import jp.liq.container.vocabulary.Factory;
21 import jp.liq.container.vocabulary.Inject;
22 import static jp.liq.container.ContainerVocabulary.*;
23
24 import org.junit.Test;
25
26
27
28
29
30 public class SentinelModuleExample {
31
32 public static class A {
33 private B b;
34
35 public A(B b) {
36 this.b = b;
37 }
38
39 public B getB() {
40 return b;
41 }
42 }
43
44 public static class B {
45
46 public B() {
47
48 }
49
50 }
51
52 public static class ExtendsB extends B {
53
54 }
55
56 public static class C {
57 private A a;
58 public C() {
59
60 }
61 public C(A a) {
62 this.a = a;
63 }
64
65 public A getA() {
66 return a;
67 }
68 }
69
70 @Test
71 public void sample1() {
72 Container container = new Container();
73 sentinel.configure(container);
74
75 A a = container.get(A.class);
76
77 assertNotNull(a);
78 assertNotNull(a.getB());
79 }
80
81 @Test
82 public void sample2() {
83 Container container = new Container();
84
85 B b = new B();
86
87 components.configure(container)
88 .define(component.instance(B.class, b));
89
90 sentinel.configure(container);
91
92 A a = container.get(A.class);
93
94 assertSame(b, a.getB());
95 }
96
97 public class CFactory {
98 @Factory
99 public C createC(A a) {
100 return new C(a);
101 }
102 }
103
104 @Test
105 public void sample3() {
106 Container container = new Container();
107
108 B b = new B();
109
110 components.configure(container)
111 .define(component.factory(new CFactory()))
112 .define(component.instance(B.class, b));
113
114 sentinel.configure(container);
115
116 C c = container.get(C.class);
117
118 assertSame(b, c.getA().getB());
119 }
120
121 @Test
122 public void sample4() {
123 Container container = new Container();
124
125 components.configure(container)
126 .define(component.mapping(B.class, ExtendsB.class));
127
128 sentinel.configure(container);
129
130 A a = container.get(A.class);
131
132 assertTrue(a.getB() instanceof ExtendsB);
133 }
134
135 public static class D {
136 @Inject
137 public A a;
138 }
139
140 @Test
141 public void sample5() {
142 Container container = new Container();
143
144 sentinel.configure(container).with(injector.toFields());
145
146 D d = container.get(D.class);
147 assertNotNull(d.a);
148
149 }
150
151 public static class E {
152 private A a;
153
154 public A getA() {
155 return a;
156 }
157
158 @Inject
159 public void setA(A a) {
160 this.a = a;
161 }
162 }
163
164 @Test
165 public void sample6() {
166 Container container = new Container();
167
168 sentinel.configure(container).with(injector.toMethods());
169
170 E e = container.get(E.class);
171 assertNotNull(e.getA());
172
173 }
174
175 @Test
176 public void sample7() {
177 Container parent = new Container();
178
179 components.configure(parent)
180 .define(component.factory(new CFactory()));
181
182 Container container = new Container();
183
184 container.include(parent);
185
186 sentinel.configure(container);
187
188 C c = container.get(C.class);
189 assertNotNull(c);
190 }
191 }