public void GetStandardValues_IContainerNullType_ReturnsExpected()
        {
            var component1 = new TestComponent();
            var component2 = new TestComponent();
            var component3 = new Component();

            var container = new Container();

            container.Add(component1);
            container.Add(component2);
            container.Add(component3);

            var converter = new ReferenceConverter(null);
            var context   = new MockTypeDescriptorContext {
                Container = container
            };

            TypeConverter.StandardValuesCollection values1 = converter.GetStandardValues(context);
            Assert.Equal(new object[] { null }, values1.Cast <object>());

            // Call again to test caching behavior.
            TypeConverter.StandardValuesCollection values2 = converter.GetStandardValues(context);
            Assert.NotSame(values1, values2);
        }
示例#2
0
        public void RemoveWithoutUnsiting_Component_Null()
        {
            ISite site;
            TestComponent component;

            component = new TestComponent();
            _container.Add(component);
            site = component.Site;
            _container.RemoveWithoutUnsiting((IComponent)null);
            Assert.Same(site, component.Site);
            Assert.Equal(1, _container.Components.Count);
            Assert.Same(component, _container.Components[0]);
        }
示例#3
0
        public void RemoveWithoutUnsiting()
        {
            TestComponent compA;
            TestComponent compB;
            ISite siteA;
            ISite siteB;

            compA = new TestComponent();
            _container.Add(compA);
            siteA = compA.Site;
            compB = new TestComponent();
            _container.Add(compB);
            siteB = compB.Site;
            _container.RemoveWithoutUnsiting(compB);
            Assert.Same(siteA, compA.Site);
            Assert.Same(siteB, compB.Site);
            Assert.Equal(1, _container.Components.Count);
            Assert.Same(compA, _container.Components[0]);

            // remove component with no site
            compB = new TestComponent();
            _container.RemoveWithoutUnsiting(compB);
            Assert.Same(siteA, compA.Site);
            Assert.Null(compB.Site);
            Assert.Equal(1, _container.Components.Count);
            Assert.Same(compA, _container.Components[0]);

            // remove component associated with other container
            TestContainer container2 = new TestContainer();
            compB = new TestComponent();
            container2.Add(compB);
            siteB = compB.Site;
            _container.RemoveWithoutUnsiting(compB);
            Assert.Same(siteA, compA.Site);
            Assert.Same(siteB, compB.Site);
            Assert.Equal(1, _container.Components.Count);
            Assert.Same(compA, _container.Components[0]);
            Assert.Equal(1, container2.Components.Count);
            Assert.Same(compB, container2.Components[0]);
        }
示例#4
0
        [Fact] // Dispose (Boolean)
        public void Dispose2()
        {
            TestComponent compA;
            TestComponent compB;

            compA = new TestComponent();
            _container.Add(compA);
            compB = new TestComponent();
            _container.Add(compB);

            _container.Dispose(false);

            Assert.Equal(2, _container.Components.Count);
            Assert.False(compA.IsDisposed);
            Assert.NotNull(compA.Site);
            Assert.False(compB.IsDisposed);
            Assert.NotNull(compB.Site);

            _container.Dispose(true);

            Assert.Equal(0, _container.Components.Count);
            Assert.True(compA.IsDisposed);
            Assert.Null(compA.Site);
            Assert.True(compB.IsDisposed);
            Assert.Null(compB.Site);

            compA = new TestComponent();
            _container.Add(compA);
            compB = new TestComponent();
            _container.Add(compB);

            Assert.Equal(2, _container.Components.Count);
            Assert.False(compA.IsDisposed);
            Assert.NotNull(compA.Site);
            Assert.False(compB.IsDisposed);
            Assert.NotNull(compB.Site);

            _container.Dispose(true);

            Assert.Equal(0, _container.Components.Count);
            Assert.True(compA.IsDisposed);
            Assert.Null(compA.Site);
            Assert.True(compB.IsDisposed);
            Assert.Null(compB.Site);
        }
