예제 #1
0
        public void RountTripTest()
        {
            // AppContext SetSwitch seems to be unreliable in the unit test case. So let's not rely on it
            // for test coverage. But let's do look at the app switch to get our verification correct.
            AppContext.TryGetSwitch("Switch.System.Runtime.Serialization.DataContracts.Auto_Import_KVP", out bool autoImportKVP);

            XsdDataContractExporter exporter = new XsdDataContractExporter();

            exporter.Export(typeof(RootClass));
            XsdDataContractImporter importer = new XsdDataContractImporter();

            importer.Options = new ImportOptions();
            importer.Options.ImportXmlType = true;
            importer.Options.ReferencedTypes.Add(typeof(DBNull));
            importer.Options.ReferencedTypes.Add(typeof(DateTimeOffset));
            importer.Import(exporter.Schemas);

            string code = SchemaUtils.DumpCode(importer.CodeCompileUnit);

            _output.WriteLine(code);

            Assert.Contains(@"This code was generated by a tool.", code);
            Assert.Contains(@"namespace System.Runtime.Serialization.Schema.Tests", code);
            Assert.Contains(@"public partial class RoundTripTestRootClass : object, System.Runtime.Serialization.IExtensibleDataObject", code);
            Assert.Contains(@"[System.Xml.Serialization.XmlRootAttribute(ElementName=""SchemaDefinedType"", Namespace=""http://schemas.datacontract.org/2004/07/System.Runtime.Serialization"")]", code);
            Assert.Contains(@"public partial class dataSetType : object, System.Xml.Serialization.IXmlSerializable", code);
            Assert.Contains(@"[System.Runtime.Serialization.DataContractAttribute(Name=""RoundTripTest.DataContractClass"", Namespace=""http://schemas.datacontract.org/2004/07/System.Runtime.Serialization.Schema.Tests""", code);
            Assert.Contains(@"public partial class RoundTripTestDataContractClass : object, System.Runtime.Serialization.IExtensibleDataObject", code);
            Assert.Contains(@"[System.Runtime.Serialization.DataContractAttribute(Name=""RoundTripTest.DataContractStruct"", Namespace=""http://schemas.datacontract.org/2004/07/System.Runtime.Serialization.Schema.Tests""", code);
            Assert.Contains(@"public partial struct RoundTripTestDataContractStruct : System.Runtime.Serialization.IExtensibleDataObject", code);
            Assert.Contains(@"[System.Runtime.Serialization.DataContractAttribute(Name=""RoundTripTest.EmitDefaultClass"", Namespace=""http://schemas.datacontract.org/2004/07/System.Runtime.Serialization.Schema.Tests""", code);
            Assert.Contains(@"public partial class RoundTripTestEmitDefaultClass : object, System.Runtime.Serialization.IExtensibleDataObject", code);
            Assert.Contains(@"public System.Nullable<System.Runtime.Serialization.Schema.Tests.RoundTripTestDataContractStruct> NullableDataContractStruct2", code);
            Assert.Contains(@"[System.Runtime.Serialization.DataContractAttribute(Name=""RoundTripTest.EncodingMismatchClass"", Namespace=""http://schemas.datacontract.org/2004/07/System.Runtime.Serialization.Schema.Tests""", code);
            Assert.Contains(@"public partial class RoundTripTestEncodingMismatchClass : object, System.Runtime.Serialization.IExtensibleDataObject", code);
            Assert.Contains(@"public enum RoundTripTestMyEnum : int", code);
            Assert.Contains(@"TwoHundred = 200", code);
            Assert.Contains(@"public enum RoundTripTestMyFlagsEnum : int", code);
            Assert.Contains(@"Four = 4,", code);
            Assert.Contains(@"public class ArrayOfNullableOfRoundTripTestMyEnumho3BZmza : System.Collections.Generic.List<System.Runtime.Serialization.Schema.Tests.RoundTripTestMyEnum>", code);
            Assert.Contains(@"namespace schemas.microsoft.com._2003._10.Serialization.Arrays", code);
            Assert.Contains(@"public partial class ArrayOfKeyValueOfintArrayOfstringty7Ep6D1 : object, System.Xml.Serialization.IXmlSerializable", code);
            Assert.Contains(@"private static System.Xml.XmlQualifiedName typeName = new System.Xml.XmlQualifiedName(""ArrayOfKeyValueOfintArrayOfstringty7Ep6D1"", ""http://schemas.microsoft.com/2003/10/Serialization/Arrays"");", code);
            Assert.Contains(@"public partial class ArrayOfKeyValueOfNullableOfunsignedByteNullableOfunsignedByte_ShTDFhl_P : object, System.Xml.Serialization.IXmlSerializable", code);

            if (autoImportKVP)
            {
                Assert.Contains(@"public partial struct KeyValuePairOfintArrayOfstringty7Ep6D1 : System.Runtime.Serialization.IExtensibleDataObject", code);
                Assert.Contains(@"public partial struct KeyValuePairOfNullableOfunsignedByteNullableOfunsignedByte_ShTDFhl_P : System.Runtime.Serialization.IExtensibleDataObject", code);
                Assert.Contains(@"[System.Runtime.Serialization.DataContractAttribute(Name=""KeyValuePairOfstringNullableOfintU6ho3Bhd"", Namespace=""http://schemas.datacontract.org/2004/07/System.Collections.Generic"")]", code);
            }
            else
            {
                Assert.DoesNotContain(@"public partial struct KeyValuePairOfintArrayOfstringty7Ep6D1 : System.Runtime.Serialization.IExtensibleDataObject", code);
                Assert.DoesNotContain(@"public partial struct KeyValuePairOfNullableOfunsignedByteNullableOfunsignedByte_ShTDFhl_P : System.Runtime.Serialization.IExtensibleDataObject", code);
                Assert.DoesNotContain(@"[System.Runtime.Serialization.DataContractAttribute(Name=""KeyValuePairOfstringNullableOfintU6ho3Bhd"", Namespace=""http://schemas.datacontract.org/2004/07/System.Collections.Generic"")]", code);
            }
        }
