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
39 public class ValidationContextTest
42 public void Constructor ()
46 vc = new ValidationContext (null, new FakeServiceProvider (), new Dictionary <object, object> ());
47 Assert.Fail ("#A1-1");
48 } catch (ArgumentNullException) {
53 vc = new ValidationContext ("stuff", null, new Dictionary<object, object> ());
55 Assert.Fail ("#A1-2");
59 vc = new ValidationContext ("stuff", new FakeServiceProvider (), null);
61 Assert.Fail ("#A1-3");
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");
71 var dict = new Dictionary<object, object> () {
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");
88 public void ServiceContainer ()
90 var vc = new ValidationContext ("stuff", null, null);
92 // Test the default container
93 IServiceContainer container = vc.ServiceContainer;
94 Assert.AreEqual ("System.ComponentModel.DataAnnotations.ValidationContext+ValidationContextServiceContainer", container.GetType ().FullName, "#A1");
96 var fs1 = new FakeService1 ();
97 container.AddService (typeof (FakeService1), fs1);
98 container.AddService (typeof (FakeService2), CreateFakeService);
100 var fs3 = new FakeService3 ();
101 container.AddService (typeof (FakeService3), fs3, false);
102 container.AddService (typeof (FakeService4), CreateFakeService, false);
105 container.AddService (null, CreateFakeService);
106 Assert.Fail ("#A2-1");
107 } catch (ArgumentNullException) {
112 container.AddService (typeof (string), null);
114 Assert.Fail ("#A2-2");
118 container.AddService (typeof (FakeService2), CreateFakeService);
119 Assert.Fail ("#A2-3");
120 } catch (ArgumentException) {
125 container.RemoveService (GetType ());
127 Assert.Fail ("#A2-4");
131 container.RemoveService (null);
132 Assert.Fail ("#A2-5");
133 } catch (ArgumentNullException) {
138 container.GetService (null);
139 } catch (ArgumentNullException) {
143 object o = container.GetService (typeof (FakeService1));
144 Assert.IsNotNull (o, "#B1-1");
145 Assert.AreSame (fs1, o, "#B1-2");
147 o = container.GetService (typeof (FakeService2));
148 Assert.IsNotNull (o, "#B2-1");
149 Assert.AreEqual (typeof (FakeService2), o.GetType (), "#B2-2");
151 o = container.GetService (typeof (FakeService3));
152 Assert.IsNotNull (o, "#B3-1");
153 Assert.AreSame (fs3, o, "#B3-2");
155 o = container.GetService (typeof (FakeService4));
156 Assert.IsNotNull (o, "#B4-1");
157 Assert.AreEqual (typeof (FakeService4), o.GetType (), "#B4-2");
159 o = container.GetService (GetType ());
160 Assert.IsNull (o, "#B5");
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");
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");
173 object CreateFakeService (IServiceContainer container, Type serviceType)
175 if (serviceType == typeof (FakeService2))
176 return Activator.CreateInstance (serviceType);
178 if (serviceType == typeof (FakeService4))
179 return Activator.CreateInstance (serviceType);
197 class FakeServiceProvider : IServiceProvider
199 #region IServiceProvider Members
201 public object GetService (Type serviceType)
203 return Activator.CreateInstance (serviceType);
209 class FakeServiceContainer : IServiceContainer
211 public void AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
215 public void AddService (Type serviceType, ServiceCreatorCallback callback)
219 public void AddService (Type serviceType, object serviceInstance, bool promote)
223 public void AddService (Type serviceType, object serviceInstance)
227 public void RemoveService (Type serviceType, bool promote)
231 public void RemoveService (Type serviceType)
235 public object GetService (Type serviceType)