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.Collections;
|
21
|
|
using System.IO;
|
22
|
|
using System.Reflection;
|
23
|
|
using System.Text;
|
24
|
|
using NUnit.Framework;
|
25
|
|
using Seasar.Framework.Aop;
|
26
|
|
using Seasar.Framework.Container;
|
27
|
|
using Seasar.Framework.Container.Deployer;
|
28
|
|
using Seasar.Framework.Container.Impl;
|
29
|
|
using log4net;
|
30
|
|
using log4net.Config;
|
31
|
|
using log4net.Util;
|
32
|
|
|
33
|
|
namespace Seasar.Tests.Framework.Container.Deployer
|
34
|
|
{
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
[TestFixture]
|
39
|
|
public class SingletonComponentDeployerTest
|
40
|
|
{
|
41
|
20
|
[SetUp]
|
42
|
|
public void Setup()
|
43
|
|
{
|
44
|
20
|
FileInfo info = new FileInfo(SystemInfo.AssemblyFileName(
|
45
|
|
Assembly.GetExecutingAssembly()) + ".config");
|
46
|
20
|
XmlConfigurator.Configure(LogManager.GetRepository(), info);
|
47
|
|
}
|
48
|
|
|
49
|
1
|
[Test]
|
50
|
|
public void TestDeployAutoAutoConstructor()
|
51
|
|
{
|
52
|
1
|
IS2Container container = new S2ContainerImpl();
|
53
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(A));
|
54
|
1
|
container.Register(cd);
|
55
|
1
|
container.Register(typeof(B));
|
56
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
57
|
1
|
A a = (A) deployer.Deploy(typeof(A));
|
58
|
1
|
Assert.AreEqual("B", a.HogeName);
|
59
|
1
|
Assert.AreEqual(a, deployer.Deploy(typeof(A)));
|
60
|
|
}
|
61
|
|
|
62
|
1
|
[Test]
|
63
|
|
public void TestDeployAutoAutoConstructorAndProperty()
|
64
|
|
{
|
65
|
1
|
IS2Container container = new S2ContainerImpl();
|
66
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(A));
|
67
|
1
|
cd.AddPropertyDef(new PropertyDefImpl("Aaa", 1));
|
68
|
1
|
container.Register(cd);
|
69
|
1
|
container.Register(typeof(B));
|
70
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
71
|
1
|
A a = (A) deployer.Deploy(typeof(A));
|
72
|
1
|
Assert.AreEqual("B", a.HogeName);
|
73
|
1
|
Assert.AreEqual(1, a.Aaa);
|
74
|
1
|
Assert.AreEqual(a, deployer.Deploy(typeof(A)));
|
75
|
|
}
|
76
|
|
|
77
|
1
|
[Test]
|
78
|
|
public void TestDeployAutoAutoConstructorAndProperty2()
|
79
|
|
{
|
80
|
1
|
IS2Container container = new S2ContainerImpl();
|
81
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(A2));
|
82
|
1
|
cd.AddPropertyDef(new PropertyDefImpl("Aaa", 1));
|
83
|
1
|
container.Register(cd);
|
84
|
1
|
container.Register(typeof(B));
|
85
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
86
|
1
|
A2 a2 = (A2) deployer.Deploy(typeof(A2));
|
87
|
1
|
Assert.AreEqual("B", a2.HogeName);
|
88
|
1
|
Assert.AreEqual(1, a2.Aaa);
|
89
|
|
}
|
90
|
|
|
91
|
1
|
[Test]
|
92
|
|
public void TestDeployAutoAutoProperty()
|
93
|
|
{
|
94
|
1
|
IS2Container container = new S2ContainerImpl();
|
95
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(A2));
|
96
|
1
|
container.Register(cd);
|
97
|
1
|
container.Register(typeof(B));
|
98
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
99
|
1
|
A2 a2 = (A2) deployer.Deploy(typeof(A2));
|
100
|
1
|
Assert.AreEqual("B", a2.HogeName);
|
101
|
|
}
|
102
|
|
|
103
|
1
|
[Test]
|
104
|
|
public void TestDeployAspect1()
|
105
|
|
{
|
106
|
1
|
IS2Container container = new S2ContainerImpl();
|
107
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(CulcImpl1));
|
108
|
|
|
109
|
1
|
IAspectDef ad = new AspectDefImpl();
|
110
|
1
|
ad.Expression = "plusOne";
|
111
|
1
|
ad.Container = container;
|
112
|
1
|
cd.AddAspeceDef(ad);
|
113
|
1
|
ComponentDefImpl plusOneCd = new ComponentDefImpl(typeof(PlusOneInterceptor), "plusOne");
|
114
|
1
|
container.Register(plusOneCd);
|
115
|
1
|
container.Register(cd);
|
116
|
|
|
117
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
118
|
1
|
ICulc culc = (ICulc) deployer.Deploy(typeof(ICulc));
|
119
|
1
|
PlusOneInterceptor.Count = 0;
|
120
|
1
|
Assert.AreEqual(1, culc.Count());
|
121
|
|
}
|
122
|
|
|
123
|
1
|
[Test]
|
124
|
|
public void TestDeployAspect2()
|
125
|
|
{
|
126
|
1
|
IS2Container container = new S2ContainerImpl();
|
127
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(CulcImpl2));
|
128
|
|
|
129
|
1
|
IAspectDef ad = new AspectDefImpl();
|
130
|
1
|
ad.Expression = "plusOne";
|
131
|
1
|
ad.Container = container;
|
132
|
1
|
cd.AddAspeceDef(ad);
|
133
|
1
|
ComponentDefImpl plusOneCd = new ComponentDefImpl(typeof(PlusOneInterceptor), "plusOne");
|
134
|
1
|
container.Register(plusOneCd);
|
135
|
1
|
container.Register(cd);
|
136
|
|
|
137
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
138
|
1
|
CulcImpl2 culc = (CulcImpl2) deployer.Deploy(typeof(CulcImpl2));
|
139
|
1
|
PlusOneInterceptor.Count = 0;
|
140
|
1
|
Assert.AreEqual(1, culc.Count());
|
141
|
|
}
|
142
|
|
|
143
|
1
|
[Test]
|
144
|
|
public void TestDeployAspect3()
|
145
|
|
{
|
146
|
1
|
IS2Container container = new S2ContainerImpl();
|
147
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(CulcImpl2));
|
148
|
|
|
149
|
1
|
IAspectDef ad = new AspectDefImpl();
|
150
|
1
|
ad.Expression = "plusOne";
|
151
|
1
|
ad.Container = container;
|
152
|
|
|
153
|
1
|
IAspectDef ad2 = new AspectDefImpl();
|
154
|
1
|
ad2.Expression = "plusOne";
|
155
|
1
|
ad2.Container = container;
|
156
|
|
|
157
|
1
|
cd.AddAspeceDef(ad);
|
158
|
1
|
cd.AddAspeceDef(ad2);
|
159
|
1
|
ComponentDefImpl plusOneCd = new ComponentDefImpl(typeof(PlusOneInterceptor), "plusOne");
|
160
|
1
|
container.Register(plusOneCd);
|
161
|
1
|
container.Register(cd);
|
162
|
|
|
163
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
164
|
1
|
CulcImpl2 culc = (CulcImpl2) deployer.Deploy(typeof(CulcImpl2));
|
165
|
1
|
PlusOneInterceptor.Count = 0;
|
166
|
1
|
Assert.AreEqual(2, culc.Count());
|
167
|
|
}
|
168
|
|
|
169
|
1
|
[Test]
|
170
|
|
public void TestDeployAutoManualConstructor()
|
171
|
|
{
|
172
|
1
|
IS2Container container = new S2ContainerImpl();
|
173
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(Decimal));
|
174
|
1
|
cd.AddArgDef(new ArgDefImpl(123));
|
175
|
1
|
container.Register(cd);
|
176
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
177
|
1
|
Assert.AreEqual(new Decimal(123), deployer.Deploy(typeof(decimal)));
|
178
|
|
}
|
179
|
|
|
180
|
1
|
[Test]
|
181
|
|
public void TestDeployAutoManualProperty()
|
182
|
|
{
|
183
|
1
|
IS2Container container = new S2ContainerImpl();
|
184
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(A2));
|
185
|
1
|
cd.AddPropertyDef(new PropertyDefImpl("Hoge", new B()));
|
186
|
1
|
container.Register(cd);
|
187
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
188
|
1
|
A2 a2 = (A2) deployer.Deploy(typeof(A2));
|
189
|
1
|
Assert.AreEqual("B", a2.HogeName);
|
190
|
|
}
|
191
|
|
|
192
|
1
|
[Test]
|
193
|
|
public void TestDeployAutoManual()
|
194
|
|
{
|
195
|
1
|
IS2Container container = new S2ContainerImpl();
|
196
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(D));
|
197
|
1
|
cd.AddArgDef(new ArgDefImpl("abc"));
|
198
|
1
|
cd.AddPropertyDef(new PropertyDefImpl("Aaa", 1));
|
199
|
1
|
container.Register(cd);
|
200
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
201
|
1
|
D d = (D) deployer.Deploy(typeof(D));
|
202
|
1
|
Assert.AreEqual("abc", d.Name);
|
203
|
1
|
Assert.AreEqual(1, d.Aaa);
|
204
|
|
}
|
205
|
|
|
206
|
1
|
[Test]
|
207
|
|
public void TestGetComponentForInitMethodDef()
|
208
|
|
{
|
209
|
1
|
IS2Container container = new S2ContainerImpl();
|
210
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(Hashtable));
|
211
|
1
|
IInitMethodDef md = new InitMethodDefImpl("Add");
|
212
|
1
|
md.AddArgDef(new ArgDefImpl("aaa"));
|
213
|
1
|
md.AddArgDef(new ArgDefImpl("hoge"));
|
214
|
1
|
cd.AddInitMethodDef(md);
|
215
|
1
|
container.Register(cd);
|
216
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
217
|
1
|
Hashtable table = (Hashtable) deployer.Deploy(typeof(Hashtable));
|
218
|
1
|
Assert.AreEqual("hoge", table["aaa"]);
|
219
|
|
}
|
220
|
|
|
221
|
1
|
[Test]
|
222
|
|
public void TestCyclicReference()
|
223
|
|
{
|
224
|
1
|
IS2Container container = new S2ContainerImpl();
|
225
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(A2));
|
226
|
1
|
ComponentDefImpl cd2 = new ComponentDefImpl(typeof(C));
|
227
|
1
|
container.Register(cd);
|
228
|
1
|
container.Register(cd2);
|
229
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
230
|
1
|
IComponentDeployer deployer2 = new SingletonComponentDeployer(cd2);
|
231
|
1
|
A2 a2 = (A2) deployer.Deploy(typeof(A2));
|
232
|
1
|
C c = (C) deployer2.Deploy(typeof(C));
|
233
|
1
|
Assert.AreEqual("C", a2.HogeName);
|
234
|
1
|
Assert.AreEqual("C", c.HogeName);
|
235
|
|
}
|
236
|
|
|
237
|
1
|
[Test]
|
238
|
|
public void TestDeployConstructor()
|
239
|
|
{
|
240
|
1
|
IS2Container container = new S2ContainerImpl();
|
241
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(A));
|
242
|
1
|
container.Register(cd);
|
243
|
1
|
container.Register(typeof(B));
|
244
|
1
|
cd.AutoBindingMode = "constructor";
|
245
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
246
|
1
|
A a = (A) deployer.Deploy(typeof(A));
|
247
|
1
|
Assert.AreEqual("B", a.HogeName);
|
248
|
|
}
|
249
|
|
|
250
|
1
|
[Test]
|
251
|
|
public void TestDeployProperty()
|
252
|
|
{
|
253
|
1
|
IS2Container container = new S2ContainerImpl();
|
254
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(A2));
|
255
|
1
|
container.Register(cd);
|
256
|
1
|
container.Register(typeof(B));
|
257
|
1
|
cd.AutoBindingMode = "property";
|
258
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
259
|
1
|
A2 a2 = (A2) deployer.Deploy(typeof(A2));
|
260
|
1
|
Assert.AreEqual("B", a2.HogeName);
|
261
|
|
}
|
262
|
|
|
263
|
1
|
[Test]
|
264
|
|
public void TestDeployNoneManualConstructor()
|
265
|
|
{
|
266
|
1
|
IS2Container container = new S2ContainerImpl();
|
267
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(Decimal));
|
268
|
1
|
cd.AddArgDef(new ArgDefImpl(123));
|
269
|
1
|
container.Register(cd);
|
270
|
1
|
cd.AutoBindingMode = "none";
|
271
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
272
|
1
|
Assert.AreEqual(new Decimal(123), deployer.Deploy(typeof(decimal)));
|
273
|
|
}
|
274
|
|
|
275
|
1
|
[Test]
|
276
|
|
public void TestDeployNoneManualProperty()
|
277
|
|
{
|
278
|
1
|
IS2Container container = new S2ContainerImpl();
|
279
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(A2));
|
280
|
1
|
cd.AddPropertyDef(new PropertyDefImpl("Hoge", new B()));
|
281
|
1
|
container.Register(cd);
|
282
|
1
|
cd.AutoBindingMode = "none";
|
283
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
284
|
1
|
A2 a2 = (A2) deployer.Deploy(typeof(A2));
|
285
|
1
|
Assert.AreEqual("B", a2.HogeName);
|
286
|
|
}
|
287
|
|
|
288
|
1
|
[Test]
|
289
|
|
public void TestDeployNoneDefault()
|
290
|
|
{
|
291
|
1
|
IS2Container container = new S2ContainerImpl();
|
292
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(StringBuilder));
|
293
|
1
|
container.Register(cd);
|
294
|
1
|
cd.AutoBindingMode = "none";
|
295
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
296
|
1
|
Assert.AreEqual("", ((StringBuilder)deployer.Deploy(typeof(StringBuilder))).ToString());
|
297
|
|
}
|
298
|
|
|
299
|
1
|
[Test]
|
300
|
|
public void TestDeploy()
|
301
|
|
{
|
302
|
1
|
IS2Container container = new S2ContainerImpl();
|
303
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(Hashtable));
|
304
|
1
|
IDestroyMethodDef md = new DestroyMethodDefImpl("Add");
|
305
|
1
|
md.AddArgDef(new ArgDefImpl("aaa"));
|
306
|
1
|
md.AddArgDef(new ArgDefImpl("hoge"));
|
307
|
1
|
cd.AddDestroyMethodDef(md);
|
308
|
1
|
container.Register(cd);
|
309
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
310
|
1
|
Hashtable table = (Hashtable) deployer.Deploy(typeof(Hashtable));
|
311
|
1
|
deployer.Destroy();
|
312
|
1
|
Assert.AreEqual("hoge", table["aaa"]);
|
313
|
|
}
|
314
|
|
|
315
|
1
|
[Test]
|
316
|
|
public void TestDestroy()
|
317
|
|
{
|
318
|
1
|
IS2Container container = new S2ContainerImpl();
|
319
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(D));
|
320
|
1
|
cd.AddArgDef(new ArgDefImpl("name"));
|
321
|
1
|
container.Register(cd);
|
322
|
1
|
container.Init();
|
323
|
1
|
container.Destroy();
|
324
|
|
}
|
325
|
|
|
326
|
1
|
[Test]
|
327
|
|
public void TestInjectDependency()
|
328
|
|
{
|
329
|
1
|
IS2Container container = new S2ContainerImpl();
|
330
|
1
|
ComponentDefImpl cd = new ComponentDefImpl(typeof(Hashtable));
|
331
|
1
|
container.Register(cd);
|
332
|
1
|
IComponentDeployer deployer = new SingletonComponentDeployer(cd);
|
333
|
1
|
try
|
334
|
|
{
|
335
|
1
|
deployer.InjectDependency(new Hashtable());
|
336
|
0
|
Assert.Fail();
|
337
|
|
}
|
338
|
|
catch(NotSupportedException ex)
|
339
|
|
{
|
340
|
1
|
Console.WriteLine(ex);
|
341
|
|
}
|
342
|
|
}
|
343
|
|
|
344
|
|
public class PlusOneInterceptor : IMethodInterceptor
|
345
|
|
{
|
346
|
|
public static int Count = 0;
|
347
|
4
|
public object Invoke(IMethodInvocation invocation)
|
348
|
|
{
|
349
|
4
|
++Count;
|
350
|
4
|
invocation.Proceed();
|
351
|
4
|
return Count;
|
352
|
|
}
|
353
|
|
}
|
354
|
|
|
355
|
|
public interface ICulc
|
356
|
|
{
|
357
|
|
int Count();
|
358
|
|
}
|
359
|
|
|
360
|
|
public class CulcImpl1 : ICulc
|
361
|
|
{
|
362
|
1
|
public int Count()
|
363
|
|
{
|
364
|
1
|
return 0;
|
365
|
|
}
|
366
|
|
}
|
367
|
|
|
368
|
|
public class CulcImpl2 : MarshalByRefObject, ICulc
|
369
|
|
{
|
370
|
2
|
public int Count()
|
371
|
|
{
|
372
|
2
|
return 0;
|
373
|
|
}
|
374
|
|
}
|
375
|
|
|
376
|
|
|
377
|
|
public interface IFoo
|
378
|
|
{
|
379
|
|
string HogeName { get; }
|
380
|
|
}
|
381
|
|
|
382
|
|
public class A
|
383
|
|
{
|
384
|
|
private IHoge hoge_;
|
385
|
|
private int aaa_;
|
386
|
|
|
387
|
3
|
public A(IHoge hoge)
|
388
|
|
{
|
389
|
3
|
hoge_ = hoge;
|
390
|
|
}
|
391
|
|
|
392
|
|
public string HogeName
|
393
|
|
{
|
394
|
3
|
get { return hoge_.Name; }
|
395
|
|
}
|
396
|
|
|
397
|
|
public int Aaa
|
398
|
|
{
|
399
|
1
|
get { return aaa_; }
|
400
|
1
|
set { aaa_ = value; }
|
401
|
|
}
|
402
|
|
}
|
403
|
|
|
404
|
|
public class A2 : IFoo
|
405
|
|
{
|
406
|
|
private IHoge hoge_;
|
407
|
|
private int aaa_;
|
408
|
|
|
409
|
|
public IHoge Hoge
|
410
|
|
{
|
411
|
7
|
set { hoge_ = value; }
|
412
|
|
}
|
413
|
|
|
414
|
|
public string HogeName
|
415
|
|
{
|
416
|
7
|
get { return hoge_.Name; }
|
417
|
|
}
|
418
|
|
|
419
|
|
public int Aaa
|
420
|
|
{
|
421
|
1
|
get { return aaa_; }
|
422
|
1
|
set { aaa_ = value; }
|
423
|
|
}
|
424
|
|
}
|
425
|
|
|
426
|
|
public interface IHoge
|
427
|
|
{
|
428
|
|
string Name { get; }
|
429
|
|
}
|
430
|
|
|
431
|
|
public class B : IHoge
|
432
|
|
{
|
433
|
|
public string Name
|
434
|
|
{
|
435
|
8
|
get { return "B"; }
|
436
|
|
}
|
437
|
|
}
|
438
|
|
|
439
|
|
public class C : IHoge
|
440
|
|
{
|
441
|
|
private IFoo foo_;
|
442
|
|
|
443
|
|
public IFoo Foo
|
444
|
|
{
|
445
|
2
|
set { foo_ = value; }
|
446
|
|
}
|
447
|
|
|
448
|
|
public string Name
|
449
|
|
{
|
450
|
2
|
get { return "C"; }
|
451
|
|
}
|
452
|
|
|
453
|
|
public string HogeName
|
454
|
|
{
|
455
|
1
|
get { return foo_.HogeName; }
|
456
|
|
}
|
457
|
|
}
|
458
|
|
|
459
|
|
public class D : IHoge, IDisposable
|
460
|
|
{
|
461
|
|
private string name_;
|
462
|
|
private int aaa_;
|
463
|
|
|
464
|
2
|
public D(string name)
|
465
|
|
{
|
466
|
2
|
name_ = name;
|
467
|
|
}
|
468
|
|
|
469
|
|
public string Name
|
470
|
|
{
|
471
|
1
|
get { return name_; }
|
472
|
|
}
|
473
|
|
|
474
|
|
public int Aaa
|
475
|
|
{
|
476
|
1
|
get { return aaa_; }
|
477
|
1
|
set { aaa_ = value; }
|
478
|
|
}
|
479
|
|
|
480
|
|
#region IDisposable メンバ
|
481
|
|
|
482
|
0
|
public void Dispose()
|
483
|
|
{
|
484
|
|
Console.WriteLine("Dispose!");
|
485
|
|
}
|
486
|
|
|
487
|
|
#endregion
|
488
|
|
}
|
489
|
|
}
|
490
|
|
}
|
491
|
|
|