コード例 #1
0
        /// <summary>
        /// Runs the test specified by this test descriptor.
        /// </summary>
        /// <param name="testConfiguration">The test configuration to use for running the test.</param>
        public virtual void RunTest(ReaderTestConfiguration testConfiguration)
        {
            if (this.ShouldSkipForTestConfiguration(testConfiguration))
            {
                return;
            }

            TestMessage message = this.CreateInputMessage(testConfiguration);
            IEdmModel   model   = this.GetMetadataProvider(testConfiguration);
            ReaderTestExpectedResult expectedResult = this.GetExpectedResult(testConfiguration);

            ExceptionUtilities.Assert(expectedResult != null, "The expected result could not be determined for the test. Did you specify it?");

            Exception exception = TestExceptionUtils.RunCatching(() =>
            {
                using (ODataMessageReaderTestWrapper messageReaderWrapper = TestReaderUtils.CreateMessageReader(message, model, testConfiguration))
                {
                    expectedResult.VerifyResult(messageReaderWrapper, this.PayloadKind, testConfiguration);
                }
            });

            try
            {
                expectedResult.VerifyException(exception);
            }
            catch (Exception)
            {
                this.TraceFailureInformation(testConfiguration);
                throw;
            }
        }
コード例 #2
0
        /// <summary>
        /// Called to create the input message for the reader test.
        /// </summary>
        /// <param name="testConfiguration">The test configuration.</param>
        /// <returns>The newly created test message to use.</returns>
        protected override TestMessage CreateInputMessage(ReaderTestConfiguration testConfiguration)
        {
            MemoryStream memoryStream  = new MemoryStream(this.GetPayload(testConfiguration));
            TestStream   messageStream = new TestStream(memoryStream);

            if (testConfiguration.Synchronous)
            {
                messageStream.FailAsynchronousCalls = true;
            }
            else
            {
                messageStream.FailSynchronousCalls = true;
            }

            this.testMessage = TestReaderUtils.CreateInputMessageFromStream(
                messageStream,
                testConfiguration,
                /*payloadKind*/ null,
                this.ContentType,
                /*urlResolver*/ null);

            if (this.TestMessageWrapper != null)
            {
                this.testMessage = this.TestMessageWrapper(this.testMessage);
            }

            return(this.testMessage);
        }
