Exemplo n.º 1
0
        public void TestSupportedXmlDeserializationModes()
        {
            const string testXml = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n"
                                   + "<ExternalCollection>\r\n"
                                   + "  <External Type=\"ClearCanvas.ImageViewer.Externals.Tests.MockExternal\">\r\n"
                                   + "    <MockExternal xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n"
                                   + "      <Name>external1</Name>\r\n"
                                   + "      <Label>Label1</Label>\r\n"
                                   + "      <Enabled>true</Enabled>\r\n"
                                   + "      <WindowStyle>Normal</WindowStyle>\r\n"
                                   + "      <Data>Data1</Data>\r\n"
                                   + "    </MockExternal>\r\n"
                                   + "  </External>\r\n"
                                   + "  <External Type=\"ClearCanvas.ImageViewer.Externals.Tests.MockXmlSerializableExternal\">\r\n"
                                   + "    <MockXmlSerializableExternal Name=\"external2\" Label=\"Label2\" Enabled=\"False\" WindowStyle=\"Hidden\">\r\n"
                                   + "      <Data>Data2</Data>\r\n"
                                   + "    </MockXmlSerializableExternal>\r\n"
                                   + "  </External>\r\n"
                                   + "</ExternalCollection>";

            var expectedExternal1 = new MockExternal {
                Name = "external1", Label = "Label1", Enabled = true, WindowStyle = WindowStyle.Normal, Data = "Data1"
            };
            var expectedExternal2 = new MockXmlSerializableExternal {
                Name = "external2", Label = "Label2", Enabled = false, WindowStyle = WindowStyle.Hidden, Data = "Data2"
            };

            Assert.IsFalse(typeof(IXmlSerializable).IsAssignableFrom(expectedExternal1.GetType()), "One of the two externals should not be IXmlSerializable");
            Assert.IsTrue(typeof(IXmlSerializable).IsAssignableFrom(expectedExternal2.GetType()), "One of the two externals should be IXmlSerializable");

            var collection = ExternalCollection.Deserialize(testXml);

            Assert.IsNotNull(collection, "Deserialization returned null");
            Assert.AreEqual(2, collection.Count, "Deserialization returned collection with wrong number of entries");

            var external1 = CollectionUtils.SelectFirst(collection, e => e.Name == "external1");

            Assert.IsNotNull(external1, "Failed to deserialize external1");
            Assert.IsInstanceOf(typeof(MockExternal), external1, "external1: Wrong concrete implementation of IExternal");
            AssertMockExternal(expectedExternal1, (MockExternal)external1, "external1");

            var external2 = CollectionUtils.SelectFirst(collection, e => e.Name == "external2");

            Assert.IsNotNull(external2, "Failed to deserialize external2");
            Assert.IsInstanceOf(typeof(MockXmlSerializableExternal), external2, "external2: Wrong concrete implementation of IExternal");
            AssertMockExternal(expectedExternal2, (MockXmlSerializableExternal)external2, "external2");
        }
Exemplo n.º 2
0
        public void TestSupportedXmlSerializationModes()
        {
            var external1 = new MockExternal {
                Name = "external1", Label = "Label1", Enabled = true, WindowStyle = WindowStyle.Normal, Data = "Data1"
            };
            var external2 = new MockXmlSerializableExternal {
                Name = "external2", Label = "Label2", Enabled = false, WindowStyle = WindowStyle.Hidden, Data = "Data2"
            };

            Assert.IsFalse(typeof(IXmlSerializable).IsAssignableFrom(external1.GetType()), "One of the two externals should not be IXmlSerializable");
            Assert.IsTrue(typeof(IXmlSerializable).IsAssignableFrom(external2.GetType()), "One of the two externals should be IXmlSerializable");

            var xmlData = ExternalCollection.Serialize(new ExternalCollection {
                external1, external2
            });

            try
            {
                var xmlDoc = LoadXml(xmlData);

                AssertXmlNodeValue(typeof(MockExternal).FullName, xmlDoc, "/ExternalCollection/External[1]/@Type", "Type should be namespace qualified type name");
                AssertXmlNodeValue("external1", xmlDoc, "/ExternalCollection/External[1]/*/Name", "external1");
                AssertXmlNodeValue("Label1", xmlDoc, "/ExternalCollection/External[1]/*/Label", "external1");
                AssertXmlNodeValueIgnoreCase("true", xmlDoc, "/ExternalCollection/External[1]/*/Enabled", "external1");                 // ignore case - the default XML serializer is case insensitive for bools
                AssertXmlNodeValue("Normal", xmlDoc, "/ExternalCollection/External[1]/*/WindowStyle", "external1");
                AssertXmlNodeValue("Data1", xmlDoc, "/ExternalCollection/External[1]/*/Data", "external1");

                AssertXmlNodeValue(typeof(MockXmlSerializableExternal).FullName, xmlDoc, "/ExternalCollection/External[2]/@Type", "Type should be namespace qualified type name");
                AssertXmlNodeValue("external2", xmlDoc, "/ExternalCollection/External[2]/*/@Name", "external2");
                AssertXmlNodeValue("Label2", xmlDoc, "/ExternalCollection/External[2]/*/@Label", "external2");
                AssertXmlNodeValue("False", xmlDoc, "/ExternalCollection/External[2]/*/@Enabled", "external2");
                AssertXmlNodeValue("Hidden", xmlDoc, "/ExternalCollection/External[2]/*/@WindowStyle", "external2");
                AssertXmlNodeValue("Data2", xmlDoc, "/ExternalCollection/External[2]/*/Data", "external2");
            }
            catch (Exception)
            {
                Trace.WriteLine("XML Data Dump");
                Trace.WriteLine(xmlData);
                throw;
            }
        }