public void Message_ShouldIncludeElementGraph()
        {
            var expectations       = new ExpectationCollection <CompositionError, string>();
            CompositionError error = null;

            error = CreateCompositionErrorWithElementChain(1);
            expectations.Add(error, GetElementGraphString(error));

            error = CreateCompositionErrorWithElementChain(2);
            expectations.Add(error, GetElementGraphString(error));

            error = CreateCompositionErrorWithElementChain(3);
            expectations.Add(error, GetElementGraphString(error));

            error = CreateCompositionErrorWithElementChain(10);
            expectations.Add(error, GetElementGraphString(error));

            foreach (var e in expectations)
            {
                var exception = CreateCompositionException(new CompositionError[] { e.Input });

                string result   = exception.ToString();
                string expected = FixMessage(e.Output);
                AssertExtensions.Contains(result, expected);
            }
        }
예제 #2
0
 public static CompositionError ComposeTookTooManyIterations(int maximumNumberOfCompositionIterations)
 {
     return(CompositionError.Create(
                CompositionErrorId.ImportEngine_ComposeTookTooManyIterations,
                SR.ImportEngine_ComposeTookTooManyIterations,
                maximumNumberOfCompositionIterations));
 }
예제 #3
0
 public static CompositionError InvalidStateForRecompposition(ComposablePart part)
 {
     return(CompositionError.Create(
                CompositionErrorId.ImportEngine_InvalidStateForRecomposition,
                SR.ImportEngine_InvalidStateForRecomposition,
                part.ToElement().DisplayName));
 }
예제 #4
0
        public static CompositionError CreatePartCannotSetImport(ComposablePart part, ImportDefinition definition, Exception innerException)
        {
            if (part == null)
            {
                throw new ArgumentNullException(nameof(part));
            }

            if (definition == null)
            {
                throw new ArgumentNullException(nameof(definition));
            }

            if (innerException == null)
            {
                throw new ArgumentNullException(nameof(innerException));
            }

            ICompositionElement element = definition.ToElement();

            return(CompositionError.Create(
                       CompositionErrorId.ImportEngine_PartCannotSetImport,
                       element,
                       innerException,
                       SR.ImportEngine_PartCannotSetImport,
                       element.DisplayName,
                       part.ToElement().DisplayName));
        }
예제 #5
0
 public static CompositionError PreventedByExistingImport(ComposablePart part, ImportDefinition import)
 {
     return(CompositionError.Create(
                CompositionErrorId.ImportEngine_PreventedByExistingImport,
                SR.ImportEngine_PreventedByExistingImport,
                import.ToElement().DisplayName,
                part.ToElement().DisplayName));
 }
예제 #6
0
        public void Constructor2_ArrayWithNullAsErrorsArgument_ShouldThrowArgument()
        {
            var errors = new CompositionError[] { null };

            ExceptionAssert.ThrowsArgument <ArgumentException>("errors", () =>
            {
                new CompositionException(errors);
            });
        }
        public void Constructor5_ArrayWithNullAsErrorsArgument_ShouldThrowArgument()
        {
            var errors = new CompositionError[] { null };

            Assert.Throws <ArgumentException>("errors", () =>
            {
                new CompositionException("Message", new Exception(), errors);
            });
        }
예제 #8
0
        private static void WriteError(StringBuilder writer, CompositionError error)
        {
            writer.AppendLine(error.Description);

            if (error.Element != null)
            {
                WriteElementGraph(writer, error.Element);
            }
        }
예제 #9
0
        public static CompositionError CreateImportCardinalityMismatch(ImportCardinalityMismatchException exception, ImportDefinition definition)
        {
            Assumes.NotNull(exception, definition);

            return(CompositionError.Create(
                       CompositionErrorId.ImportEngine_ImportCardinalityMismatch,
                       exception.Message,
                       definition.ToElement(),
                       (Exception)null));
        }
예제 #10
0
        public void Constructor4_ValueAsMessageArgument_ShouldSetMessageProperty()
        {
            var expectations = Expectations.GetExceptionMessages();

            Assert.All(expectations, e =>
            {
                var exception = new CompositionError(e, ElementFactory.Create(), new Exception());
                Assert.Equal(e, exception.Description);
            });
        }