示例#5
0
        [Fact] // Dispose ()
        public void Dispose1()
        {
            TestComponent compA;
            TestComponent compB;

            compA = new TestComponent();
            _container.Add(compA);
            compB = new TestComponent();
            _container.Add(compB);

            _container.Dispose();

            Assert.Equal(0, _container.Components.Count);
            Assert.True(compA.IsDisposed);
            Assert.Null(compA.Site);
            Assert.True(compB.IsDisposed);
            Assert.Null(compB.Site);

            _container = new TestContainer();
            compA = new TestComponent();
            compA.ThrowOnDispose = true;
            _container.Add(compA);
            compB = new TestComponent();
            _container.Add(compB);

            Assert.Throws<InvalidOperationException>(() => _container.Dispose());
            // assert that component is not removed from components until after
            // Dispose of component has succeeded
            Assert.Equal(0, _container.Components.Count);
            Assert.False(compA.IsDisposed);
            Assert.Null(compA.Site);
            Assert.True(compB.IsDisposed);
            Assert.Null(compB.Site);

            compA.ThrowOnDispose = false;

            _container = new TestContainer();
            compA = new TestComponent();
            _container.Add(compA);
            compB = new TestComponent();
            compB.ThrowOnDispose = true;
            _container.Add(compB);

            Assert.Throws<InvalidOperationException>(() => _container.Dispose());
            Assert.Equal(1, _container.Components.Count);
            Assert.Same(compA, _container.Components[0]);
            Assert.False(compA.IsDisposed);
            Assert.NotNull(compA.Site);
            Assert.False(compB.IsDisposed);
            Assert.Null(compB.Site);
            compB.ThrowOnDispose = false;
        }
示例#6
0
        public void AddRemove()
        {
            TestComponent component = new TestComponent();

            _container.Add(component);
            Assert.NotNull(component.Site);
            Assert.True(_container.Contains(component));

            _container.Remove(component);
            Assert.Null(component.Site);
            Assert.False(_container.Contains(component));
        }
示例#7
0
        public void Add2_Name_Duplicate()
        {
            TestContainer container = new TestContainer();
            TestComponent c1 = new TestComponent();
            container.Add(c1, "dup");

            // new component, same case
            TestComponent c2 = new TestComponent();
            ArgumentException ex;

            ex = Assert.Throws<ArgumentException>(() => container.Add(c2, "dup"));
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf("'dup'") != -1);
            Assert.Null(ex.ParamName);
            Assert.Equal(1, container.Components.Count);

            // new component, different case
            TestComponent c3 = new TestComponent();
            ex = Assert.Throws<ArgumentException>(() => container.Add(c3, "duP"));
            // Duplicate component name 'duP'.  Component names must be
            // unique and case-insensitive
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf("'duP'") != -1);
            Assert.Null(ex.ParamName);
            Assert.Equal(1, container.Components.Count);

            // existing component, same case
            TestComponent c4 = new TestComponent();
            container.Add(c4, "C4");
            Assert.Equal(2, container.Components.Count);
            container.Add(c4, "dup");
            Assert.Equal(2, container.Components.Count);
            Assert.Equal("C4", c4.Site.Name);

            // component of other container, same case
            TestContainer container2 = new TestContainer();
            TestComponent c5 = new TestComponent();
            container2.Add(c5, "C5");
            ex = Assert.Throws<ArgumentException>(() => container.Add(c5, "dup"));
            // Duplicate component name 'dup'.  Component names must be
            // unique and case-insensitive
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf("'dup'") != -1);
            Assert.Null(ex.ParamName);
            Assert.Equal(2, container.Components.Count);
            Assert.Equal(1, container2.Components.Count);
            Assert.Same(c5, container2.Components[0]);

            container.AllowDuplicateNames = true;
            TestComponent c6 = new TestComponent();
            container.Add(c6, "dup");
            Assert.Equal(3, container.Components.Count);
            Assert.NotNull(c1.Site);
            Assert.Equal("dup", c1.Site.Name);
            Assert.NotNull(c6.Site);
            Assert.Equal("dup", c6.Site.Name);
            Assert.False(object.ReferenceEquals(c1.Site, c6.Site));
        }
        public void ConvertTo()
        {
            ReferenceConverter converter = new ReferenceConverter(typeof(ITestInterface));
            string referenceName = "reference name";

            Assert.Equal("(none)", (string)converter.ConvertTo(null, null, null, typeof(string)));

            TestComponent component = new TestComponent();

            // no context
            Assert.Equal(String.Empty, (string)converter.ConvertTo(null, null, component, typeof(string)));

            // context with IReferenceService
            TestReferenceService referenceService = new TestReferenceService();
            referenceService.AddReference(referenceName, component);
            TestTypeDescriptorContext context = new TestTypeDescriptorContext(referenceService);
            Assert.Equal(referenceName, (string)converter.ConvertTo(context, null, component, typeof(string)));

            // context with Component without IReferenceService
            Container container = new Container();
            container.Add(component, referenceName);
            context = new TestTypeDescriptorContext();
            context.Container = container;
            Assert.Equal(referenceName, (string)converter.ConvertTo(context, null, component, typeof(string)));
        }
