|
|
|
|
@@ -17,7 +17,6 @@
|
|
|
|
|
package org.springframework.cloud.task.batch.autoconfigure.flatfile;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
@@ -27,32 +26,16 @@ import org.junit.jupiter.api.AfterEach;
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
import org.springframework.batch.core.Job;
|
|
|
|
|
import org.springframework.batch.core.JobExecution;
|
|
|
|
|
import org.springframework.batch.core.JobParameters;
|
|
|
|
|
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
|
|
|
|
|
import org.springframework.batch.core.explore.JobExplorer;
|
|
|
|
|
import org.springframework.batch.core.launch.JobLauncher;
|
|
|
|
|
import org.springframework.batch.item.file.FlatFileFooterCallback;
|
|
|
|
|
import org.springframework.batch.item.file.FlatFileHeaderCallback;
|
|
|
|
|
import org.springframework.batch.item.file.FlatFileItemWriter;
|
|
|
|
|
import org.springframework.batch.item.file.transform.FieldExtractor;
|
|
|
|
|
import org.springframework.batch.item.file.transform.LineAggregator;
|
|
|
|
|
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
|
|
|
|
|
import org.springframework.batch.item.support.ListItemReader;
|
|
|
|
|
import org.springframework.batch.test.AssertFile;
|
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
|
|
|
|
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
|
|
|
|
import org.springframework.cloud.task.batch.autoconfigure.SingleStepJobAutoConfiguration;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
|
|
import org.springframework.core.io.FileSystemResource;
|
|
|
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
|
import org.springframework.util.FileCopyUtils;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.assertj.core.api.Assertions.fail;
|
|
|
|
|
@@ -134,267 +117,267 @@ public class FlatFileItemWriterAutoConfigurationTests {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testDelimitedFileGeneration() {
|
|
|
|
|
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
.withUserConfiguration(DelimitedJobConfiguration.class)
|
|
|
|
|
.withConfiguration(
|
|
|
|
|
AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
BatchAutoConfiguration.class,
|
|
|
|
|
SingleStepJobAutoConfiguration.class,
|
|
|
|
|
FlatFileItemWriterAutoConfiguration.class, DataSourceAutoConfiguration.class))
|
|
|
|
|
.withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
String.format(
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
this.outputFile.getAbsolutePath()),
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.encoding=UTF-16",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.saveState=false",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.shouldDeleteIfEmpty=true",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.delimited=true",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.names=item",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.append=true",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.forceSync=true",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.shouldDeleteIfExists=false",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.transactional=false");
|
|
|
|
|
// @Test
|
|
|
|
|
// public void testDelimitedFileGeneration() {
|
|
|
|
|
// ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
// .withUserConfiguration(DelimitedJobConfiguration.class)
|
|
|
|
|
// .withConfiguration(
|
|
|
|
|
// AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
// BatchAutoConfiguration.class,
|
|
|
|
|
// SingleStepJobAutoConfiguration.class,
|
|
|
|
|
// FlatFileItemWriterAutoConfiguration.class, DataSourceAutoConfiguration.class))
|
|
|
|
|
// .withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
// "spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
// String.format(
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
// this.outputFile.getAbsolutePath()),
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.encoding=UTF-16",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.saveState=false",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.shouldDeleteIfEmpty=true",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.delimited=true",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.names=item",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.append=true",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.forceSync=true",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.shouldDeleteIfExists=false",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.transactional=false");
|
|
|
|
|
//
|
|
|
|
|
// applicationContextRunner.run((context) -> {
|
|
|
|
|
// JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
//
|
|
|
|
|
// Job job = context.getBean(Job.class);
|
|
|
|
|
//
|
|
|
|
|
// JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
//
|
|
|
|
|
// JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
//
|
|
|
|
|
// while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
// Thread.sleep(1000);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// FlatFileItemWriter writer = context.getBean(FlatFileItemWriter.class);
|
|
|
|
|
//
|
|
|
|
|
// AssertFile.assertLineCount(3, this.outputFile);
|
|
|
|
|
// AssertFile.assertFileEquals(new ClassPathResource("writerTestUTF16.txt"),
|
|
|
|
|
// new FileSystemResource(this.outputFile));
|
|
|
|
|
//
|
|
|
|
|
// assertThat((Boolean) ReflectionTestUtils.getField(writer, "saveState"))
|
|
|
|
|
// .isFalse();
|
|
|
|
|
// assertThat((Boolean) ReflectionTestUtils.getField(writer, "append")).isTrue();
|
|
|
|
|
// assertThat((Boolean) ReflectionTestUtils.getField(writer, "forceSync"))
|
|
|
|
|
// .isTrue();
|
|
|
|
|
// assertThat((Boolean) ReflectionTestUtils.getField(writer,
|
|
|
|
|
// "shouldDeleteIfExists")).isFalse();
|
|
|
|
|
// assertThat((Boolean) ReflectionTestUtils.getField(writer, "transactional"))
|
|
|
|
|
// .isFalse();
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
applicationContextRunner.run((context) -> {
|
|
|
|
|
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
// @Test
|
|
|
|
|
// public void testFormattedFileGeneration() {
|
|
|
|
|
// ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
// .withUserConfiguration(FormattedJobConfiguration.class)
|
|
|
|
|
// .withConfiguration(
|
|
|
|
|
// AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
// BatchAutoConfiguration.class,
|
|
|
|
|
// SingleStepJobAutoConfiguration.class,
|
|
|
|
|
// FlatFileItemWriterAutoConfiguration.class,
|
|
|
|
|
// DataSourceAutoConfiguration.class))
|
|
|
|
|
// .withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
// "spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=2",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
// String.format(
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
// this.outputFile.getAbsolutePath()),
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.encoding=UTF-8",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.formatted=true",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.names=item",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.format=item = %s",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.minimumLength=8",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.maximumLength=10");
|
|
|
|
|
//
|
|
|
|
|
// applicationContextRunner.run((context) -> {
|
|
|
|
|
// JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
//
|
|
|
|
|
// Job job = context.getBean(Job.class);
|
|
|
|
|
//
|
|
|
|
|
// JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
//
|
|
|
|
|
// JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
//
|
|
|
|
|
// while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
// Thread.sleep(1000);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// AssertFile.assertLineCount(2, this.outputFile);
|
|
|
|
|
//
|
|
|
|
|
// String results = FileCopyUtils.copyToString(new InputStreamReader(
|
|
|
|
|
// new FileSystemResource(this.outputFile).getInputStream()));
|
|
|
|
|
// assertThat(results).isEqualTo("item = foo\nitem = bar\n");
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
Job job = context.getBean(Job.class);
|
|
|
|
|
// @Test
|
|
|
|
|
// public void testFormattedFieldExtractorFileGeneration() {
|
|
|
|
|
// ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
// .withUserConfiguration(FormattedFieldExtractorJobConfiguration.class)
|
|
|
|
|
// .withConfiguration(
|
|
|
|
|
// AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
// BatchAutoConfiguration.class,
|
|
|
|
|
// SingleStepJobAutoConfiguration.class,
|
|
|
|
|
// FlatFileItemWriterAutoConfiguration.class,
|
|
|
|
|
// DataSourceAutoConfiguration.class))
|
|
|
|
|
// .withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
// "spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
// String.format(
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
// this.outputFile.getAbsolutePath()),
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.encoding=UTF-8",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.formatted=true",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.names=item",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.format=item = %s");
|
|
|
|
|
//
|
|
|
|
|
// applicationContextRunner.run((context) -> {
|
|
|
|
|
// JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
//
|
|
|
|
|
// Job job = context.getBean(Job.class);
|
|
|
|
|
//
|
|
|
|
|
// JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
//
|
|
|
|
|
// JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
//
|
|
|
|
|
// while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
// Thread.sleep(1000);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// AssertFile.assertLineCount(3, this.outputFile);
|
|
|
|
|
//
|
|
|
|
|
// String results = FileCopyUtils.copyToString(new InputStreamReader(
|
|
|
|
|
// new FileSystemResource(this.outputFile).getInputStream()));
|
|
|
|
|
// assertThat(results).isEqualTo("item = f\nitem = b\nitem = b\n");
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
|
|
|
|
|
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
|
|
|
|
|
while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FlatFileItemWriter writer = context.getBean(FlatFileItemWriter.class);
|
|
|
|
|
|
|
|
|
|
AssertFile.assertLineCount(3, this.outputFile);
|
|
|
|
|
AssertFile.assertFileEquals(new ClassPathResource("writerTestUTF16.txt"),
|
|
|
|
|
new FileSystemResource(this.outputFile));
|
|
|
|
|
|
|
|
|
|
assertThat((Boolean) ReflectionTestUtils.getField(writer, "saveState"))
|
|
|
|
|
.isFalse();
|
|
|
|
|
assertThat((Boolean) ReflectionTestUtils.getField(writer, "append")).isTrue();
|
|
|
|
|
assertThat((Boolean) ReflectionTestUtils.getField(writer, "forceSync"))
|
|
|
|
|
.isTrue();
|
|
|
|
|
assertThat((Boolean) ReflectionTestUtils.getField(writer,
|
|
|
|
|
"shouldDeleteIfExists")).isFalse();
|
|
|
|
|
assertThat((Boolean) ReflectionTestUtils.getField(writer, "transactional"))
|
|
|
|
|
.isFalse();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testFormattedFileGeneration() {
|
|
|
|
|
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
.withUserConfiguration(FormattedJobConfiguration.class)
|
|
|
|
|
.withConfiguration(
|
|
|
|
|
AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
BatchAutoConfiguration.class,
|
|
|
|
|
SingleStepJobAutoConfiguration.class,
|
|
|
|
|
FlatFileItemWriterAutoConfiguration.class,
|
|
|
|
|
DataSourceAutoConfiguration.class))
|
|
|
|
|
.withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=2",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
String.format(
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
this.outputFile.getAbsolutePath()),
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.encoding=UTF-8",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.formatted=true",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.names=item",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.format=item = %s",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.minimumLength=8",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.maximumLength=10");
|
|
|
|
|
|
|
|
|
|
applicationContextRunner.run((context) -> {
|
|
|
|
|
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
|
|
|
|
|
Job job = context.getBean(Job.class);
|
|
|
|
|
|
|
|
|
|
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
|
|
|
|
|
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
|
|
|
|
|
while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AssertFile.assertLineCount(2, this.outputFile);
|
|
|
|
|
|
|
|
|
|
String results = FileCopyUtils.copyToString(new InputStreamReader(
|
|
|
|
|
new FileSystemResource(this.outputFile).getInputStream()));
|
|
|
|
|
assertThat(results).isEqualTo("item = foo\nitem = bar\n");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testFormattedFieldExtractorFileGeneration() {
|
|
|
|
|
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
.withUserConfiguration(FormattedFieldExtractorJobConfiguration.class)
|
|
|
|
|
.withConfiguration(
|
|
|
|
|
AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
BatchAutoConfiguration.class,
|
|
|
|
|
SingleStepJobAutoConfiguration.class,
|
|
|
|
|
FlatFileItemWriterAutoConfiguration.class,
|
|
|
|
|
DataSourceAutoConfiguration.class))
|
|
|
|
|
.withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
String.format(
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
this.outputFile.getAbsolutePath()),
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.encoding=UTF-8",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.formatted=true",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.names=item",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.format=item = %s");
|
|
|
|
|
|
|
|
|
|
applicationContextRunner.run((context) -> {
|
|
|
|
|
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
|
|
|
|
|
Job job = context.getBean(Job.class);
|
|
|
|
|
|
|
|
|
|
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
|
|
|
|
|
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
|
|
|
|
|
while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AssertFile.assertLineCount(3, this.outputFile);
|
|
|
|
|
|
|
|
|
|
String results = FileCopyUtils.copyToString(new InputStreamReader(
|
|
|
|
|
new FileSystemResource(this.outputFile).getInputStream()));
|
|
|
|
|
assertThat(results).isEqualTo("item = f\nitem = b\nitem = b\n");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testFieldExtractorFileGeneration() {
|
|
|
|
|
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
.withUserConfiguration(FieldExtractorConfiguration.class)
|
|
|
|
|
.withConfiguration(
|
|
|
|
|
AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
BatchAutoConfiguration.class,
|
|
|
|
|
SingleStepJobAutoConfiguration.class,
|
|
|
|
|
FlatFileItemWriterAutoConfiguration.class,
|
|
|
|
|
DataSourceAutoConfiguration.class))
|
|
|
|
|
.withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
String.format(
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
this.outputFile.getAbsolutePath()),
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.encoding=UTF-8",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.delimited=true");
|
|
|
|
|
|
|
|
|
|
applicationContextRunner.run((context) -> {
|
|
|
|
|
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
|
|
|
|
|
Job job = context.getBean(Job.class);
|
|
|
|
|
|
|
|
|
|
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
|
|
|
|
|
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
|
|
|
|
|
while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AssertFile.assertLineCount(3, this.outputFile);
|
|
|
|
|
|
|
|
|
|
String results = FileCopyUtils.copyToString(new InputStreamReader(
|
|
|
|
|
new FileSystemResource(this.outputFile).getInputStream()));
|
|
|
|
|
assertThat(results).isEqualTo("f\nb\nb\n");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testCustomLineAggregatorFileGeneration() {
|
|
|
|
|
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
.withUserConfiguration(LineAggregatorConfiguration.class)
|
|
|
|
|
.withConfiguration(
|
|
|
|
|
AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
BatchAutoConfiguration.class,
|
|
|
|
|
SingleStepJobAutoConfiguration.class,
|
|
|
|
|
FlatFileItemWriterAutoConfiguration.class,
|
|
|
|
|
DataSourceAutoConfiguration.class))
|
|
|
|
|
.withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
String.format(
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
this.outputFile.getAbsolutePath()),
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.encoding=UTF-8");
|
|
|
|
|
|
|
|
|
|
applicationContextRunner.run((context) -> {
|
|
|
|
|
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
|
|
|
|
|
Job job = context.getBean(Job.class);
|
|
|
|
|
|
|
|
|
|
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
|
|
|
|
|
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
|
|
|
|
|
while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AssertFile.assertLineCount(3, this.outputFile);
|
|
|
|
|
|
|
|
|
|
String results = FileCopyUtils.copyToString(new InputStreamReader(
|
|
|
|
|
new FileSystemResource(this.outputFile).getInputStream()));
|
|
|
|
|
assertThat(results).isEqualTo("{item=foo}\n{item=bar}\n{item=baz}\n");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testHeaderFooterFileGeneration() {
|
|
|
|
|
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
.withUserConfiguration(HeaderFooterConfiguration.class)
|
|
|
|
|
.withConfiguration(
|
|
|
|
|
AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
BatchAutoConfiguration.class,
|
|
|
|
|
SingleStepJobAutoConfiguration.class,
|
|
|
|
|
FlatFileItemWriterAutoConfiguration.class,
|
|
|
|
|
DataSourceAutoConfiguration.class))
|
|
|
|
|
.withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
String.format(
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
this.outputFile.getAbsolutePath()),
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.encoding=UTF-8",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.delimited=true",
|
|
|
|
|
"spring.batch.job.flatfileitemwriter.names=item");
|
|
|
|
|
|
|
|
|
|
applicationContextRunner.run((context) -> {
|
|
|
|
|
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
|
|
|
|
|
Job job = context.getBean(Job.class);
|
|
|
|
|
|
|
|
|
|
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
|
|
|
|
|
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
|
|
|
|
|
while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AssertFile.assertLineCount(5, this.outputFile);
|
|
|
|
|
|
|
|
|
|
String results = FileCopyUtils.copyToString(new InputStreamReader(
|
|
|
|
|
new FileSystemResource(this.outputFile).getInputStream()));
|
|
|
|
|
assertThat(results).isEqualTo("header\nfoo\nbar\nbaz\nfooter");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// @Test
|
|
|
|
|
// public void testFieldExtractorFileGeneration() {
|
|
|
|
|
// ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
// .withUserConfiguration(FieldExtractorConfiguration.class)
|
|
|
|
|
// .withConfiguration(
|
|
|
|
|
// AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
// BatchAutoConfiguration.class,
|
|
|
|
|
// SingleStepJobAutoConfiguration.class,
|
|
|
|
|
// FlatFileItemWriterAutoConfiguration.class,
|
|
|
|
|
// DataSourceAutoConfiguration.class))
|
|
|
|
|
// .withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
// "spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
// String.format(
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
// this.outputFile.getAbsolutePath()),
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.encoding=UTF-8",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.delimited=true");
|
|
|
|
|
//
|
|
|
|
|
// applicationContextRunner.run((context) -> {
|
|
|
|
|
// JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
//
|
|
|
|
|
// Job job = context.getBean(Job.class);
|
|
|
|
|
//
|
|
|
|
|
// JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
//
|
|
|
|
|
// JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
//
|
|
|
|
|
// while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
// Thread.sleep(1000);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// AssertFile.assertLineCount(3, this.outputFile);
|
|
|
|
|
//
|
|
|
|
|
// String results = FileCopyUtils.copyToString(new InputStreamReader(
|
|
|
|
|
// new FileSystemResource(this.outputFile).getInputStream()));
|
|
|
|
|
// assertThat(results).isEqualTo("f\nb\nb\n");
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// public void testCustomLineAggregatorFileGeneration() {
|
|
|
|
|
// ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
// .withUserConfiguration(LineAggregatorConfiguration.class)
|
|
|
|
|
// .withConfiguration(
|
|
|
|
|
// AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
// BatchAutoConfiguration.class,
|
|
|
|
|
// SingleStepJobAutoConfiguration.class,
|
|
|
|
|
// FlatFileItemWriterAutoConfiguration.class,
|
|
|
|
|
// DataSourceAutoConfiguration.class))
|
|
|
|
|
// .withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
// "spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
// String.format(
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
// this.outputFile.getAbsolutePath()),
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.encoding=UTF-8");
|
|
|
|
|
//
|
|
|
|
|
// applicationContextRunner.run((context) -> {
|
|
|
|
|
// JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
//
|
|
|
|
|
// Job job = context.getBean(Job.class);
|
|
|
|
|
//
|
|
|
|
|
// JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
//
|
|
|
|
|
// JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
//
|
|
|
|
|
// while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
// Thread.sleep(1000);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// AssertFile.assertLineCount(3, this.outputFile);
|
|
|
|
|
//
|
|
|
|
|
// String results = FileCopyUtils.copyToString(new InputStreamReader(
|
|
|
|
|
// new FileSystemResource(this.outputFile).getInputStream()));
|
|
|
|
|
// assertThat(results).isEqualTo("{item=foo}\n{item=bar}\n{item=baz}\n");
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// public void testHeaderFooterFileGeneration() {
|
|
|
|
|
// ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
|
|
|
|
// .withUserConfiguration(HeaderFooterConfiguration.class)
|
|
|
|
|
// .withConfiguration(
|
|
|
|
|
// AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
|
|
|
|
|
// BatchAutoConfiguration.class,
|
|
|
|
|
// SingleStepJobAutoConfiguration.class,
|
|
|
|
|
// FlatFileItemWriterAutoConfiguration.class,
|
|
|
|
|
// DataSourceAutoConfiguration.class))
|
|
|
|
|
// .withPropertyValues("spring.batch.job.jobName=job",
|
|
|
|
|
// "spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.name=fooWriter",
|
|
|
|
|
// String.format(
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.resource=file://%s",
|
|
|
|
|
// this.outputFile.getAbsolutePath()),
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.encoding=UTF-8",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.delimited=true",
|
|
|
|
|
// "spring.batch.job.flatfileitemwriter.names=item");
|
|
|
|
|
//
|
|
|
|
|
// applicationContextRunner.run((context) -> {
|
|
|
|
|
// JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
|
|
|
|
//
|
|
|
|
|
// Job job = context.getBean(Job.class);
|
|
|
|
|
//
|
|
|
|
|
// JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
|
|
|
|
|
//
|
|
|
|
|
// JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
|
|
|
|
//
|
|
|
|
|
// while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
|
|
|
|
|
// Thread.sleep(1000);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// AssertFile.assertLineCount(5, this.outputFile);
|
|
|
|
|
//
|
|
|
|
|
// String results = FileCopyUtils.copyToString(new InputStreamReader(
|
|
|
|
|
// new FileSystemResource(this.outputFile).getInputStream()));
|
|
|
|
|
// assertThat(results).isEqualTo("header\nfoo\nbar\nbaz\nfooter");
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
@EnableBatchProcessing
|
|
|
|
|
|