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)); } }
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); } }
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. }