[eglib] Prefer <langinfo.h> to <localcharset.h>
[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 #if NET_4_0
39         [TestFixture]
40         public class ValidationContextTest
41         {
42                 [Test]
43                 public void Constructor ()
44                 {
45                         ValidationContext vc;
46                         try {
47                                 vc = new ValidationContext (null, new FakeServiceProvider (), new Dictionary <object, object> ());
48                                 Assert.Fail ("#A1-1");
49                         } catch (ArgumentNullException) {
50                                 // success
51                         }
52
53                         try {
54                                 vc = new ValidationContext ("stuff", null, new Dictionary<object, object> ());
55                         } catch {
56                                 Assert.Fail ("#A1-2");
57                         }
58
59                         try {
60                                 vc = new ValidationContext ("stuff", new FakeServiceProvider (), null);
61                         } catch {
62                                 Assert.Fail ("#A1-3");
63                         }
64
65                         object o = "stuff";
66                         vc = new ValidationContext (o, null, null);
67                         Assert.AreSame (o, vc.ObjectInstance, "#A2-1");
68                         Assert.AreEqual (o.GetType ().Name, vc.DisplayName, "#A2-2");
69                         Assert.AreEqual (o.GetType (), vc.ObjectType, "#A2-3");
70
71                         o = 12;
72                         var dict = new Dictionary<object, object> () {
73                                 {"stuff", 1}
74                         };
75                         vc = new ValidationContext (o, null, dict);
76                         Assert.IsNotNull (vc.Items, "#A3-1");
77                         Assert.AreEqual (1, vc.Items.Count, "#A3-2");
78                         Assert.AreNotSame (dict, vc.Items, "#A3-3");
79                         Assert.AreEqual (typeof (Dictionary <object, object>), vc.Items.GetType (), "#A3-4");
80                         Assert.AreEqual (1, vc.Items ["stuff"], "#A3-5");
81                         Assert.AreEqual (o.GetType ().Name, vc.DisplayName, "#A3-6");
82                         Assert.AreEqual (o.GetType (), vc.ObjectType, "#A3-7");
83                         Assert.AreEqual (null, vc.MemberName, "#A3-8");
84                         Assert.IsNotNull (vc.ServiceContainer, "#A3-9");
85                         Assert.AreEqual ("System.ComponentModel.DataAnnotations.ValidationContext+ValidationContextServiceContainer", vc.ServiceContainer.GetType ().FullName, "#A3-10");
86                 }
87
88                 [Test]
89                 public void ServiceContainer ()
90                 {
91                         var vc = new ValidationContext ("stuff", null, null);
92
93                         // Test the default container
94                         IServiceContainer container = vc.ServiceContainer;
95                         Assert.AreEqual ("System.ComponentModel.DataAnnotations.ValidationContext+ValidationContextServiceContainer", container.GetType ().FullName, "#A1");
96
97                         var fs1 = new FakeService1 ();
98                         container.AddService (typeof (FakeService1), fs1);
99                         container.AddService (typeof (FakeService2), CreateFakeService);
100
101                         var fs3 = new FakeService3 ();
102                         container.AddService (typeof (FakeService3), fs3, false);
103                         container.AddService (typeof (FakeService4), CreateFakeService, false);
104
105                         try {
106                                 container.AddService (null, CreateFakeService);
107                                 Assert.Fail ("#A2-1");
108                         } catch (ArgumentNullException) {
109                                 // success
110                         }
111
112                         try {
113                                 container.AddService (typeof (string), null);
114                         } catch {
115                                 Assert.Fail ("#A2-2");
116                         }
117
118                         try {
119                                 container.AddService (typeof (FakeService2), CreateFakeService);
120                                 Assert.Fail ("#A2-3");
121                         } catch (ArgumentException) {
122                                 // success
123                         }
124
125                         try {
126                                 container.RemoveService (GetType ());
127                         } catch {
128                                 Assert.Fail ("#A2-4");
129                         }
130
131                         try {
132                                 container.RemoveService (null);
133                                 Assert.Fail ("#A2-5");
134                         } catch (ArgumentNullException) {
135                                 // success
136                         }
137
138                         try {
139                                 container.GetService (null);
140                         } catch (ArgumentNullException) {
141                                 // success
142                         }
143
144                         object o = container.GetService (typeof (FakeService1));
145                         Assert.IsNotNull (o, "#B1-1");
146                         Assert.AreSame (fs1, o, "#B1-2");
147
148                         o = container.GetService (typeof (FakeService2));
149                         Assert.IsNotNull (o, "#B2-1");
150                         Assert.AreEqual (typeof (FakeService2), o.GetType (), "#B2-2");
151
152                         o = container.GetService (typeof (FakeService3));
153                         Assert.IsNotNull (o, "#B3-1");
154                         Assert.AreSame (fs3, o, "#B3-2");
155
156                         o = container.GetService (typeof (FakeService4));
157                         Assert.IsNotNull (o, "#B4-1");
158                         Assert.AreEqual (typeof (FakeService4), o.GetType (), "#B4-2");
159
160                         o = container.GetService (GetType ());
161                         Assert.IsNull (o, "#B5");
162
163                         // Test custom container
164                         var fsc = new FakeServiceContainer ();
165                         vc = new ValidationContext ("stuff", fsc, null);
166                         container = vc.ServiceContainer;
167                         Assert.IsNotNull (container, "#B6-1");
168
169                         // LAMESPEC: MSDN says vc.ServiceContainer should be initialized with the passed container if it implements 
170                         // the IServiceContainer interface - not the case, though.
171                         Assert.AreNotSame (fsc, container, "#B6-2");
172                 }
173
174                 object CreateFakeService (IServiceContainer container, Type serviceType)
175                 {
176                         if (serviceType == typeof (FakeService2))
177                                 return Activator.CreateInstance (serviceType);
178
179                         if (serviceType == typeof (FakeService4))
180                                 return Activator.CreateInstance (serviceType);
181
182                         return null;
183                 }
184         }
185
186         class FakeService1
187         { }
188
189         class FakeService2
190         { }
191
192         class FakeService3
193         { }
194
195         class FakeService4
196         { }
197
198         class FakeServiceProvider : IServiceProvider
199         {
200                 #region IServiceProvider Members
201
202                 public object GetService (Type serviceType)
203                 {
204                         return Activator.CreateInstance (serviceType);
205                 }
206
207                 #endregion
208         }
209
210         class FakeServiceContainer : IServiceContainer
211         {
212                 public void AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
213                 {
214                 }
215
216                 public void AddService (Type serviceType, ServiceCreatorCallback callback)
217                 {
218                 }
219
220                 public void AddService (Type serviceType, object serviceInstance, bool promote)
221                 {
222                 }
223
224                 public void AddService (Type serviceType, object serviceInstance)
225                 {
226                 }
227
228                 public void RemoveService (Type serviceType, bool promote)
229                 {
230                 }
231
232                 public void RemoveService (Type serviceType)
233                 {
234                 }
235
236                 public object GetService (Type serviceType)
237                 {
238                         return null;
239                 }
240         }
241 #endif
242 }