1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System;
|
20
|
|
using System.Text;
|
21
|
|
using System.Collections;
|
22
|
|
using NUnit.Framework;
|
23
|
|
using Seasar.Framework.Container;
|
24
|
|
using Seasar.Framework.Container.Impl;
|
25
|
|
|
26
|
|
namespace Seasar.Tests.Framework.Container.Impl
|
27
|
|
{
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
[TestFixture]
|
32
|
|
public class S2ContainerImplTest
|
33
|
|
{
|
34
|
1
|
[Test]
|
35
|
|
public void TestRegister()
|
36
|
|
{
|
37
|
1
|
IS2Container container = new S2ContainerImpl();
|
38
|
1
|
container.Register(typeof(A));
|
39
|
1
|
container.Register(typeof(B));
|
40
|
1
|
container.Register(typeof(B2));
|
41
|
1
|
try
|
42
|
|
{
|
43
|
1
|
A a = (A) container.GetComponent(typeof(A));
|
44
|
0
|
Assert.Fail();
|
45
|
|
}
|
46
|
|
catch(TooManyRegistrationRuntimeException ex)
|
47
|
|
{
|
48
|
1
|
Console.WriteLine(ex.Message);
|
49
|
1
|
Assert.AreEqual(typeof(IHoge), ex.Key);
|
50
|
1
|
Assert.AreEqual(2, ex.ComponentTypes.Length);
|
51
|
1
|
Assert.AreEqual(typeof(B), ex.ComponentTypes[0]);
|
52
|
1
|
Assert.AreEqual(typeof(B2), ex.ComponentTypes[1]);
|
53
|
|
}
|
54
|
|
}
|
55
|
|
|
56
|
1
|
[Test]
|
57
|
|
public void TestRegisterForAlreadyRegistration()
|
58
|
|
{
|
59
|
1
|
IS2Container container = new S2ContainerImpl();
|
60
|
1
|
IComponentDef cd = new ComponentDefImpl(typeof(B), "B");
|
61
|
1
|
IComponentDef cd2 = new ComponentDefImpl(typeof(B2), "B");
|
62
|
1
|
container.Register(cd);
|
63
|
1
|
container.Register(cd2);
|
64
|
1
|
try
|
65
|
|
{
|
66
|
1
|
container.GetComponent("B");
|
67
|
0
|
Assert.Fail();
|
68
|
|
}
|
69
|
|
catch(TooManyRegistrationRuntimeException ex)
|
70
|
|
{
|
71
|
1
|
Console.WriteLine(ex.Message);
|
72
|
1
|
Assert.AreEqual("B", ex.Key);
|
73
|
1
|
Assert.AreEqual(2, ex.ComponentTypes.Length);
|
74
|
1
|
Assert.AreEqual(typeof(B), ex.ComponentTypes[0]);
|
75
|
1
|
Assert.AreEqual(typeof(B2), ex.ComponentTypes[1]);
|
76
|
|
}
|
77
|
|
}
|
78
|
|
|
79
|
1
|
[Test]
|
80
|
|
public void TestInclude()
|
81
|
|
{
|
82
|
1
|
IS2Container container = new S2ContainerImpl();
|
83
|
1
|
container.Register(typeof(A));
|
84
|
1
|
IS2Container container2 = new S2ContainerImpl();
|
85
|
1
|
container2.Register(typeof(B));
|
86
|
1
|
container.Include(container2);
|
87
|
1
|
A a = (A) container.GetComponent(typeof(A));
|
88
|
1
|
Assert.AreEqual("B", a.HogeName);
|
89
|
|
}
|
90
|
|
|
91
|
1
|
[Test]
|
92
|
|
public void TestInclude2()
|
93
|
|
{
|
94
|
1
|
IS2Container root = new S2ContainerImpl();
|
95
|
1
|
IS2Container child = new S2ContainerImpl();
|
96
|
1
|
child.Namespace = "aaa";
|
97
|
1
|
child.Register("hoge", "hoge");
|
98
|
1
|
root.Include(child);
|
99
|
1
|
IS2Container child2 = new S2ContainerImpl();
|
100
|
1
|
child2.Namespace = "bbb";
|
101
|
1
|
child2.Register("hoge2", "hoge");
|
102
|
1
|
IS2Container grandchild = new S2ContainerImpl();
|
103
|
1
|
grandchild.Namespace = "ccc";
|
104
|
1
|
grandchild.Register("hoge3", "hoge");
|
105
|
1
|
child2.Include(grandchild);
|
106
|
1
|
root.Include(child2);
|
107
|
1
|
Assert.AreEqual("hoge", child.GetComponent("hoge"));
|
108
|
1
|
Assert.AreEqual("hoge3", grandchild.GetComponent("hoge"));
|
109
|
1
|
Assert.AreEqual(child, root.GetComponent("aaa"));
|
110
|
1
|
Assert.AreEqual(child2, root.GetComponent("bbb"));
|
111
|
1
|
Assert.AreEqual("hoge", root.GetComponent("aaa.hoge"));
|
112
|
1
|
Assert.AreEqual("hoge2", root.GetComponent("bbb.hoge"));
|
113
|
1
|
Assert.AreEqual("hoge3", root.GetComponent("ccc.hoge"));
|
114
|
1
|
Assert.AreEqual("hoge", child.GetComponent("aaa.hoge"));
|
115
|
1
|
Assert.AreEqual(false, child.HasComponentDef("bbb.hoge"));
|
116
|
1
|
Assert.AreEqual(false, child.HasComponentDef("ccc.hoge"));
|
117
|
1
|
Assert.AreEqual("hoge2", child2.GetComponent("hoge"));
|
118
|
1
|
Assert.AreEqual("hoge3", child2.GetComponent("ccc.hoge"));
|
119
|
1
|
Assert.AreEqual(0, root.ComponentDefSize);
|
120
|
|
}
|
121
|
|
|
122
|
1
|
[Test]
|
123
|
|
public void TestInclude3()
|
124
|
|
{
|
125
|
1
|
IS2Container container = new S2ContainerImpl();
|
126
|
1
|
IS2Container child = new S2ContainerImpl();
|
127
|
1
|
child.Path = "aaa.xml";
|
128
|
1
|
IS2Container grandchild = new S2ContainerImpl();
|
129
|
1
|
grandchild.Path = "bbb.xml";
|
130
|
1
|
grandchild.Namespace = "bbb";
|
131
|
1
|
child.Include(grandchild);
|
132
|
1
|
container.Include(child);
|
133
|
1
|
container.Include(grandchild);
|
134
|
1
|
Assert.IsNotNull(container.GetComponent("bbb"));
|
135
|
|
}
|
136
|
|
|
137
|
1
|
[Test]
|
138
|
|
public void TestInclude4()
|
139
|
|
{
|
140
|
1
|
IS2Container aaa = new S2ContainerImpl();
|
141
|
1
|
aaa.Path = "aaa.xml";
|
142
|
1
|
aaa.Namespace = "aaa";
|
143
|
1
|
IS2Container bbb = new S2ContainerImpl();
|
144
|
1
|
bbb.Path = "bbb.xml";
|
145
|
1
|
IS2Container aaa2 = new S2ContainerImpl();
|
146
|
1
|
aaa2.Path = "aaa.xml";
|
147
|
1
|
aaa2.Namespace = "aaa";
|
148
|
1
|
bbb.Include(aaa2);
|
149
|
1
|
aaa.Include(bbb);
|
150
|
1
|
Assert.IsNotNull(aaa.GetComponentDef("aaa"));
|
151
|
|
}
|
152
|
|
|
153
|
1
|
[Test]
|
154
|
|
public void TestInclude5()
|
155
|
|
{
|
156
|
1
|
IS2Container container = new S2ContainerImpl();
|
157
|
1
|
IS2Container child = new S2ContainerImpl();
|
158
|
1
|
child.Namespace = "aaa";
|
159
|
1
|
IS2Container child2 = new S2ContainerImpl();
|
160
|
1
|
child2.Namespace = "aaa";
|
161
|
1
|
container.Include(child);
|
162
|
1
|
container.Include(child2);
|
163
|
1
|
try
|
164
|
|
{
|
165
|
1
|
container.GetComponent("aaa");
|
166
|
0
|
Assert.Fail();
|
167
|
|
}
|
168
|
|
catch(TooManyRegistrationRuntimeException ex)
|
169
|
|
{
|
170
|
1
|
Console.WriteLine(ex.Message);
|
171
|
|
}
|
172
|
|
}
|
173
|
|
|
174
|
1
|
[Test]
|
175
|
|
public void TestInitAndDestroy()
|
176
|
|
{
|
177
|
1
|
IS2Container container = new S2ContainerImpl();
|
178
|
1
|
IS2Container child = new S2ContainerImpl();
|
179
|
1
|
IList initList = new ArrayList();
|
180
|
1
|
IList destroyList = new ArrayList();
|
181
|
1
|
IComponentDef componentDef = new ComponentDefImpl(typeof(C), "c1");
|
182
|
1
|
componentDef.AddInitMethodDef(new InitMethodDefImpl("Init"));
|
183
|
1
|
componentDef.AddDestroyMethodDef(new DestroyMethodDefImpl("Destroy"));
|
184
|
1
|
componentDef.AddArgDef(new ArgDefImpl("c1"));
|
185
|
1
|
componentDef.AddArgDef(new ArgDefImpl(initList));
|
186
|
1
|
componentDef.AddArgDef(new ArgDefImpl(destroyList));
|
187
|
1
|
container.Register(componentDef);
|
188
|
|
|
189
|
1
|
componentDef = new ComponentDefImpl(typeof(C), "c2");
|
190
|
1
|
componentDef.AddInitMethodDef(new InitMethodDefImpl("Init"));
|
191
|
1
|
componentDef.AddDestroyMethodDef(new DestroyMethodDefImpl("Destroy"));
|
192
|
1
|
componentDef.AddArgDef(new ArgDefImpl("c2"));
|
193
|
1
|
componentDef.AddArgDef(new ArgDefImpl(initList));
|
194
|
1
|
componentDef.AddArgDef(new ArgDefImpl(destroyList));
|
195
|
1
|
container.Register(componentDef);
|
196
|
|
|
197
|
1
|
componentDef = new ComponentDefImpl(typeof(C), "c3");
|
198
|
1
|
componentDef.AddInitMethodDef(new InitMethodDefImpl("Init"));
|
199
|
1
|
componentDef.AddDestroyMethodDef(new DestroyMethodDefImpl("Destroy"));
|
200
|
1
|
componentDef.AddArgDef(new ArgDefImpl("c3"));
|
201
|
1
|
componentDef.AddArgDef(new ArgDefImpl(initList));
|
202
|
1
|
componentDef.AddArgDef(new ArgDefImpl(destroyList));
|
203
|
1
|
child.Register(componentDef);
|
204
|
1
|
container.Include(child);
|
205
|
|
|
206
|
1
|
container.Init();
|
207
|
1
|
Assert.AreEqual(3, initList.Count);
|
208
|
1
|
Assert.AreEqual("c3", initList[0]);
|
209
|
1
|
Assert.AreEqual("c1", initList[1]);
|
210
|
1
|
Assert.AreEqual("c2", initList[2]);
|
211
|
1
|
container.Destroy();
|
212
|
1
|
Assert.AreEqual(3, destroyList.Count);
|
213
|
1
|
Assert.AreEqual("c2", destroyList[0]);
|
214
|
1
|
Assert.AreEqual("c1", destroyList[1]);
|
215
|
1
|
Assert.AreEqual("c3", destroyList[2]);
|
216
|
|
}
|
217
|
|
|
218
|
1
|
[Test]
|
219
|
|
public void TestInjectDependency()
|
220
|
|
{
|
221
|
1
|
IS2Container container = new S2ContainerImpl();
|
222
|
1
|
IComponentDef cd = new ComponentDefImpl(typeof(Hashtable), "hoge");
|
223
|
1
|
cd.InstanceMode = "outer";
|
224
|
1
|
IInitMethodDef md = new InitMethodDefImpl("Add");
|
225
|
1
|
md.AddArgDef(new ArgDefImpl("aaa"));
|
226
|
1
|
md.AddArgDef(new ArgDefImpl("111"));
|
227
|
1
|
cd.AddInitMethodDef(md);
|
228
|
1
|
container.Register(cd);
|
229
|
|
|
230
|
1
|
Hashtable table = new Hashtable();
|
231
|
1
|
container.InjectDependency(table);
|
232
|
1
|
Assert.AreEqual("111", table["aaa"]);
|
233
|
|
|
234
|
1
|
Hashtable table2 = new Hashtable();
|
235
|
1
|
container.InjectDependency(table2, typeof(Hashtable));
|
236
|
1
|
Assert.AreEqual("111", table2["aaa"]);
|
237
|
|
|
238
|
1
|
Hashtable table3 = new Hashtable();
|
239
|
1
|
container.InjectDependency(table3, "hoge");
|
240
|
1
|
Assert.AreEqual("111", table3["aaa"]);
|
241
|
|
}
|
242
|
|
|
243
|
1
|
[Test]
|
244
|
|
public void TestSelf()
|
245
|
|
{
|
246
|
1
|
IS2Container container = new S2ContainerImpl();
|
247
|
1
|
container.Register(typeof(D));
|
248
|
1
|
D d = (D) container.GetComponent(typeof(D));
|
249
|
1
|
Assert.AreSame(container, d.Container);
|
250
|
|
}
|
251
|
|
|
252
|
1
|
[Test]
|
253
|
|
public void TestSelf2()
|
254
|
|
{
|
255
|
1
|
IS2Container container = new S2ContainerImpl();
|
256
|
1
|
IComponentDef cd = new ComponentDefImpl(typeof(D));
|
257
|
1
|
IPropertyDef pd = new PropertyDefImpl("container");
|
258
|
1
|
pd.Expression = ContainerConstants.CONTAINER_NAME;
|
259
|
1
|
cd.AddPropertyDef(pd);
|
260
|
1
|
container.Register(cd);
|
261
|
1
|
D d = (D) container.GetComponent(typeof(D));
|
262
|
1
|
Assert.AreSame(container, d.Container);
|
263
|
|
}
|
264
|
|
|
265
|
1
|
[Test]
|
266
|
|
public void TestConstructor()
|
267
|
|
{
|
268
|
1
|
IS2Container container = new S2ContainerImpl();
|
269
|
1
|
Assert.AreEqual(0, container.ComponentDefSize);
|
270
|
|
}
|
271
|
|
|
272
|
1
|
[Test]
|
273
|
|
public void TestNamespace()
|
274
|
|
{
|
275
|
1
|
IS2Container container = new S2ContainerImpl();
|
276
|
1
|
container.Namespace = "aaa";
|
277
|
1
|
container.Register(typeof(StringBuilder), "bbb");
|
278
|
1
|
Assert.IsNotNull(container.GetComponent("bbb"));
|
279
|
1
|
Assert.IsNotNull(container.GetComponent("aaa.bbb"));
|
280
|
|
}
|
281
|
|
|
282
|
1
|
[Test]
|
283
|
|
public void TestGetComponentDef()
|
284
|
|
{
|
285
|
1
|
IS2Container aaa = new S2ContainerImpl();
|
286
|
1
|
aaa.Namespace = "aaa";
|
287
|
1
|
IS2Container bbb = new S2ContainerImpl();
|
288
|
1
|
bbb.Namespace = "bbb";
|
289
|
1
|
bbb.Register(typeof(StringBuilder), "hoge");
|
290
|
1
|
aaa.Include(bbb);
|
291
|
1
|
Assert.IsNotNull(aaa.GetComponentDef("bbb.hoge"));
|
292
|
1
|
Assert.IsNotNull(bbb.GetComponentDef("bbb.hoge"));
|
293
|
|
}
|
294
|
|
|
295
|
1
|
[Test]
|
296
|
|
public void TestGetComponentDef2()
|
297
|
|
{
|
298
|
1
|
IS2Container container = new S2ContainerImpl();
|
299
|
1
|
container.Register(typeof(FooImpl));
|
300
|
1
|
IHoge hoge = (IHoge) container.GetComponent(typeof(IHoge));
|
301
|
1
|
Assert.AreEqual("Foo", hoge.Name);
|
302
|
|
}
|
303
|
|
|
304
|
|
public class A
|
305
|
|
{
|
306
|
|
private IHoge hoge_;
|
307
|
|
|
308
|
1
|
public A(IHoge hoge)
|
309
|
|
{
|
310
|
1
|
hoge_ = hoge;
|
311
|
|
}
|
312
|
|
|
313
|
|
public string HogeName
|
314
|
|
{
|
315
|
1
|
get { return hoge_.Name; }
|
316
|
|
}
|
317
|
|
}
|
318
|
|
|
319
|
|
public interface IHoge
|
320
|
|
{
|
321
|
|
string Name { get; }
|
322
|
|
}
|
323
|
|
|
324
|
|
public interface IFoo : IHoge
|
325
|
|
{
|
326
|
|
}
|
327
|
|
|
328
|
|
public class B : IHoge
|
329
|
|
{
|
330
|
|
public string Name
|
331
|
|
{
|
332
|
1
|
get { return "B"; }
|
333
|
|
}
|
334
|
|
}
|
335
|
|
|
336
|
|
public class B2 : IHoge
|
337
|
|
{
|
338
|
0
|
public string Name
|
339
|
|
{
|
340
|
|
get { return "B2"; }
|
341
|
|
}
|
342
|
|
}
|
343
|
|
|
344
|
|
public class C
|
345
|
|
{
|
346
|
|
private string name_;
|
347
|
|
private IList initList_;
|
348
|
|
private IList destroyList_;
|
349
|
|
|
350
|
3
|
public C(string name, IList initList, IList destroyList)
|
351
|
|
{
|
352
|
3
|
name_ = name;
|
353
|
3
|
initList_ = initList;
|
354
|
3
|
destroyList_ = destroyList;
|
355
|
|
}
|
356
|
|
|
357
|
3
|
public void Init()
|
358
|
|
{
|
359
|
3
|
initList_.Add(name_);
|
360
|
|
}
|
361
|
|
|
362
|
3
|
public void Destroy()
|
363
|
|
{
|
364
|
3
|
destroyList_.Add(name_);
|
365
|
|
}
|
366
|
|
}
|
367
|
|
|
368
|
|
public class D
|
369
|
|
{
|
370
|
|
private IS2Container container_;
|
371
|
|
|
372
|
|
public IS2Container Container
|
373
|
|
{
|
374
|
2
|
get { return container_; }
|
375
|
2
|
set { container_ = value; }
|
376
|
|
}
|
377
|
|
}
|
378
|
|
|
379
|
|
public class FooImpl : IFoo
|
380
|
|
{
|
381
|
|
public string Name
|
382
|
|
{
|
383
|
1
|
get { return "Foo"; }
|
384
|
|
}
|
385
|
|
}
|
386
|
|
|
387
|
|
}
|
388
|
|
}
|
389
|
|
|