2 // ValidationContextTest.cs
5 // Marek Habersack <mhabersack@novell.com>
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
30 using System.ComponentModel.DataAnnotations;
31 using System.ComponentModel.Design;
34 using NUnit.Framework;
36 namespace MonoTests.System.ComponentModel.DataAnnotations
40 public class ValidationContextTest
43 public void Constructor ()
47 vc = new ValidationContext (null, new FakeServiceProvider (), new Dictionary <object, object> ());
48 Assert.Fail ("#A1-1");
49 } catch (ArgumentNullException) {
54 vc = new ValidationContext ("stuff", null, new Dictionary<object, object> ());
56 Assert.Fail ("#A1-2");
60 vc = new ValidationContext ("stuff", new FakeServiceProvider (), null);
62 Assert.Fail ("#A1-3");
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");
72 var dict = new Dictionary<object, object> () {
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");
89 public void ServiceContainer ()
91 var vc = new ValidationContext ("stuff", null, null);
93 // Test the default container
94 IServiceContainer container = vc.ServiceContainer;
95 Assert.AreEqual ("System.ComponentModel.DataAnnotations.ValidationContext+ValidationContextServiceContainer", container.GetType ().FullName, "#A1");
97 var fs1 = new FakeService1 ();
98 container.AddService (typeof (FakeService1), fs1);
99 container.AddService (typeof (FakeService2), CreateFakeService);
101 var fs3 = new FakeService3 ();
102 container.AddService (typeof (FakeService3), fs3, false);
103 container.AddService (typeof (FakeService4), CreateFakeService, false);
106 container.AddService (null, CreateFakeService);
107 Assert.Fail ("#A2-1");
108 } catch (ArgumentNullException) {
113 container.AddService (typeof (string), null);
115 Assert.Fail ("#A2-2");
119 container.AddService (typeof (FakeService2), CreateFakeService);
120 Assert.Fail ("#A2-3");
121 } catch (ArgumentException) {
126 container.RemoveService (GetType ());
128 Assert.Fail ("#A2-4");
132 container.RemoveService (null);
133 Assert.Fail ("#A2-5");
134 } catch (ArgumentNullException) {
139 container.GetService (null);
140 } catch (ArgumentNullException) {
144 object o = container.GetService (typeof (FakeService1));
145 Assert.IsNotNull (o, "#B1-1");
146 Assert.AreSame (fs1, o, "#B1-2");
148 o = container.GetService (typeof (FakeService2));
149 Assert.IsNotNull (o, "#B2-1");
150 Assert.AreEqual (typeof (FakeService2), o.GetType (), "#B2-2");
152 o = container.GetService (typeof (FakeService3));
153 Assert.IsNotNull (o, "#B3-1");
154 Assert.AreSame (fs3, o, "#B3-2");
156 o = container.GetService (typeof (FakeService4));
157 Assert.IsNotNull (o, "#B4-1");
158 Assert.AreEqual (typeof (FakeService4), o.GetType (), "#B4-2");
160 o = container.GetService (GetType ());
161 Assert.IsNull (o, "#B5");
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");
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");
174 object CreateFakeService (IServiceContainer container, Type serviceType)
176 if (serviceType == typeof (FakeService2))
177 return Activator.CreateInstance (serviceType);
179 if (serviceType == typeof (FakeService4))
180 return Activator.CreateInstance (serviceType);
198 class FakeServiceProvider : IServiceProvider
200 #region IServiceProvider Members
202 public object GetService (Type serviceType)
204 return Activator.CreateInstance (serviceType);
210 class FakeServiceContainer : IServiceContainer
212 public void AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
216 public void AddService (Type serviceType, ServiceCreatorCallback callback)
220 public void AddService (Type serviceType, object serviceInstance, bool promote)
224 public void AddService (Type serviceType, object serviceInstance)
228 public void RemoveService (Type serviceType, bool promote)
232 public void RemoveService (Type serviceType)
236 public object GetService (Type serviceType)