예제 #11
0
        public void Constructor5_ValueAsElementArgument_ShouldSetElementProperty()
        {
            var expectations = Expectations.GetCompositionElements();

            Assert.All(expectations, e =>
            {
                var exception = new CompositionError(CompositionErrorId.Unknown, "Description", (ICompositionElement)e, new Exception());
                Assert.Same(e, exception.Element);
            });
        }
예제 #12
0
        public void Constructor5_ValueAsIdArgument_ShouldSetICompositionErrorIdProperty()
        {
            var expectations = Expectations.GetEnumValues <CompositionErrorId>();

            Assert.All(expectations, e =>
            {
                var error = new CompositionError(e, "Description", ElementFactory.Create(), new Exception());
                Assert.Equal(e, error.Id);
            });
        }
예제 #13
0
        public void Constructor4_ValueAsExceptionArgument_ShouldSetInnerExceptionProperty()
        {
            var expectations = Expectations.GetInnerExceptions();

            Assert.All(expectations, e =>
            {
                var error = new CompositionError("Description", ElementFactory.Create(), e);
                Assert.Same(e, error.InnerException);
            });
        }
예제 #14
0
        public void Constructor3_ValueAsMessageArgument_ShouldSetMessageProperty()
        {
            var expectations = Expectations.GetExceptionMessages();

            foreach (var e in expectations)
            {
                var exception = new CompositionError(e, new Exception());

                Assert.AreEqual(e, exception.Description);
            }
        }
예제 #15
0
        public void Constructor4_ValueAsElementArgument_ShouldSetElementProperty()
        {
            var expectations = Expectations.GetCompositionElements();

            foreach (var e in expectations)
            {
                var exception = new CompositionError("Description", (ICompositionElement)e, new Exception());

                Assert.AreSame(e, exception.Element);
            }
        }
예제 #16
0
        public void Constructor3_ValueAsExceptionArgument_ShouldSetInnerExceptionProperty()
        {
            var expectations = Expectations.GetInnerExceptions();

            foreach (var e in expectations)
            {
                var error = new CompositionError("Description", e);

                Assert.AreSame(e, error.InnerException);
            }
        }
예제 #17
0
        public void Constructor5_ValueAsIdArgument_ShouldSetICompositionErrorIdProperty()
        {
            var expectations = Expectations.GetEnumValues <CompositionErrorId>();

            foreach (var e in expectations)
            {
                var error = new CompositionError(e, "Description", ElementFactory.Create(), new Exception());

                Assert.AreEqual(e, ((ICompositionError)error).Id);
            }
        }
예제 #18
0
        public void Constructor5_ValueAsExceptionArgument_ShouldSetExceptionProperty()
        {
            var expectations = Expectations.GetInnerExceptions();

            foreach (var e in expectations)
            {
                var error = new CompositionError(CompositionErrorId.Unknown, "Description", ElementFactory.Create(), e);

                Assert.AreSame(e, error.Exception);
            }
        }
        private static CompositionError[] CreateCompositionErrors(int count)
        {
            CompositionError[] errors = new CompositionError[count];

            for (int i = 0; i < count; i++)
            {
                errors[i] = CreateCompositionError("Description" + (i + 1));
            }

            return(errors);
        }
예제 #20
0
        public void Constructor2_ValueAsMessageArgument_ShouldSetMessageProperty()
        {
            var expectations = Expectations.GetExceptionMessages();

            foreach (var e in expectations)
            {
                var exception = new CompositionError(e, ElementFactory.Create());

                Assert.AreEqual(e, exception.Description);
            }
        }
        public void Constructor5_ArrayAsAsErrorsArgument_ShouldNotAllowModificationAfterConstruction()
        {
            var error  = CreateCompositionError();
            var errors = new CompositionError[] { error };

            var exception = new CompositionException("Message", new Exception(), errors);

            errors[0] = null;

            EnumerableAssert.AreEqual(exception.Errors, error);
        }
