public void FormatValueFromDirectory_ZeroLengthAndNotNullable_ThrowsMappingException()
        {
            //prepare
            _mappingArguments.PropertyType = typeof(DateTime);
            var mapping = new DatePropertyMapping<DatePropertyMappingTest>(_mappingArguments, "yyyyMMddHHmmss.0Z");

            //act
            Executing.This(() => mapping.FormatValueFromDirectory(new DirectoryAttribute("name"), "dn"))
                .Should().Throw<MappingException>();
        }
        public void FormatValueFromDirectory_InvalidFomrat_CatchesExceptionAndRethrowsAsMappingException()
        {
            //prepare

            var mapping = new DatePropertyMapping<DatePropertyMappingTest>(_mappingArguments, null);

            //act
            Executing.This(() => mapping.FormatValueFromDirectory(new DirectoryAttribute("name", "a"), "dn"))
                .Should().Throw<MappingException>()
                .And.Exception.InnerException.Should().Be.InstanceOf<FormatException>();
        }
        public void FormatValueToFilter_DateTime_ReturnsDateTime()
        {
            //prepare
            var date    = DateTime.Now;
            var mapping = new DatePropertyMapping <DateCollectionPropertyMappingTest>(_mappingArguments, "yyyyMMddHHmmss.0Z");

            //act
            var value = mapping.FormatValueToFilter(date);

            //assert
            value.Should().Be.EqualTo(date.FormatLdapDateTime("yyyyMMddHHmmss.0Z"));
        }
        public void FormatValueFromDirectory_ZeroLengthAndNullable_ReturnsNull()
        {
            //prepare
            _mappingArguments.PropertyType = typeof(DateTime?);
            var mapping = new DatePropertyMapping<DatePropertyMappingTest>(_mappingArguments, "yyyyMMddHHmmss.0Z");

            //act
            var value = mapping.FormatValueFromDirectory(new DirectoryAttribute("name"), "dn");

            //assert
            value.Should().Be.Null();
        }
        public void FormatValueToFilter_FileTime_ReturnsFileTime()
        {
            //prepare
            var date = DateTime.Now;
            var mapping = new DatePropertyMapping<DatePropertyMappingTest>(_mappingArguments, null);

            //aact
            var value = mapping.FormatValueToFilter(date);

            //assert
            value.Should().Be.EqualTo(date.ToFileTime().ToString());
        }
        public void FormatValueFromDirectory_FileTime_ReturnsDateTimeFromFileTime()
        {
            //prepare
            var date = DateTime.Now;
            var mapping = new DatePropertyMapping<DatePropertyMappingTest>(_mappingArguments, null);

            //act
            var value = mapping.FormatValueFromDirectory(new DirectoryAttribute("name", date.ToFileTime().ToString()), "dn");

            //assert
            value.Should().Be.EqualTo(date);
        }
        public void IsEqual_OneNull_ReturnsFalse()
        {
            //prepare
            _mappingArguments.Getter = t => null;
            var propertyMapping = new DatePropertyMapping<DatePropertyMappingTest>(_mappingArguments, null);
            DirectoryAttributeModification modification;
            //act
            var value = propertyMapping.IsEqual(this, DateTime.Now, out modification);

            //assert
            value.Should().Be.False();
            modification.Should().Not.Be.Null();
        }
        public void FormatValueFromDirectory_DateTime_ReturnsDateTimeFromDateTime()
        {
            //prepare
            var date = DateTime.Now;
            date = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
            var mapping = new DatePropertyMapping<DatePropertyMappingTest>(_mappingArguments, "yyyyMMddHHmmss.0Z");

            //act
            var value = mapping.FormatValueFromDirectory(new DirectoryAttribute("name", date.FormatLdapDateTime("yyyyMMddHHmmss.0Z")), "dn");

            //assert
            value.Should().Be.EqualTo(date);
        }
        public void IsEqual_SameDateTimes_ReturnsTrue()
        {
            //prepare
            var now = DateTime.Now;
            _mappingArguments.Getter = t => now;
            var propertyMapping = new DatePropertyMapping<DatePropertyMappingTest>(_mappingArguments, null);
            DirectoryAttributeModification modification;
            //act
            var value = propertyMapping.IsEqual(this, now, out modification);

            //assert
            value.Should().Be.True();
            modification.Should().Be.Null();
        }