コード例 #3
0
        /// <summary>
        /// Verifies that the result of the test (the message reader) is what the test expected.
        /// </summary>
        /// <param name="messageReader">The message reader which is the result of the test. This method should use it to read the results
        /// of the parsing and verify those.</param>
        /// <param name="payloadKind">The payload kind specified in the test descriptor.</param>
        /// <param name="testConfiguration">The test configuration to use.</param>
        public override void VerifyResult(
            ODataMessageReaderTestWrapper messageReader,
            ODataPayloadKind payloadKind,
            ReaderTestConfiguration testConfiguration)
        {
            // First compare the payload kind detection results.
            IEnumerable <ODataPayloadKindDetectionResult> actualDetectionResults = messageReader.DetectPayloadKind();

            this.VerifyPayloadKindDetectionResult(actualDetectionResults);

            // Then try to read the message as the detected kind if requested
            if (this.ReadDetectedPayloads)
            {
                bool firstResult = true;
                foreach (PayloadKindDetectionResult result in this.ExpectedDetectionResults)
                {
                    if (firstResult)
                    {
                        // For the first result use the existing message reader
                        firstResult = false;
                    }
                    else
                    {
                        // For all subsequent results we need to reset the test stream and create a new message reader
                        // over it.
                        this.TestMessage.Reset();
                        messageReader = TestReaderUtils.CreateMessageReader(this.TestMessage, result.Model, testConfiguration);

                        // Detect the payload kinds again and make sure we can also read the subsequent payload kinds
                        // immediately after detection.
                        actualDetectionResults = messageReader.DetectPayloadKind();
                        this.VerifyPayloadKindDetectionResult(actualDetectionResults);
                    }

                    TestExceptionUtils.ExpectedException(
                        this.settings.Assert,
                        () =>
                    {
                        using (messageReader)
                        {
                            this.settings.MessageToObjectModelReader.ReadMessage(
                                messageReader,
                                result.PayloadKind,
                                result.Model,
                                new PayloadReaderTestDescriptor.ReaderMetadata(result.ExpectedType),
                                /*expectedBatchPayload*/ null,
                                testConfiguration);
                        }
                    },
                        result.ExpectedException,
                        this.settings.ExceptionVerifier);
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Called to create the input message for the reader test.
        /// </summary>
        /// <param name="testConfiguration">The test configuration.</param>
        /// <returns>The newly created test message to use.</returns>
        protected override TestMessage CreateInputMessage(ReaderTestConfiguration testConfiguration)
        {
            MemoryStream memoryStream = new MemoryStream();

            this.WriteInputMessageContent(testConfiguration, new TestStream(memoryStream, ignoreDispose: true));
            memoryStream.Seek(0, SeekOrigin.Begin);
            TestStream  messageStream = new TestStream(memoryStream);
            TestMessage testMessage   = TestReaderUtils.CreateInputMessageFromStream(
                messageStream,
                testConfiguration,
                this.PayloadKind,
                /*customContentTypeHeader*/ null,
                /*urlResolver*/ null);

            return(testMessage);
        }
コード例 #5
0
        /// <summary>
        /// Called to create the input message for the reader test.
        /// </summary>
        /// <param name="testConfiguration">The test configuration.</param>
        /// <returns>The newly created test message to use.</returns>
        protected override TestMessage CreateInputMessage(ReaderTestConfiguration testConfiguration)
        {
            MemoryStream memoryStream  = new MemoryStream(this.GetPayload(testConfiguration));
            TestStream   messageStream = new TestStream(memoryStream);

            if (testConfiguration.Synchronous)
            {
                messageStream.FailAsynchronousCalls = true;
            }
            else
            {
                messageStream.FailSynchronousCalls = true;
            }

            TestMessage testMessage = TestReaderUtils.CreateInputMessageFromStream(
                messageStream,
                testConfiguration,
                ODataPayloadKind.MetadataDocument,
                this.ContentType,
                /*urlResolver*/ null);

            return(testMessage);
        }
コード例 #6
0
 /// <summary>
 /// If overridden dumps the content of an input message which would be created for the specified test configuration
 /// into a string and returns it. This is used only for debugging purposes.
 /// </summary>
 /// <param name="testConfiguration">The test configuration to use.</param>
 /// <returns>The string content of the input message.</returns>
 protected override string DumpInputMessageContent(ReaderTestConfiguration testConfiguration)
 {
     byte[] payload = TestReaderUtils.GetPayload(testConfiguration, this.PayloadNormalizers, this.settings, this.PayloadElement);
     return(Encoding.UTF8.GetString(payload, 0, payload.Length));
 }
コード例 #7
0
 /// <summary>
 /// Called to create the input message for the reader test.
 /// </summary>
 /// <param name="testConfiguration">The test configuration.</param>
 /// <returns>The newly created test message to use.</returns>
 protected override TestMessage CreateInputMessage(ReaderTestConfiguration testConfiguration)
 {
     return(TestReaderUtils.CreateInputMessage(testConfiguration, this, this.settings, this.ApplyPayloadTransformations));
 }
コード例 #8
0
ファイル: TestReaderUtils.cs プロジェクト: zdzislaw/odata.net
        /// <summary>
        /// Creates the input message for the test descriptor
        /// </summary>
        /// <param name="testConfiguration">the test configuration to use</param>
        /// <param name="readerTestDescriptor">The test descriptor</param>
        /// <param name="settings">The test descriptor settings</param>
        /// <param name="applyPayloadTransformations">Whether or not to apply payload transformations</param>
        /// <returns>The message for the test</returns>
        public static TestMessage CreateInputMessage(ReaderTestConfiguration testConfiguration,
                                                     PayloadReaderTestDescriptor readerTestDescriptor,
                                                     PayloadReaderTestDescriptor.Settings settings,
                                                     bool?applyPayloadTransformations)
        {
            TestMessage testMessage;
            bool        originalApplyTransformValue = false;
            var         odataTransformFactory       = settings.PayloadTransformFactory as ODataLibPayloadTransformFactory;

            try
            {
                if (applyPayloadTransformations.HasValue && odataTransformFactory != null)
                {
                    originalApplyTransformValue          = odataTransformFactory.ApplyTransform;
                    odataTransformFactory.ApplyTransform = applyPayloadTransformations.Value;
                }

                if (readerTestDescriptor.TestDescriptorNormalizers != null)
                {
                    foreach (var testDescriptorNormalizer in readerTestDescriptor.TestDescriptorNormalizers)
                    {
                        var normalizerAction = testDescriptorNormalizer(testConfiguration);
                        if (normalizerAction != null)
                        {
                            normalizerAction(readerTestDescriptor);
                        }
                    }
                }

                MemoryStream memoryStream  = new MemoryStream(GetPayload(testConfiguration, readerTestDescriptor.PayloadNormalizers, settings, readerTestDescriptor.PayloadElement));
                TestStream   messageStream = new TestStream(memoryStream);
                if (testConfiguration.Synchronous)
                {
                    messageStream.FailAsynchronousCalls = true;
                }
                else
                {
                    messageStream.FailSynchronousCalls = true;
                }

                testMessage = TestReaderUtils.CreateInputMessageFromStream(
                    messageStream,
                    testConfiguration,
                    readerTestDescriptor.PayloadElement.GetPayloadKindFromPayloadElement(),
                    readerTestDescriptor.PayloadElement.GetCustomContentTypeHeader(), readerTestDescriptor.UrlResolver);

                if (readerTestDescriptor.TestMessageWrapper != null)
                {
                    testMessage = readerTestDescriptor.TestMessageWrapper(testMessage);
                }

                return(testMessage);
            }
            finally
            {
                if (applyPayloadTransformations.HasValue && odataTransformFactory != null)
                {
                    odataTransformFactory.ApplyTransform = originalApplyTransformValue;
                }
            }
        }