예제 #2
0
        public void ImportXmlType()
        {
            XsdDataContractImporter importer = SchemaUtils.CreateImporterWithDefaultOptions();

            importer.Options.ImportXmlType = true;
            importer.Import(SchemaUtils.PositiveSchemas, SchemaUtils.ValidTypeNames[0]);
            _output.WriteLine(SchemaUtils.DumpCode(importer.CodeCompileUnit));
            Assert.True(importer.Options.ImportXmlType);
        }
예제 #3
0
        public static IEnumerable <object[]> CanImport_MemberData()
        {
            // CanImport(XmlSchemaSet)
            yield return(new object[] { true, (XsdDataContractImporter imp) => imp.CanImport(SchemaUtils.PositiveSchemas) });

            yield return(new object[] { false, (XsdDataContractImporter imp) => imp.CanImport(null), typeof(ArgumentNullException), @"Value cannot be null. (Parameter 'schemas')" });

            yield return(new object[] { false, (XsdDataContractImporter imp) => imp.CanImport(SchemaUtils.MixedSchemas) });

            // CanImport(XmlSchemaSet, ICollection<XmlQualifiedName>)
            yield return(new object[] { true, (XsdDataContractImporter imp) => imp.CanImport(SchemaUtils.PositiveSchemas, new XmlQualifiedName[] { SchemaUtils.ValidTypeNames[0] }) });

            yield return(new object[] { false, (XsdDataContractImporter imp) => imp.CanImport(null, SchemaUtils.InvalidTypeNames), typeof(ArgumentNullException), @"Value cannot be null. (Parameter 'schemas')" });

            yield return(new object[] { false, (XsdDataContractImporter imp) => imp.CanImport(SchemaUtils.MixedSchemas, (ICollection <XmlQualifiedName>)null), typeof(ArgumentNullException), @"Value cannot be null. (Parameter 'typeNames')" });

            yield return(new object[] { false, (XsdDataContractImporter imp) => imp.CanImport(SchemaUtils.MixedSchemas, new XmlQualifiedName[] { null }), typeof(ArgumentException), @"Cannot import type for null XmlQualifiedName specified via parameter." });

            yield return(new object[] { false, (XsdDataContractImporter imp) => imp.CanImport(SchemaUtils.MixedSchemas, SchemaUtils.InvalidTypeNames) });

            // CanImport(XmlSchemaSet, XmlQualifiedName)
            yield return(new object[] { true, (XsdDataContractImporter imp) => imp.CanImport(SchemaUtils.PositiveSchemas, SchemaUtils.ValidTypeNames[0]) });

            yield return(new object[] { false, (XsdDataContractImporter imp) => imp.CanImport(null, SchemaUtils.InvalidTypeNames[0]), typeof(ArgumentNullException), @"Value cannot be null. (Parameter 'schemas')" });

            yield return(new object[] { false, (XsdDataContractImporter imp) => imp.CanImport(SchemaUtils.MixedSchemas, (XmlQualifiedName)null), typeof(ArgumentNullException), @"Value cannot be null. (Parameter 'typeName')" });

            yield return(new object[] { false, (XsdDataContractImporter imp) => imp.CanImport(SchemaUtils.MixedSchemas, SchemaUtils.InvalidTypeNames[0]) });

            // CanImport(XmlSchemaSet, XmlSchemaElement)
            // TODO

            // CanImportTests.cs
            foreach (var citArgs in SchemaUtils.CanImportTests)
            {
                XmlSchemaSet schemaSet = SchemaUtils.ReadStringsIntoSchemaSet(citArgs.schemaString);
                if (citArgs.qnames == null)
                {
                    yield return new object[] { citArgs.expectedResult, (XsdDataContractImporter imp) => imp.CanImport(schemaSet) }
                }
                ;
                else if (citArgs.qnames.Length == 1 && citArgs.isElement)
                {
                    yield return new object[] { citArgs.expectedResult, (XsdDataContractImporter imp) => imp.CanImport(schemaSet, SchemaUtils.GetSchemaElement(schemaSet, citArgs.qnames[0])) }
                }
                ;
                else if (citArgs.qnames.Length == 1)
                {
                    yield return new object[] { citArgs.expectedResult, (XsdDataContractImporter imp) => imp.CanImport(schemaSet, citArgs.qnames[0]) }
                }
                ;
                else
                {
                    yield return new object[] { citArgs.expectedResult, (XsdDataContractImporter imp) => imp.CanImport(schemaSet, citArgs.qnames) }
                };
            }
        }
