コード例 #1
0
        public void Extensibility_DeserializeWithInvalidRegisteredExtensionHandlers()
        {
            XliffDocument document;
            XliffDataList expectedChildren;
            List<IExtensionAttribute> expectedAttributes;
            Dictionary<string, IExtensionHandler> handlers;
            IExtensible extensible;
            XliffData data1;
            XliffData data2;
            XliffData data3;

            handlers = new Dictionary<string, IExtensionHandler>();
            handlers["bogus"] = new CustomHandler("bogus", "bogus");
            document = TestUtilities.Deserialize(TestData.DocumentWithExtensions, true, handlers);

            Console.WriteLine("Verifying document extensions.");
            extensible = (IExtensible)document;
            Assert.IsNotNull(extensible.Extensions, "Extensions should not be null");
            Assert.AreEqual(1, extensible.Extensions.Count, "Extensions count is incorrect.");

            Console.WriteLine("  Verifying Extensions[0].");
            Assert.AreEqual(ExtensibilityTests.DefaultExtensionName, extensible.Extensions[0].Name, "Extension name is incorrect.");
            expectedAttributes = new List<IExtensionAttribute>();
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 1"));
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute2", "attribute 2"));
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace2, "attribute1", "attribute 3"));
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace2, "attribute2", "attribute 4"));
            this.VerifyAttributes(expectedAttributes, extensible.Extensions[0]);
            this.VerifyExtensionChildren("    ", null, extensible.Extensions[0]);

            Console.WriteLine("Verifying file extensions.");
            extensible = (IExtensible)document.Files[0];
            Assert.IsNotNull(extensible.Extensions, "Extensions should not be null");
            Assert.AreEqual(1, extensible.Extensions.Count, "Extensions count is incorrect.");

            Console.WriteLine("  Verifying Extensions[0].");
            Assert.AreEqual(ExtensibilityTests.DefaultExtensionName, extensible.Extensions[0].Name, "Extension name is incorrect.");
            expectedAttributes = new List<IExtensionAttribute>();
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 5"));
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute2", "attribute 6"));
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace2, "attribute1", "attribute 7"));
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace2, "attribute2", "attribute 8"));
            this.VerifyAttributes(expectedAttributes, extensible.Extensions[0]);

            expectedChildren = new XliffDataList();
            // Child 0.
            data1 = expectedChildren.AddChild(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element1", typeof(GenericElement));
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute1", "attribute 9");
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute2", "attribute 10");
            data2 = data1.Children.AddChild(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element2", typeof(GenericElement));
            data2.AddAttribute(ExtensibilityTests.Namespace1, "attribute3", "attribute 11");
            data3 = data2.Children.AddChild(NamespaceValues.Core, "source", typeof(Source));
            data3.AddAttribute(NamespacePrefixes.Xml, null, "lang", "de-de", true);
            data3.AddAttribute(NamespacePrefixes.Xml, null, "space", Preservation.Preserve, true);

            // Child 1.
            data1 = expectedChildren.AddChild(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element1", typeof(GenericElement));
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute1", "attribute 12");
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute2", "attribute 13");
            data2 = data1.Children.AddChild(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element2", typeof(GenericElement));
            data2.AddAttribute(ExtensibilityTests.Namespace1, "attribute3", "attribute 14");
            data2.Text = "text 1";

            // Child 2.
            data1 = expectedChildren.AddChild(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element1", typeof(GenericElement));
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute1", "attribute 15");
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute2", "attribute 16");
            data1.AddAttribute(ExtensibilityTests.Namespace2, "attribute1", "attribute 17");
            data2 = data1.Children.AddChild(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element1", typeof(GenericElement));
            data2.AddAttribute(ExtensibilityTests.Namespace1, "attribute2", "attribute 18");
            data2.Text = "text 3";

            // Child 3.
            data1 = expectedChildren.AddChild(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element1", typeof(GenericElement));
            data1.AddAttribute(ExtensibilityTests.Namespace2, "attribute1", "attribute 19");
            data1.AddAttribute(ExtensibilityTests.Namespace2, "attribute2", "attribute 20");
            data2 = data1.Children.AddChild(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element2", typeof(GenericElement));
            data2.AddAttribute(ExtensibilityTests.Namespace2, "attribute3", "attribute 21");
            data3 = data2.Children.AddChild(NamespaceValues.Core, "target", typeof(Target));
            data3.AddAttribute(null, "order", null, false);
            data3.AddAttribute(NamespacePrefixes.Xml, null, "lang", null, false);
            data3.AddAttribute(NamespacePrefixes.Xml, null, "space", Preservation.Default, false);

            // Child 4.
            data1 = expectedChildren.AddChild(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element1", typeof(GenericElement));
            data1.AddAttribute(ExtensibilityTests.Namespace2, "attribute1", "attribute 22");
            data1.AddAttribute(ExtensibilityTests.Namespace2, "attribute2", "attribute 23");
            data2 = data1.Children.AddChild(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element2", typeof(GenericElement));
            data2.AddAttribute(ExtensibilityTests.Namespace2, "attribute3", "attribute 24");
            data2.Text = "text 2";

            this.VerifyExtensionChildren("    ", expectedChildren, extensible.Extensions[0]);

            Console.WriteLine("Verifying unit extensions.");
            extensible = (IExtensible)document.Files[0].Containers[0];
            Assert.IsFalse(extensible.HasExtensions, "HasExtensions is incorrect.");
        }
コード例 #2
0
        public void Extensibility_DeserializeWithTwoRegisteredExtensionHandlers()
        {
            CustomExtension extension;
            XliffDocument document;
            XliffDataList expectedChildren;
            List<IExtensionAttribute> expectedAttributes;
            Dictionary<string, IExtensionHandler> handlers;
            IExtensible extensible;
            XliffData data1;
            XliffData data2;
            XliffData data3;

            handlers = new Dictionary<string, IExtensionHandler>();
            handlers[ExtensibilityTests.Namespace1] = new CustomHandler(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1);
            handlers[ExtensibilityTests.Namespace2] = new CustomHandler(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2);
            document = TestUtilities.Deserialize(TestData.DocumentWithExtensions, true, handlers);

            Console.WriteLine("Verifying document extensions.");
            extensible = (IExtensible)document;
            Assert.IsNotNull(extensible.Extensions, "Extensions should not be null");
            Assert.AreEqual(2, extensible.Extensions.Count, "Extensions count is incorrect.");

            Console.WriteLine("  Verifying Extensions[0].");
            Assert.IsInstanceOfType(extensible.Extensions[0], typeof(CustomExtension), "Extension[0] type is incorrect.");
            Assert.AreEqual(ExtensibilityTests.Namespace1, extensible.Extensions[0].Name, "Extension name is incorrect.");
            expectedAttributes = new List<IExtensionAttribute>();
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 1"));
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute2", "attribute 2"));
            this.VerifyAttributes(expectedAttributes, extensible.Extensions[0]);
            this.VerifyExtensionChildren("    ", null, extensible.Extensions[0]);

            Console.WriteLine("  Verifying Extensions[1].");
            Assert.IsInstanceOfType(extensible.Extensions[1], typeof(CustomExtension), "Extension[1] type is incorrect.");
            Assert.AreEqual(ExtensibilityTests.Namespace2, extensible.Extensions[1].Name, "Extension name is incorrect.");
            expectedAttributes = new List<IExtensionAttribute>();
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace2, "attribute1", "attribute 3"));
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace2, "attribute2", "attribute 4"));
            this.VerifyAttributes(expectedAttributes, extensible.Extensions[1]);
            this.VerifyExtensionChildren("    ", null, extensible.Extensions[1]);

            Console.WriteLine("Verifying file extensions.");
            extensible = (IExtensible)document.Files[0];
            Assert.IsNotNull(extensible.Extensions, "Extensions should not be null");
            Assert.AreEqual(2, extensible.Extensions.Count, "Extensions count is incorrect.");

            Console.WriteLine("  Verifying Extensions[0].");
            Assert.AreEqual(ExtensibilityTests.Namespace1, extensible.Extensions[0].Name, "Extension name is incorrect.");
            expectedAttributes = new List<IExtensionAttribute>();
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute1", "attribute 5"));
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "attribute2", "attribute 6"));
            this.VerifyAttributes(expectedAttributes, extensible.Extensions[0]);

            expectedChildren = new XliffDataList();
            // Child 0.
            data1 = expectedChildren.AddChild(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element1", typeof(CustomElement1));
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute1", "attribute 9");
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute2", "attribute 10");
            data2 = data1.Children.AddChild(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element2", typeof(CustomElement2));
            data2.AddAttribute(ExtensibilityTests.Namespace1, "attribute3", "attribute 11");
            data3 = data2.Children.AddChild(NamespaceValues.Core, "source", typeof(GenericElement));
            data3.AddAttribute(null, "http://www.w3.org/XML/1998/namespace", "lang", "de-de", true);
            data3.AddAttribute(null, "http://www.w3.org/XML/1998/namespace", "space", "preserve", true);

            // Child 1.
            data1 = expectedChildren.AddChild(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element1", typeof(CustomElement1));
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute1", "attribute 12");
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute2", "attribute 13");
            data2 = data1.Children.AddChild(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element2", typeof(CustomElement2));
            data2.AddAttribute(ExtensibilityTests.Namespace1, "attribute3", "attribute 14");
            data2.Text = "text 1";

            // Child 2.
            data1 = expectedChildren.AddChild(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace1, "element1", typeof(CustomElement1));
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute1", "attribute 15");
            data1.AddAttribute(ExtensibilityTests.Namespace1, "attribute2", "attribute 16");
            // Attribute22 is in a different namespace than CustomElement1 so it is discarded.
            data2 = data1.Children.AddChild(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element1", typeof(GenericElement));
            data2.AddAttribute(ExtensibilityTests.Namespace1, "attribute2", "attribute 18");
            data2.Text = "text 3";

            this.VerifyExtensionChildren("    ", expectedChildren, extensible.Extensions[0]);

            // Verify the attribute got set.
            extension = (CustomExtension)extensible.Extensions[0];
            Assert.AreEqual(
                            "attribute 9",
                            ((CustomElement1)extension.GetChildren().ToList()[0].Element).Attribute1,
                            "Attribute1 is incorrect.");

            Console.WriteLine("  Verifying Extensions[1].");
            Assert.AreEqual(ExtensibilityTests.Namespace2, extensible.Extensions[1].Name, "Extension name is incorrect.");
            expectedAttributes = new List<IExtensionAttribute>();
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace2, "attribute1", "attribute 7"));
            expectedAttributes.Add(new GenericExtensionAttribute(ExtensibilityTests.Prefix1, ExtensibilityTests.Namespace2, "attribute2", "attribute 8"));
            this.VerifyAttributes(expectedAttributes, extensible.Extensions[1]);

            expectedChildren = new XliffDataList();
            // Child 0.
            data1 = expectedChildren.AddChild(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element1", typeof(CustomElement1));
            data1.AddAttribute(ExtensibilityTests.Namespace2, "attribute1", "attribute 19");
            data1.AddAttribute(ExtensibilityTests.Namespace2, "attribute2", "attribute 20");
            data2 = data1.Children.AddChild(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element2", typeof(CustomElement2));
            data2.AddAttribute(ExtensibilityTests.Namespace2, "attribute3", "attribute 21");
            data3 = data2.Children.AddChild(NamespaceValues.Core, "target", typeof(GenericElement));

            // Child 1.
            data1 = expectedChildren.AddChild(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element1", typeof(CustomElement1));
            data1.AddAttribute(ExtensibilityTests.Namespace2, "attribute1", "attribute 22");
            data1.AddAttribute(ExtensibilityTests.Namespace2, "attribute2", "attribute 23");
            data2 = data1.Children.AddChild(ExtensibilityTests.Prefix2, ExtensibilityTests.Namespace2, "element2", typeof(CustomElement2));
            data2.AddAttribute(ExtensibilityTests.Namespace2, "attribute3", "attribute 24");
            data2.Text = "text 2";

            this.VerifyExtensionChildren("    ", expectedChildren, extensible.Extensions[1]);

            Console.WriteLine("Verifying unit extensions.");
            extensible = (IExtensible)document.Files[0].Containers[0];
            Assert.IsFalse(extensible.HasExtensions, "HasExtensions is incorrect.");
        }