Exemple #1
0
        private static void BuildErrors(ModValidationResult result, StringBuilder builder)
        {
            foreach (var modError in result.ModErrors)
            {
                builder.AppendLine(modError);
            }

            foreach (var fileValidationErrors in result.XmlErrors)
            {
                if (fileValidationErrors.Errors.Count > 0)
                {
                    builder.AppendLine(fileValidationErrors.Path);
                    builder.AppendLine(String.Format("{0} error(s) found.", fileValidationErrors.Errors.Count));

                    foreach (var error in fileValidationErrors.Errors)
                    {
                        builder.AppendLine(
                            String.Format(
                                "Line {0}, Position: {1} -- {2}",
                                error.LineNumber,
                                error.LinePosition,
                                XmlErrorTranslator.Translate(error.Message)
                                )
                            );
                    }

                    builder.AppendLine();
                }
            }
        }
        // ReSharper disable once UnusedParameter.Local (the assertion is the whole point, R#!)
        private void CheckExceptionMessage(string expectedFriendlyMessage, string providedException)
        {
            var translator   = new XmlErrorTranslator();
            var testXElement = GetTestXElement();

            string schemaVersion   = MemberRegistrationSchemaVersion.Version_3_07.GetAttribute <DisplayAttribute>().Name;
            var    friendlyMessage = translator.MakeFriendlyErrorMessage(testXElement, providedException, TestLineNumber, schemaVersion);

            Assert.Equal(expectedFriendlyMessage, friendlyMessage);
        }
        public void XmlErrorTranslator_DoesntMatchAnything_OriginalMessage()
        {
            string providedException       = "Here is some random text. There's no way this'll match any regex, surely.";
            string expectedFriendlyMessage = providedException;

            var translator = new XmlErrorTranslator();

            var friendlyMessage = translator.MakeFriendlyErrorMessage(providedException, A.Dummy <string>());

            Assert.Equal(expectedFriendlyMessage, friendlyMessage);
        }
        public void MakeFriendlyErrorMessage_WithDataAtTheRootLevelIsInvalid_ReturnsFriendlyMessage()
        {
            // Arrange
            string schemaValidationErrorMessage = "Data at the root level is invalid. Line 1, position 1.";

            XmlErrorTranslator translator = new XmlErrorTranslator();

            // Act
            string result = translator.MakeFriendlyErrorMessage(schemaValidationErrorMessage, A.Dummy <string>());

            // Assert
            Assert.Equal("The file you're trying to upload is not a correctly formatted XML file. Upload a valid XML file.", result);
        }
        public void XmlErrorTranslator_ErrorInXmlDocument_CorrectMessage()
        {
            const string lineNumber = "57";

            string providedException       = string.Format("There is an error in XML document ({0}, 109).", lineNumber);
            string expectedFriendlyMessage = string.Format("'{0}' This can be caused by an error on this line, or before it (XML line {1}).", providedException, lineNumber);

            var translator = new XmlErrorTranslator();

            var friendlyMessage = translator.MakeFriendlyErrorMessage(providedException, A.Dummy <string>());

            Assert.Equal(expectedFriendlyMessage, friendlyMessage);
        }
        public void MakeFriendlyErrorMessage_EntityNameParsingError_ReturnsFriendlyMessage()
        {
            // Arrange
            string xmlValidationErrorMessage = "An error occurred while parsing EntityName. Line 123, position 456.";

            XmlErrorTranslator translator = new XmlErrorTranslator();

            // Act
            string result = translator.MakeFriendlyErrorMessage(xmlValidationErrorMessage, A.Dummy <string>());

            // Assert

            string expectedResult = "Your XML file is not encoded correctly. Check for any characters " +
                                    "which need to be encoded. For example, replace ampersand (&) characters with &amp; (XML line 123).";

            Assert.Equal(expectedResult, result);
        }
        public void MakeFriendlyErrorMessage_InvalidCharacterEntityReference_ReturnsFriendlyMessage()
        {
            // Arrange
            string xmlValidationErrorMessage = "Reference to undeclared entity 'Foo'. Line 123, position 456.";

            XmlErrorTranslator translator = new XmlErrorTranslator();

            // Act
            string result = translator.MakeFriendlyErrorMessage(xmlValidationErrorMessage, A.Dummy <string>());

            // Assert

            string expectedResult = "Your XML file is not encoded correctly. Check for any characters " +
                                    "which need to be encoded. For example, replace ampersand (&) characters with &amp; (XML line 123).";

            Assert.Equal(expectedResult, result);
        }
        public void MakeFriendlyErrorMessage_InvalidCharacterEntityReference_ReturnsFriendlyMessage()
        {
            // Arrange
            string xmlValidationErrorMessage = "Reference to undeclared entity 'Foo'. Line 123, position 456.";

            XmlErrorTranslator translator = new XmlErrorTranslator();

            // Act
            string result = translator.MakeFriendlyErrorMessage(xmlValidationErrorMessage, A.Dummy<string>());

            // Assert

            string expectedResult = "Your XML file is not encoded correctly. Check for any characters " +
                "which need to be encoded. For example, replace ampersand (&) characters with &amp; (XML line 123).";

            Assert.Equal(expectedResult, result);
        }
        public void MakeFriendlyErrorMessage_EntityNameParsingError_ReturnsFriendlyMessage()
        {
            // Arrange
            string xmlValidationErrorMessage = "An error occurred while parsing EntityName. Line 123, position 456.";

            XmlErrorTranslator translator = new XmlErrorTranslator();

            // Act
            string result = translator.MakeFriendlyErrorMessage(xmlValidationErrorMessage, A.Dummy<string>());

            // Assert

            string expectedResult = "Your XML file is not encoded correctly. Check for any characters " +
                "which need to be encoded. For example, replace ampersand (&) characters with &amp; (XML line 123).";

            Assert.Equal(expectedResult, result);
        }
