Skip to content

Commit

Permalink
Configure the right FieldSetMapper based on the type of items in Flat…
Browse files Browse the repository at this point in the history
…FileItemReaderBuilder

Resolves #4160
  • Loading branch information
fmbenhassine committed Jul 20, 2022
1 parent 9a8fb1e commit 4d46d58
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,7 @@
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.mapping.RecordFieldSetMapper;
import org.springframework.batch.item.file.separator.RecordSeparatorPolicy;
import org.springframework.batch.item.file.separator.SimpleRecordSeparatorPolicy;
import org.springframework.batch.item.file.transform.DefaultFieldSetFactory;
Expand Down Expand Up @@ -459,21 +460,26 @@ else if (this.delimitedBuilder != null) {
}

if (this.targetType != null || StringUtils.hasText(this.prototypeBeanName)) {
BeanWrapperFieldSetMapper<T> mapper = new BeanWrapperFieldSetMapper<>();
mapper.setTargetType(this.targetType);
mapper.setPrototypeBeanName(this.prototypeBeanName);
mapper.setStrict(this.beanMapperStrict);
mapper.setBeanFactory(this.beanFactory);
mapper.setDistanceLimit(this.distanceLimit);
mapper.setCustomEditors(this.customEditors);
try {
mapper.afterPropertiesSet();
if (this.targetType != null && this.targetType.isRecord()) {
RecordFieldSetMapper<T> mapper = new RecordFieldSetMapper(this.targetType);
lineMapper.setFieldSetMapper(mapper);
}
catch (Exception e) {
throw new IllegalStateException("Unable to initialize BeanWrapperFieldSetMapper", e);
else {
BeanWrapperFieldSetMapper<T> mapper = new BeanWrapperFieldSetMapper<>();
mapper.setTargetType(this.targetType);
mapper.setPrototypeBeanName(this.prototypeBeanName);
mapper.setStrict(this.beanMapperStrict);
mapper.setBeanFactory(this.beanFactory);
mapper.setDistanceLimit(this.distanceLimit);
mapper.setCustomEditors(this.customEditors);
try {
mapper.afterPropertiesSet();
lineMapper.setFieldSetMapper(mapper);
}
catch (Exception e) {
throw new IllegalStateException("Unable to initialize BeanWrapperFieldSetMapper", e);
}
}

lineMapper.setFieldSetMapper(mapper);
}
else if (this.fieldSetMapper != null) {
lineMapper.setFieldSetMapper(this.fieldSetMapper);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,11 +20,16 @@
import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.RecordFieldSetMapper;
import org.springframework.batch.item.file.separator.DefaultRecordSeparatorPolicy;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.DefaultFieldSet;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
Expand Down Expand Up @@ -442,6 +447,49 @@ public void testErrorMessageWhenNoLineTokenizerWasProvided() {
}
}

@Test
public void testSetupWithRecordTargetType() {
// given
record Person(int id, String name) {
}

// when
FlatFileItemReader<Person> reader = new FlatFileItemReaderBuilder<Person>().name("personReader")
.resource(getResource("1,foo")).targetType(Person.class).delimited().names("id", "name").build();

// then
Object lineMapper = ReflectionTestUtils.getField(reader, "lineMapper");
Assert.assertNotNull(lineMapper);
Assert.assertTrue(lineMapper instanceof DefaultLineMapper);
Object fieldSetMapper = ReflectionTestUtils.getField(lineMapper, "fieldSetMapper");
Assert.assertNotNull(fieldSetMapper);
Assert.assertTrue(fieldSetMapper instanceof RecordFieldSetMapper);
}

@Test
public void testSetupWithClassTargetType() {
// given
class Person {

int id;

String name;

}

// when
FlatFileItemReader<Person> reader = new FlatFileItemReaderBuilder<Person>().name("personReader")
.resource(getResource("1,foo")).targetType(Person.class).delimited().names("id", "name").build();

// then
Object lineMapper = ReflectionTestUtils.getField(reader, "lineMapper");
Assert.assertNotNull(lineMapper);
Assert.assertTrue(lineMapper instanceof DefaultLineMapper);
Object fieldSetMapper = ReflectionTestUtils.getField(lineMapper, "fieldSetMapper");
Assert.assertNotNull(fieldSetMapper);
Assert.assertTrue(fieldSetMapper instanceof BeanWrapperFieldSetMapper);
}

private Resource getResource(String contents) {
return new ByteArrayResource(contents.getBytes());
}
Expand Down

0 comments on commit 4d46d58

Please sign in to comment.