Implementation of T:IResource for a file in the file system.
Inheritance: Summer.Batch.Common.IO.AbstractResource
        public void TestGetFileInfo2()
        {
            var resource = new FileSystemResource(TestDataDirectory);
            var fileInfo = resource.GetFileInfo();

            Assert.IsNotNull(fileInfo);
            Assert.AreEqual("TestData", fileInfo.Name);
        }
示例#2
0
        public void EbcdicFileTestTest()
        {
            // local resources
            IResource fileResource = new FileSystemResource(new FileInfo("C:/temp/outputs/PersonWritten.txt"));
            var executionContext = new ExecutionContext();
            FileInfo fileInfo = new FileInfo("Ebcdic/Resources/copybooks/Person.fileformat");

            IResource copybookResource = new FileSystemResource(fileInfo);

            //1. WRITE
            EbcdicWriterMapper writerMapper = new EbcdicWriterMapper();
            writerMapper.AfterPropertiesSet();

            var writer = new EbcdicFileWriter<Ebcdic.Test.Person>
            {
                AppendAllowed = false,
                WriteRdw = false,
                Name = "PersonWriter",
                Resource = fileResource,
                EbcdicWriterMapper = writerMapper,
                DefaultValue = EbcdicEncoder.DefaultValue.LowValue,
                Copybooks = new List<IResource> {new FileSystemResource(fileInfo)}
            };


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

            //2.READ WHAT WAS WRITTEN
            var reader = new EbcdicFileReader<Ebcdic.Test.Person>
            {
                EbcdicReaderMapper = new PersonMapper(),
                Rdw = false,
                Resource = fileResource,
                Name = "PersonReader",
                SaveState = false,
                Copybook = copybookResource
            };
            reader.AfterPropertiesSet();

            var persons = new List<Ebcdic.Test.Person>();
            reader.Open(executionContext);
            Ebcdic.Test.Person person;
            while ((person = reader.Read()) != null)
            {
                persons.Add(person);
            }
            reader.Close();

            Assert.AreEqual(CountObject,persons.Count );
            foreach (Ebcdic.Test.Person p in persons)
            {
                Assert.AreEqual(p.Id, p.Value);
                Assert.IsTrue(p.Name.StartsWith("Name_" + p.Id));
            }
        }
        /// <summary>
        /// Registers the artifacts required to execute the steps (tasklets, readers, writers, etc.)
        /// </summary>
        /// <param name="container">the unity container to use for registrations</param>
        public override void LoadArtifacts(IUnityContainer container)
        {
            //Connection string
            var writerConnectionstring = ConfigurationManager.ConnectionStrings["Default"];

            //input file
            var inputFileResource = new FileSystemResource("data/input/LargeFlatFile.txt");

            // Reader - FlatFileReader/FlatFileReader
            container.StepScopeRegistration<IItemReader<FlatFileRecord>, FlatFileItemReader<FlatFileRecord>>("FlatFileReader/FlatFileReader")
                .Property("Resource").Value(inputFileResource)
                .Property("Encoding").Value(Encoding.GetEncoding("UTF-8"))
                .Property("LineMapper").Reference<ILineMapper<FlatFileRecord>>("FlatFileReader/FlatFileReader/LineMapper")
                .Register();

            // Line mapper
            container.StepScopeRegistration<ILineMapper<FlatFileRecord>, DefaultLineMapper<FlatFileRecord>>("FlatFileReader/FlatFileReader/LineMapper")
                .Property("Tokenizer").Reference<ILineTokenizer>("FlatFileReader/FlatFileReader/Tokenizer")
                .Property("FieldSetMapper").Reference<IFieldSetMapper<FlatFileRecord>>("FlatFileReader/FlatFileReader/FieldSetMapper")
                .Register();

            // Tokenizer
            container.StepScopeRegistration<ILineTokenizer, DelimitedLineTokenizer>("FlatFileReader/FlatFileReader/Tokenizer")
                .Property("Delimiter").Value(";")
                .Register();

            // Field set mapper
            container.RegisterStepScope<IFieldSetMapper<FlatFileRecord>, FlatFileRecordMapper>("FlatFileReader/FlatFileReader/FieldSetMapper");

            // Processor - FlatFileReader/Processor
            container.RegisterStepScope<IItemProcessor<FlatFileRecord, FlatFileRecord>,FlatFileRecordProcessor >("FlatFileReader/Processor");

            // Writer - FlatFileReader/DatabaseWriter
            container.StepScopeRegistration<IItemWriter<FlatFileRecord>, DatabaseBatchItemWriter<FlatFileRecord>>("FlatFileReader/DatabaseWriter")
                .Property("ConnectionString").Instance(writerConnectionstring)
                .Property("Query").Value("INSERT INTO BA_FLATFILE_READER_TABLE (CODE,NAME,DESCRIPTION,DATE) VALUES (:code,:name,:description,:date)")
                .Property("DbParameterSourceProvider").Reference<PropertyParameterSourceProvider<FlatFileRecord>>()
                .Register();
        }
