public void GetBytesTest() { ErrorCodeAttribute target = new ErrorCodeAttribute() { ErrorCode = 123, ReasonPhrase = @"Test!", }; byte[] expected = new byte[] { 0xee, 0xee, 0xee, 0xee, 0x00, 0x09, 0x00, 0x09, 0x00, 0x00, 1, 23, (byte)'T', (byte)'e', (byte)'s', (byte)'t', (byte)'!', }; byte[] actual = new byte[] { 0xee, 0xee, 0xee, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; int startIndex = 4; target.GetBytes(actual, ref startIndex); Assert.AreEqual(17, startIndex); Helpers.AreArrayEqual(expected, actual); }
public IntranetExceptionBase Build() { ErrorCodeAttribute errorCodeAttribute = GetErrorCodeAttribute(_errorCode); string message = _argumentCollection == null ? errorCodeAttribute.Message : string.Format(errorCodeAttribute.Message, _argumentCollection.ToArray()); IntranetExceptionBase intranetException = _innerException == null ? Build(errorCodeAttribute.ExceptionType, _errorCode, message) : Build(errorCodeAttribute.ExceptionType, _errorCode, message, _innerException); if (_methodBase != null) { AddMethodBase(intranetException, _methodBase); } if (_validatingType != null) { AddValidatingType(intranetException, _validatingType); } if (string.IsNullOrWhiteSpace(_validatingField)) { return(intranetException); } AddValidatingField(intranetException, _validatingField); return(intranetException); }
public ErrorCodeMetadata(int errorCode, ErrorCodeAttribute errorCodeAttribute, IConverter coreModelConverter) { NullGuard.NotNull(errorCodeAttribute, nameof(errorCodeAttribute)) .NotNull(coreModelConverter, nameof(coreModelConverter)); ErrorCode = errorCode; ErrorType = coreModelConverter.Convert <Type, string>(errorCodeAttribute.ExceptionType); ErrorMessage = errorCodeAttribute.Message; }
public void Build_WhenCalledForIntranetValidationException_AssertMessageIsCorrect() { const ErrorCode errorCode = ErrorCode.ValueNotGreaterThanZero; string validatingField = _fixture.Create <string>(); ErrorCodeAttribute errorCodeAttribute = _errorCodeAttributeTestHelper.GetErrorCodeAttribute(errorCode); IIntranetExceptionBuilder sut = CreateSut(errorCode, validatingField); IntranetExceptionBase result = sut.Build(); Assert.That(result.Message, Is.EqualTo(string.Format(errorCodeAttribute.Message, validatingField))); }
public void Build_WhenCalledForIntranetCommandBusException_AssertMessageIsCorrect() { const ErrorCode errorCode = ErrorCode.NoCommandHandlerSupportingCommandWithoutResultType; string commandTypeName = _fixture.Create <string>(); ErrorCodeAttribute errorCodeAttribute = _errorCodeAttributeTestHelper.GetErrorCodeAttribute(errorCode); IIntranetExceptionBuilder sut = CreateSut(errorCode, commandTypeName); IntranetExceptionBase result = sut.Build(); Assert.That(result.Message, Is.EqualTo(string.Format(errorCodeAttribute.Message, commandTypeName))); }
public void Build_WhenCalledForIntranetSystemException_AssertMessageIsCorrect() { const ErrorCode errorCode = ErrorCode.ObjectIsNull; string commandTypeName = _fixture.Create <string>(); ErrorCodeAttribute errorCodeAttribute = _errorCodeAttributeTestHelper.GetErrorCodeAttribute(errorCode); IIntranetExceptionBuilder sut = CreateSut(errorCode, commandTypeName); IntranetExceptionBase result = sut.Build(); Assert.That(result.Message, Is.EqualTo(string.Format(errorCodeAttribute.Message, commandTypeName))); }
public void Build_WhenCalledForIntranetRepositoryException_AssertMessageIsCorrect() { const ErrorCode errorCode = ErrorCode.RepositoryError; string methodName = _fixture.Create <string>(); string error = _fixture.Create <string>(); ErrorCodeAttribute errorCodeAttribute = _errorCodeAttributeTestHelper.GetErrorCodeAttribute(errorCode); IIntranetExceptionBuilder sut = CreateSut(errorCode, methodName, error); IntranetExceptionBase result = sut.Build(); Assert.That(result.Message, Is.EqualTo(string.Format(errorCodeAttribute.Message, methodName, error))); }
public void ParseTest() { byte[] bytes = new byte[] { 0x00, 0x09, 0x00, 0x0A, 0x00, 0x00, 3, 21, (byte)'H', (byte)'e', (byte)'l', (byte)'l', (byte)'o', (byte)'!', }; int startIndex = 0; ErrorCodeAttribute target = new ErrorCodeAttribute(); target.Parse(bytes, ref startIndex); Assert.AreEqual(14, startIndex); Assert.AreEqual(AttributeType.ErrorCode, target.AttributeType); Assert.AreEqual(3 * 100 + 21, target.ErrorCode); Assert.AreEqual("Hello!", target.ReasonPhrase); }
private static ErrorCodeAttribute GetErrorCodeAttribute(ErrorCode errorCode) { FieldInfo fieldInfo = errorCode.GetType().GetField(Convert.ToString(errorCode), BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy); if (fieldInfo == null) { throw new MissingFieldException($"Unable to find a field named '{errorCode}' in '{errorCode.GetType().Name}'", Convert.ToString(errorCode)); } ErrorCodeAttribute errorCodeAttribute = fieldInfo.GetCustomAttributes <ErrorCodeAttribute>().SingleOrDefault(); if (errorCodeAttribute == null) { throw new InvalidOperationException($"The error code named '{errorCode}' has no '{typeof(ErrorCodeAttribute).Name}'."); } return(errorCodeAttribute); }
public void ErrorCode_ForAllErrorCodes_EnsureIntranetExceptionBaseCanBeCreated() { IEnumerable <Interfaces.Enums.ErrorCode> sutCollection = CreateSut(); IList <Interfaces.Enums.ErrorCode> result = sutCollection.AsParallel() .Where(errorCode => { try { ErrorCodeAttribute errorCodeAttribute = _errorCodeAttributeTestHelper.GetErrorCodeAttribute(errorCode); int numberOfArguments = _argumentCounterRegex.Matches(errorCodeAttribute.Message).Count; new Core.IntranetExceptionBuilder(errorCode, _fixture.CreateMany <object>(numberOfArguments).ToArray()).Build(); return(false); } catch (ArgumentNullException) { return(false); } catch (ArgumentException) { return(false); } }) .OrderBy(errorCode => (int)errorCode) .ToList(); if (result.Count == 0) { return; } StringBuilder resultBuilder = new StringBuilder(); foreach (Interfaces.Enums.ErrorCode errorCode in result) { resultBuilder.AppendLine($"Unable to build an '{typeof(IntranetExceptionBase).Name}' for the error code named '{errorCode}'."); } Assert.Fail(resultBuilder.ToString()); }
public void ErrorCode_ForAllErrorCodes_EnsureExceptionTypeIsIntranetExceptionBase() { IEnumerable <Interfaces.Enums.ErrorCode> sutCollection = CreateSut(); IList <Interfaces.Enums.ErrorCode> result = sutCollection.AsParallel() .Where(errorCode => { try { ErrorCodeAttribute errorCodeAttribute = _errorCodeAttributeTestHelper.GetErrorCodeAttribute(errorCode); return(errorCodeAttribute != null && errorCodeAttribute.ExceptionType.IsSubclassOf(typeof(IntranetExceptionBase)) == false); } catch (ArgumentNullException) { return(false); } catch (ArgumentException ex) { return(string.Compare("exceptionType", ex.ParamName, StringComparison.Ordinal) == 0); } }) .OrderBy(errorCode => (int)errorCode) .ToList(); if (result.Count == 0) { return; } StringBuilder resultBuilder = new StringBuilder(); foreach (Interfaces.Enums.ErrorCode errorCode in result) { resultBuilder.AppendLine($"The error code named '{errorCode}' has an invalid exception type which are not based on '{typeof(IntranetExceptionBase).Name}'."); } Assert.Fail(resultBuilder.ToString()); }
public void ErrorCode_ForAllErrorCodes_EnsureMessageIsNotNullEmptyOrWhiteSpace() { IEnumerable <Interfaces.Enums.ErrorCode> sutCollection = CreateSut(); IList <Interfaces.Enums.ErrorCode> result = sutCollection.AsParallel() .Where(errorCode => { try { ErrorCodeAttribute errorCodeAttribute = _errorCodeAttributeTestHelper.GetErrorCodeAttribute(errorCode); return(errorCodeAttribute != null && string.IsNullOrWhiteSpace(errorCodeAttribute.Message)); } catch (ArgumentNullException ex) { return(string.Compare("message", ex.ParamName, StringComparison.Ordinal) == 0); } catch (ArgumentException) { return(false); } }) .OrderBy(errorCode => (int)errorCode) .ToList(); if (result.Count == 0) { return; } StringBuilder resultBuilder = new StringBuilder(); foreach (Interfaces.Enums.ErrorCode errorCode in result) { resultBuilder.AppendLine($"The error code named '{errorCode}' has no valid message."); } Assert.Fail(resultBuilder.ToString()); }
/// <summary> /// 返回枚举项的描述信息。 /// </summary> /// <param name="value">要获取描述信息的枚举项。</param> /// <returns>枚举想的描述信息。</returns> public static string GetErrorCodeAlert(this Enum value) { Type enumType = value.GetType(); // 获取枚举常数名称。 string name = Enum.GetName(enumType, value); if (name != null) { // 获取枚举字段。 FieldInfo fieldInfo = enumType.GetField(name); if (fieldInfo != null) { // 获取描述的属性。 ErrorCodeAttribute attr = Attribute.GetCustomAttribute(fieldInfo, typeof(ErrorCodeAttribute), false) as ErrorCodeAttribute; if (attr != null) { return(attr.Alert); } } } return(null); }
public void ErrorCodeAttributeConstructorTest() { ErrorCodeAttribute target = new ErrorCodeAttribute(); Assert.AreEqual(AttributeType.ErrorCode, target.AttributeType); }