예제 #22
0
        public static CompositionError CreatePartCycle(ComposablePart part)
        {
            Assumes.NotNull(part);

            ICompositionElement element = part.ToElement();

            return(CompositionError.Create(
                       CompositionErrorId.ImportEngine_PartCycle,
                       element,
                       SR.ImportEngine_PartCycle,
                       element.DisplayName));
        }
예제 #23
0
        public static CompositionError CreatePartCannotActivate(ComposablePart part, Exception innerException)
        {
            Assumes.NotNull(part, innerException);

            ICompositionElement element = part.ToElement();

            return(CompositionError.Create(
                       CompositionErrorId.ImportEngine_PartCannotActivate,
                       element,
                       innerException,
                       SR.ImportEngine_PartCannotActivate,
                       element.DisplayName));
        }
        private string GetElementGraphString(CompositionError error)
        {
            StringBuilder       writer  = new StringBuilder();
            ICompositionElement element = error.Element;

            writer.AppendFormat(CultureInfo.CurrentCulture, SR.CompositionException_ElementPrefix, element.DisplayName);

            while ((element = element.Origin) != null)
            {
                writer.AppendFormat(CultureInfo.CurrentCulture, SR.CompositionException_OriginFormat, SR.CompositionException_OriginSeparator, element.DisplayName);
            }

            return(writer.ToString());
        }
예제 #25
0
        public static CompositionError CreateCannotGetExportedValue(ComposablePart part, ExportDefinition definition, Exception innerException)
        {
            Assumes.NotNull(part, definition, innerException);

            ICompositionElement element = definition.ToElement();

            return(CompositionError.Create(
                       CompositionErrorId.ImportEngine_PartCannotGetExportedValue,
                       element,
                       innerException,
                       SR.ImportEngine_PartCannotGetExportedValue,
                       element.DisplayName,
                       part.ToElement().DisplayName));
        }
예제 #26
0
        public static CompositionError CreatePartCycle(ComposablePart part)
        {
            if (part == null)
            {
                throw new ArgumentNullException(nameof(part));
            }

            ICompositionElement element = part.ToElement();

            return(CompositionError.Create(
                       CompositionErrorId.ImportEngine_PartCycle,
                       element,
                       SR.ImportEngine_PartCycle,
                       element.DisplayName));
        }
        private object GetInstance()
        {
            var result = CompositionResult.SucceededResult;

            // We only need this guard if we are pulling on the lazy exports during this call
            // but if we do the pulling in SetImport it isn't needed.
            //if (currentlyExecuting)
            //{
            //    var issue = CompositionError.Create("CM:CreationCycleDetected",
            //        "This failed because there is a creation cycle");
            //    return result.MergeIssue(issue).ToResult<object>(null);
            //}

            try
            {
                currentlyExecuting = true;

                List <object> constructorArgs = new List <object>();

                foreach (ImportDefinition import in this.ImportDefinitions
                         .Where(i => i.IsPrerequisite))
                {
                    object importValue;
                    if (!this._imports.TryGetValue(import, out importValue))
                    {
                        result = result.MergeError(CompositionError.Create(CompositionErrorId.ImportNotSetOnPart,
                                                                           "The import '{0}' is required for construction of '{1}'", import.ToString(), _type.FullName));

                        continue;
                    }

                    constructorArgs.Add(importValue);
                }

                if (!result.Succeeded)
                {
                    throw new CompositionException(result.Errors);
                }

                object obj = this._constructor.Invoke(constructorArgs.ToArray());

                return(obj);
            }
            finally
            {
                currentlyExecuting = false;
            }
        }
예제 #28
0
        public static CompositionError CreatePartCannotSetImport(ComposablePart part, ImportDefinition definition, Exception innerException)
        {
            ArgumentNullException.ThrowIfNull(part);
            ArgumentNullException.ThrowIfNull(definition);
            ArgumentNullException.ThrowIfNull(innerException);

            ICompositionElement element = definition.ToElement();

            return(CompositionError.Create(
                       CompositionErrorId.ImportEngine_PartCannotSetImport,
                       element,
                       innerException,
                       SR.ImportEngine_PartCannotSetImport,
                       element.DisplayName,
                       part.ToElement().DisplayName));
        }