示例#9
0
        public void Add2_Name_Duplicate()
        {
            TestContainer container = new TestContainer();
            TestComponent c1        = new TestComponent();

            container.Add(c1, "dup");

            // new component, same case
            TestComponent     c2 = new TestComponent();
            ArgumentException ex;

            ex = AssertExtensions.Throws <ArgumentException>(null, () => container.Add(c2, "dup"));
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf("'dup'") != -1);
            Assert.Null(ex.ParamName);
            Assert.Equal(1, container.Components.Count);

            // new component, different case
            TestComponent c3 = new TestComponent();

            ex = AssertExtensions.Throws <ArgumentException>(null, () => container.Add(c3, "duP"));
            // Duplicate component name 'duP'.  Component names must be
            // unique and case-insensitive
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf("'duP'") != -1);
            Assert.Null(ex.ParamName);
            Assert.Equal(1, container.Components.Count);

            // existing component, same case
            TestComponent c4 = new TestComponent();

            container.Add(c4, "C4");
            Assert.Equal(2, container.Components.Count);
            container.Add(c4, "dup");
            Assert.Equal(2, container.Components.Count);
            Assert.Equal("C4", c4.Site.Name);

            // component of other container, same case
            TestContainer container2 = new TestContainer();
            TestComponent c5         = new TestComponent();

            container2.Add(c5, "C5");
            ex = AssertExtensions.Throws <ArgumentException>(null, () => container.Add(c5, "dup"));
            // Duplicate component name 'dup'.  Component names must be
            // unique and case-insensitive
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf("'dup'") != -1);
            Assert.Null(ex.ParamName);
            Assert.Equal(2, container.Components.Count);
            Assert.Equal(1, container2.Components.Count);
            Assert.Same(c5, container2.Components[0]);

            container.AllowDuplicateNames = true;
            TestComponent c6 = new TestComponent();

            container.Add(c6, "dup");
            Assert.Equal(3, container.Components.Count);
            Assert.NotNull(c1.Site);
            Assert.Equal("dup", c1.Site.Name);
            Assert.NotNull(c6.Site);
            Assert.Equal("dup", c6.Site.Name);
            Assert.False(object.ReferenceEquals(c1.Site, c6.Site));
        }
