< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Configuration.ExtensionElementCollection
Assembly: CoreWCF.ConfigurationManager
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.ConfigurationManager/src/CoreWCF/Configuration/ExtensionElementCollection.cs
Line coverage
57%
Covered lines: 24
Uncovered lines: 18
Coverable lines: 42
Total lines: 125
Line coverage: 57.1%
Branch coverage
53%
Covered branches: 15
Total branches: 28
Branch coverage: 53.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
GetElementExtension(...)100%11100%
Add(...)100%11100%
BaseAdd(...)100%22100%
BaseAdd(...)0%220%
GetElementKey(...)50%2275%
ContainsKey(...)100%11100%
InheritedElementExists(...)50%8837.5%
EnforceUniqueElement(...)57.14%141446.66%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.ConfigurationManager/src/CoreWCF/Configuration/ExtensionElementCollection.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.Configuration;
 6
 7namespace CoreWCF.Configuration
 8{
 9    [ConfigurationCollection(typeof(ExtensionElement), CollectionType = ConfigurationElementCollectionType.BasicMap)]
 10    internal class ExtensionElementCollection : ServiceModelConfigurationElementCollection<ExtensionElement>
 11    {
 12        public ExtensionElementCollection()
 213            : base(ConfigurationElementCollectionType.BasicMap, ConfigurationStrings.Add)
 14        {
 215        }
 16
 17        internal ExtensionElement GetElementExtension(object key)
 18        {
 1719            return (ExtensionElement)BaseGet(key);
 20        }
 21        internal void Add(ConfigurationElement element)
 22        {
 1623            BaseAdd(element);
 1624        }
 25
 26        protected override void BaseAdd(ConfigurationElement element)
 27        {
 1628            if (!InheritedElementExists((ExtensionElement)element))
 29            {
 1630                EnforceUniqueElement((ExtensionElement)element);
 1631                base.BaseAdd(element);
 32            }
 1633        }
 34
 35        protected override void BaseAdd(int index, ConfigurationElement element)
 36        {
 037            if (!InheritedElementExists((ExtensionElement)element))
 38            {
 039                EnforceUniqueElement((ExtensionElement)element);
 040                base.BaseAdd(index, element);
 41            }
 042        }
 43
 44        protected override object GetElementKey(ConfigurationElement element)
 45        {
 38146            if (null == element)
 47            {
 048                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element));
 49            }
 50
 38151            ExtensionElement configElementKey = (ExtensionElement)element;
 38152            return configElementKey.Name;
 53        }
 54
 55        internal bool ContainsKey(object key)
 56        {
 3357            return BaseGet(key) != null;
 58        }
 59
 60        private bool InheritedElementExists(ExtensionElement element)
 61        {
 62            // This is logic from ServiceModelEnhancedConfigurationElementCollection
 63            // The idea is to allow duplicate identical extension definition in different level (i.e. app level and mach
 64            // We however do not allow them on the same level.
 65            // Identical extension is defined by same name and type.
 1666            object newElementKey = GetElementKey(element);
 1667            if (ContainsKey(newElementKey))
 68            {
 069                ExtensionElement oldElement = (ExtensionElement)BaseGet(newElementKey);
 070                if (oldElement != null)
 71                {
 72                    // Is oldElement present in the different level of original config
 73                    // and name/type matching
 074                    if (!oldElement.ElementInformation.IsPresent &&
 075                        element.Type.Equals(oldElement.Type, StringComparison.Ordinal))
 76                    {
 077                        return true;
 78                    }
 79                }
 80            }
 81
 1682            return false;
 83        }
 84
 85        private void EnforceUniqueElement(ExtensionElement element)
 86        {
 14687            foreach (ExtensionElement extension in this)
 88            {
 5789                if (element.Name.Equals(extension.Name, StringComparison.Ordinal))
 90                {
 091                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
 092                        string.Format(SR.ConfigDuplicateExtensionName, element.Name)));
 93                }
 94
 5795                bool foundDuplicateType = false;
 5796                if (element.Type.Equals(extension.Type, StringComparison.OrdinalIgnoreCase))
 97                {
 098                    foundDuplicateType = true;
 99                }
 57100                else if (element.TypeName.Equals(extension.TypeName, StringComparison.Ordinal))
 101                {
 0102                    Type elementType = Type.GetType(element.Type, false);
 0103                    if (elementType != null && elementType == Type.GetType(extension.Type, false))
 104                    {
 0105                        foundDuplicateType = true;
 106                    }
 107                }
 108
 57109                if (foundDuplicateType)
 110                {
 0111                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
 0112                        string.Format(SR.ConfigDuplicateExtensionType, element.Type)));
 113                }
 114            }
 16115        }
 116
 117        protected override bool ThrowOnDuplicate
 118        {
 119            get
 120            {
 16121                return true;
 122            }
 123        }
 124    }
 125}