public void ServiceContainer()
        {
            var vc = new ValidationContext("stuff", null, null);

            // Test the default container
            IServiceContainer container = vc.ServiceContainer;

            Assert.AreEqual("System.ComponentModel.DataAnnotations.ValidationContext+ValidationContextServiceContainer", container.GetType().FullName, "#A1");

            var fs1 = new FakeService1();

            container.AddService(typeof(FakeService1), fs1);
            container.AddService(typeof(FakeService2), CreateFakeService);

            var fs3 = new FakeService3();

            container.AddService(typeof(FakeService3), fs3, false);
            container.AddService(typeof(FakeService4), CreateFakeService, false);

            try {
                container.AddService(null, CreateFakeService);
                Assert.Fail("#A2-1");
            } catch (ArgumentNullException) {
                // success
            }

            try {
                container.AddService(typeof(string), null);
            } catch {
                Assert.Fail("#A2-2");
            }

            try {
                container.AddService(typeof(FakeService2), CreateFakeService);
                Assert.Fail("#A2-3");
            } catch (ArgumentException) {
                // success
            }

            try {
                container.RemoveService(GetType());
            } catch {
                Assert.Fail("#A2-4");
            }

            try {
                container.RemoveService(null);
                Assert.Fail("#A2-5");
            } catch (ArgumentNullException) {
                // success
            }

            try {
                container.GetService(null);
            } catch (ArgumentNullException) {
                // success
            }

            object o = container.GetService(typeof(FakeService1));

            Assert.IsNotNull(o, "#B1-1");
            Assert.AreSame(fs1, o, "#B1-2");

            o = container.GetService(typeof(FakeService2));
            Assert.IsNotNull(o, "#B2-1");
            Assert.AreEqual(typeof(FakeService2), o.GetType(), "#B2-2");

            o = container.GetService(typeof(FakeService3));
            Assert.IsNotNull(o, "#B3-1");
            Assert.AreSame(fs3, o, "#B3-2");

            o = container.GetService(typeof(FakeService4));
            Assert.IsNotNull(o, "#B4-1");
            Assert.AreEqual(typeof(FakeService4), o.GetType(), "#B4-2");

            o = container.GetService(GetType());
            Assert.IsNull(o, "#B5");

            // Test custom container
            var fsc = new FakeServiceContainer();

            vc        = new ValidationContext("stuff", fsc, null);
            container = vc.ServiceContainer;
            Assert.IsNotNull(container, "#B6-1");

            // LAMESPEC: MSDN says vc.ServiceContainer should be initialized with the passed container if it implements
            // the IServiceContainer interface - not the case, though.
            Assert.AreNotSame(fsc, container, "#B6-2");
        }
Exemplo n.º 2
0
		public void ServiceContainer ()
		{
			var vc = new ValidationContext ("stuff", null, null);

			// Test the default container
			IServiceContainer container = vc.ServiceContainer;
			Assert.AreEqual ("System.ComponentModel.DataAnnotations.ValidationContext+ValidationContextServiceContainer", container.GetType ().FullName, "#A1");

			var fs1 = new FakeService1 ();
			container.AddService (typeof (FakeService1), fs1);
			container.AddService (typeof (FakeService2), CreateFakeService);

			var fs3 = new FakeService3 ();
			container.AddService (typeof (FakeService3), fs3, false);
			container.AddService (typeof (FakeService4), CreateFakeService, false);

			try {
				container.AddService (null, CreateFakeService);
				Assert.Fail ("#A2-1");
			} catch (ArgumentNullException) {
				// success
			}

			try {
				container.AddService (typeof (string), null);
			} catch {
				Assert.Fail ("#A2-2");
			}

			try {
				container.AddService (typeof (FakeService2), CreateFakeService);
				Assert.Fail ("#A2-3");
			} catch (ArgumentException) {
				// success
			}

			try {
				container.RemoveService (GetType ());
			} catch {
				Assert.Fail ("#A2-4");
			}

			try {
				container.RemoveService (null);
				Assert.Fail ("#A2-5");
			} catch (ArgumentNullException) {
				// success
			}

			try {
				container.GetService (null);
			} catch (ArgumentNullException) {
				// success
			}

			object o = container.GetService (typeof (FakeService1));
			Assert.IsNotNull (o, "#B1-1");
			Assert.AreSame (fs1, o, "#B1-2");

			o = container.GetService (typeof (FakeService2));
			Assert.IsNotNull (o, "#B2-1");
			Assert.AreEqual (typeof (FakeService2), o.GetType (), "#B2-2");

			o = container.GetService (typeof (FakeService3));
			Assert.IsNotNull (o, "#B3-1");
			Assert.AreSame (fs3, o, "#B3-2");

			o = container.GetService (typeof (FakeService4));
			Assert.IsNotNull (o, "#B4-1");
			Assert.AreEqual (typeof (FakeService4), o.GetType (), "#B4-2");

			o = container.GetService (GetType ());
			Assert.IsNull (o, "#B5");

			// Test custom container
			var fsc = new FakeServiceContainer ();
			vc = new ValidationContext ("stuff", fsc, null);
			container = vc.ServiceContainer;
			Assert.IsNotNull (container, "#B6-1");

			// LAMESPEC: MSDN says vc.ServiceContainer should be initialized with the passed container if it implements 
			// the IServiceContainer interface - not the case, though.
			Assert.AreNotSame (fsc, container, "#B6-2");
		}