Merge pull request #2333 from joelmartinez/docs-classic-fix
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / Test / System.ComponentModel.DataAnnotations / ValidationContextTest.cs
1 //
2 // ValidationContextTest.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.ComponentModel.DataAnnotations;
31 using System.ComponentModel.Design;
32 using System.Text;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.ComponentModel.DataAnnotations
37 {
38         [TestFixture]
39         public class ValidationContextTest
40         {
41                 [Test]
42                 public void Constructor ()
43                 {
44                         ValidationContext vc;
45                         try {
46                                 vc = new ValidationContext (null, new FakeServiceProvider (), new Dictionary <object, object> ());
47                                 Assert.Fail ("#A1-1");
48                         } catch (ArgumentNullException) {
49                                 // success
50                         }
51
52                         try {
53                                 vc = new ValidationContext ("stuff", null, new Dictionary<object, object> ());
54                         } catch {
55                                 Assert.Fail ("#A1-2");
56                         }
57
58                         try {
59                                 vc = new ValidationContext ("stuff", new FakeServiceProvider (), null);
60                         } catch {
61                                 Assert.Fail ("#A1-3");
62                         }
63
64                         object o = "stuff";
65                         vc = new ValidationContext (o, null, null);
66                         Assert.AreSame (o, vc.ObjectInstance, "#A2-1");
67                         Assert.AreEqual (o.GetType ().Name, vc.DisplayName, "#A2-2");
68                         Assert.AreEqual (o.GetType (), vc.ObjectType, "#A2-3");
69
70                         o = 12;
71                         var dict = new Dictionary<object, object> () {
72                                 {"stuff", 1}
73                         };
74                         vc = new ValidationContext (o, null, dict);
75                         Assert.IsNotNull (vc.Items, "#A3-1");
76                         Assert.AreEqual (1, vc.Items.Count, "#A3-2");
77                         Assert.AreNotSame (dict, vc.Items, "#A3-3");
78                         Assert.AreEqual (typeof (Dictionary <object, object>), vc.Items.GetType (), "#A3-4");
79                         Assert.AreEqual (1, vc.Items ["stuff"], "#A3-5");
80                         Assert.AreEqual (o.GetType ().Name, vc.DisplayName, "#A3-6");
81                         Assert.AreEqual (o.GetType (), vc.ObjectType, "#A3-7");
82                         Assert.AreEqual (null, vc.MemberName, "#A3-8");
83                         Assert.IsNotNull (vc.ServiceContainer, "#A3-9");
84                         Assert.AreEqual ("System.ComponentModel.DataAnnotations.ValidationContext+ValidationContextServiceContainer", vc.ServiceContainer.GetType ().FullName, "#A3-10");
85                 }
86
87                 [Test]
88                 public void ServiceContainer ()
89                 {
90                         var vc = new ValidationContext ("stuff", null, null);
91
92                         // Test the default container
93                         IServiceContainer container = vc.ServiceContainer;
94                         Assert.AreEqual ("System.ComponentModel.DataAnnotations.ValidationContext+ValidationContextServiceContainer", container.GetType ().FullName, "#A1");
95
96                         var fs1 = new FakeService1 ();
97                         container.AddService (typeof (FakeService1), fs1);
98                         container.AddService (typeof (FakeService2), CreateFakeService);
99
100                         var fs3 = new FakeService3 ();
101                         container.AddService (typeof (FakeService3), fs3, false);
102                         container.AddService (typeof (FakeService4), CreateFakeService, false);
103
104                         try {
105                                 container.AddService (null, CreateFakeService);
106                                 Assert.Fail ("#A2-1");
107                         } catch (ArgumentNullException) {
108                                 // success
109                         }
110
111                         try {
112                                 container.AddService (typeof (string), null);
113                         } catch {
114                                 Assert.Fail ("#A2-2");
115                         }
116
117                         try {
118                                 container.AddService (typeof (FakeService2), CreateFakeService);
119                                 Assert.Fail ("#A2-3");
120                         } catch (ArgumentException) {
121                                 // success
122                         }
123
124                         try {
125                                 container.RemoveService (GetType ());
126                         } catch {
127                                 Assert.Fail ("#A2-4");
128                         }
129
130                         try {
131                                 container.RemoveService (null);
132                                 Assert.Fail ("#A2-5");
133                         } catch (ArgumentNullException) {
134                                 // success
135                         }
136
137                         try {
138                                 container.GetService (null);
139                         } catch (ArgumentNullException) {
140                                 // success
141                         }
142
143                         object o = container.GetService (typeof (FakeService1));
144                         Assert.IsNotNull (o, "#B1-1");
145                         Assert.AreSame (fs1, o, "#B1-2");
146
147                         o = container.GetService (typeof (FakeService2));
148                         Assert.IsNotNull (o, "#B2-1");
149                         Assert.AreEqual (typeof (FakeService2), o.GetType (), "#B2-2");
150
151                         o = container.GetService (typeof (FakeService3));
152                         Assert.IsNotNull (o, "#B3-1");
153                         Assert.AreSame (fs3, o, "#B3-2");
154
155                         o = container.GetService (typeof (FakeService4));
156                         Assert.IsNotNull (o, "#B4-1");
157                         Assert.AreEqual (typeof (FakeService4), o.GetType (), "#B4-2");
158
159                         o = container.GetService (GetType ());
160                         Assert.IsNull (o, "#B5");
161
162                         // Test custom container
163                         var fsc = new FakeServiceContainer ();
164                         vc = new ValidationContext ("stuff", fsc, null);
165                         container = vc.ServiceContainer;
166                         Assert.IsNotNull (container, "#B6-1");
167
168                         // LAMESPEC: MSDN says vc.ServiceContainer should be initialized with the passed container if it implements 
169                         // the IServiceContainer interface - not the case, though.
170                         Assert.AreNotSame (fsc, container, "#B6-2");
171                 }
172
173                 object CreateFakeService (IServiceContainer container, Type serviceType)
174                 {
175                         if (serviceType == typeof (FakeService2))
176                                 return Activator.CreateInstance (serviceType);
177
178                         if (serviceType == typeof (FakeService4))
179                                 return Activator.CreateInstance (serviceType);
180
181                         return null;
182                 }
183         }
184
185         class FakeService1
186         { }
187
188         class FakeService2
189         { }
190
191         class FakeService3
192         { }
193
194         class FakeService4
195         { }
196
197         class FakeServiceProvider : IServiceProvider
198         {
199                 #region IServiceProvider Members
200
201                 public object GetService (Type serviceType)
202                 {
203                         return Activator.CreateInstance (serviceType);
204                 }
205
206                 #endregion
207         }
208
209         class FakeServiceContainer : IServiceContainer
210         {
211                 public void AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
212                 {
213                 }
214
215                 public void AddService (Type serviceType, ServiceCreatorCallback callback)
216                 {
217                 }
218
219                 public void AddService (Type serviceType, object serviceInstance, bool promote)
220                 {
221                 }
222
223                 public void AddService (Type serviceType, object serviceInstance)
224                 {
225                 }
226
227                 public void RemoveService (Type serviceType, bool promote)
228                 {
229                 }
230
231                 public void RemoveService (Type serviceType)
232                 {
233                 }
234
235                 public object GetService (Type serviceType)
236                 {
237                         return null;
238                 }
239         }
240 }