< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Configuration.CustomBindingElement
Assembly: CoreWCF.ConfigurationManager
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.ConfigurationManager/src/CoreWCF/Configuration/CustomBindingElement.cs
Line coverage
56%
Covered lines: 40
Uncovered lines: 31
Coverable lines: 71
Total lines: 184
Line coverage: 56.3%
Branch coverage
57%
Covered branches: 16
Total branches: 28
Branch coverage: 57.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
.ctor(...)100%11100%
Add(...)50%8841.17%
ApplyConfiguration(...)50%4469.23%
CanAdd(...)50%8880%
CanAddEncodingElement(...)100%11100%
CanAddExclusiveElement(...)66.66%6650%
CanAddStreamUpgradeElement(...)100%11100%
CanAddTransportElement(...)100%11100%
OnApplyConfiguration(...)100%22100%
CreateBinding()100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.ConfigurationManager/src/CoreWCF/Configuration/CustomBindingElement.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.ComponentModel;
 6using System.Configuration;
 7using CoreWCF.Channels;
 8
 9namespace CoreWCF.Configuration
 10{
 11    public sealed class CustomBindingElement : NamedServiceModelExtensionCollectionElement<BindingElementExtensionElemen
 12    {
 13        public CustomBindingElement()
 4014            : this(null) { }
 15
 16        public CustomBindingElement(string name) :
 4017            base(ConfigurationStrings.BindingElementExtensions, name) { }
 18
 19        [ConfigurationProperty(ConfigurationStrings.CloseTimeout, DefaultValue = ServiceDefaults.CloseTimeoutString)]
 20        [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
 21        public TimeSpan CloseTimeout
 22        {
 2023            get { return (TimeSpan)base[ConfigurationStrings.CloseTimeout]; }
 24            set
 25            {
 026                base[ConfigurationStrings.CloseTimeout] = value;
 027                SetIsModified();
 028            }
 29        }
 30
 31        [ConfigurationProperty(ConfigurationStrings.OpenTimeout, DefaultValue = ServiceDefaults.OpenTimeoutString)]
 32        [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
 33        public TimeSpan OpenTimeout
 34        {
 2035            get { return (TimeSpan)base[ConfigurationStrings.OpenTimeout]; }
 36            set
 37            {
 038                base[ConfigurationStrings.OpenTimeout] = value;
 039                SetIsModified();
 040            }
 41        }
 42
 43        [ConfigurationProperty(ConfigurationStrings.ReceiveTimeout, DefaultValue = ServiceDefaults.ReceiveTimeoutString)
 44        [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
 45        public TimeSpan ReceiveTimeout
 46        {
 2047            get { return (TimeSpan)base[ConfigurationStrings.ReceiveTimeout]; }
 48            set
 49            {
 050                base[ConfigurationStrings.ReceiveTimeout] = value;
 051                SetIsModified();
 052            }
 53        }
 54
 55        [ConfigurationProperty(ConfigurationStrings.SendTimeout, DefaultValue = ServiceDefaults.SendTimeoutString)]
 56        [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
 57        public TimeSpan SendTimeout
 58        {
 2059            get { return (TimeSpan)base[ConfigurationStrings.SendTimeout]; }
 60            set
 61            {
 062                base[ConfigurationStrings.SendTimeout] = value;
 063                SetIsModified();
 064            }
 65        }
 66
 67        public override void Add(BindingElementExtensionElement element)
 68        {
 1769            if (null == element)
 70            {
 071                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element));
 72            }
 73
 1774            BindingElementExtensionElement existingElement = null;
 1775            if (!CanAddEncodingElement(element, ref existingElement))
 76            {
 077                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.Format(SR.
 078                    existingElement.ConfigurationElementName,
 079                    existingElement.GetType().AssemblyQualifiedName)));
 80            }
 1781            else if (!CanAddStreamUpgradeElement(element, ref existingElement))
 82            {
 083                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.Format(SR.
 084                    existingElement.ConfigurationElementName,
 085                    existingElement.GetType().AssemblyQualifiedName)));
 86            }
 1787            else if (!CanAddTransportElement(element, ref existingElement))
 88            {
 089                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.Format(SR.
 090                    existingElement.ConfigurationElementName,
 091                    existingElement.GetType().AssemblyQualifiedName)));
 92            }
 93            else
 94            {
 1795                base.Add(element);
 96            }
 1797        }
 98
 99        public void ApplyConfiguration(Binding binding)
 100        {
 20101            if (null == binding)
 102            {
 0103                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(binding));
 104            }
 20105            if (binding.GetType() != typeof(CustomBinding))
 106            {
 0107                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.ConfigInvalidTypeForBinding,
 0108                    typeof(CustomBinding).AssemblyQualifiedName,
 0109                    binding.GetType().AssemblyQualifiedName));
 110            }
 111
 20112            binding.Name = Name;
 20113            binding.CloseTimeout = CloseTimeout;
 20114            binding.OpenTimeout = OpenTimeout;
 20115            binding.ReceiveTimeout = ReceiveTimeout;
 20116            binding.SendTimeout = SendTimeout;
 117
 20118            OnApplyConfiguration(binding);
 18119        }
 120
 121        public override bool CanAdd(BindingElementExtensionElement element)
 122        {
 17123            if (null == element)
 124            {
 0125                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element));
 126            }
 127
 17128            BindingElementExtensionElement existingElement = null;
 17129            return !ContainsKey(element.GetType()) && CanAddEncodingElement(element, ref existingElement) &&
 17130                CanAddStreamUpgradeElement(element, ref existingElement) && CanAddTransportElement(element, ref existing
 131        }
 132
 133        private bool CanAddEncodingElement(BindingElementExtensionElement element, ref BindingElementExtensionElement ex
 134        {
 34135            return CanAddExclusiveElement(typeof(MessageEncodingBindingElement), element.BindingElementType, ref existin
 136        }
 137
 138        private bool CanAddExclusiveElement(Type exclusiveType, Type bindingElementType, ref BindingElementExtensionElem
 139        {
 102140            bool retval = true;
 102141            if (exclusiveType.IsAssignableFrom(bindingElementType))
 142            {
 56143                foreach (BindingElementExtensionElement existing in this)
 144                {
 0145                    if (exclusiveType.IsAssignableFrom(existing.BindingElementType))
 146                    {
 0147                        retval = false;
 0148                        existingElement = existing;
 0149                        break;
 150                    }
 151                }
 152            }
 102153            return retval;
 154        }
 155
 156        private bool CanAddStreamUpgradeElement(BindingElementExtensionElement element, ref BindingElementExtensionEleme
 157        {
 34158            return CanAddExclusiveElement(typeof(StreamUpgradeBindingElement), element.BindingElementType, ref existingE
 159        }
 160
 161        private bool CanAddTransportElement(BindingElementExtensionElement element, ref BindingElementExtensionElement e
 162        {
 34163            return CanAddExclusiveElement(typeof(TransportBindingElement), element.BindingElementType, ref existingEleme
 164        }
 165
 166        private void OnApplyConfiguration(Binding binding)
 167        {
 20168            CustomBinding theBinding = (CustomBinding)binding;
 72169            foreach (BindingElementExtensionElement bindingConfig in this)
 170            {
 17171                theBinding.Elements.Add(bindingConfig.CreateBindingElement());
 172            }
 18173        }
 174
 175        public Binding CreateBinding()
 176        {
 20177            CustomBinding customBinding = new CustomBinding();
 20178            ApplyConfiguration(customBinding);
 18179            return customBinding;
 180        }
 181
 182        //protected override object GetElementKey(ConfigurationElement element) => throw new NotImplementedException();
 183    }
 184}