示例#10
0
        [Fact] // Add (IComponent, String)
        public void Add2()
        {
            TestContainer containerA = new TestContainer();
            TestContainer containerB = new TestContainer();

            ISite siteA;
            ISite siteB;

            TestComponent compA = new TestComponent();

            Assert.Null(compA.Site);
            TestComponent compB = new TestComponent();

            Assert.Null(compB.Site);
            Assert.Equal(0, containerA.Components.Count);
            Assert.Equal(0, containerB.Components.Count);

            containerA.Add(compA, "A");
            siteA = compA.Site;
            Assert.NotNull(siteA);
            Assert.Same(compA, siteA.Component);
            Assert.Same(containerA, siteA.Container);
            Assert.False(siteA.DesignMode);
            Assert.Equal("A", siteA.Name);
            containerA.Add(compB, "B");
            siteB = compB.Site;
            Assert.NotNull(siteB);
            Assert.Same(compB, siteB.Component);
            Assert.Same(containerA, siteB.Container);
            Assert.False(siteB.DesignMode);
            Assert.Equal("B", siteB.Name);

            Assert.False(object.ReferenceEquals(siteA, siteB));
            Assert.Equal(2, containerA.Components.Count);
            Assert.Equal(0, containerB.Components.Count);
            Assert.Same(compA, containerA.Components[0]);
            Assert.Same(compB, containerA.Components[1]);

            // check effect of adding component that is already member of
            // another container
            containerB.Add(compA, "A2");
            Assert.False(object.ReferenceEquals(siteA, compA.Site));
            siteA = compA.Site;
            Assert.NotNull(siteA);
            Assert.Same(compA, siteA.Component);
            Assert.Same(containerB, siteA.Container);
            Assert.False(siteA.DesignMode);
            Assert.Equal("A2", siteA.Name);

            Assert.Equal(1, containerA.Components.Count);
            Assert.Equal(1, containerB.Components.Count);
            Assert.Same(compB, containerA.Components[0]);
            Assert.Same(compA, containerB.Components[0]);

            // check effect of add component twice to same container
            containerB.Add(compA, "A2");
            Assert.Same(siteA, compA.Site);
            Assert.Equal("A2", siteA.Name);

            Assert.Equal(1, containerA.Components.Count);
            Assert.Equal(1, containerB.Components.Count);
            Assert.Same(compB, containerA.Components[0]);
            Assert.Same(compA, containerB.Components[0]);

            // add again with different name
            containerB.Add(compA, "A3");
            Assert.Same(siteA, compA.Site);
            Assert.Equal("A2", siteA.Name);

            Assert.Equal(1, containerA.Components.Count);
            Assert.Equal(1, containerB.Components.Count);
            Assert.Same(compB, containerA.Components[0]);
            Assert.Same(compA, containerB.Components[0]);

            // check effect of add component twice to same container
            containerB.Add(compA, "A2");
            Assert.Same(siteA, compA.Site);
            Assert.Equal("A2", siteA.Name);
        }
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            // This does actually succeed despite CanConvertFrom returning false.
            ConvertTest noContext = ConvertTest.Valid("reference name", null).WithContext(null);

            noContext.CanConvert = false;
            yield return(noContext);

            // No IReferenceService or IContainer.
            yield return(ConvertTest.Valid(string.Empty, null));

            yield return(ConvertTest.Valid("   ", null));

            yield return(ConvertTest.Valid("(none)", null).WithInvariantRemoteInvokeCulture());

            yield return(ConvertTest.Valid("nothing", null));

            // IReferenceService.
            var nullReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(null);
                }
            };

            yield return(ConvertTest.Valid("reference name", null).WithContext(nullReferenceServiceContext));

            var invalidReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(new object());
                }
            };

            yield return(ConvertTest.Valid("reference name", null).WithContext(invalidReferenceServiceContext));

            var component1       = new TestComponent();
            var component2       = new TestComponent();
            var nonComponent     = new object();
            var referenceService = new MockReferenceService();

            referenceService.AddReference("reference name", component1);
            referenceService.AddReference(string.Empty, component2);
            referenceService.AddReference("non component", nonComponent);
            var validReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(referenceService);
                }
            };

            yield return(ConvertTest.Valid("reference name", component1).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid("  reference name  ", component1).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid(string.Empty, component2).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid("non component", nonComponent).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid("no such reference", null).WithContext(validReferenceServiceContext));

            // IContainer.
            var containerComponent1 = new TestComponent();
            var containerComponent2 = new TestComponent();
            var container           = new Container();

            container.Add(containerComponent1, "reference name");
            container.Add(containerComponent2, string.Empty);
            var contextWithContainer = new MockTypeDescriptorContext {
                Container = container
            };

            yield return(ConvertTest.Valid("reference name", containerComponent1).WithContext(contextWithContainer));

            yield return(ConvertTest.Valid(string.Empty, containerComponent2).WithContext(contextWithContainer));

            yield return(ConvertTest.Valid("no such reference", null).WithContext(contextWithContainer));

            yield return(ConvertTest.CantConvertFrom(1));

            yield return(ConvertTest.CantConvertFrom(new object()));
        }
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            var component1   = new TestComponent();
            var component2   = new TestComponent();
            var component3   = new TestComponent();
            var nonComponent = new object();

            // No Context.
            yield return(ConvertTest.Valid(null, "(none)").WithInvariantRemoteInvokeCulture());

            yield return(ConvertTest.Valid(null, "(none)").WithContext(null).WithInvariantRemoteInvokeCulture());

            yield return(ConvertTest.Valid(string.Empty, string.Empty).WithContext(null));

            // IReferenceCollection.
            var nullReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(null);
                }
            };

            yield return(ConvertTest.Valid(component1, string.Empty).WithContext(nullReferenceServiceContext));

            yield return(ConvertTest.Valid(new Component(), string.Empty).WithContext(nullReferenceServiceContext));

            var invalidReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(new object());
                }
            };

            yield return(ConvertTest.Valid(component1, string.Empty).WithContext(invalidReferenceServiceContext));

            yield return(ConvertTest.Valid(new Component(), string.Empty).WithContext(invalidReferenceServiceContext));

            var referenceService = new MockReferenceService();

            referenceService.AddReference("reference name", component1);
            referenceService.AddReference(string.Empty, component2);
            referenceService.AddReference(null, component3);
            referenceService.AddReference("non component", nonComponent);
            var validReferenceServiceContext = new MockTypeDescriptorContext
            {
                GetServiceAction = (serviceType) =>
                {
                    Assert.Equal(typeof(IReferenceService), serviceType);
                    return(referenceService);
                }
            };

            yield return(ConvertTest.Valid(component1, "reference name").WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid(component2, string.Empty).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid(component3, string.Empty).WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid(nonComponent, "non component").WithContext(validReferenceServiceContext));

            yield return(ConvertTest.Valid(new Component(), string.Empty).WithContext(validReferenceServiceContext));

            // IContainer.
            var containerComponent1 = new TestComponent();
            var containerComponent2 = new TestComponent();
            var container           = new Container();

            container.Add(containerComponent1, "reference name");
            container.Add(containerComponent2, string.Empty);
            var contextWithContainer = new MockTypeDescriptorContext {
                Container = container
            };

            yield return(ConvertTest.Valid(containerComponent1, "reference name").WithContext(contextWithContainer));

            yield return(ConvertTest.Valid(containerComponent2, string.Empty).WithContext(contextWithContainer));

            yield return(ConvertTest.Valid(new Component(), string.Empty).WithContext(contextWithContainer));

            yield return(ConvertTest.CantConvertTo(1, typeof(int)));
        }