예제 #4
0
        public void Namespaces(string dcns, string clrns)
        {
            XsdDataContractImporter importer = SchemaUtils.CreateImporterWithDefaultOptions();

            Assert.NotNull(importer.Options.Namespaces);
            Assert.Empty(importer.Options.Namespaces);

            importer.Options.Namespaces.Add(dcns, clrns);
            importer.Import(SchemaUtils.PositiveSchemas, SchemaUtils.ValidTypeNames[0]);
            _output.WriteLine(SchemaUtils.DumpCode(importer.CodeCompileUnit));
        }
예제 #5
0
        public void CodeProvider()
        {
            XsdDataContractImporter importer     = SchemaUtils.CreateImporterWithDefaultOptions();
            CodeDomProvider         codeProvider = CodeDomProvider.CreateProvider("csharp");

            importer.Options.CodeProvider = codeProvider;
            Console.WriteLine(importer.Options.CodeProvider.GetType().FullName);
            importer.Import(SchemaUtils.PositiveSchemas, SchemaUtils.ValidTypeNames[0]);
            _output.WriteLine(SchemaUtils.DumpCode(importer.CodeCompileUnit));
            Assert.Equal(codeProvider, importer.Options.CodeProvider);
        }
예제 #6
0
        public void Import(Action <XsdDataContractImporter> import, int codeLength = -1)
        {
            XsdDataContractImporter importer = SchemaUtils.CreateImporterWithDefaultOptions();

            import(importer);
            string code = SchemaUtils.DumpCode(importer.CodeCompileUnit);

            _output.WriteLine(code);
            if (codeLength >= 0)
            {
                Assert.Equal(codeLength, code.Length);
            }
        }