Exemplo n.º 10
0
        public void IsEqual_DiffferentDates_ReturnsFalse()
        {
            //prepare
            var date = DateTime.Now;
            _mappingArguments.Getter = t => DateTime.Now;
            var propertyMapping = new DatePropertyMapping<DatePropertyMappingTest>(_mappingArguments, null);
            DirectoryAttributeModification modification;
            //act
            var value = propertyMapping.IsEqual(this, date.AddDays(1), out modification);

            //assert
            value.Should().Be.False();
            modification.Should().Not.Be.Null();
        }
Exemplo n.º 11
0
        public void GetDirectoryAttributeModification_FileTime_ReturnsFileTime()
        {
            //prepare
            var now = DateTime.Now;
            _mappingArguments.Getter = t => now;
            var propertyMapping = new DatePropertyMapping<DatePropertyMappingTest>(_mappingArguments, null);

            //act
            var value = propertyMapping.GetDirectoryAttributeModification(this);

            //assert
            value.Name.Should().Be.EqualTo(_mappingArguments.AttributeName);
            value.Operation.Should().Be.EqualTo(DirectoryAttributeOperation.Replace);
            value[0].Should().Be.EqualTo(now.ToFileTime().ToString());
        }
Exemplo n.º 12
0
        public void GetDirectoryAttributeModification_DateTime_ReturnsDateTime()
        {
            //prepare
            var now = DateTime.Now;
            _mappingArguments.Getter = t => now;
            var propertyMapping = new DatePropertyMapping<DatePropertyMappingTest>(_mappingArguments, ExtensionMethods.LdapFormat);

            //act
            var value = propertyMapping.GetDirectoryAttributeModification(this);

            //assert
            value.Name.Should().Be.EqualTo(_mappingArguments.AttributeName);
            value.Operation.Should().Be.EqualTo(DirectoryAttributeOperation.Replace);
            value[0].Should().Be.EqualTo(now.FormatLdapDateTime(ExtensionMethods.LdapFormat));
        }
        public IPropertyMapping ToPropertyMapping()
        {
            var type      = typeof(T);
            var arguments = new PropertyMappingArguments <T>
            {
                PropertyName  = PropertyInfo.Name,
                PropertyType  = PropertyInfo.PropertyType,
                AttributeName = AttributeName ?? PropertyInfo.Name.Replace('_', '-'),
                Getter        = DelegateBuilder.BuildGetter <T>(PropertyInfo),
                Setter        = !type.IsAnonymous()
                             ? DelegateBuilder.BuildSetter <T>(PropertyInfo)
                             : null,
                IsDistinguishedName = IsDistinguishedName,
                ReadOnly            = ReadOnlyConfiguration.GetValueOrDefault(ReadOnly.Never),
                DirectoryMappings   = _directoryMappings,
                InstanceMappings    = _instanceMappings
            };

            var mapping = new DatePropertyMapping <T>(arguments, DateTimeFormat);

            return(mapping);
        }
