コード例 #1
0
        public void EngineShouldCallTypeConverterWhenConverterAttributeIsPresent()
        {
            var converter = A.Fake<ITypeConverter>();
            A.CallTo(converter).WithReturnType<object>().Returns("foo");
            A.CallTo(converter).WithReturnType<bool>().Returns(true);

            var attribute = A.Fake<IFixedFieldSettings>();
            attribute.CallsTo(x => x.Index).Returns(1);
            attribute.CallsTo(x => x.Length).Returns(1);
            attribute.CallsTo(x => x.TypeConverter).Returns(converter);

            var properties = typeof (ConverterTestObject).GetProperties(BindingFlags.Instance | BindingFlags.Public).ToDictionary(info => info.Name);

            var container = new FieldsContainer<IFixedFieldSettingsContainer>();
            container.AddOrUpdate(properties["Foo"], new FixedFieldSettings(properties["Foo"], attribute));

            var descriptor = new LayoutDescriptorBase<IFixedFieldSettingsContainer>(container) {HasHeader = false};

            var engine = fileEngineFactory.GetEngine(descriptor);

            using (var stream = new MemoryStream())
            using (var writer = new StreamWriter(stream))
            {
                writer.WriteLine("A");
                writer.Flush();
                stream.Seek(0, SeekOrigin.Begin);
                // Capture first result to force enumerable to be iterated
                var result = engine.Read<ConverterTestObject>(stream).FirstOrDefault();
            }

            A.CallTo(converter).WithReturnType<object>().MustHaveHappened();
        }
コード例 #2
0
        public void GivenIHaveSpecificationForType(string type, Table table)
        {
            var targetType = Assembly
                .GetExecutingAssembly()
                .GetTypes()
                .FirstOrDefault(x => x.FullName.EndsWith(type));

            var properties = targetType
                .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .ToDictionary(info => info.Name);

            var typeMappings = table.CreateSet<FixedLengthTypeMapping>().ToArray();

            var container = new FieldsContainer<IFixedFieldSettingsContainer>();

            foreach (var typeMapping in typeMappings)
            {
                var propertyInfo = properties[typeMapping.Name];

                var settings = new FixedFieldSettingsConstructor(propertyInfo);

                settings.WithLength(typeMapping.Length);

                if (!string.IsNullOrEmpty(typeMapping.NullValue))
                {
                    settings.AllowNull(typeMapping.NullValue);
                }

                switch (typeMapping.Padding)
                {
                    case Padding.Right:
                        settings.WithRightPadding(typeMapping.PaddingCharElement);
                        break;
                    case Padding.Left:
                        settings.WithLeftPadding(typeMapping.PaddingCharElement);
                        break;
                }

                container.AddOrUpdate(propertyInfo, settings);
            }

            var descriptor = new LayoutDescriptorBase<IFixedFieldSettingsContainer>(container)
            {
                HasHeader = false
            };

            ScenarioContext.Current.Add(() => descriptor, descriptor);

        }
コード例 #3
0
        public FieldsContainerTests()
        {
            _fieldsContainer = new AutoOrderedFieldsContainer<FixedFieldSettings>();
            _testObject = new TestObject();

            _properties = new[]
            {
                ExpressionExtensions.GetPropertyInfo(() => _testObject.Id),
                ExpressionExtensions.GetPropertyInfo(() => _testObject.Description),
                ExpressionExtensions.GetPropertyInfo(() => _testObject.NullableInt)
            };

            _propertyInfo = ExpressionExtensions.GetPropertyInfo(() => _testObject.Description);

            _fieldSettings = new FixedFieldSettings(_propertyInfo);
        }
