/** * Test should read succesfully. * * @throws Exception */ @Test public void testSuccessfulReading() throws Exception { // init linetokenizer DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer(); lineTokenizer.setNames(new String[] {"id", "value"}); // init linemapper DefaultLineMapper<FieldSet> lineMapper = new DefaultLineMapper<FieldSet>(); lineMapper.setLineTokenizer(lineTokenizer); lineMapper.setFieldSetMapper(new PassThroughFieldSetMapper()); // init reader reader.setLineMapper(lineMapper); reader.setResource(new FileSystemResource(INPUT_FILE)); // open, provide "mock" ExecutionContext reader.open(MetaDataInstanceFactory.createStepExecution().getExecutionContext()); // read try { int count = 0; FieldSet line; while ((line = reader.read()) != null) { // really test for the fieldSet names and values assertEquals("id", line.getNames()[0]); assertEquals(String.valueOf(count), line.getValues()[0]); assertEquals("value", line.getNames()[1]); // csv contains entry like '0,foo0' assertEquals("foo" + String.valueOf(count), line.getValues()[1]); count++; } assertEquals(EXPECTED_COUNT, count); } catch (Exception e) { throw e; } finally { reader.close(); } }
private LineMapper<Product> productLineMapper() { DefaultLineMapper<Product> bean = new DefaultLineMapper<Product>(); DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(); tokenizer.setDelimiter(DelimitedLineTokenizer.DELIMITER_COMMA); tokenizer.setNames(new String[] {"productId", "store", "quantity", "description"}); bean.setLineTokenizer(tokenizer); bean.setFieldSetMapper(new ProductFieldSetMapper()); bean.afterPropertiesSet(); return bean; }
@Bean public LineMapper<Partner> lineMapper() { DefaultLineMapper<Partner> lineMapper = new DefaultLineMapper<Partner>(); DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer(); lineTokenizer.setNames(new String[] {"name", "email", "gender"}); lineTokenizer.setIncludedFields(new int[] {0, 2, 3}); BeanWrapperFieldSetMapper<Partner> fieldSetMapper = new BeanWrapperFieldSetMapper<Partner>(); fieldSetMapper.setTargetType(Partner.class); lineMapper.setLineTokenizer(lineTokenizer); lineMapper.setFieldSetMapper(fieldSetMapper); return lineMapper; }
@Bean DefaultLineMapper<Contact> lineMapper() { DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(); tokenizer.setNames("fullName,email".split(",")); BeanWrapperFieldSetMapper<Contact> mapper = new BeanWrapperFieldSetMapper<>(); mapper.setTargetType(Contact.class); DefaultLineMapper<Contact> defaultLineMapper = new DefaultLineMapper<>(); defaultLineMapper.setFieldSetMapper(mapper); defaultLineMapper.setLineTokenizer(tokenizer); return defaultLineMapper; }
@Bean public FlatFileItemReader<StockWithinDepot> reader( final FieldSetMapper<StockWithinDepot> fieldSetMapper) { final FlatFileItemReader<StockWithinDepot> reader = new FlatFileItemReader<StockWithinDepot>(); reader.setLinesToSkip(1); reader.setResource(new ClassPathResource(STOCK_DATA_CSV)); final DefaultLineMapper<StockWithinDepot> defaultLineMapper = new DefaultLineMapper<>(); defaultLineMapper.setFieldSetMapper(fieldSetMapper); final DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(PortfolioConstants.CSV_SEPERATOR); tokenizer.setNames(NAMES); defaultLineMapper.setLineTokenizer(tokenizer); reader.setLineMapper(defaultLineMapper); return reader; }
/** Méthode permetant l'initialisation du lecteur */ public void init() { // REMAQUE : Il est possible d'instancier le delegate par configuration XML delegate = new FlatFileItemReader<FieldSet>(); // passage de la ressource au reader delegate.setResource(resource); // Création d'un line Mapper DefaultLineMapper<FieldSet> defaultLineMapper = new DefaultLineMapper<FieldSet>(); // délimiter par défaut defaultLineMapper.setLineTokenizer(new DelimitedLineTokenizer()); // fieldMapper par défaut defaultLineMapper.setFieldSetMapper(new PassThroughFieldSetMapper()); // Ajout du line mapper delegate.setLineMapper(defaultLineMapper); }