/// <summary>
        /// Gets the matching single value from the multivalued attribute
        /// </summary>
        /// <param name="source">The source attribute array</param>
        /// <returns>An attribute value</returns>
        private DateTime GetValueFromMvDateTime(IList <object> source)
        {
            DateTime?lastValue = null;

            foreach (object value in source)
            {
                DateTime valueDateTime = TypeConverter.ConvertData <DateTime>(value);

                switch (this.SelectorOperator)
                {
                case ValueOperator.Equals:
                case ValueOperator.NotEquals:
                case ValueOperator.GreaterThan:
                case ValueOperator.LessThan:
                case ValueOperator.GreaterThanOrEq:
                case ValueOperator.LessThanOrEq:
                case ValueOperator.And:
                case ValueOperator.Or:
                    if (ComparisonEngine.CompareDateTime(valueDateTime, DateTime.Parse(this.SelectorValue), this.SelectorOperator))
                    {
                        return(valueDateTime);
                    }

                    break;

                case ValueOperator.Largest:
                    if (!lastValue.HasValue || valueDateTime > lastValue)
                    {
                        lastValue = valueDateTime;
                    }

                    break;

                case ValueOperator.Smallest:
                    if (!lastValue.HasValue || valueDateTime < lastValue)
                    {
                        lastValue = valueDateTime;
                    }

                    break;

                case ValueOperator.Contains:
                case ValueOperator.NotContains:
                case ValueOperator.StartsWith:
                case ValueOperator.EndsWith:
                case ValueOperator.IsPresent:
                case ValueOperator.NotPresent:
                default:
                    throw new UnknownOrUnsupportedValueOperatorException(this.SelectorOperator, typeof(long));
                }
            }

            if (lastValue.HasValue)
            {
                return(lastValue.Value);
            }

            throw new NotFoundException();
        }
示例#2
0
        public void CompareDateTimeLessThanTest()
        {
            ValueOperator valueOperator    = ValueOperator.LessThan;
            DateTime      actualValue      = DateTime.Parse("1/1/1999");
            DateTime      matchingValue    = DateTime.Parse("1/1/2000");
            DateTime      nonMatchingValue = DateTime.Parse("1/1/1989");

            Assert.AreEqual(true, ComparisonEngine.CompareDateTime(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareDateTime(actualValue, nonMatchingValue, valueOperator));
        }
        /// <summary>
        /// Evaluates a specific value against the rule
        /// </summary>
        /// <param name="actualValue">The value to evaluate</param>
        /// <returns>A value indicating whether the rule conditions were met</returns>
        protected bool EvaluateAttributeValue(object actualValue, IList <object> expectedValues)
        {
            if (actualValue == null)
            {
                if (this.CompareAs == ExtendedAttributeType.Boolean)
                {
                    actualValue = false;
                }
                else
                {
                    this.RaiseRuleFailure(string.Format("The source value was null"));
                    return(false);
                }
            }

            bool result = false;

            foreach (object expectedValue in expectedValues)
            {
                switch (this.CompareAs)
                {
                case ExtendedAttributeType.Binary:
                    result = ComparisonEngine.CompareBinary(TypeConverter.ConvertData <byte[]>(actualValue), TypeConverter.ConvertData <byte[]>(expectedValue), this.ValueOperator);
                    break;

                case ExtendedAttributeType.Boolean:
                    result = ComparisonEngine.CompareBoolean(TypeConverter.ConvertData <bool>(actualValue), TypeConverter.ConvertData <bool>(expectedValue), this.ValueOperator);
                    break;

                case ExtendedAttributeType.Integer:
                    result = ComparisonEngine.CompareLong(TypeConverter.ConvertData <long>(actualValue), TypeConverter.ConvertData <long>(expectedValue), this.ValueOperator);
                    break;

                case ExtendedAttributeType.DateTime:
                    result = ComparisonEngine.CompareDateTime(TypeConverter.ConvertData <DateTime>(actualValue), TypeConverter.ConvertData <DateTime>(expectedValue), this.ValueOperator);
                    break;

                case ExtendedAttributeType.String:
                    result = ComparisonEngine.CompareString(TypeConverter.ConvertData <string>(actualValue), TypeConverter.ConvertData <string>(expectedValue), this.ValueOperator);
                    break;

                case ExtendedAttributeType.Reference:
                    result = ComparisonEngine.CompareString(TypeConverter.ConvertData <string>(actualValue), TypeConverter.ConvertData <string>(expectedValue), this.ValueOperator);
                    break;

                default:
                    throw new UnknownOrUnsupportedDataTypeException();
                }

                if (result)
                {
                    break;
                }
            }

            if (!result)
            {
                this.RaiseRuleFailure("Comparison failed\nComparison Operator: {0}\nExpected Values: {1}\nActual Value: {2}", this.ValueOperator.ToString(), expectedValues.Select(t => t.ToSmartStringOrNull()).ToCommaSeparatedString(), actualValue.ToSmartString());
            }

            return(result);
        }