示例#13
0
        public void ValidateName_Name_Duplicate()
        {
            TestComponent compA = new TestComponent();

            _container.Add(compA, "dup");

            // same component, same case
            _container.InvokeValidateName(compA, "dup");

            // existing component, same case
            TestComponent compB = new TestComponent();

            _container.Add(compB, "B");

            ArgumentException ex;

            ex = Assert.Throws <ArgumentException>(() => _container.InvokeValidateName(compB, "dup"));
            // Duplicate component name 'duP'.  Component names must be
            // unique and case-insensitive
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            if (!PlatformDetection.IsNetNative) // .Net Native toolchain optimizes away exception messages and paramnames.
            {
                Assert.NotNull(ex.Message);
                Assert.True(ex.Message.IndexOf("'dup'") != -1);
                Assert.Null(ex.ParamName);
            }
            Assert.Equal(2, _container.Components.Count);
            _container.InvokeValidateName(compB, "whatever");

            // new component, different case
            TestComponent compC = new TestComponent();

            ex = Assert.Throws <ArgumentException>(() => _container.InvokeValidateName(compC, "dup"));
            // Duplicate component name 'duP'.  Component names must be
            // unique and case-insensitive
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            if (!PlatformDetection.IsNetNative) // .Net Native toolchain optimizes away exception messages and paramnames.
            {
                Assert.NotNull(ex.Message);
                Assert.True(ex.Message.IndexOf("'dup'") != -1);
                Assert.Null(ex.ParamName);
            }
            Assert.Equal(2, _container.Components.Count);
            _container.InvokeValidateName(compC, "whatever");

            // component of other container, different case
            TestContainer container2 = new TestContainer();
            TestComponent compD      = new TestComponent();

            container2.Add(compD, "B");
            ex = Assert.Throws <ArgumentException>(() => _container.InvokeValidateName(compD, "dup"));
            // Duplicate component name 'duP'.  Component names must be
            // unique and case-insensitive
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            if (!PlatformDetection.IsNetNative) // .Net Native toolchain optimizes away exception messages and paramnames.
            {
                Assert.NotNull(ex.Message);
                Assert.True(ex.Message.IndexOf("'dup'") != -1);
                Assert.Null(ex.ParamName);
            }
            Assert.Equal(2, _container.Components.Count);
            _container.InvokeValidateName(compD, "whatever");
            Assert.Equal(1, container2.Components.Count);
            Assert.Same(compD, container2.Components[0]);
        }
        public void GetStandardValues()
        {
            ReferenceConverter converter = new ReferenceConverter(typeof(TestComponent));

            TestComponent component1 = new TestComponent();
            TestComponent component2 = new TestComponent();
            TestReferenceService referenceService = new TestReferenceService();
            referenceService.AddReference("reference name 1", component1);
            referenceService.AddReference("reference name 2", component2);
            ITypeDescriptorContext context = new TestTypeDescriptorContext(referenceService);

            TypeConverter.StandardValuesCollection values = converter.GetStandardValues(context);
            Assert.NotNull(values);
            // 2 components + 1 null value
            Assert.Equal(3, values.Count);
        }
