< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.ScopedMessagePartSpecification
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/ScopedMessagePartSpecification.cs
Line coverage
49%
Covered lines: 43
Uncovered lines: 44
Coverable lines: 87
Total lines: 222
Line coverage: 49.4%
Branch coverage
42%
Covered branches: 22
Total branches: 52
Branch coverage: 42.3%
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(...)66.66%6660%
.ctor(...)0%220%
AddParts(...)50%2280%
AddParts(...)66.66%6677.77%
AddParts(...)0%220%
IsEmpty()0%880%
TryGetParts(...)58.33%121266.66%
CopyTo(...)62.5%8866.66%
TryGetParts(...)100%11100%
MakeReadOnly()0%440%
ThrowIfReadOnly()50%2266.66%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/ScopedMessagePartSpecification.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.Generic;
 6using System.Xml;
 7using CoreWCF.Channels;
 8
 9namespace CoreWCF.Security
 10{
 11    public class ScopedMessagePartSpecification
 12    {
 13        private readonly Dictionary<string, MessagePartSpecification> _actionParts;
 14        private Dictionary<string, MessagePartSpecification> _readOnlyNormalizedActionParts;
 15
 314016        public ScopedMessagePartSpecification()
 17        {
 314018            ChannelParts = new MessagePartSpecification();
 314019            _actionParts = new Dictionary<string, MessagePartSpecification>();
 314020        }
 21
 22        public ICollection<string> Actions
 23        {
 24            get
 25            {
 29626                return _actionParts.Keys;
 27            }
 28        }
 29
 557030        public MessagePartSpecification ChannelParts { get; }
 31
 387832        public bool IsReadOnly { get; private set; }
 33
 34        public ScopedMessagePartSpecification(ScopedMessagePartSpecification other)
 215635            : this()
 36        {
 215637            if (other == null)
 38            {
 039                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(other)));
 40            }
 41
 215642            ChannelParts.Union(other.ChannelParts);
 215643            if (other._actionParts != null)
 44            {
 431245                foreach (string action in other._actionParts.Keys)
 46                {
 047                    MessagePartSpecification p = new MessagePartSpecification();
 048                    p.Union(other._actionParts[action]);
 049                    _actionParts[action] = p;
 50                }
 51            }
 215652        }
 53
 54        internal ScopedMessagePartSpecification(ScopedMessagePartSpecification other, bool newIncludeBody)
 055            : this(other)
 56        {
 057            ChannelParts.IsBodyIncluded = newIncludeBody;
 058            foreach (string action in _actionParts.Keys)
 59            {
 060                _actionParts[action].IsBodyIncluded = newIncludeBody;
 61            }
 062        }
 63
 64        public void AddParts(MessagePartSpecification parts)
 65        {
 83066            if (parts == null)
 67            {
 068                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(parts)));
 69            }
 70
 83071            ThrowIfReadOnly();
 72
 83073            ChannelParts.Union(parts);
 83074        }
 75
 76        public void AddParts(MessagePartSpecification parts, string action)
 77        {
 203278            if (action == null)
 79            {
 080                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(action)));
 81            }
 82
 203283            if (parts == null)
 84            {
 085                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(parts)));
 86            }
 87
 203288            ThrowIfReadOnly();
 89
 203290            if (!_actionParts.ContainsKey(action))
 91            {
 189292                _actionParts[action] = new MessagePartSpecification();
 93            }
 94
 203295            _actionParts[action].Union(parts);
 203296        }
 97
 98        internal void AddParts(MessagePartSpecification parts, XmlDictionaryString action)
 99        {
 0100            if (action == null)
 101            {
 0102                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(action)));
 103            }
 104
 0105            AddParts(parts, action.Value);
 0106        }
 107
 108        internal bool IsEmpty()
 109        {
 110            bool result;
 0111            if (!ChannelParts.IsEmpty())
 112            {
 0113                result = false;
 114            }
 115            else
 116            {
 0117                result = true;
 0118                foreach (string action in Actions)
 119                {
 0120                    if (TryGetParts(action, true, out MessagePartSpecification parts))
 121                    {
 0122                        if (!parts.IsEmpty())
 123                        {
 0124                            result = false;
 0125                            break;
 126                        }
 127                    }
 128                }
 129            }
 130
 0131            return result;
 132        }
 133
 134        public bool TryGetParts(string action, bool excludeChannelScope, out MessagePartSpecification parts)
 135        {
 1016136            if (action == null)
 137            {
 0138                action = MessageHeaders.WildcardAction;
 139            }
 140
 1016141            parts = null;
 142
 1016143            if (IsReadOnly)
 144            {
 0145                if (_readOnlyNormalizedActionParts.ContainsKey(action))
 146                {
 0147                    if (excludeChannelScope)
 148                    {
 0149                        parts = _actionParts[action];
 150                    }
 151                    else
 152                    {
 0153                        parts = _readOnlyNormalizedActionParts[action];
 154                    }
 155                }
 156            }
 1016157            else if (_actionParts.ContainsKey(action))
 158            {
 1016159                MessagePartSpecification p = new MessagePartSpecification();
 1016160                p.Union(_actionParts[action]);
 1016161                if (!excludeChannelScope)
 162                {
 72163                    p.Union(ChannelParts);
 164                }
 165
 1016166                parts = p;
 167            }
 168
 1016169            return parts != null;
 170        }
 171
 172        internal void CopyTo(ScopedMessagePartSpecification target)
 173        {
 12174            if (target == null)
 175            {
 0176                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(target));
 177            }
 12178            target.ChannelParts.IsBodyIncluded = ChannelParts.IsBodyIncluded;
 24179            foreach (XmlQualifiedName headerType in ChannelParts.HeaderTypes)
 180            {
 0181                if (!target.ChannelParts.IsHeaderIncluded(headerType.Name, headerType.Namespace))
 182                {
 0183                    target.ChannelParts.HeaderTypes.Add(headerType);
 184                }
 185            }
 168186            foreach (string action in _actionParts.Keys)
 187            {
 72188                target.AddParts(_actionParts[action], action);
 189            }
 12190        }
 191
 192        public bool TryGetParts(string action, out MessagePartSpecification parts)
 193        {
 72194            return TryGetParts(action, false, out parts);
 195        }
 196
 197        public void MakeReadOnly()
 198        {
 0199            if (!IsReadOnly)
 200            {
 0201                _readOnlyNormalizedActionParts = new Dictionary<string, MessagePartSpecification>();
 0202                foreach (string action in _actionParts.Keys)
 203                {
 0204                    MessagePartSpecification p = new MessagePartSpecification();
 0205                    p.Union(_actionParts[action]);
 0206                    p.Union(ChannelParts);
 0207                    p.MakeReadOnly();
 0208                    _readOnlyNormalizedActionParts[action] = p;
 209                }
 0210                IsReadOnly = true;
 211            }
 0212        }
 213
 214        private void ThrowIfReadOnly()
 215        {
 2862216            if (IsReadOnly)
 217            {
 0218                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO
 219            }
 2862220        }
 221    }
 222}