|
1 | 1 | /* |
2 | | - * Copyright 2016-2020 the original author or authors. |
| 2 | + * Copyright 2016-2022 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
20 | 20 | import java.util.ArrayList; |
21 | 21 | import java.util.List; |
22 | 22 |
|
| 23 | +import org.junit.Assert; |
23 | 24 | import org.junit.Test; |
24 | 25 |
|
25 | 26 | import org.springframework.batch.item.ExecutionContext; |
26 | 27 | import org.springframework.batch.item.file.FlatFileItemReader; |
| 28 | +import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; |
| 29 | +import org.springframework.batch.item.file.mapping.DefaultLineMapper; |
| 30 | +import org.springframework.batch.item.file.mapping.RecordFieldSetMapper; |
27 | 31 | import org.springframework.batch.item.file.separator.DefaultRecordSeparatorPolicy; |
| 32 | +import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor; |
28 | 33 | import org.springframework.batch.item.file.transform.DefaultFieldSet; |
29 | 34 | import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; |
30 | 35 | import org.springframework.batch.item.file.transform.FieldSet; |
@@ -442,6 +447,49 @@ public void testErrorMessageWhenNoLineTokenizerWasProvided() { |
442 | 447 | } |
443 | 448 | } |
444 | 449 |
|
| 450 | + @Test |
| 451 | + public void testSetupWithRecordTargetType() { |
| 452 | + // given |
| 453 | + record Person(int id, String name) { |
| 454 | + } |
| 455 | + |
| 456 | + // when |
| 457 | + FlatFileItemReader<Person> reader = new FlatFileItemReaderBuilder<Person>().name("personReader") |
| 458 | + .resource(getResource("1,foo")).targetType(Person.class).delimited().names("id", "name").build(); |
| 459 | + |
| 460 | + // then |
| 461 | + Object lineMapper = ReflectionTestUtils.getField(reader, "lineMapper"); |
| 462 | + Assert.assertNotNull(lineMapper); |
| 463 | + Assert.assertTrue(lineMapper instanceof DefaultLineMapper); |
| 464 | + Object fieldSetMapper = ReflectionTestUtils.getField(lineMapper, "fieldSetMapper"); |
| 465 | + Assert.assertNotNull(fieldSetMapper); |
| 466 | + Assert.assertTrue(fieldSetMapper instanceof RecordFieldSetMapper); |
| 467 | + } |
| 468 | + |
| 469 | + @Test |
| 470 | + public void testSetupWithClassTargetType() { |
| 471 | + // given |
| 472 | + class Person { |
| 473 | + |
| 474 | + int id; |
| 475 | + |
| 476 | + String name; |
| 477 | + |
| 478 | + } |
| 479 | + |
| 480 | + // when |
| 481 | + FlatFileItemReader<Person> reader = new FlatFileItemReaderBuilder<Person>().name("personReader") |
| 482 | + .resource(getResource("1,foo")).targetType(Person.class).delimited().names("id", "name").build(); |
| 483 | + |
| 484 | + // then |
| 485 | + Object lineMapper = ReflectionTestUtils.getField(reader, "lineMapper"); |
| 486 | + Assert.assertNotNull(lineMapper); |
| 487 | + Assert.assertTrue(lineMapper instanceof DefaultLineMapper); |
| 488 | + Object fieldSetMapper = ReflectionTestUtils.getField(lineMapper, "fieldSetMapper"); |
| 489 | + Assert.assertNotNull(fieldSetMapper); |
| 490 | + Assert.assertTrue(fieldSetMapper instanceof BeanWrapperFieldSetMapper); |
| 491 | + } |
| 492 | + |
445 | 493 | private Resource getResource(String contents) { |
446 | 494 | return new ByteArrayResource(contents.getBytes()); |
447 | 495 | } |
|
0 commit comments