Throwing error when TargetType and FieldSetMapper is provided

Co-authored-by: martinfrancois <f.martin@fastmail.com>
Signed-off-by: Patrick Baumgartner <patrick.baumgartner@42talents.com>
Signed-off-by: martinfrancois <f.martin@fastmail.com>
This commit is contained in:
Patrick Baumgartner
2025-06-13 15:25:49 +02:00
committed by Mahmoud Ben Hassine
parent 673cfe3013
commit c329b9fd9b
2 changed files with 18 additions and 0 deletions

View File

@@ -58,6 +58,8 @@ import org.springframework.util.StringUtils;
* @author Glenn Renfro
* @author Mahmoud Ben Hassine
* @author Drummond Dawson
* @author Patrick Baumgartner
* @author François Martin
* @since 4.0
* @see FlatFileItemReader
*/
@@ -459,6 +461,9 @@ public class FlatFileItemReaderBuilder<T> {
throw new IllegalStateException("No LineTokenizer implementation was provided.");
}
Assert.state(this.targetType == null || this.fieldSetMapper == null,
"Either a TargetType or FieldSetMapper can be set, can't be both.");
if (this.targetType != null || StringUtils.hasText(this.prototypeBeanName)) {
if (this.targetType != null && this.targetType.isRecord()) {
RecordFieldSetMapper<T> mapper = new RecordFieldSetMapper<>(this.targetType);

View File

@@ -56,6 +56,8 @@ import static org.junit.jupiter.api.Assertions.fail;
* @author Mahmoud Ben Hassine
* @author Drummond Dawson
* @author Glenn Renfro
* @author Patrick Baumgartner
* @author François Martin
*/
class FlatFileItemReaderBuilderTests {
@@ -563,6 +565,17 @@ class FlatFileItemReaderBuilderTests {
}
}
@Test
void testErrorWhenTargetTypeAndFieldSetMapperIsProvided() {
var builder = new FlatFileItemReaderBuilder<Foo>().name("fooReader")
.resource(getResource("1;2;3"))
.lineTokenizer(line -> new DefaultFieldSet(line.split(";")))
.targetType(Foo.class)
.fieldSetMapper(fieldSet -> new Foo());
var exception = assertThrows(IllegalStateException.class, builder::build);
assertEquals("Either a TargetType or FieldSetMapper can be set, can't be both.", exception.getMessage());
}
@Test
void testSetupWithRecordTargetType() {
// given