public void Log_ExceptionWithMultipleLevelsOfInnerExceptions_LogsWholeHierachyAsExpected()
        {
            // Arrange
            int expectedNumberOfLoggedExceptions = 6;

            var exceptionWithMultipleInnerExceptions = new CompositeException("failure 1 / parent",
                                                                              new Exception[]
            {
                new ArgumentException("failure 2 / inner level 1"),
                new InvalidOperationException("failure 3 / inner level 1"),
                new MultipleFailuresException("failure 4 / parent level 1")
                {
                    InnerExceptions = new Exception[]
                    {
                        new ArgumentException("failure 5 / inner level 2"),
                        new InvalidOperationException("failure 6 / inner level 2")
                    },
                }
            });

            var logger = new CachingSqlLoggingProvider();

            // Act
            logger.Log(exceptionWithMultipleInnerExceptions);

            // Arrange
            Assert.AreEqual(expectedNumberOfLoggedExceptions, logger.LoggedExceptions.Count,
                            "Inner exceptions that contain multiple exceptions by them selves should be logged.");
        }
        public void GetObjectData_InnerExceptions_SerializesAndDeserializesSuccessfully()
        {
            // Arrange
            CompositeException exceptionToSerialize = null;
            Exception          firstInnerException  = new Exception("a");
            Exception          secondInnerException = new Exception("b");

            try
            {
                throw new CompositeException(string.Empty, new[] { firstInnerException, secondInnerException });
            }
            catch (CompositeException ex)
            {
                exceptionToSerialize = ex;
            }

            // Act
            byte[]             serializedException   = SerializeCompositeException(exceptionToSerialize);
            CompositeException deserializedException = DeserializeCompositeException(serializedException);

            // Assert
            Assert.IsNotNull(deserializedException);
            Assert.AreEqual(2, deserializedException.InnerExceptions.Count);
            Assert.AreEqual(firstInnerException.Message, deserializedException.InnerExceptions[0].Message);
            Assert.AreEqual(secondInnerException.Message, deserializedException.InnerExceptions[1].Message);
        }
        public void ConstructorDefault_Always_ContainsDefaultMessage()
        {
            // Arrange
            var exceptionUnderTest = new CompositeException();

            // Assert
            Assert.AreEqual("One or more errors occurred.", exceptionUnderTest.Message);
        }
        public void GetObjectData_SerializationInfoArgumentNull_ThrowsException()
        {
            // Arrange
            var exception = new CompositeException();

            // Act
            exception.GetObjectData(null, new StreamingContext());
        }
        private static byte[] SerializeCompositeException(CompositeException exceptionToSerialize)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();

                formatter.Serialize(stream, exceptionToSerialize);

                return(stream.ToArray());
            }
        }
        public void ConstructorMessageInnerException_ValidException_AssignsExceptionToInnerExceptionProperty()
        {
            // Arrange
            var expectedInnerException = new Exception();
            var exceptionUnderTest     = new CompositeException("Valid message", expectedInnerException);

            // Arrange
            var actualInnerException = exceptionUnderTest.InnerException;

            // Assert
            Assert.AreEqual(expectedInnerException, actualInnerException);
        }
        public void ConstructorMessageInnerException_ValidMessage_ContainsSuppliedMessage()
        {
            // Arrange
            var expectedMessage    = "Valid message";
            var exceptionUnderTest = new CompositeException(expectedMessage, new Exception());

            // Arrange
            var actualMessage = exceptionUnderTest.Message;

            // Assert
            Assert.AreEqual(expectedMessage, actualMessage);
        }
        public void ConstructorDefault_Always_ContainsNoInnerExceptions()
        {
            // Arrange
            var exceptionUnderTest = new CompositeException();

            // Act
            var innerExceptions = exceptionUnderTest.InnerExceptions;

            // Assert
            Assert.IsNotNull(innerExceptions);
            Assert.AreEqual(0, innerExceptions.Count);
        }
        public void ConstructorMessageInnerException_ValidException_AddsExceptionToInnerExceptionsCollection()
        {
            // Arrange
            var expectedInnerException = new Exception();
            var exceptionUnderTest     = new CompositeException("Valid message", expectedInnerException);

            // Arrange
            var actualInnerExceptions = exceptionUnderTest.InnerExceptions;

            // Assert
            Assert.AreEqual(1, actualInnerExceptions.Count);
            Assert.AreEqual(expectedInnerException, actualInnerExceptions[0]);
        }
        public void ToString_WithSingleInnerException_ReturnsExpectedText()
        {
            // Arrange
            Exception innerException          = new InvalidOperationException("bad, bad things have happened.");
            var       exceptionUnderTest      = new CompositeException("Errors have occurred.", innerException);
            var       expectedBeginningOfText =
                "CuttingEdge.Logging.CompositeException: Errors have occurred. ---> " +
                "System.InvalidOperationException: bad, bad things have happened.";

            var expectedEndingOfText =
                @"---> (Inner Exception #0) " +
                "System.InvalidOperationException: bad, bad things have happened.<---" + Environment.NewLine;

            // Act
            var actualText = exceptionUnderTest.ToString();

            // Arrange
            Assert.IsTrue(actualText.StartsWith(expectedBeginningOfText));
            Assert.IsTrue(actualText.EndsWith(expectedEndingOfText));
        }