< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.BodyWriter
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/BodyWriter.cs
Line coverage
53%
Covered lines: 32
Uncovered lines: 28
Coverable lines: 60
Total lines: 155
Line coverage: 53.3%
Branch coverage
55%
Covered branches: 10
Total branches: 18
Branch coverage: 55.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%22100%
CreateBufferedCopy(...)50%8864.28%
OnCreateBufferedCopy(...)100%110%
OnCreateBufferedCopy(...)100%110%
OnWriteBodyContentsAsync(...)100%11100%
EnsureWriteBodyContentsState(...)66.66%6677.77%
WriteBodyContents(...)100%11100%
WriteBodyContentsAsync(...)100%11100%
.ctor(...)100%110%
OnWriteBodyContents(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/BodyWriter.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.Threading.Tasks;
 6using System.Xml;
 7
 8namespace CoreWCF.Channels
 9{
 10    public abstract class BodyWriter
 11    {
 12        private bool _canWrite;
 13        private readonly object _thisLock;
 14
 160715        protected BodyWriter(bool isBuffered)
 16        {
 160717            IsBuffered = isBuffered;
 160718            _canWrite = true;
 160719            if (!IsBuffered)
 20            {
 22921                _thisLock = new object();
 22            }
 160723        }
 24
 339025        public bool IsBuffered { get; }
 26
 27        internal virtual bool IsEmpty
 28        {
 229            get { return false; }
 30        }
 31
 32        internal virtual bool IsFault
 33        {
 40534            get { return false; }
 35        }
 36
 37        public BodyWriter CreateBufferedCopy(int maxBufferSize)
 38        {
 139            if (maxBufferSize < 0)
 40            {
 041                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(maxBuff
 042                                                    SRCommon.ValueMustBeNonNegative));
 43            }
 44
 145            if (IsBuffered)
 46            {
 047                return this;
 48            }
 49            else
 50            {
 151                lock (_thisLock)
 52                {
 153                    if (!_canWrite)
 54                    {
 055                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.BodyW
 56                    }
 57
 158                    _canWrite = false;
 159                }
 160                BodyWriter bodyWriter = OnCreateBufferedCopy(maxBufferSize);
 161                if (!bodyWriter.IsBuffered)
 62                {
 063                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.BodyWrite
 64                }
 65
 166                return bodyWriter;
 67            }
 68        }
 69
 70        protected virtual BodyWriter OnCreateBufferedCopy(int maxBufferSize)
 71        {
 072            return OnCreateBufferedCopy(maxBufferSize, XmlDictionaryReaderQuotas.Max);
 73        }
 74
 75        internal BodyWriter OnCreateBufferedCopy(int maxBufferSize, XmlDictionaryReaderQuotas quotas)
 76        {
 077            XmlBuffer buffer = new XmlBuffer(maxBufferSize);
 078            using (XmlDictionaryWriter writer = buffer.OpenSection(quotas))
 79            {
 080                writer.WriteStartElement("a");
 081                OnWriteBodyContents(writer);
 082                writer.WriteEndElement();
 083            }
 084            buffer.CloseSection();
 085            buffer.Close();
 086            return new BufferedBodyWriter(buffer);
 87        }
 88
 89        protected abstract void OnWriteBodyContents(XmlDictionaryWriter writer);
 90
 91        protected virtual Task OnWriteBodyContentsAsync(XmlDictionaryWriter writer)
 92        {
 49693            OnWriteBodyContents(writer);
 49694            return Task.CompletedTask;
 95        }
 96
 97        private void EnsureWriteBodyContentsState(XmlDictionaryWriter writer)
 98        {
 151199            if (writer == null)
 100            {
 0101                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 102            }
 103
 1511104            if (!IsBuffered)
 105            {
 223106                lock (_thisLock)
 107                {
 223108                    if (!_canWrite)
 109                    {
 0110                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.BodyW
 111                    }
 112
 223113                    _canWrite = false;
 223114                }
 115            }
 1511116        }
 117
 118        public void WriteBodyContents(XmlDictionaryWriter writer)
 119        {
 995120            EnsureWriteBodyContentsState(writer);
 995121            OnWriteBodyContents(writer);
 995122        }
 123
 124        public Task WriteBodyContentsAsync(XmlDictionaryWriter writer)
 125        {
 516126            EnsureWriteBodyContentsState(writer);
 516127            return OnWriteBodyContentsAsync(writer);
 128        }
 129
 130        private class BufferedBodyWriter : BodyWriter
 131        {
 132            private readonly XmlBuffer _buffer;
 133
 134            public BufferedBodyWriter(XmlBuffer buffer)
 0135                : base(true)
 136            {
 0137                _buffer = buffer;
 0138            }
 139
 140            protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 141            {
 0142                XmlDictionaryReader reader = _buffer.GetReader(0);
 0143                using (reader)
 144                {
 0145                    reader.ReadStartElement();
 0146                    while (reader.NodeType != XmlNodeType.EndElement)
 147                    {
 0148                        writer.WriteNode(reader, false);
 149                    }
 0150                    reader.ReadEndElement();
 0151                }
 0152            }
 153        }
 154    }
 155}