1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
|
//
// System.ComponentModel.Container test cases
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
// Ivan N. Zlatev (contact i-nZ.net)
// Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
// Copyright (c) 2006 Ivan N. Zlatev
//
#if !MOBILE
using NUnit.Framework;
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
namespace MonoTests.System.ComponentModel
{
class TestService {
}
class TestContainer : Container {
ServiceContainer _services = new ServiceContainer ();
bool allowDuplicateNames;
public TestContainer()
{
_services.AddService (typeof (TestService), new TestService ());
}
public bool AllowDuplicateNames {
get { return allowDuplicateNames; }
set { allowDuplicateNames = value; }
}
protected override object GetService (Type serviceType)
{
return _services.GetService (serviceType);
}
#if NET_2_0
public new void RemoveWithoutUnsiting (IComponent component)
{
base.RemoveWithoutUnsiting (component);
}
public void InvokeValidateName (IComponent component, string name)
{
ValidateName (component, name);
}
protected override void ValidateName (IComponent component, string name)
{
if (AllowDuplicateNames)
return;
base.ValidateName (component, name);
}
#endif
public bool Contains (IComponent component)
{
bool found = false;
foreach (IComponent c in Components) {
if (component.Equals (c)) {
found = true;
break;
}
}
return found;
}
public new void Dispose (bool disposing)
{
base.Dispose (disposing);
}
}
class TestComponent : Component {
public override ISite Site {
get {
return base.Site;
}
set {
base.Site = value;
if (value != null) {
Assert.IsNotNull (value.GetService (typeof (ISite)), "ISite");
Assert.IsNotNull (value.GetService (typeof (TestService)), "TestService");
}
}
}
public bool IsDisposed {
get { return disposed; }
}
public bool ThrowOnDispose {
get { return throwOnDispose; }
set { throwOnDispose = value; }
}
protected override void Dispose (bool disposing)
{
if (ThrowOnDispose)
throw new InvalidOperationException ();
base.Dispose (disposing);
disposed = true;
}
private bool disposed;
private bool throwOnDispose;
}
[TestFixture]
public class ContainerTest
{
private TestContainer _container;
[SetUp]
public void Init ()
{
_container = new TestContainer ();
}
[Test] // Add (IComponent)
public void Add1 ()
{
TestContainer containerA = new TestContainer ();
TestContainer containerB = new TestContainer ();
ISite siteA;
ISite siteB;
TestComponent compA = new TestComponent ();
Assert.IsNull (compA.Site);
TestComponent compB = new TestComponent ();
Assert.IsNull (compB.Site);
Assert.AreEqual (0, containerA.Components.Count);
Assert.AreEqual (0, containerB.Components.Count);
containerA.Add (compA);
siteA = compA.Site;
Assert.IsNotNull (siteA);
Assert.AreSame (compA, siteA.Component);
Assert.AreSame (containerA, siteA.Container);
Assert.IsFalse (siteA.DesignMode);
Assert.IsNull (siteA.Name);
containerA.Add (compB);
siteB = compB.Site;
Assert.IsNotNull (siteB);
Assert.AreSame (compB, siteB.Component);
Assert.AreSame (containerA, siteB.Container);
Assert.IsFalse (siteB.DesignMode);
Assert.IsNull (siteB.Name);
Assert.IsFalse (object.ReferenceEquals (siteA, siteB));
Assert.AreEqual (2, containerA.Components.Count);
Assert.AreEqual (0, containerB.Components.Count);
Assert.AreSame (compA, containerA.Components [0]);
Assert.AreSame (compB, containerA.Components [1]);
// check effect of adding component that is already member of
// another container
containerB.Add (compA);
Assert.IsFalse (object.ReferenceEquals (siteA, compA.Site));
siteA = compA.Site;
Assert.IsNotNull (siteA);
Assert.AreSame (compA, siteA.Component);
Assert.AreSame (containerB, siteA.Container);
Assert.IsFalse (siteA.DesignMode);
Assert.IsNull (siteA.Name);
Assert.AreEqual (1, containerA.Components.Count);
Assert.AreEqual (1, containerB.Components.Count);
Assert.AreSame (compB, containerA.Components [0]);
Assert.AreSame (compA, containerB.Components [0]);
// check effect of add component twice to same container
containerB.Add (compA);
Assert.AreSame (siteA, compA.Site);
Assert.AreEqual (1, containerA.Components.Count);
Assert.AreEqual (1, containerB.Components.Count);
Assert.AreSame (compB, containerA.Components [0]);
Assert.AreSame (compA, containerB.Components [0]);
}
[Test]
public void Add1_Component_Null ()
{
_container.Add ((IComponent) null);
Assert.AreEqual (0, _container.Components.Count);
}
[Test] // Add (IComponent, String)
public void Add2 ()
{
TestContainer containerA = new TestContainer ();
TestContainer containerB = new TestContainer ();
ISite siteA;
ISite siteB;
TestComponent compA = new TestComponent ();
Assert.IsNull (compA.Site);
TestComponent compB = new TestComponent ();
Assert.IsNull (compB.Site);
Assert.AreEqual (0, containerA.Components.Count);
Assert.AreEqual (0, containerB.Components.Count);
containerA.Add (compA, "A");
siteA = compA.Site;
Assert.IsNotNull (siteA);
Assert.AreSame (compA, siteA.Component);
Assert.AreSame (containerA, siteA.Container);
Assert.IsFalse (siteA.DesignMode);
Assert.AreEqual ("A", siteA.Name);
containerA.Add (compB, "B");
siteB = compB.Site;
Assert.IsNotNull (siteB);
Assert.AreSame (compB, siteB.Component);
Assert.AreSame (containerA, siteB.Container);
Assert.IsFalse (siteB.DesignMode);
Assert.AreEqual ("B", siteB.Name);
Assert.IsFalse (object.ReferenceEquals (siteA, siteB));
Assert.AreEqual (2, containerA.Components.Count);
Assert.AreEqual (0, containerB.Components.Count);
Assert.AreSame (compA, containerA.Components [0]);
Assert.AreSame (compB, containerA.Components [1]);
// check effect of adding component that is already member of
// another container
containerB.Add (compA, "A2");
Assert.IsFalse (object.ReferenceEquals (siteA, compA.Site));
siteA = compA.Site;
Assert.IsNotNull (siteA);
Assert.AreSame (compA, siteA.Component);
Assert.AreSame (containerB, siteA.Container);
Assert.IsFalse (siteA.DesignMode);
Assert.AreEqual ("A2", siteA.Name);
Assert.AreEqual (1, containerA.Components.Count);
Assert.AreEqual (1, containerB.Components.Count);
Assert.AreSame (compB, containerA.Components [0]);
Assert.AreSame (compA, containerB.Components [0]);
// check effect of add component twice to same container
containerB.Add (compA, "A2");
Assert.AreSame (siteA, compA.Site);
Assert.AreEqual ("A2", siteA.Name);
Assert.AreEqual (1, containerA.Components.Count);
Assert.AreEqual (1, containerB.Components.Count);
Assert.AreSame (compB, containerA.Components [0]);
Assert.AreSame (compA, containerB.Components [0]);
// add again with different name
containerB.Add (compA, "A3");
Assert.AreSame (siteA, compA.Site);
Assert.AreEqual ("A2", siteA.Name);
Assert.AreEqual (1, containerA.Components.Count);
Assert.AreEqual (1, containerB.Components.Count);
Assert.AreSame (compB, containerA.Components [0]);
Assert.AreSame (compA, containerB.Components [0]);
// check effect of add component twice to same container
containerB.Add (compA, "A2");
Assert.AreSame (siteA, compA.Site);
Assert.AreEqual ("A2", siteA.Name);
}
[Test]
public void Add2_Component_Null ()
{
_container.Add ((IComponent) null, "A");
Assert.AreEqual (0, _container.Components.Count);
_container.Add (new TestComponent (), "A");
Assert.AreEqual (1, _container.Components.Count);
_container.Add ((IComponent) null, "A");
Assert.AreEqual (1, _container.Components.Count);
}
[Test]
public void Add2_Name_Duplicate ()
{
TestContainer container = new TestContainer ();
TestComponent c1 = new TestComponent ();
container.Add (c1, "dup");
// new component, same case
TestComponent c2 = new TestComponent ();
try {
container.Add (c2, "dup");
Assert.Fail ("#A1");
} catch (ArgumentException ex) {
// Duplicate component name 'dup'. Component names must be
// unique and case-insensitive
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
Assert.IsTrue (ex.Message.IndexOf ("'dup'") != -1, "#A5");
Assert.IsNull (ex.ParamName, "#A6");
Assert.AreEqual (1, container.Components.Count, "#A7");
}
// new component, different case
TestComponent c3 = new TestComponent ();
try {
container.Add (c3, "duP");
Assert.Fail ("#B1");
} catch (ArgumentException ex) {
// Duplicate component name 'duP'. Component names must be
// unique and case-insensitive
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
Assert.IsTrue (ex.Message.IndexOf ("'duP'") != -1, "#B5");
Assert.IsNull (ex.ParamName, "#B6");
Assert.AreEqual (1, container.Components.Count, "#B7");
}
// existing component, same case
TestComponent c4 = new TestComponent ();
container.Add (c4, "C4");
Assert.AreEqual (2, container.Components.Count, "#C1");
container.Add (c4, "dup");
Assert.AreEqual (2, container.Components.Count, "#C2");
Assert.AreEqual ("C4", c4.Site.Name, "#C3");
// component of other container, same case
TestContainer container2 = new TestContainer ();
TestComponent c5 = new TestComponent ();
container2.Add (c5, "C5");
try {
container.Add (c5, "dup");
Assert.Fail ("#D1");
} catch (ArgumentException ex) {
// Duplicate component name 'dup'. Component names must be
// unique and case-insensitive
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
Assert.IsNull (ex.InnerException, "#D3");
Assert.IsNotNull (ex.Message, "#D4");
Assert.IsTrue (ex.Message.IndexOf ("'dup'") != -1, "#D5");
Assert.IsNull (ex.ParamName, "#D6");
Assert.AreEqual (2, container.Components.Count, "#D7");
}
Assert.AreEqual (1, container2.Components.Count, "#D8");
Assert.AreSame (c5, container2.Components [0], "#D9");
#if NET_2_0
container.AllowDuplicateNames = true;
TestComponent c6 = new TestComponent ();
container.Add (c6, "dup");
Assert.AreEqual (3, container.Components.Count, "#E1");
Assert.IsNotNull (c1.Site, "#E2");
Assert.AreEqual ("dup", c1.Site.Name, "#E3");
Assert.IsNotNull (c6.Site, "#E4");
Assert.AreEqual ("dup", c6.Site.Name, "#E5");
Assert.IsFalse (object.ReferenceEquals (c1.Site, c6.Site), "#E6");
#endif
}
[Test]
public void AddRemove ()
{
TestComponent component = new TestComponent ();
_container.Add (component);
Assert.IsNotNull (component.Site, "#1");
Assert.IsTrue (_container.Contains (component), "#2");
_container.Remove (component);
Assert.IsNull (component.Site, "#3");
Assert.IsFalse (_container.Contains (component), "#4");
}
[Test] // Dispose ()
public void Dispose1 ()
{
TestComponent compA;
TestComponent compB;
compA = new TestComponent ();
_container.Add (compA);
compB = new TestComponent ();
_container.Add (compB);
_container.Dispose ();
Assert.AreEqual (0, _container.Components.Count, "#A1");
Assert.IsTrue (compA.IsDisposed, "#A2");
Assert.IsNull (compA.Site, "#A3");
Assert.IsTrue (compB.IsDisposed, "#A4");
Assert.IsNull (compB.Site, "#A5");
_container = new TestContainer ();
compA = new TestComponent ();
compA.ThrowOnDispose = true;
_container.Add (compA);
compB = new TestComponent ();
_container.Add (compB);
// assert that component is not removed from components until after
// Dispose of component has succeeded
try {
_container.Dispose ();
Assert.Fail ("#B1");
} catch (InvalidOperationException) {
Assert.AreEqual (0, _container.Components.Count, "#B2");
Assert.IsFalse (compA.IsDisposed, "#B4");
Assert.IsNull (compA.Site, "#B5");
Assert.IsTrue (compB.IsDisposed, "#B6");
Assert.IsNull (compB.Site, "#B7");
} finally {
compA.ThrowOnDispose = false;
}
_container = new TestContainer ();
compA = new TestComponent ();
_container.Add (compA);
compB = new TestComponent ();
compB.ThrowOnDispose = true;
_container.Add (compB);
try {
_container.Dispose ();
Assert.Fail ("#C1");
} catch (InvalidOperationException) {
Assert.AreEqual (1, _container.Components.Count, "#C2");
Assert.AreSame (compA, _container.Components [0], "#C3");
Assert.IsFalse (compA.IsDisposed, "#C4");
Assert.IsNotNull (compA.Site, "#C5");
Assert.IsFalse (compB.IsDisposed, "#C6");
Assert.IsNull (compB.Site, "#C7");
} finally {
compB.ThrowOnDispose = false;
}
}
[Test] // Dispose (Boolean)
public void Dispose2 ()
{
TestComponent compA;
TestComponent compB;
compA = new TestComponent ();
_container.Add (compA);
compB = new TestComponent ();
_container.Add (compB);
_container.Dispose (false);
Assert.AreEqual (2, _container.Components.Count, "#A1");
Assert.IsFalse (compA.IsDisposed, "#A2");
Assert.IsNotNull (compA.Site, "#A3");
Assert.IsFalse (compB.IsDisposed, "#A4");
Assert.IsNotNull (compB.Site, "#A5");
_container.Dispose (true);
Assert.AreEqual (0, _container.Components.Count, "#B1");
Assert.IsTrue (compA.IsDisposed, "#B2");
Assert.IsNull (compA.Site, "#B3");
Assert.IsTrue (compB.IsDisposed, "#B4");
Assert.IsNull (compB.Site, "#B5");
compA = new TestComponent ();
_container.Add (compA);
compB = new TestComponent ();
_container.Add (compB);
Assert.AreEqual (2, _container.Components.Count, "#C1");
Assert.IsFalse (compA.IsDisposed, "#C2");
Assert.IsNotNull (compA.Site, "#C3");
Assert.IsFalse (compB.IsDisposed, "#C4");
Assert.IsNotNull (compB.Site, "#C5");
_container.Dispose (true);
Assert.AreEqual (0, _container.Components.Count, "#D1");
Assert.IsTrue (compA.IsDisposed, "#D2");
Assert.IsNull (compA.Site, "#D3");
Assert.IsTrue (compB.IsDisposed, "#D4");
Assert.IsNull (compB.Site, "#D5");
}
[Test] // bug #522474
public void Dispose_Recursive ()
{
MyComponent comp = new MyComponent ();
Container container = comp.CreateContainer ();
comp.Dispose ();
Assert.AreEqual (0, container.Components.Count);
}
[Test]
public void GetService ()
{
object service;
GetServiceContainer container = new GetServiceContainer ();
container.Add (new MyComponent ());
service = container.GetService (typeof (MyComponent));
Assert.IsNull (service, "#1");
service = container.GetService (typeof (Component));
Assert.IsNull (service, "#2");
service = container.GetService (typeof (IContainer));
Assert.AreSame (container, service, "#3");
service = container.GetService ((Type) null);
Assert.IsNull (service, "#4");
}
[Test]
public void Remove ()
{
TestComponent compA;
TestComponent compB;
ISite siteA;
ISite siteB;
compA = new TestComponent ();
_container.Add (compA);
siteA = compA.Site;
compB = new TestComponent ();
_container.Add (compB);
siteB = compB.Site;
_container.Remove (compB);
Assert.AreSame (siteA, compA.Site, "#A1");
Assert.IsNull (compB.Site, "#A2");
Assert.AreEqual (1, _container.Components.Count, "#A3");
Assert.AreSame (compA, _container.Components [0], "#A4");
// remove component with no site
compB = new TestComponent ();
_container.Remove (compB);
Assert.AreSame (siteA, compA.Site, "#B1");
Assert.IsNull (compB.Site, "#B2");
Assert.AreEqual (1, _container.Components.Count, "#B3");
Assert.AreSame (compA, _container.Components [0], "#B4");
// remove component associated with other container
TestContainer container2 = new TestContainer ();
compB = new TestComponent ();
container2.Add (compB);
siteB = compB.Site;
_container.Remove (compB);
Assert.AreSame (siteA, compA.Site, "#C1");
Assert.AreSame (siteB, compB.Site, "#C2");
Assert.AreEqual (1, _container.Components.Count, "#C3");
Assert.AreSame (compA, _container.Components [0], "#C4");
Assert.AreEqual (1, container2.Components.Count, "#C5");
Assert.AreSame (compB, container2.Components [0], "#C6");
}
[Test]
public void Remove_Component_Null ()
{
_container.Add (new TestComponent ());
_container.Remove ((IComponent) null);
Assert.AreEqual (1, _container.Components.Count);
}
#if NET_2_0
[Test]
public void RemoveWithoutUnsiting ()
{
TestComponent compA;
TestComponent compB;
ISite siteA;
ISite siteB;
compA = new TestComponent ();
_container.Add (compA);
siteA = compA.Site;
compB = new TestComponent ();
_container.Add (compB);
siteB = compB.Site;
_container.RemoveWithoutUnsiting (compB);
Assert.AreSame (siteA, compA.Site, "#A1");
Assert.AreSame (siteB, compB.Site, "#A2");
Assert.AreEqual (1, _container.Components.Count, "#A3");
Assert.AreSame (compA, _container.Components [0], "#A4");
// remove component with no site
compB = new TestComponent ();
_container.RemoveWithoutUnsiting (compB);
Assert.AreSame (siteA, compA.Site, "#B1");
Assert.IsNull (compB.Site, "#B2");
Assert.AreEqual (1, _container.Components.Count, "#B3");
Assert.AreSame (compA, _container.Components [0], "#B4");
// remove component associated with other container
TestContainer container2 = new TestContainer ();
compB = new TestComponent ();
container2.Add (compB);
siteB = compB.Site;
_container.RemoveWithoutUnsiting (compB);
Assert.AreSame (siteA, compA.Site, "#C1");
Assert.AreSame (siteB, compB.Site, "#C2");
Assert.AreEqual (1, _container.Components.Count, "#C3");
Assert.AreSame (compA, _container.Components [0], "#C4");
Assert.AreEqual (1, container2.Components.Count, "#C5");
Assert.AreSame (compB, container2.Components [0], "#C6");
}
[Test]
public void RemoveWithoutUnsiting_Component_Null ()
{
ISite site;
TestComponent component;
component = new TestComponent ();
_container.Add (component);
site = component.Site;
_container.RemoveWithoutUnsiting ((IComponent) null);
Assert.AreSame (site, component.Site, "#1");
Assert.AreEqual (1, _container.Components.Count, "#2");
Assert.AreSame (component, _container.Components [0], "#3");
}
[Test]
public void ValidateName_Component_Null ()
{
try {
_container.InvokeValidateName ((IComponent) null, "A");
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("component", ex.ParamName, "#5");
}
}
[Test]
public void ValidateName_Name_Null ()
{
TestComponent compA = new TestComponent ();
_container.Add (compA, (string) null);
TestComponent compB = new TestComponent ();
_container.InvokeValidateName (compB, (string) null);
}
[Test]
public void ValidateName_Name_Duplicate ()
{
TestComponent compA = new TestComponent ();
_container.Add (compA, "dup");
// same component, same case
_container.InvokeValidateName (compA, "dup");
// existing component, same case
TestComponent compB = new TestComponent ();
_container.Add (compB, "B");
try {
_container.InvokeValidateName (compB, "dup");
Assert.Fail ("#A1");
} catch (ArgumentException ex) {
// Duplicate component name 'duP'. Component names must be
// unique and case-insensitive
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
Assert.IsTrue (ex.Message.IndexOf ("'dup'") != -1, "#A5");
Assert.IsNull (ex.ParamName, "#A6");
Assert.AreEqual (2, _container.Components.Count, "#A7");
}
_container.InvokeValidateName (compB, "whatever");
// new component, different case
TestComponent compC = new TestComponent ();
try {
_container.InvokeValidateName (compC, "dup");
Assert.Fail ("#B1");
} catch (ArgumentException ex) {
// Duplicate component name 'duP'. Component names must be
// unique and case-insensitive
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
Assert.IsTrue (ex.Message.IndexOf ("'dup'") != -1, "#B5");
Assert.IsNull (ex.ParamName, "#B6");
Assert.AreEqual (2, _container.Components.Count, "#B7");
}
_container.InvokeValidateName (compC, "whatever");
// component of other container, different case
TestContainer container2 = new TestContainer ();
TestComponent compD = new TestComponent ();
container2.Add (compD, "B");
try {
_container.InvokeValidateName (compD, "dup");
Assert.Fail ("#C1");
} catch (ArgumentException ex) {
// Duplicate component name 'duP'. Component names must be
// unique and case-insensitive
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
Assert.IsNull (ex.InnerException, "#C3");
Assert.IsNotNull (ex.Message, "#C4");
Assert.IsTrue (ex.Message.IndexOf ("'dup'") != -1, "#C5");
Assert.IsNull (ex.ParamName, "#C6");
Assert.AreEqual (2, _container.Components.Count, "#C7");
}
_container.InvokeValidateName (compD, "whatever");
Assert.AreEqual (1, container2.Components.Count, "#C8");
Assert.AreSame (compD, container2.Components [0], "#C9");
}
#endif
class MyComponent : Component
{
private Container container;
protected override void Dispose (bool disposing)
{
if (container != null)
container.Dispose ();
base.Dispose (disposing);
}
public Container CreateContainer ()
{
if (container != null)
throw new InvalidOperationException ();
container = new Container ();
container.Add (new MyComponent ());
container.Add (this);
return container;
}
}
class MyContainer : IContainer
{
private ComponentCollection components = new ComponentCollection (
new Component [0]);
public ComponentCollection Components {
get { return components; }
}
public void Add (IComponent component)
{
}
public void Add (IComponent component, string name)
{
}
public void Remove (IComponent component)
{
}
public void Dispose ()
{
}
}
public class GetServiceContainer : Container
{
public new object GetService (Type service)
{
return base.GetService (service);
}
}
}
}
#endif
|