< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Description.SchemaHelper
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Description/SchemaHelper.cs
Line coverage
48%
Covered lines: 46
Uncovered lines: 49
Coverable lines: 95
Total lines: 237
Line coverage: 48.4%
Branch coverage
41%
Covered branches: 32
Total branches: 78
Branch coverage: 41%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
AddElementForm(...)50%2266.66%
AddElementToSchema(...)25%8860%
AddImportToSchema(...)100%1616100%
AddTypeToSchema(...)25%4462.5%
GetSchema(...)83.33%1212100%
GetTypeName(...)0%440%
IsMatch(...)0%10100%
NamespacesEqual(...)33.33%6666.66%
Compile(...)100%1187.5%
HandleSchemaValidationError(...)0%660%
.cctor()100%110%
IsElementValueType(...)0%10100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Description/SchemaHelper.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections;
 6using System.Collections.Generic;
 7using System.Collections.ObjectModel;
 8using System.Xml;
 9using System.Xml.Schema;
 10using CoreWCF.Runtime;
 11
 12namespace CoreWCF.Description
 13{
 14    internal static class SchemaHelper
 15    {
 16        internal static void AddElementForm(XmlSchemaElement element, XmlSchema schema)
 17        {
 21718            if (schema.ElementFormDefault != XmlSchemaForm.Qualified)
 19            {
 020                element.Form = XmlSchemaForm.Qualified;
 21            }
 21722        }
 23
 24        internal static void AddElementToSchema(XmlSchemaElement element, XmlSchema schema, XmlSchemaSet schemaSet)
 25        {
 22026            XmlSchemaElement existingElement = (XmlSchemaElement)schema.Elements[new XmlQualifiedName(element.Name, sche
 22027            if (existingElement != null)
 28            {
 029                if (element.SchemaType == existingElement.SchemaType && element.SchemaTypeName == existingElement.Schema
 30                {
 031                    return;
 32                }
 33
 034                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx
 35            }
 36
 22037            schema.Items.Add(element);
 22038            if (!element.SchemaTypeName.IsEmpty)
 39            {
 040                AddImportToSchema(element.SchemaTypeName.Namespace, schema);
 41            }
 42
 22043            schemaSet.Reprocess(schema);
 22044        }
 45
 46        internal static void AddImportToSchema(string ns, XmlSchema schema)
 47        {
 21648            if (NamespacesEqual(ns, schema.TargetNamespace)
 21649                || NamespacesEqual(ns, XmlSchema.Namespace)
 21650                || NamespacesEqual(ns, XmlSchema.InstanceNamespace))
 51            {
 11852                return;
 53            }
 54
 28455            foreach (object item in schema.Includes)
 56            {
 8257                if (item is XmlSchemaImport && NamespacesEqual(ns, ((XmlSchemaImport)item).Namespace))
 58                {
 7659                    return;
 60                }
 61            }
 62
 2263            XmlSchemaImport import = new XmlSchemaImport();
 2264            if (ns != null && ns.Length > 0)
 65            {
 2266                import.Namespace = ns;
 67            }
 68
 2269            schema.Includes.Add(import);
 9870        }
 71
 72        internal static void AddTypeToSchema(XmlSchemaType type, XmlSchema schema, XmlSchemaSet schemaSet)
 73        {
 1574            XmlSchemaType existingType = (XmlSchemaType)schema.SchemaTypes[new XmlQualifiedName(type.Name, schema.Target
 1575            if (existingType != null)
 76            {
 077                if (existingType == type)
 78                {
 079                    return;
 80                }
 81
 082                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx
 83            }
 84
 1585            schema.Items.Add(type);
 86
 1587            schemaSet.Reprocess(schema);
 1588        }
 89
 90        internal static XmlSchema GetSchema(string ns, XmlSchemaSet schemaSet)
 91        {
 49792            if (ns == null) { ns = string.Empty; }
 93
 49794            ICollection schemas = schemaSet.Schemas();
 169495            foreach (XmlSchema existingSchema in schemas)
 96            {
 58097                if ((existingSchema.TargetNamespace == null && ns.Length == 0) || ns.Equals(existingSchema.TargetNamespa
 98                {
 46099                    return existingSchema;
 100                }
 101            }
 102
 37103            XmlSchema schema = new XmlSchema();
 37104            schema.ElementFormDefault = XmlSchemaForm.Qualified;
 37105            if (ns.Length > 0)
 106            {
 37107                schema.TargetNamespace = ns;
 108            }
 109
 37110            schemaSet.Add(schema);
 37111            return schema;
 460112        }
 113
 114        private static string GetTypeName(XmlSchemaElement element)
 115        {
 0116            if (element.SchemaType != null)
 117            {
 0118                return "anonymous";
 119            }
 120
 0121            if (!element.SchemaTypeName.IsEmpty)
 122            {
 0123                return element.SchemaTypeName.ToString();
 124            }
 125
 0126            return string.Empty;
 127        }
 128
 129        // Compare XmlSchemaElement properties set by WsdlExporter: do not check element QName,
 130        // this code only called for elements with matching name and namespace
 131        internal static bool IsMatch(XmlSchemaElement e1, XmlSchemaElement e2)
 132        {
 133            // this code only called for elements with matching name and namespace
 134            Fx.Assert(e1.Name == e2.Name, "");
 135            // Anonymous types never match
 0136            if (e1.SchemaType != null || e2.SchemaType != null)
 137            {
 0138                return false;
 139            }
 140
 0141            if (e1.SchemaTypeName != e2.SchemaTypeName)
 142            {
 0143                return false;
 144            }
 145
 146            // do not need to check parent Schema.ElementFormDefault: see AddElementForm in this class.
 0147            if (e1.Form != e2.Form)
 148            {
 0149                return false;
 150            }
 151
 0152            if (e1.IsNillable != e2.IsNillable)
 153            {
 0154                return false;
 155            }
 156
 0157            return true;
 158        }
 159
 160        internal static bool NamespacesEqual(string ns1, string ns2)
 161        {
 1972162            if (ns1 == null || ns1.Length == 0)
 163            {
 0164                return ns2 == null || ns2.Length == 0;
 165            }
 166            else
 167            {
 1972168                return ns1 == ns2;
 169            }
 170        }
 171
 172        internal static void Compile(XmlSchemaSet schemaSet, Collection<MetadataConversionError> errors)
 173        {
 243174            ValidationEventHandler validationEventHandler = new ValidationEventHandler(delegate (object sender, Validati
 243175            {
 0176                HandleSchemaValidationError(sender, args, errors);
 243177            });
 243178            schemaSet.ValidationEventHandler += validationEventHandler;
 243179            schemaSet.Compile();
 243180            schemaSet.ValidationEventHandler -= validationEventHandler;
 243181        }
 182
 183        internal static void HandleSchemaValidationError(object sender, ValidationEventArgs args, Collection<MetadataCon
 184        {
 185            MetadataConversionError warning;
 0186            if (args.Exception != null && args.Exception.SourceUri != null)
 187            {
 0188                XmlSchemaException ex = args.Exception;
 0189                warning = new MetadataConversionError(SR.Format(SR.SchemaValidationError, ex.SourceUri, ex.LineNumber, e
 190            }
 191            else
 192            {
 0193                warning = new MetadataConversionError(SR.Format(SR.GeneralSchemaValidationError, args.Message));
 194            }
 195
 0196            if (!errors.Contains(warning))
 197            {
 0198                errors.Add(warning);
 199            }
 0200        }
 201
 0202        private static IList<string> xsdValueTypePrimitives = new string[]
 0203        {
 0204            "boolean", "float", "double", "decimal", "long", "unsignedLong", "int", "unsignedInt", "short", "unsignedSho
 0205            "duration", "dateTime", "integer", "positiveInteger", "negativeInteger", "nonPositiveInteger"
 0206        };
 0207        private static IList<string> dataContractPrimitives = new string[] { "char", "guid" };
 0208        private static string dataContractSerializerNamespace = "http://schemas.microsoft.com/2003/10/Serialization/";
 0209        private static string xmlSerializerNamespace = "http://microsoft.com/wsdl/types/";
 210
 211        internal static bool IsElementValueType(XmlSchemaElement element)
 212        {
 0213            XmlQualifiedName typeName = element.SchemaTypeName;
 0214            if (typeName == null || typeName.IsEmpty)
 215            {
 0216                return false;
 217            }
 218
 0219            if (typeName.Namespace == XmlSchema.Namespace)
 220            {
 0221                return xsdValueTypePrimitives.Contains(typeName.Name);
 222            }
 223
 0224            if (typeName.Namespace == dataContractSerializerNamespace)
 225            {
 0226                return dataContractPrimitives.Contains(typeName.Name);
 227            }
 228
 0229            if (typeName.Namespace == xmlSerializerNamespace)
 230            {
 0231                return dataContractPrimitives.Contains(typeName.Name);
 232            }
 233
 0234            return false;
 235        }
 236    }
 237}