示例#4
0
        public void EbcdicFileTestRestart() {
            // local resources
            System.IO.File.Copy("Ebcdic/Resources/outputs/simple.txt", "C:/temp/outputs/SimpleWritten.txt", true);
            IResource fileResource = new FileSystemResource(new FileInfo("C:/temp/outputs/SimpleWritten.txt"));
            var executionContext = new ExecutionContext();
            executionContext.PutLong("EbcdicFileWriter.current.count",21L);
            executionContext.PutLong("EbcdicFileWriter.written", 1L);
            FileInfo fileInfo = new FileInfo("Ebcdic/Resources/copybooks/simple.fileformat");
            
            IResource copybookResource = new FileSystemResource(fileInfo);

            //1. WRITE
            EbcdicWriterMapper writerMapper = new EbcdicWriterMapper();
            writerMapper.AfterPropertiesSet();

            var writer = new EbcdicFileWriter<Simple>
            {
                AppendAllowed = false,
                WriteRdw = false,
                Resource = fileResource,
                EbcdicWriterMapper = writerMapper,
                DefaultValue = EbcdicEncoder.DefaultValue.LowValue,
                DeleteIfExist = true
            };


            var mySimple = new Simple() { LongValue = 12, FloatValue = 12.5f, Date = DateTime.Now, BooleanValue = true };
            List<Simple> simpleList = new List<Simple> { mySimple };

            writer.Copybooks = new List<IResource> { new FileSystemResource(fileInfo) };

            writer.AfterPropertiesSet();
            writer.Open(executionContext);
            writer.Write(simpleList);
            writer.Close();

            //2.READ WHAT WAS WRITTEN
            var reader = new EbcdicFileReader<Simple>
            {
                EbcdicReaderMapper = new SimpleEbcdicMapper(),
                Rdw = false,
                Resource = fileResource,
                Name = "PersonReader",
                SaveState = false,
                Copybook = copybookResource
            };
            reader.AfterPropertiesSet();

            var simples = new List<Simple>();
            reader.Open(executionContext);
            Simple simple;
            while ((simple = reader.Read()) != null)
            {
                simples.Add(simple);
            }
            reader.Close();

            /*foreach (Ebcdic.Test.Person p in simple)
            {
                Assert.AreEqual(p.Id, p.Value);
                Assert.IsTrue(p.Name.StartsWith("Name_" + p.Id));
            }*/
        }
