예제 #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 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));
        }
예제 #4
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);
        }
예제 #5
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);
            }
        }
예제 #6
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);
        }
예제 #7
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);
        }
예제 #8
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);
                }
            }
        }
예제 #9
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);
        }