|
| 1 | +/* |
| 2 | + * Copyright 2023 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 | +package org.springframework.batch.experimental.item.support; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | + |
| 20 | +import javax.sql.DataSource; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Assertions; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import org.springframework.batch.core.ExitStatus; |
| 26 | +import org.springframework.batch.core.Job; |
| 27 | +import org.springframework.batch.core.JobExecution; |
| 28 | +import org.springframework.batch.core.JobParameters; |
| 29 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 30 | +import org.springframework.batch.core.job.builder.JobBuilder; |
| 31 | +import org.springframework.batch.core.launch.JobLauncher; |
| 32 | +import org.springframework.batch.core.repository.JobRepository; |
| 33 | +import org.springframework.batch.core.step.builder.StepBuilder; |
| 34 | +import org.springframework.batch.item.database.JdbcBatchItemWriter; |
| 35 | +import org.springframework.batch.item.database.JdbcCursorItemReader; |
| 36 | +import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder; |
| 37 | +import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder; |
| 38 | +import org.springframework.batch.item.file.FlatFileItemReader; |
| 39 | +import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; |
| 40 | +import org.springframework.context.ApplicationContext; |
| 41 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 42 | +import org.springframework.context.annotation.Bean; |
| 43 | +import org.springframework.context.annotation.Configuration; |
| 44 | +import org.springframework.core.io.ClassPathResource; |
| 45 | +import org.springframework.jdbc.core.DataClassRowMapper; |
| 46 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 47 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 48 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 49 | +import org.springframework.jdbc.support.JdbcTransactionManager; |
| 50 | +import org.springframework.test.jdbc.JdbcTestUtils; |
| 51 | + |
| 52 | +public class CompositeItemReaderIntegrationTests { |
| 53 | + |
| 54 | + record Person(int id, String name) { |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testCompositeItemReader() throws Exception { |
| 59 | + // given |
| 60 | + ApplicationContext context = new AnnotationConfigApplicationContext(JobConfiguration.class); |
| 61 | + JobLauncher jobLauncher = context.getBean(JobLauncher.class); |
| 62 | + Job job = context.getBean(Job.class); |
| 63 | + |
| 64 | + // when |
| 65 | + JobExecution jobExecution = jobLauncher.run(job, new JobParameters()); |
| 66 | + |
| 67 | + // then |
| 68 | + Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); |
| 69 | + JdbcTemplate jdbcTemplate = new JdbcTemplate(context.getBean(DataSource.class)); |
| 70 | + int personsCount = JdbcTestUtils.countRowsInTable(jdbcTemplate, "person_target"); |
| 71 | + Assertions.assertEquals(6, personsCount); |
| 72 | + } |
| 73 | + |
| 74 | + @Configuration |
| 75 | + @EnableBatchProcessing |
| 76 | + static class JobConfiguration { |
| 77 | + |
| 78 | + @Bean |
| 79 | + public FlatFileItemReader<Person> itemReader1() { |
| 80 | + return new FlatFileItemReaderBuilder<Person>() |
| 81 | + .name("personItemReader1") |
| 82 | + .resource(new ClassPathResource("persons1.csv")) |
| 83 | + .delimited() |
| 84 | + .names("id", "name") |
| 85 | + .targetType(Person.class) |
| 86 | + .build(); |
| 87 | + } |
| 88 | + |
| 89 | + @Bean |
| 90 | + public FlatFileItemReader<Person> itemReader2() { |
| 91 | + return new FlatFileItemReaderBuilder<Person>() |
| 92 | + .name("personItemReader2") |
| 93 | + .resource(new ClassPathResource("persons2.csv")) |
| 94 | + .delimited() |
| 95 | + .names("id", "name") |
| 96 | + .targetType(Person.class) |
| 97 | + .build(); |
| 98 | + } |
| 99 | + |
| 100 | + @Bean |
| 101 | + public JdbcCursorItemReader<Person> itemReader3() { |
| 102 | + String sql = "select * from person_source"; |
| 103 | + return new JdbcCursorItemReaderBuilder<Person>() |
| 104 | + .name("personItemReader3") |
| 105 | + .dataSource(dataSource()) |
| 106 | + .sql(sql) |
| 107 | + .rowMapper(new DataClassRowMapper<>(Person.class)) |
| 108 | + .build(); |
| 109 | + } |
| 110 | + |
| 111 | + @Bean |
| 112 | + public CompositeItemReader<Person> itemReader() { |
| 113 | + return new CompositeItemReader<>(Arrays.asList(itemReader1(), itemReader2(), itemReader3())); |
| 114 | + } |
| 115 | + |
| 116 | + @Bean |
| 117 | + public JdbcBatchItemWriter<Person> itemWriter() { |
| 118 | + String sql = "insert into person_target (id, name) values (:id, :name)"; |
| 119 | + return new JdbcBatchItemWriterBuilder<Person>() |
| 120 | + .dataSource(dataSource()) |
| 121 | + .sql(sql) |
| 122 | + .beanMapped() |
| 123 | + .build(); |
| 124 | + } |
| 125 | + |
| 126 | + @Bean |
| 127 | + public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager) { |
| 128 | + return new JobBuilder("job", jobRepository) |
| 129 | + .start(new StepBuilder("step", jobRepository) |
| 130 | + .<Person, Person>chunk(5, transactionManager) |
| 131 | + .reader(itemReader()) |
| 132 | + .writer(itemWriter()) |
| 133 | + .build()) |
| 134 | + .build(); |
| 135 | + } |
| 136 | + |
| 137 | + @Bean |
| 138 | + public DataSource dataSource() { |
| 139 | + return new EmbeddedDatabaseBuilder() |
| 140 | + .setType(EmbeddedDatabaseType.H2) |
| 141 | + .addScript("/org/springframework/batch/core/schema-drop-h2.sql") |
| 142 | + .addScript("/org/springframework/batch/core/schema-h2.sql") |
| 143 | + .addScript("schema.sql") |
| 144 | + .addScript("data.sql") |
| 145 | + .build(); |
| 146 | + } |
| 147 | + |
| 148 | + @Bean |
| 149 | + public JdbcTransactionManager transactionManager(DataSource dataSource) { |
| 150 | + return new JdbcTransactionManager(dataSource); |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | +} |
0 commit comments