< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.CorrelationDataMessageProperty
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/CorrelationDataMessageProperty.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 54
Coverable lines: 54
Total lines: 164
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 32
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
.ctor(...)0%440%
Add(...)0%660%
Remove(...)0%220%
TryGetValue(...)0%440%
TryGet(...)0%220%
TryGet(...)0%220%
AddData(...)0%10100%
CreateCopy()100%110%
.ctor(...)100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/CorrelationDataMessageProperty.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 CoreWCF.Runtime;
 7
 8namespace CoreWCF.Channels
 9{
 10    internal class CorrelationDataMessageProperty : IMessageProperty
 11    {
 12        private const string PropertyName = "CorrelationDataMessageProperty";
 13        private Dictionary<string, DataProviderEntry> _dataProviders;
 14
 015        public CorrelationDataMessageProperty()
 16        {
 017        }
 18
 019        private CorrelationDataMessageProperty(IDictionary<string, DataProviderEntry> dataProviders)
 20        {
 021            if (dataProviders != null && dataProviders.Count > 0)
 22            {
 023                _dataProviders = new Dictionary<string, DataProviderEntry>(dataProviders);
 24            }
 025        }
 26
 27        public static string Name
 28        {
 029            get { return PropertyName; }
 30        }
 31
 32        public void Add(string name, Func<string> dataProvider)
 33        {
 034            if (string.IsNullOrEmpty(name))
 35            {
 036                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name));
 37            }
 38
 039            if (dataProvider == null)
 40            {
 041                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dataProvider));
 42            }
 43
 044            if (_dataProviders == null)
 45            {
 046                _dataProviders = new Dictionary<string, DataProviderEntry>();
 47            }
 048            _dataProviders.Add(name, new DataProviderEntry(dataProvider));
 049        }
 50
 51        public bool Remove(string name)
 52        {
 053            if (_dataProviders != null)
 54            {
 055                return _dataProviders.Remove(name);
 56            }
 57            else
 58            {
 059                return false;
 60            }
 61        }
 62
 63        public bool TryGetValue(string name, out string value)
 64        {
 065            if (_dataProviders != null && _dataProviders.TryGetValue(name, out DataProviderEntry entry))
 66            {
 067                value = entry.Data;
 068                return true;
 69            }
 70            else
 71            {
 072                value = null;
 073                return false;
 74            }
 75        }
 76
 77        public static bool TryGet(Message message, out CorrelationDataMessageProperty property)
 78        {
 079            if (message == null)
 80            {
 081                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message));
 82            }
 083            return TryGet(message.Properties, out property);
 84        }
 85
 86        public static bool TryGet(MessageProperties properties, out CorrelationDataMessageProperty property)
 87        {
 088            if (properties.TryGetValue(PropertyName, out object value))
 89            {
 090                property = value as CorrelationDataMessageProperty;
 91            }
 92            else
 93            {
 094                property = null;
 95            }
 096            return property != null;
 97        }
 98
 99        public static void AddData(Message message, string name, Func<string> dataProvider)
 100        {
 0101            if (string.IsNullOrEmpty(name))
 102            {
 0103                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name));
 104            }
 105
 0106            if (dataProvider == null)
 107            {
 0108                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dataProvider));
 109            }
 110
 0111            CorrelationDataMessageProperty data = null;
 0112            if (message.Properties.TryGetValue(PropertyName, out object value))
 113            {
 0114                data = value as CorrelationDataMessageProperty;
 115            }
 116
 0117            bool addNewProperty = false;
 0118            if (data == null)
 119            {
 0120                data = new CorrelationDataMessageProperty();
 0121                addNewProperty = true;
 122            }
 123
 0124            data.Add(name, dataProvider);
 125
 0126            if (addNewProperty)
 127            {
 0128                message.Properties[PropertyName] = data;
 129            }
 0130        }
 131
 132        public IMessageProperty CreateCopy()
 133        {
 0134            return new CorrelationDataMessageProperty(_dataProviders);
 135        }
 136
 137        private class DataProviderEntry
 138        {
 139            private string _resolvedData;
 140            private Func<string> _dataProvider;
 141
 0142            public DataProviderEntry(Func<string> dataProvider)
 143            {
 144                Fx.Assert(dataProvider != null, "dataProvider required");
 0145                _dataProvider = dataProvider;
 0146                _resolvedData = null;
 0147            }
 148
 149            public string Data
 150            {
 151                get
 152                {
 0153                    if (_dataProvider != null)
 154                    {
 0155                        _resolvedData = _dataProvider();
 0156                        _dataProvider = null;
 157                    }
 158
 0159                    return _resolvedData;
 160                }
 161            }
 162        }
 163    }
 164}