< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.StreamFormatter
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/StreamFormatter.cs
Line coverage
87%
Covered lines: 75
Uncovered lines: 11
Coverable lines: 86
Total lines: 235
Line coverage: 87.2%
Branch coverage
78%
Covered branches: 33
Total branches: 42
Branch coverage: 78.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Create(...)100%22100%
.ctor(...)100%22100%
Serialize(...)100%11100%
SerializeAsync()100%11100%
GetStreamAndWriteStartWrapperIfNecessary(...)75%4485.71%
WriteEndWrapperIfNecessary(...)100%22100%
WriteEndWrapperIfNecessaryAsync(...)100%22100%
Deserialize(...)100%11100%
GetStreamValue(...)100%22100%
SetStreamValue(...)50%2275%
ValidateAndGetStreamPart(...)37.5%8850%
HasStream(...)75%8857.14%
GetStreamPart(...)100%1010100%
IsStream(...)100%110%
.ctor(...)100%11100%
GetStream()100%11100%
ReleaseStream(...)100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/StreamFormatter.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.IO;
 6using System.Threading.Tasks;
 7using System.Xml;
 8using CoreWCF.Channels;
 9using CoreWCF.Description;
 10using CoreWCF.Diagnostics;
 11using CoreWCF.Runtime;
 12
 13namespace CoreWCF.Dispatcher
 14{
 15    internal class StreamFormatter
 16    {
 17        private readonly int _streamIndex;
 18        private readonly bool _isRequest;
 19        private readonly string _operationName;
 20        private const int returnValueIndex = -1;
 21
 22        internal static StreamFormatter Create(MessageDescription messageDescription, string operationName, bool isReque
 23        {
 196824            MessagePartDescription streamPart = ValidateAndGetStreamPart(messageDescription, isRequest, operationName);
 196825            if (streamPart == null)
 26            {
 134927                return null;
 28            }
 29
 61930            return new StreamFormatter(messageDescription, streamPart, operationName, isRequest);
 31        }
 32
 61933        private StreamFormatter(MessageDescription messageDescription, MessagePartDescription streamPart, string operati
 34        {
 61935            if ((object)streamPart == (object)messageDescription.Body.ReturnValue)
 36            {
 22837                _streamIndex = returnValueIndex;
 38            }
 39            else
 40            {
 39141                _streamIndex = streamPart.Index;
 42            }
 43
 61944            WrapperName = messageDescription.Body.WrapperName;
 61945            WrapperNamespace = messageDescription.Body.WrapperNamespace;
 61946            PartName = streamPart.Name;
 61947            PartNamespace = streamPart.Namespace;
 61948            _isRequest = isRequest;
 61949            _operationName = operationName;
 61950        }
 51
 52        internal void Serialize(XmlDictionaryWriter writer, object[] parameters, object returnValue)
 53        {
 854            Stream streamValue = GetStreamAndWriteStartWrapperIfNecessary(writer, parameters, returnValue);
 855            var streamProvider = new OperationStreamProvider(streamValue);
 856            writer.WriteValue(streamProvider);
 857            WriteEndWrapperIfNecessary(writer);
 858        }
 59
 60        internal async Task SerializeAsync(XmlDictionaryWriter writer, object[] parameters, object returnValue)
 61        {
 462            using (TaskHelpers.RunTaskContinuationsOnOurThreads()) // If inner stream doesn't have sync implementation, 
 63            {
 464                Stream streamValue = GetStreamAndWriteStartWrapperIfNecessary(writer, parameters, returnValue);
 465                var streamProvider = new OperationStreamProvider(streamValue);
 466                await writer.WriteValueAsync(streamProvider);
 467                await WriteEndWrapperIfNecessaryAsync(writer);
 468            }
 469        }
 70
 71        private Stream GetStreamAndWriteStartWrapperIfNecessary(XmlDictionaryWriter writer, object[] parameters, object 
 72        {
 1273            Stream streamValue = GetStreamValue(parameters, returnValue);
 1274            if (streamValue == null)
 75            {
 076                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(PartName);
 77            }
 78
 1279            if (WrapperName != null)
 80            {
 1281                writer.WriteStartElement(null, WrapperName, WrapperNamespace);
 82            }
 83
 1284            writer.WriteStartElement(null, PartName, PartNamespace);
 1285            return streamValue;
 86        }
 87
 88        private void WriteEndWrapperIfNecessary(XmlDictionaryWriter writer)
 89        {
 890            writer.WriteEndElement();
 891            if (WrapperName != null)
 92            {
 893                writer.WriteEndElement();
 94            }
 895        }
 96
 97        private Task WriteEndWrapperIfNecessaryAsync(XmlDictionaryWriter writer)
 98        {
 499            writer.WriteEndElement();
 4100            if (WrapperName != null)
 101            {
 4102                writer.WriteEndElement();
 103            }
 104
 4105            return Task.CompletedTask;
 106        }
 107
 108        internal void Deserialize(object[] parameters, ref object retVal, Message message)
 109        {
 16110            SetStreamValue(parameters, ref retVal, new MessageBodyStream(message, WrapperName, WrapperNamespace, PartNam
 16111        }
 112
 731113        internal string WrapperName { get; set; }
 114
 707115        internal string WrapperNamespace { get; set; }
 116
 88117        internal string PartName { get; }
 118
 88119        internal string PartNamespace { get; }
 120
 121        private Stream GetStreamValue(object[] parameters, object returnValue)
 122        {
 12123            if (_streamIndex == returnValueIndex)
 124            {
 5125                return (Stream)returnValue;
 126            }
 127
 7128            return (Stream)parameters[_streamIndex];
 129        }
 130
 131        private void SetStreamValue(object[] parameters, ref object returnValue, Stream streamValue)
 132        {
 16133            if (_streamIndex == returnValueIndex)
 134            {
 0135                returnValue = streamValue;
 136            }
 137            else
 138            {
 16139                parameters[_streamIndex] = streamValue;
 140            }
 16141        }
 142
 143        private static MessagePartDescription ValidateAndGetStreamPart(MessageDescription messageDescription, bool isReq
 144        {
 1968145            MessagePartDescription part = GetStreamPart(messageDescription);
 1968146            if (part != null)
 147            {
 619148                return part;
 149            }
 150
 1349151            if (HasStream(messageDescription))
 152            {
 0153                if (messageDescription.IsTypedMessage)
 154                {
 0155                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 156                }
 0157                else if (isRequest)
 158                {
 0159                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 160                }
 161                else
 162                {
 0163                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 164                }
 165            }
 1349166            return null;
 167        }
 168
 169        private static bool HasStream(MessageDescription messageDescription)
 170        {
 1349171            if (messageDescription.Body.ReturnValue != null && messageDescription.Body.ReturnValue.Type == typeof(Stream
 172            {
 0173                return true;
 174            }
 175
 4578176            foreach (MessagePartDescription part in messageDescription.Body.Parts)
 177            {
 940178                if (part.Type == typeof(Stream))
 179                {
 0180                    return true;
 181                }
 182            }
 1349183            return false;
 0184        }
 185
 186        private static MessagePartDescription GetStreamPart(MessageDescription messageDescription)
 187        {
 1968188            if (OperationFormatter.IsValidReturnValue(messageDescription.Body.ReturnValue))
 189            {
 722190                if (messageDescription.Body.Parts.Count == 0)
 191                {
 584192                    if (messageDescription.Body.ReturnValue.Type == typeof(Stream))
 193                    {
 228194                        return messageDescription.Body.ReturnValue;
 195                    }
 196                }
 197            }
 198            else
 199            {
 1246200                if (messageDescription.Body.Parts.Count == 1)
 201                {
 1157202                    if (messageDescription.Body.Parts[0].Type == typeof(Stream))
 203                    {
 391204                        return messageDescription.Body.Parts[0];
 205                    }
 206                }
 207            }
 1349208            return null;
 209        }
 210
 211        internal static bool IsStream(MessageDescription messageDescription)
 212        {
 0213            return GetStreamPart(messageDescription) != null;
 214        }
 215
 216        internal class OperationStreamProvider : IStreamProvider
 217        {
 218            private readonly Stream _stream;
 219
 12220            internal OperationStreamProvider(Stream stream)
 221            {
 12222                _stream = stream;
 12223            }
 224
 225            public Stream GetStream()
 226            {
 12227                return _stream;
 228            }
 229            public void ReleaseStream(Stream stream)
 230            {
 231                //Noop
 12232            }
 233        }
 234    }
 235}