Exemplo n.º 14
0
        public virtual IPropertyMapping ToPropertyMapping()
        {
            IPropertyMapping mapping;

            var type      = typeof(T);
            var arguments = new PropertyMappingArguments <T>
            {
                PropertyName  = PropertyInfo.Name,
                PropertyType  = PropertyInfo.PropertyType,
                AttributeName = AttributeName ?? PropertyInfo.Name.Replace('_', '-'),
                Getter        = DelegateBuilder.BuildGetter <T>(PropertyInfo),
                Setter        = !type.IsAnonymous()
                                                 ? DelegateBuilder.BuildSetter <T>(PropertyInfo)
                                                 : null,
                IsDistinguishedName = IsDistinguishedName,
                ReadOnly            = IsDistinguishedName ? ReadOnly.Always : ReadOnlyConfiguration.GetValueOrDefault(ReadOnly.Never),
                DirectoryMappings   = null,
                InstanceMappings    = null
            };

            if (PropertyInfo.PropertyType == typeof(DateTime) || PropertyInfo.PropertyType == typeof(DateTime?))
            {
                mapping = new DatePropertyMapping <T>(arguments, DateTimeFormat);
            }
            else if (PropertyInfo.PropertyType.IsEnum || (Nullable.GetUnderlyingType(PropertyInfo.PropertyType) != null &&
                                                          Nullable.GetUnderlyingType(PropertyInfo.PropertyType).IsEnum))
            {
                mapping = new EnumPropertyMapping <T>(arguments, IsEnumStoredAsInt);
            }
            else if (PropertyInfo.PropertyType == typeof(byte[]))
            {
                mapping = new ByteArrayPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(Guid) || PropertyInfo.PropertyType == typeof(Guid?))
            {
                mapping = new GuidPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(string))
            {
                mapping = new StringPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(string[]))
            {
                mapping = new StringArrayPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(DateTime[]) || PropertyInfo.PropertyType == typeof(DateTime?[]))
            {
                mapping = new DateArrayPropertyMapping <T>(arguments, DateTimeFormat);
            }
            else if (PropertyInfo.PropertyType == typeof(ICollection <DateTime>) || PropertyInfo.PropertyType == typeof(Collection <DateTime>) ||
                     PropertyInfo.PropertyType == typeof(ICollection <DateTime?>) || PropertyInfo.PropertyType == typeof(Collection <DateTime?>))
            {
                mapping = new DateCollectionPropertyMapping <T>(arguments, DateTimeFormat);
            }
            else if (PropertyInfo.PropertyType == typeof(Collection <string>) || PropertyInfo.PropertyType == typeof(ICollection <string>))
            {
                mapping = new StringCollectionPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(Collection <byte[]>) || PropertyInfo.PropertyType == typeof(ICollection <byte[]>))
            {
                mapping = new ByteArrayCollectionPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(bool) || PropertyInfo.PropertyType == typeof(bool?))
            {
                mapping = new BooleanPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(SecurityIdentifier))
            {
                mapping = new SecurityIdentifierPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(SecurityIdentifier[]))
            {
                mapping = new SecurityIdentifierArrayPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(ICollection <SecurityIdentifier>) || PropertyInfo.PropertyType == typeof(Collection <SecurityIdentifier>))
            {
                mapping = new SecurityIdentifierCollectionPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(X509Certificate2) || PropertyInfo.PropertyType == typeof(X509Certificate))
            {
                mapping = new X509Certificate2PropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(byte[][]))
            {
                mapping = new ByteArrayArrayPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(ICollection <X509Certificate>) || PropertyInfo.PropertyType == typeof(Collection <X509Certificate>) ||
                     PropertyInfo.PropertyType == typeof(ICollection <X509Certificate2>) || PropertyInfo.PropertyType == typeof(Collection <X509Certificate2>))
            {
                mapping = new X509Certificate2CollectionPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(X509Certificate[]) || PropertyInfo.PropertyType == typeof(X509Certificate2[]))
            {
                mapping = new X509Certificate2ArrayPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType.IsValueType || (Nullable.GetUnderlyingType(PropertyInfo.PropertyType) != null))
            {
                mapping = new NumericPropertyMapping <T>(arguments);
            }
            else if (typeof(IDirectoryAttributes).IsAssignableFrom(PropertyInfo.PropertyType))
            {
                mapping = new CatchAllPropertyMapping <T>(arguments);
            }
            else
            {
                throw new MappingException(string.Format("Type '{0}' could not be mapped.", PropertyInfo.PropertyType.FullName));
            }

            return(mapping);
        }