예제 #29
0
        private static void VisitError(CompositionError error, VisitContext context)
        {
            context.Path.Push(error);

            if (error.Exception == null)
            {   // This error is a root cause, so write
                // out the stack from this point

                context.LeafVisitor(context.Path);
            }
            else
            {
                VisitException(error.Exception, context);
            }

            context.Path.Pop();
        }
예제 #30
0
        private static CompositionError CreateCompositionError(params string[] messages)
        {
            CompositionError error = null;

            foreach (string message in messages.Reverse())
            {
                CompositionException exception = null;
                if (error != null)
                {
                    exception = CreateCompositionException(error);
                }

                error = ErrorFactory.Create(message, exception);
            }

            return(error);
        }
예제 #31
0
        public static CompositionError CreateImportCardinalityMismatch(ImportCardinalityMismatchException exception, ImportDefinition definition)
        {
            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }

            if (definition == null)
            {
                throw new ArgumentNullException(nameof(definition));
            }

            return(CompositionError.Create(
                       CompositionErrorId.ImportEngine_ImportCardinalityMismatch,
                       exception.Message,
                       definition.ToElement(),
                       (Exception?)null));
        }
예제 #32
0
        public void Constructor4_ValueAsExceptionArgument_ShouldSetInnerExceptionProperty()
        {
            var expectations = Expectations.GetInnerExceptions();

            foreach (var e in expectations)
            {
                var error = new CompositionError("Description", ElementFactory.Create(), e);

                Assert.AreSame(e, ((ICompositionError)error).InnerException);
            }
        }
예제 #33
0
        public CompositionErrorDebuggerProxy(CompositionError error)
        {
            Requires.NotNull(error, "error");

            this._error = error;
        }
예제 #34
0
        public void Constructor4_NullAsExceptionArgument_ShouldSetExceptionPropertyToNull()
        {
            var error = new CompositionError("Description", ElementFactory.Create(), (Exception)null);

            Assert.IsNull(error.Exception);
        }
        private static void VisitError(CompositionError error, VisitContext context)
        {
            context.Path.Push(error);

            if (error.Exception == null)
            {   // This error is a root cause, so write 
                // out the stack from this point

                context.LeafVisitor(context.Path);
            }
            else
            {
                VisitException(error.Exception, context);
            }

            context.Path.Pop();
        }
예제 #36
0
        public void Constructor5_NullAsMessageArgument_ShouldSetMessagePropertyToEmptyString()
        {
            var error = new CompositionError(CompositionErrorId.Unknown, (string)null, ElementFactory.Create(), new Exception());

            Assert.AreEqual("", error.Description);
        }
예제 #37
0
 private static CompositionException CreateCompositionException(CompositionError error)
 {
     return new CompositionException(error);
 }
예제 #38
0
        public void Constructor5_ValueAsElementArgument_ShouldSetElementProperty()
        {
            var expectations = Expectations.GetCompositionElements();

            foreach (var e in expectations)
            {
                var exception = new CompositionError(CompositionErrorId.Unknown, "Description", (ICompositionElement)e, new Exception());

                Assert.AreSame(e, exception.Element);
            }
        }
예제 #39
0
        public void Constructor3_NullAsMessageArgument_ShouldSetMessagePropertyToEmptyString()
        {
            var error = new CompositionError((string)null, new Exception());

            Assert.AreEqual("", error.Description);
        }
예제 #40
0
        public void Constructor5_NullAsElementArgument_ShouldSetElementPropertyToNull()
        {
            var exception = new CompositionError(CompositionErrorId.Unknown, "Description", (ICompositionElement)null, new Exception());

            Assert.IsNull(exception.Element);
        }
예제 #41
0
        public void Constructor3_ShouldSetElementPropertyToNull()
        {
            var exception = new CompositionError("Description", new Exception());

            Assert.IsNull(exception.Element);
        }
예제 #42
0
        public void Constructor2_NullAsElementArgument_ShouldSetElementPropertyToNull()
        {
            var exception = new CompositionError("Description", (ICompositionElement)null);

            Assert.IsNull(exception.Element);
        }
