Beispiel #1
0
        /// <summary>
        /// Determines if this AttributeValue object has the same value as another object
        /// </summary>
        /// <param name="obj">The value to compare</param>
        /// <returns>A boolean value indicating if the two values are the same</returns>
        public bool ValueEquals(object obj)
        {
            if (this.Value == null && obj == null)
            {
                return(true);
            }

            if (this.Value == null ^ obj == null)
            {
                return(false);
            }

            object comparisonValue;

            if (obj is AttributeValue)
            {
                AttributeValue otherValue = (AttributeValue)obj;

                if (this.IsNull && otherValue.IsNull)
                {
                    //// If both values are null
                    return(true);
                }
                else if (this.IsNull || otherValue.IsNull)
                {
                    //// If one of the two values are null
                    return(false);
                }
                else if (this.Attribute != otherValue.Attribute)
                {
                    //// If the types don't match
                    return(false);
                }

                comparisonValue = otherValue.Value;
            }
            else
            {
                comparisonValue = obj;
            }

            switch (this.attribute.Type)
            {
            case ExtendedAttributeType.Boolean:
            case ExtendedAttributeType.Integer:
            case ExtendedAttributeType.DateTime:
            case ExtendedAttributeType.Reference:
            case ExtendedAttributeType.Binary:
                return(ComparisonEngine.Compare(comparisonValue, this.Value, ValueOperator.Equals, this.Attribute.Type));

            case ExtendedAttributeType.String:
                return(ComparisonEngine.CompareString(TypeConverter.ConvertData <string>(comparisonValue), this.ValueString, ValueOperator.Equals, StringComparison.CurrentCulture));

            case ExtendedAttributeType.Undefined:
            default:
                throw new ArgumentException("Unknown or unsupported attribute type");
            }
        }
Beispiel #2
0
        public void CompareStringEndsWithTest()
        {
            ValueOperator valueOperator    = ValueOperator.EndsWith;
            string        actualValue      = "abcdefghijklmnop";
            string        matchingValue    = "mnop";
            string        nonMatchingValue = "zzz";

            Assert.AreEqual(true, ComparisonEngine.CompareString(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareString(actualValue, nonMatchingValue, valueOperator));
        }
Beispiel #3
0
        public void CompareStringNotEqualsTest()
        {
            ValueOperator valueOperator    = ValueOperator.NotEquals;
            string        actualValue      = "abcd";
            string        matchingValue    = "defg";
            string        nonMatchingValue = "abcd";

            Assert.AreEqual(true, ComparisonEngine.CompareString(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareString(actualValue, nonMatchingValue, valueOperator));
        }
Beispiel #4
0
        public void CompareStringNotPresentTest()
        {
            ValueOperator valueOperator    = ValueOperator.NotPresent;
            string        actualValue      = null;
            string        matchingValue    = null;
            string        nonMatchingValue = null;

            Assert.AreEqual(true, ComparisonEngine.CompareString(actualValue, matchingValue, valueOperator));

            actualValue = "abds";
            Assert.AreEqual(false, ComparisonEngine.CompareString(actualValue, nonMatchingValue, valueOperator));
        }
        /// <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 Guid GetValueFromMvReference(IList <object> source)
        {
            foreach (object value in source)
            {
                Guid valueGuid = TypeConverter.ConvertData <Guid>(value);

                if (ComparisonEngine.CompareString(valueGuid.ToString(), this.SelectorValue, this.SelectorOperator))
                {
                    return(valueGuid);
                }
            }

            throw new NotFoundException();
        }
        /// <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 string GetValueFromMvString(IList <object> source)
        {
            foreach (object value in source)
            {
                string valueString = TypeConverter.ConvertData <string>(value);

                if (ComparisonEngine.CompareString(valueString, this.SelectorValue, this.SelectorOperator))
                {
                    return(valueString);
                }
            }

            throw new NotFoundException();
        }
        /// <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);
        }