示例#5
0
        public void EbcdicFileTestTransactionalWrite()
        {
            // local resources
            IResource fileResource = new FileSystemResource(new FileInfo("C:/temp/outputs/PersonWritten.txt"));
            var executionContext = new ExecutionContext();
            FileInfo fileInfo = new FileInfo("Ebcdic/Resources/copybooks/Person.fileformat");

            IResource copybookResource = new FileSystemResource(fileInfo);

            //1. WRITE
            EbcdicWriterMapper writerMapper = new EbcdicWriterMapper();
            writerMapper.AfterPropertiesSet();

            var writer = new EbcdicFileWriter<Ebcdic.Test.Person>
            {
                AppendAllowed = false,
                WriteRdw = false,
                Name = "PersonWriter",
                Resource = fileResource,
                EbcdicWriterMapper = writerMapper,
                DefaultValue = EbcdicEncoder.DefaultValue.LowValue,
                Copybooks = new List<IResource> {new FileSystemResource(fileInfo)}
            };

            writer.AfterPropertiesSet();
            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)
            {
               // DISCARDED (JUST TO AVOID UNIT TEST FAILURE ...)
            }
            writer.Close();

            Assert.IsTrue(System.IO.File.Exists("C:/temp/outputs/PersonWritten.txt"));
            Assert.IsTrue(new FileInfo("C:/temp/outputs/PersonWritten.txt").Length > 0);

            //2.READ WHAT WAS WRITTEN
            var reader = new EbcdicFileReader<Ebcdic.Test.Person>
            {
                EbcdicReaderMapper = new PersonMapper(),
                Rdw = false,
                Resource = fileResource,
                Name = "PersonReader",
                SaveState = false,
                Copybook = copybookResource
            };
            reader.AfterPropertiesSet();

            var persons = new List<Ebcdic.Test.Person>();
            reader.Open(executionContext);
            Ebcdic.Test.Person person;
            while ((person = reader.Read()) != null)
            {
                persons.Add(person);
            }
            reader.Close();

            Assert.AreEqual(CountObject*(CountTransactions-1), persons.Count);
            foreach (Ebcdic.Test.Person p in persons)
            {
                Assert.AreEqual(p.Id, p.Value);
                Assert.IsTrue(p.Name.StartsWith("Name_" + p.Id));
            }
        }
示例#6
0
        public void EbcdicFileTestTest2()
        {
            // local resources
            IResource fileResource = new FileSystemResource(new FileInfo("C:/temp/outputs/CustomerWritten.txt"));
            var executionContext = new ExecutionContext();
            FileInfo fileInfo = new FileInfo("Ebcdic/Resources/copybooks/Customer.fileformat");

            IResource copybookResource = new FileSystemResource(fileInfo);

            //1. WRITE
            EbcdicWriterMapper writerMapper = new EbcdicWriterMapper();
            writerMapper.AfterPropertiesSet();

            var writer = new EbcdicFileWriter<Customer>
            {
                AppendAllowed = false,
                WriteRdw = false,
                Name = "CustomerWriter",
                Resource = fileResource,
                EbcdicWriterMapper = writerMapper,
                DefaultValue = EbcdicEncoder.DefaultValue.LowValue,
                Copybooks = new List<IResource> {new FileSystemResource(fileInfo)}
            };


            writer.AfterPropertiesSet();
            writer.Open(executionContext);
            writer.Write(GetCustomers());
            writer.Close();

            //2.READ WHAT WAS WRITTEN
            var reader = new EbcdicFileReader<Customer>
            {
                EbcdicReaderMapper = new CustomerEbcdicMapper(),
                Rdw = false,
                Resource = fileResource,
                Name = "CustomerReader",
                SaveState = false,
                Copybook = copybookResource
            };
            reader.AfterPropertiesSet();

            var customers = new List<Customer>();
            reader.Open(executionContext);
            Customer customer;
            while ((customer = reader.Read()) != null)
            {
                customers.Add(customer);
            }
            reader.Close();

            Assert.AreEqual(CountObject,customers.Count);

        }
 public void TestExists2()
 {
     var resource = new FileSystemResource(Path.Combine(TestDataDirectory, "NonExistingFile"));
     Assert.IsFalse(resource.Exists());
 }
 public void TestDirectory()
 {
     var testDir = new FileSystemResource(TestDataDirectory);
     Assert.IsTrue(testDir.GetFileInfo().Attributes.HasFlag(FileAttributes.Directory));
     Assert.IsFalse(testDir.GetFileInfo().Attributes.HasFlag(FileAttributes.Normal));
 }