Exemple #10
0
        private void WriteErrors(List <XmlValidationError> list)
        {
            output.AppendText(Environment.NewLine);
            foreach (var error in list)
            {
                output.SelectionFont = errorFont;
                output.AppendText(
                    String.Format(
                        "Line {0}, Position: {1} -- {2}",
                        error.LineNumber,
                        error.LinePosition,
                        XmlErrorTranslator.Translate(error.Message)
                        )
                    );

                output.AppendText(Environment.NewLine);
                output.AppendText(Environment.NewLine);
            }
        }
        public void MakeFriendlyErrorMessage_InvalidSingleDataType_ReturnsFriendlyMessage()
        {
            // Arrange
            string value      = "One thousand pounds";
            string xmlType    = "Single";
            string xmlMessage = string.Format(
                "The 'http://www.environment-agency.gov.uk/WEEE/XMLSchema:fieldName' element is invalid - The value '{0}' is invalid according to its datatype 'http://www.environment-agency.gov.uk/WEEE/XMLSchema:dataType' - The string '{0}' is not a valid {1} value.", value, xmlType);
            XmlErrorTranslator translator = new XmlErrorTranslator();
            XElement           sender     = new XElement("fieldName");

            sender.Value = value;

            // Act
            string result = translator.MakeFriendlyErrorMessage(sender, xmlMessage, 12, A.Dummy <string>());

            // Assert
            string expectedResult = string.Format("The value '{0}' supplied for field 'fieldName' doesn't match the required data type. The value '{0}' must be a number (XML line 12).", value);

            Assert.Equal(expectedResult, result);
        }
        public void MakeFriendlyErrorMessage_ForFixedValueError_ReturnsFriendlyMessage()
        {
            // Arrange
            XElement element      = XElement.Parse("<foo>bar</foo>");
            string   errorMessage = "The value of the 'http://some.domain.com/schema/foo' element does not equal its fixed value.";
            int      lineNmber    = 123;

            XmlErrorTranslator xmlErrorTranslator = new XmlErrorTranslator();

            // Act
            string result = xmlErrorTranslator.MakeFriendlyErrorMessage(
                element,
                errorMessage,
                lineNmber,
                A.Dummy <string>());

            // Assert
            Assert.Equal(
                "The value 'bar' supplied for field 'foo' is not permitted. " +
                "Only the value specified in the schema is allowed (XML line 123).",
                result);
        }
        public void MakeFriendlyErrorMessage_InvalidSingleDataType_ReturnsFriendlyMessage()
        {
            // Arrange
            string value = "One thousand pounds";
            string xmlType = "Single";
            string xmlMessage = string.Format(
                "The 'http://www.environment-agency.gov.uk/WEEE/XMLSchema:fieldName' element is invalid - The value '{0}' is invalid according to its datatype 'http://www.environment-agency.gov.uk/WEEE/XMLSchema:dataType' - The string '{0}' is not a valid {1} value.", value, xmlType);
            XmlErrorTranslator translator = new XmlErrorTranslator();
            XElement sender = new XElement("fieldName");
            sender.Value = value;

            // Act
            string result = translator.MakeFriendlyErrorMessage(sender, xmlMessage, 12, A.Dummy<string>());

            // Assert
            string expectedResult = string.Format("The value '{0}' supplied for field 'fieldName' doesn't match the required data type. The value '{0}' must be a number (XML line 12).", value);

            Assert.Equal(expectedResult, result);
        }
        // ReSharper disable once UnusedParameter.Local (the assertion is the whole point, R#!)
        private void CheckExceptionMessage(string expectedFriendlyMessage, string providedException)
        {
            var translator = new XmlErrorTranslator();
            var testXElement = GetTestXElement();

            string schemaVersion = MemberRegistrationSchemaVersion.Version_3_07.GetAttribute<DisplayAttribute>().Name;
            var friendlyMessage = translator.MakeFriendlyErrorMessage(testXElement, providedException, TestLineNumber, schemaVersion);

            Assert.Equal(expectedFriendlyMessage, friendlyMessage);
        }
        public void MakeFriendlyErrorMessage_ForFixedValueError_ReturnsFriendlyMessage()
        {
            // Arrange
            XElement element = XElement.Parse("<foo>bar</foo>");
            string errorMessage = "The value of the 'http://some.domain.com/schema/foo' element does not equal its fixed value.";
            int lineNmber = 123;

            XmlErrorTranslator xmlErrorTranslator = new XmlErrorTranslator();

            // Act
            string result = xmlErrorTranslator.MakeFriendlyErrorMessage(
                element,
                errorMessage,
                lineNmber,
                A.Dummy<string>());

            // Assert
            Assert.Equal(
                "The value 'bar' supplied for field 'foo' is not permitted. " +
                "Only the value specified in the schema is allowed (XML line 123).",
                result);
        }
        public void MakeFriendlyErrorMessage_WithDataAtTheRootLevelIsInvalid_ReturnsFriendlyMessage()
        {
            // Arrange
            string schemaValidationErrorMessage = "Data at the root level is invalid. Line 1, position 1.";

            XmlErrorTranslator translator = new XmlErrorTranslator();

            // Act
            string result = translator.MakeFriendlyErrorMessage(schemaValidationErrorMessage, A.Dummy<string>());

            // Assert
            Assert.Equal("The file you're trying to upload is not a correctly formatted XML file. Upload a valid XML file.", result);
        }
        public void XmlErrorTranslator_DoesntMatchAnything_OriginalMessage()
        {
            string providedException = "Here is some random text. There's no way this'll match any regex, surely.";
            string expectedFriendlyMessage = providedException;

            var translator = new XmlErrorTranslator();

            var friendlyMessage = translator.MakeFriendlyErrorMessage(providedException, A.Dummy<string>());

            Assert.Equal(expectedFriendlyMessage, friendlyMessage);
        }
        public void XmlErrorTranslator_ErrorInXmlDocument_CorrectMessage()
        {
            const string lineNumber = "57";

            string providedException = string.Format("There is an error in XML document ({0}, 109).", lineNumber);
            string expectedFriendlyMessage = string.Format("'{0}' This can be caused by an error on this line, or before it (XML line {1}).", providedException, lineNumber);

            var translator = new XmlErrorTranslator();

            var friendlyMessage = translator.MakeFriendlyErrorMessage(providedException, A.Dummy<string>());

            Assert.Equal(expectedFriendlyMessage, friendlyMessage);
        }