コード例 #4
0
        public IDelimitedLayoutDescriptor GetDescriptor <T>()
        {
            var container = new FieldsContainer <IDelimitedFieldSettingsContainer>();

            var fileMappingType = typeof(T);

            var fileAttribute = fileMappingType.GetAttribute <DelimitedFileAttribute>();

            if (fileAttribute == null)
            {
                throw new NotSupportedException(string.Format("Mapping type {0} should be marked with {1} attribute",
                                                              fileMappingType.Name,
                                                              typeof(DelimitedFileAttribute).Name));
            }

            var properties = fileMappingType.GetTypeDescription <DelimitedFieldAttribute>();

            foreach (var p in properties)
            {
                var settings = p.Attributes.FirstOrDefault() as IDelimitedFieldSettings;

                if (settings != null)
                {
                    container.AddOrUpdate(p.Property, new DelimitedFieldSettings(p.Property, settings));
                }
            }

            var descriptor = new DelimitedLayout <T>(new DelimitedFieldSettingsFactory(), container)
                             .WithDelimiter(fileAttribute.Delimiter)
                             .WithQuote(fileAttribute.Quotes);

            if (fileAttribute.HasHeader)
            {
                descriptor.WithHeader();
            }

            return(descriptor);
        }
コード例 #5
0
        public void EngineShouldCallTypeConverterWhenConverterAttributeIsPresent()
        {
            // a converter to convert "A" to "foo"
            var converter = A.Fake <ITypeConverter>();

            A.CallTo(() => converter.ConvertFromString("A")).Returns("foo");
            A.CallTo(() => converter.CanConvertFrom(typeof(string))).Returns(true);
            A.CallTo(() => converter.CanConvertTo(typeof(string))).Returns(true);

            // an attribute to assign the property
            var attribute = A.Fake <IDelimitedFieldSettings>();

            A.CallTo(() => attribute.Index).Returns(1);
            A.CallTo(() => attribute.TypeConverter).Returns(converter);

            // the properties of the class
            var properties = typeof(ConverterTestObject).GetProperties(BindingFlags.Instance | BindingFlags.Public).ToDictionary(info => info.Name);

            // assign the attribute to the Foo property
            var container = new FieldsContainer <IDelimitedFieldSettingsContainer>();

            container.AddOrUpdate(properties["Foo"], new DelimitedFieldSettings(properties["Foo"], attribute));

            var layout = new DelimitedLayout <ConverterTestObject>(new DelimitedFieldSettingsFactory(), container);
            var engine = _fileEngineFactory.GetEngine(layout);

            // write "A" to the stream and verify it is converted to "foo"
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                {
                    writer.WriteLine("A");
                    writer.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    // Capture first result to force enumerable to be iterated
                    var result = engine.Read <ConverterTestObject>(stream).FirstOrDefault();
                    Assert.Equal("foo", result.Foo);
                }
        }
コード例 #6
0
        public bool AggregateIfPossible(List <NotificationMessage> listMessages, FieldsContainer fields)
        {
            if (_aggregators == null || !fields.Fields.Any() || listMessages.Count == 0)
            {
                return(false);
            }

            var aggregationColumnNameList = PrepareAggregationColumnNameList(fields);

            if (!aggregationColumnNameList.Any())
            {
                return(false);
            }

            NotificationMessage message = null;

            foreach (var m in listMessages)
            {
                var aggregationColumnNameListCount = 0;

                aggregationColumnNameListCount += aggregationColumnNameList.Count(a => m.Fields.Any(x => x.Value.Equals(a.Value)));

                if (aggregationColumnNameListCount == aggregationColumnNameList.Count)
                {
                    message = m;
                    break;
                }
            }

            if (message == null)
            {
                return(false);
            }

            IncermenetCount(message);
            return(true);
        }
コード例 #7
0
        public void Validate_MessageMatchTheFiltersForLessOrEqualPattern_ReturnTrue()
        {
            var notificationFiltersValidator = new NotificationFiltersValidator();
            var message = new FieldsContainer();

            message.Add("Message", "110");

            var filters = new Filters
            {
                FilterParams = new[]
                {
                    new FilterInfo
                    {
                        ColumnName           = "Message",
                        Patterns             = new[] { "110" },
                        MatchPatternOperator = "<="
                    }
                }
            };

            notificationFiltersValidator.SetFilters(filters);

            notificationFiltersValidator.Validate(message).Should().BeTrue();
        }
コード例 #8
0
        public void FieldsContainer_CreateObject_ReturnNewObject()
        {
            var fieldsContainer = new FieldsContainer();

            fieldsContainer.Fields.Should().BeEmpty();
        }