Пример #1
0
        public void FlatFileTestTransactionalTest()
        {
            var writer = new FlatFileItemWriter <Person>
            {
                Resource       = new FileSystemResource(_testPath2),
                LineAggregator = new LineAggregator(),
                HeaderWriter   = new HeaderWriter()
            };
            var reader = new FlatFileItemReader <Person>
            {
                Resource    = new FileSystemResource(_testPath2),
                LinesToSkip = 2,
                LineMapper  = new LineMapper()
            };

            var executionContext = new ExecutionContext();

            writer.Open(executionContext);
            try
            {
                for (int i = 0; i < CountTransactions; i++)
                {
                    using (TransactionScope scope = TransactionScopeManager.CreateScope())
                    {
                        writer.Write(GetPersons(i));
                        if (i == CountTransactions - 1) //SIMULATE FAILURE
                        {
                            throw new Exception("Bailing out ... should rollback ...");
                        }
                        scope.Complete();
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "An unexpected exception occured :");
            }
            writer.Close();

            var persons = new List <Person>();

            reader.Open(executionContext);
            Person person;

            while ((person = reader.Read()) != null)
            {
                persons.Add(person);
            }
            reader.Close();

            Assert.AreEqual((CountTransactions - 1) * CountObjects, persons.Count);

            for (var i = 0; i < persons.Count; i++)
            {
                Assert.AreEqual(i % CountObjects, persons[i].Id);
                Assert.IsTrue(persons[i].Name.StartsWith("Person" + i % CountObjects));
            }
        }
Пример #2
0
        public void MapIsCalledAsManyTimesAsChunkSize(int chunkSize, int expected)
        {
            var fileService = new Mock<IFileService>();
            fileService.Setup(f => f.ReadLines(0, chunkSize)).Returns(Enumerable.Range(0, chunkSize).Select(s => "item read"));

            var itemReader = new FlatFileItemReader<string>(_lineMapper.Object, fileService.Object);

            itemReader.Read(0, chunkSize);

            _lineMapper.Verify(m => m.MapToModel(It.IsAny<string>()), Times.Exactly(expected));
        }
Пример #3
0
        public void LineMapperIsNotCalledIfItemIsNullOrEmpty()
        {
            var fileService = new Mock<IFileService>();
            fileService.Setup(f => f.ReadLines(0, 1)).Returns(Enumerable.Empty<string>());

            var itemReader = new FlatFileItemReader<string>(_lineMapper.Object, fileService.Object);

            itemReader.Read(0, 1);

            _lineMapper.Verify(m => m.MapToModel(It.IsAny<string>()), Times.Never());
        }
Пример #4
0
        public void LineMapperIsNotCalledIfItemIsNullOrEmpty()
        {
            var fileService = new Mock <IFileService>();

            fileService.Setup(f => f.ReadLines(0, 1)).Returns(Enumerable.Empty <string>());

            var itemReader = new FlatFileItemReader <string>(_lineMapper.Object, fileService.Object);

            itemReader.Read(0, 1);

            _lineMapper.Verify(m => m.MapToModel(It.IsAny <string>()), Times.Never());
        }
Пример #5
0
        public void MapIsCalledAsManyTimesAsChunkSize(int chunkSize, int expected)
        {
            var fileService = new Mock <IFileService>();

            fileService.Setup(f => f.ReadLines(0, chunkSize)).Returns(Enumerable.Range(0, chunkSize).Select(s => "item read"));

            var itemReader = new FlatFileItemReader <string>(_lineMapper.Object, fileService.Object);

            itemReader.Read(0, chunkSize);

            _lineMapper.Verify(m => m.MapToModel(It.IsAny <string>()), Times.Exactly(expected));
        }
Пример #6
0
        public void SkipsLinesOnlyWhenReadingForFirstTime(int linesToSkip, int expected)
        {
            const int CHUNK_SIZE = 10;
            var fileService = new Mock<IFileService>();
            fileService.Setup(f => f.ReadLines(0, CHUNK_SIZE)).Returns(Enumerable.Range(0, CHUNK_SIZE).Select(s => "item read"));
            var itemReader = new FlatFileItemReader<string>(_lineMapper.Object, fileService.Object)
                             {
                                 LinesToSkip = linesToSkip
                             };

            itemReader.Read(0, CHUNK_SIZE);

            _lineMapper.Verify(m => m.MapToModel(It.IsAny<string>()), Times.Exactly(expected));
        }
Пример #7
0
        public void FlatFileTestTest()
        {
            if (System.IO.File.Exists(_testPath))
            {
                System.IO.File.Delete(_testPath);
            }

            Assert.IsFalse(System.IO.File.Exists(_testPath));

            var writer = new FlatFileItemWriter <Person>
            {
                Resource       = new FileSystemResource(_testPath),
                LineAggregator = new LineAggregator(),
                HeaderWriter   = new HeaderWriter()
            };
            var reader = new FlatFileItemReader <Person>
            {
                Resource    = new FileSystemResource(_testPath),
                LinesToSkip = 2,
                LineMapper  = new LineMapper()
            };

            var executionContext = new ExecutionContext();

            writer.Open(executionContext);
            writer.Write(GetPersons());
            writer.Close();

            Assert.IsTrue(System.IO.File.Exists(_testPath));
            Assert.IsTrue(new FileInfo(_testPath).Length > 0);

            var persons = new List <Person>();

            reader.Open(executionContext);
            Person person;

            while ((person = reader.Read()) != null)
            {
                persons.Add(person);
            }
            reader.Close();

            Assert.AreEqual(CountObjects, persons.Count);

            for (var i = 0; i < CountObjects; i++)
            {
                Assert.AreEqual(i, persons[i].Id);
                Assert.AreEqual("Person" + i, persons[i].Name);
            }
        }
Пример #8
0
        public void SkipsLinesOnlyWhenReadingForFirstTime(int linesToSkip, int expected)
        {
            const int CHUNK_SIZE  = 10;
            var       fileService = new Mock <IFileService>();

            fileService.Setup(f => f.ReadLines(0, CHUNK_SIZE)).Returns(Enumerable.Range(0, CHUNK_SIZE).Select(s => "item read"));
            var itemReader = new FlatFileItemReader <string>(_lineMapper.Object, fileService.Object)
            {
                LinesToSkip = linesToSkip
            };

            itemReader.Read(0, CHUNK_SIZE);

            _lineMapper.Verify(m => m.MapToModel(It.IsAny <string>()), Times.Exactly(expected));
        }
Пример #9
0
        public void FlatFileTestRestart()
        {
            System.IO.File.Copy("TestData/FlatFile/output/Test.txt", _testpath3, true);
            IResource fileResource = new FileSystemResource(new FileInfo(_testpath3));

            Assert.IsTrue(fileResource.Exists());

            var writer = new FlatFileItemWriter <Person>
            {
                Resource       = new FileSystemResource(_testpath3),
                LineAggregator = new LineAggregator(),
                HeaderWriter   = new HeaderWriter()
            };
            var reader = new FlatFileItemReader <Person>
            {
                Resource    = new FileSystemResource(_testpath3),
                LinesToSkip = 2,
                LineMapper  = new LineMapper()
            };

            var executionContext = new ExecutionContext();

            executionContext.PutLong("FlatFileItemWriter.current.count", 133L); // Last position in Test.txt
            executionContext.PutLong("FlatFileItemWriter.written", 10L);        // Record already written in Test.txt

            writer.Open(executionContext);
            writer.Write(GetPersons());
            writer.Close();

            Assert.IsTrue(System.IO.File.Exists(_testpath3));
            Assert.IsTrue(new FileInfo(_testpath3).Length > 0);

            var persons = new List <Person>();

            reader.Open(executionContext);
            Person person;

            while ((person = reader.Read()) != null)
            {
                persons.Add(person);
            }
            reader.Close();

            Assert.AreEqual(CountObjects + 10, persons.Count); // The final record number must be 20+10.
        }