예제 #7
0
        public void IsReferenceType()
        {
            XsdDataContractExporter exporter = new XsdDataContractExporter();

            exporter.Export(typeof(RootIsReferenceContainer));
            XsdDataContractImporter importer = new XsdDataContractImporter();

            importer.Options = new ImportOptions();
            importer.Options.ImportXmlType = true;
            importer.Import(exporter.Schemas);
            string code = SchemaUtils.DumpCode(importer.CodeCompileUnit);

            _output.WriteLine(code);

            Assert.True(code.Length > 616);
        }
예제 #8
0
        public static IEnumerable <object[]> Import_MemberData()
        {
            int newlineSize = Environment.NewLine.Length;

            // Import(XmlSchemaSet)
            yield return(new object[] { (XsdDataContractImporter imp) => imp.Import(SchemaUtils.PositiveSchemas), 5060 + (168 * newlineSize) });   // 168 lines

            // Import(XmlSchemaSet, ICollection<XmlQualifiedName>)
            yield return(new object[] { (XsdDataContractImporter imp) => imp.Import(SchemaUtils.PositiveSchemas, new XmlQualifiedName[] { SchemaUtils.ValidTypeNames[0] }), 1515 + (50 * newlineSize) }); // 50 lines

            // Import(XmlSchemaSet, XmlQualifiedName)
            yield return(new object[] { (XsdDataContractImporter imp) => imp.Import(SchemaUtils.PositiveSchemas, SchemaUtils.ValidTypeNames[0]), 1515 + (50 * newlineSize) }); // 50 lines

            // Import(XmlSchemaSet, XmlSchemaElement)
            // TODO

            // From CanImportTests.cs
            foreach (var citArgs in SchemaUtils.CanImportTests)
            {
                if (citArgs.expectedResult)
                {
                    XmlSchemaSet schemaSet = SchemaUtils.ReadStringsIntoSchemaSet(citArgs.schemaString);

                    if (citArgs.qnames == null)
                    {
                        yield return new object[] { (XsdDataContractImporter imp) => imp.Import(schemaSet) }
                    }
                    ;
                    else if (citArgs.qnames.Length == 1 && citArgs.isElement)
                    {
                        yield return new object[] { (XsdDataContractImporter imp) => { imp.Import(schemaSet, SchemaUtils.GetSchemaElement(schemaSet, citArgs.qnames[0])); } }
                    }
                    ;
                    else if (citArgs.qnames.Length == 1 && !citArgs.isElement)
                    {
                        yield return new object[] { (XsdDataContractImporter imp) => imp.Import(schemaSet, citArgs.qnames[0]) }
                    }
                    ;
                    else
                    {
                        yield return new object[] { (XsdDataContractImporter imp) => imp.Import(schemaSet, citArgs.qnames) }
                    };
                }
            }

            // From FormatVersioning.cs : Positive tests
            (string msg, Type type, string xpath, string xmlFrag)[] formatVersioningArgs = new (string, Type, string, string)[] {
예제 #9
0
        public void CanImport(bool expectedResult, Func <XsdDataContractImporter, bool> canImport, Type expectedExceptionType = null, string msg = null)
        {
            XsdDataContractImporter importer = SchemaUtils.CreateImporterWithDefaultOptions();

            if (expectedExceptionType == null)
            {
                Assert.Equal(expectedResult, canImport(importer));
            }
            else
            {
                var ex = Assert.Throws(expectedExceptionType, () => canImport(importer));

                if (!string.IsNullOrEmpty(msg))
                {
                    Assert.Equal(msg, ex.Message);
                }
            }
        }
예제 #10
0
        public void DefaultOptions()
        {
            XsdDataContractImporter importer = SchemaUtils.CreateImporterWithDefaultOptions();

            Assert.NotNull(importer);
            Assert.NotNull(importer.Options);
            Assert.False(importer.Options.EnableDataBinding);
            Assert.False(importer.Options.GenerateInternal);
            Assert.False(importer.Options.GenerateSerializable);
            Assert.False(importer.Options.ImportXmlType);
            Assert.Null(importer.Options.CodeProvider);
            Assert.NotNull(importer.Options.Namespaces);
            Assert.Empty(importer.Options.Namespaces);
            Assert.NotNull(importer.Options.ReferencedCollectionTypes);
            Assert.Empty(importer.Options.ReferencedCollectionTypes);
            Assert.NotNull(importer.Options.ReferencedTypes);
            Assert.Empty(importer.Options.ReferencedTypes);
            Assert.Null(importer.Options.DataContractSurrogate);
        }
예제 #11
0
        public void WithSurrogateBinding()
        {
            XsdDataContractExporter exporter = new XsdDataContractExporter();

            exporter.Options = new ExportOptions();
            exporter.Options.DataContractSurrogate = new SurrogateProvider(true);
            for (int i = 0; i < testTypes.Length; i++)
            {
                exporter.Export((Type)testTypes[i]);
            }

            XsdDataContractImporter importer = new XsdDataContractImporter();

            importer         = new XsdDataContractImporter();
            importer.Options = new ImportOptions();
            importer.Options.DataContractSurrogate = exporter.Options.DataContractSurrogate;
            importer.Options.ImportXmlType         = true;
            importer.Options.ReferencedTypes.Add(typeof(Circle));
            importer.Import(exporter.Schemas);

            string code = SchemaUtils.DumpCode(importer.CodeCompileUnit);

            _output.WriteLine(code);

            Assert.Contains(@"[assembly: System.Runtime.Serialization.ContractNamespaceAttribute(""http://special1.tempuri.org"", ClrNamespace=""special1.tempuri.org"")]", code);
            Assert.DoesNotContain(@"[assembly: System.Runtime.Serialization.ContractNamespaceAttribute("""", ClrNamespace="""")]", code);

            Assert.Contains(@"namespace special1.tempuri.org", code);
            Assert.Matches(@"\[System.Runtime.Serialization.DataContractAttribute\(Name=""CircleContainer"", Namespace=""http://special1.tempuri.org""\)\]\s*public partial class CircleContainer : object, System.Runtime.Serialization.IExtensibleDataObject", code);
            Assert.Contains(@"private System.Runtime.Serialization.Schema.Tests.DataContracts.SerializableCircle[] circlesField;", code);
            Assert.Contains(@"private System.Runtime.Serialization.Schema.Tests.DataContracts.SerializableCircle CircleField;", code);
            Assert.Matches(@"\[System.Runtime.Serialization.DataMemberAttribute\(\)\]\s*public System.Runtime.Serialization.Schema.Tests.DataContracts.SerializableCircle Circle", code);

            Assert.DoesNotContain(@"namespace System.Runtime.Serialization.Schema.Tests.DataContracts", code);
            Assert.DoesNotContain(@"class SerializableSquare", code);
            Assert.DoesNotContain(@"class SerializableNode", code);
            Assert.DoesNotContain(@"class XmlSerializerPerson", code);
        }
예제 #12
0
        public void ReferencedTypes(XmlSchemaSet schemas, XmlQualifiedName qname, Type[] referencedTypes, Type expectedExceptionType = null, string msg = null)
        {
            XsdDataContractImporter importer = SchemaUtils.CreateImporterWithDefaultOptions();

            for (int i = 0; i < referencedTypes.Length; i++)
            {
                importer.Options.ReferencedTypes.Add(referencedTypes[i]);
            }

            if (expectedExceptionType == null)
            {
                importer.Import(schemas, qname);
                _output.WriteLine(SchemaUtils.DumpCode(importer.CodeCompileUnit));
            }
            else
            {
                var ex = Assert.Throws(expectedExceptionType, () => importer.Import(schemas, qname));

                if (!string.IsNullOrEmpty(msg))
                {
                    Assert.StartsWith(msg, ex.Message);
                }
            }
        }
예제 #13
0
        public void DefaultScenario()
        {
            XsdDataContractExporter exporter = new XsdDataContractExporter();

            exporter.Options = new ExportOptions();
            exporter.Options.DataContractSurrogate = new SurrogateProvider(false);
            for (int i = 0; i < testTypes.Length; i++)
            {
                exporter.Export((Type)testTypes[i]);
            }

            XsdDataContractImporter importer = new XsdDataContractImporter();

            importer.Options = new ImportOptions();
            importer.Options.DataContractSurrogate = exporter.Options.DataContractSurrogate;
            importer.Options.ImportXmlType         = true;
            importer.Import(exporter.Schemas);

            string code = SchemaUtils.DumpCode(importer.CodeCompileUnit);

            _output.WriteLine(code);

            Assert.Contains(@"[assembly: System.Runtime.Serialization.ContractNamespaceAttribute(""http://special1.tempuri.org"", ClrNamespace=""special1.tempuri.org"")]", code);
            Assert.Contains(@"[assembly: System.Runtime.Serialization.ContractNamespaceAttribute("""", ClrNamespace="""")]", code);

            Assert.Contains(@"namespace special1.tempuri.org", code);
            Assert.Matches(@"\[System.Runtime.Serialization.DataContractAttribute\(Name=""CircleContainer"", Namespace=""http://special1.tempuri.org""\)\]\s*public partial class CircleContainer : object, System.Runtime.Serialization.IExtensibleDataObject", code);
            Assert.Contains(@"private System.Runtime.Serialization.Schema.Tests.DataContracts.SerializableSquare[] circlesField;", code);
            Assert.Contains(@"private System.Runtime.Serialization.Schema.Tests.DataContracts.SerializableSquare CircleField;", code);
            Assert.Matches(@"\[System.Runtime.Serialization.DataMemberAttribute\(\)\]\s*public System.Runtime.Serialization.Schema.Tests.DataContracts.SerializableSquare Circle", code);
            Assert.Contains(@"public partial class SerializableSquare : object, System.Runtime.Serialization.IExtensibleDataObject", code);

            Assert.Contains(@"namespace System.Runtime.Serialization.Schema.Tests.DataContracts", code);
            Assert.Matches(@"\[System.Runtime.Serialization.DataContractAttribute\(Name\s*=\s*""SerializableNode"", Namespace\s*=\s*""http://schemas.datacontract.org/2004/07/System.Runtime.Serialization.Schema.Tests""\s*\+\s*"".DataContracts""\)\]\s*public partial class SerializableNode : object, System.Runtime.Serialization.IExtensibleDataObject", code);
            Assert.Matches(@"\[System.Xml.Serialization.XmlSchemaProviderAttribute\(""ExportSchema""\)\]\s*\[System.Xml.Serialization.XmlRootAttribute\(ElementName\s*=\s*""XmlSerializerPersonElement"", Namespace\s*=\s*""""\)\]\s*public partial class XmlSerializerPerson : object, System.Xml.Serialization.IXmlSerializable", code);
        }