示例#15
0
 public void ValidateName_Name_Null()
 {
     TestComponent compA = new TestComponent();
     _container.Add(compA, (string)null);
     TestComponent compB = new TestComponent();
     _container.InvokeValidateName(compB, (string)null);
 }
示例#16
0
        [Fact] // Add (IComponent, String)
        public void Add2()
        {
            TestContainer containerA = new TestContainer();
            TestContainer containerB = new TestContainer();

            ISite siteA;
            ISite siteB;

            TestComponent compA = new TestComponent();
            Assert.Null(compA.Site);
            TestComponent compB = new TestComponent();
            Assert.Null(compB.Site);
            Assert.Equal(0, containerA.Components.Count);
            Assert.Equal(0, containerB.Components.Count);

            containerA.Add(compA, "A");
            siteA = compA.Site;
            Assert.NotNull(siteA);
            Assert.Same(compA, siteA.Component);
            Assert.Same(containerA, siteA.Container);
            Assert.False(siteA.DesignMode);
            Assert.Equal("A", siteA.Name);
            containerA.Add(compB, "B");
            siteB = compB.Site;
            Assert.NotNull(siteB);
            Assert.Same(compB, siteB.Component);
            Assert.Same(containerA, siteB.Container);
            Assert.False(siteB.DesignMode);
            Assert.Equal("B", siteB.Name);

            Assert.False(object.ReferenceEquals(siteA, siteB));
            Assert.Equal(2, containerA.Components.Count);
            Assert.Equal(0, containerB.Components.Count);
            Assert.Same(compA, containerA.Components[0]);
            Assert.Same(compB, containerA.Components[1]);

            // check effect of adding component that is already member of
            // another container
            containerB.Add(compA, "A2");
            Assert.False(object.ReferenceEquals(siteA, compA.Site));
            siteA = compA.Site;
            Assert.NotNull(siteA);
            Assert.Same(compA, siteA.Component);
            Assert.Same(containerB, siteA.Container);
            Assert.False(siteA.DesignMode);
            Assert.Equal("A2", siteA.Name);

            Assert.Equal(1, containerA.Components.Count);
            Assert.Equal(1, containerB.Components.Count);
            Assert.Same(compB, containerA.Components[0]);
            Assert.Same(compA, containerB.Components[0]);

            // check effect of add component twice to same container
            containerB.Add(compA, "A2");
            Assert.Same(siteA, compA.Site);
            Assert.Equal("A2", siteA.Name);

            Assert.Equal(1, containerA.Components.Count);
            Assert.Equal(1, containerB.Components.Count);
            Assert.Same(compB, containerA.Components[0]);
            Assert.Same(compA, containerB.Components[0]);

            // add again with different name
            containerB.Add(compA, "A3");
            Assert.Same(siteA, compA.Site);
            Assert.Equal("A2", siteA.Name);

            Assert.Equal(1, containerA.Components.Count);
            Assert.Equal(1, containerB.Components.Count);
            Assert.Same(compB, containerA.Components[0]);
            Assert.Same(compA, containerB.Components[0]);

            // check effect of add component twice to same container
            containerB.Add(compA, "A2");
            Assert.Same(siteA, compA.Site);
            Assert.Equal("A2", siteA.Name);
        }
