|
| 1 | +/* |
| 2 | + * Copyright 2006-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.batch.item; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.springframework.batch.item.support.CompositeItemWriter; |
| 21 | +import org.springframework.batch.item.support.ListItemWriter; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.springframework.batch.item.ItemWriter.mapping; |
| 27 | + |
| 28 | +class ItemWriterIntegrationTests { |
| 29 | + |
| 30 | + @Test |
| 31 | + void testMappingWithCompositeItemWriter() throws Exception { |
| 32 | + ListItemWriter<String> nameItemWriter = new ListItemWriter<>(); |
| 33 | + ListItemWriter<Integer> ageItemWriter = new ListItemWriter<>(); |
| 34 | + |
| 35 | + record Person(String name, int age) { |
| 36 | + } |
| 37 | + |
| 38 | + ItemWriter<Person> personItemWriter = new CompositeItemWriter<>(List.of( // |
| 39 | + mapping(Person::name, nameItemWriter), // |
| 40 | + mapping(Person::age, ageItemWriter))); |
| 41 | + |
| 42 | + personItemWriter.write(Chunk.of(new Person("Foo", 42), new Person("Bar", 24))); |
| 43 | + personItemWriter.write(Chunk.of(new Person("Baz", 21), new Person("Qux", 12))); |
| 44 | + |
| 45 | + assertThat(nameItemWriter.getWrittenItems()).containsExactly("Foo", "Bar", "Baz", "Qux"); |
| 46 | + assertThat(ageItemWriter.getWrittenItems()).containsExactly(42, 24, 21, 12); |
| 47 | + } |
| 48 | + |
| 49 | +} |
0 commit comments