예제 #43
0
        private static CompositionError[] CreateCompositionErrors(int count)
        {
            CompositionError[] errors = new CompositionError[count];

            for (int i = 0; i < count; i++)
            {
                errors[i] = CreateCompositionError("Description" + (i + 1));
            }

            return errors;
        }
예제 #44
0
        public void Constructor5_ArrayAsAsErrorsArgument_ShouldNotAllowModificationAfterConstruction()
        {
            var error = CreateCompositionError();
            var errors = new CompositionError[] { error };

            var exception = new CompositionException("Message", new Exception(), errors);

            errors[0] = null;

            EnumerableAssert.AreEqual(exception.Errors, error);
        }
예제 #45
0
        public void Constructor3_NullAsExceptionArgument_ShouldSetInnerExceptionPropertyToNull()
        {
            var error = new CompositionError("Description", (Exception)null);

            Assert.IsNull(((ICompositionError)error).InnerException);
        }
예제 #46
0
        public void Constructor5_NullAsExceptionArgument_ShouldSetInnerExceptionPropertyToNull()
        {
            var error = new CompositionError(CompositionErrorId.Unknown, "Description", ElementFactory.Create(), (Exception)null);

            Assert.IsNull(((ICompositionError)error).InnerException);
        }
예제 #47
0
        public void Constructor5_ArrayWithNullAsErrorsArgument_ShouldThrowArgument()
        {
            var errors = new CompositionError[] { null };

            ExceptionAssert.ThrowsArgument<ArgumentException>("errors", () =>
            {
                new CompositionException("Message", new Exception(), errors);
            });
        }
예제 #48
0
        public void Constructor1_ShouldSetICompositionErrorIdPropertyToCompositionErrorIdUnknown()
        {
            var error = new CompositionError("Description");

            Assert.AreEqual(CompositionErrorId.Unknown, ((ICompositionError)error).Id);
        }
예제 #49
0
        private string GetElementGraphString(CompositionError error)
        {
            StringBuilder writer = new StringBuilder();
            ICompositionElement element = error.Element;
            writer.AppendFormat(CultureInfo.CurrentCulture, Strings.CompositionException_ElementPrefix, element.DisplayName);

            while ((element = element.Origin) != null)
            {
                writer.AppendFormat(CultureInfo.CurrentCulture, Strings.CompositionException_OriginFormat, Strings.CompositionException_OriginSeparator, element.DisplayName);
            }

            return writer.ToString();
        }
예제 #50
0
        public void Constructor4_ShouldSetICompositionErrorIdPropertyToCompositionErrorIdUnknown()
        {
            var error = new CompositionError("Description", ElementFactory.Create(), new Exception());

            Assert.AreEqual(CompositionErrorId.Unknown, ((ICompositionError)error).Id);
        }
예제 #51
0
        public void Constructor3_ValueAsExceptionArgument_ShouldSetExceptionProperty()
        {
            var expectations = Expectations.GetInnerExceptions();

            foreach (var e in expectations)
            {
                var error = new CompositionError("Description", e);

                Assert.AreSame(e, error.Exception);
            }
        }
예제 #52
0
        public void Constructor5_ValueAsIdArgument_ShouldSetICompositionErrorIdProperty()
        {
            var expectations = Expectations.GetEnumValues<CompositionErrorId>();

            foreach (var e in expectations)
            {
                var error = new CompositionError(e, "Description", ElementFactory.Create(), new Exception());

                Assert.AreEqual(e, ((ICompositionError)error).Id);
            }
        }
        private static void WriteError(StringBuilder writer, CompositionError error)
        {
            writer.AppendLine(error.Description);

            if (error.Element != null)
            {
                WriteElementGraph(writer, error.Element);
            }
        }
예제 #54
0
        public void Constructor1_ShouldSetInnerExceptionPropertyToNull()
        {
            var error = new CompositionError("Description");

            Assert.IsNull(((ICompositionError)error).InnerException);
        }
 internal CompositionException(CompositionError error)
     : this(new CompositionError[] { error })
 {
 }
예제 #56
0
        public void Constructor2_ShouldSetInnerExceptionPropertyToNull()
        {
            var error = new CompositionError("Description", ElementFactory.Create());

            Assert.IsNull(((ICompositionError)error).InnerException);
        }