示例#17
0
        public void ValidateName_Name_Duplicate()
        {
            TestComponent compA = new TestComponent();
            _container.Add(compA, "dup");

            // same component, same case
            _container.InvokeValidateName(compA, "dup");

            // existing component, same case
            TestComponent compB = new TestComponent();
            _container.Add(compB, "B");

            ArgumentException ex;
            ex = Assert.Throws<ArgumentException>(() => _container.InvokeValidateName(compB, "dup"));
            // Duplicate component name 'duP'.  Component names must be
            // unique and case-insensitive
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf("'dup'") != -1);
            Assert.Null(ex.ParamName);
            Assert.Equal(2, _container.Components.Count);
            _container.InvokeValidateName(compB, "whatever");

            // new component, different case
            TestComponent compC = new TestComponent();
            ex = Assert.Throws<ArgumentException>(() => _container.InvokeValidateName(compC, "dup"));
            // Duplicate component name 'duP'.  Component names must be
            // unique and case-insensitive
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf("'dup'") != -1);
            Assert.Null(ex.ParamName);
            Assert.Equal(2, _container.Components.Count);
            _container.InvokeValidateName(compC, "whatever");

            // component of other container, different case
            TestContainer container2 = new TestContainer();
            TestComponent compD = new TestComponent();
            container2.Add(compD, "B");
            ex = Assert.Throws<ArgumentException>(() => _container.InvokeValidateName(compD, "dup"));
            // Duplicate component name 'duP'.  Component names must be
            // unique and case-insensitive
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf("'dup'") != -1);
            Assert.Null(ex.ParamName);
            Assert.Equal(2, _container.Components.Count);
            _container.InvokeValidateName(compD, "whatever");
            Assert.Equal(1, container2.Components.Count);
            Assert.Same(compD, container2.Components[0]);
        }
        public void ConvertFrom()
        {
            ReferenceConverter converter = new ReferenceConverter(typeof(ITestInterface));
            string referenceName = "reference name";
            // no context
            Assert.Null(converter.ConvertFrom(null, null, referenceName));

            TestComponent component = new TestComponent();

            // context with IReferenceService
            TestReferenceService referenceService = new TestReferenceService();
            referenceService.AddReference(referenceName, component);
            TestTypeDescriptorContext context = new TestTypeDescriptorContext(referenceService);
            Assert.Same(component, converter.ConvertFrom(context, null, referenceName));

            // context with Component without IReferenceService
            Container container = new Container();
            container.Add(component, referenceName);
            context = new TestTypeDescriptorContext();
            context.Container = container;
            Assert.Same(component, converter.ConvertFrom(context, null, referenceName));
        }