org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration Java Examples

The following examples show how to use org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ComposedRunnerVisitorTests.java    From composed-task-runner with Apache License 2.0 5 votes vote down vote up
private void setupContextForGraph(String[] args) {
	this.applicationContext = SpringApplication.run(new Class[]{ComposedRunnerVisitorConfiguration.class,
			PropertyPlaceholderAutoConfiguration.class,
			EmbeddedDataSourceConfiguration.class,
			BatchAutoConfiguration.class,
			TaskBatchAutoConfiguration.class,
			SimpleTaskAutoConfiguration.class}, args);
}
 
Example #2
Source File: ComposedRunnerVisitorTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private void setupContextForGraph(String[] args) {
	this.applicationContext = SpringApplication.run(new Class[]{ComposedRunnerVisitorConfiguration.class,
			PropertyPlaceholderAutoConfiguration.class,
			EmbeddedDataSourceConfiguration.class,
			BatchAutoConfiguration.class,
			TaskBatchAutoConfiguration.class,
			SimpleTaskAutoConfiguration.class}, args);
}
 
Example #3
Source File: SingleStepJobAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleConfiguration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(SimpleConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1",
					"spring.batch.job.chunkSize=5");

	applicationContextRunner.run((context) -> {
		JobLauncher jobLauncher = context.getBean(JobLauncher.class);

		Job job = context.getBean(Job.class);

		ListItemWriter itemWriter = context.getBean(ListItemWriter.class);

		JobExecution jobExecution = jobLauncher.run(job, new JobParameters());

		JobExplorer jobExplorer = context.getBean(JobExplorer.class);

		while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
			Thread.sleep(1000);
		}

		List<Map<Object, Object>> writtenItems = itemWriter.getWrittenItems();

		assertThat(writtenItems.size()).isEqualTo(3);

		assertThat(writtenItems.get(0).get("item")).isEqualTo("foo");
		assertThat(writtenItems.get(1).get("item")).isEqualTo("bar");
		assertThat(writtenItems.get(2).get("item")).isEqualTo("baz");
	});
}
 
Example #4
Source File: FlatFileItemReaderAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomLineMapper() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(CustomLineMapperConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemReaderAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilereader.name=fixedWidthConfiguration",
					"spring.batch.job.flatfilereader.resource=/test.txt",
					"spring.batch.job.flatfilereader.strict=true");

	applicationContextRunner.run((context) -> {
		JobLauncher jobLauncher = context.getBean(JobLauncher.class);

		Job job = context.getBean(Job.class);

		ListItemWriter itemWriter = context.getBean(ListItemWriter.class);

		JobExecution jobExecution = jobLauncher.run(job, new JobParameters());

		JobExplorer jobExplorer = context.getBean(JobExplorer.class);

		while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
			Thread.sleep(1000);
		}

		List writtenItems = itemWriter.getWrittenItems();

		assertThat(writtenItems.size()).isEqualTo(8);
	});
}
 
Example #5
Source File: FlatFileItemReaderAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomMapping() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(CustomMappingConfiguration.class)
			.withConfiguration(AutoConfigurations.of(
					PropertyPlaceholderAutoConfiguration.class,
					BatchAutoConfiguration.class,
					SingleStepJobAutoConfiguration.class,
					FlatFileItemReaderAutoConfiguration.class, RangeConverter.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilereader.name=fixedWidthConfiguration",
					"spring.batch.job.flatfilereader.resource=/test.txt",
					"spring.batch.job.flatfilereader.maxItemCount=1",
					"spring.batch.job.flatfilereader.strict=true");

	applicationContextRunner.run((context) -> {
		JobLauncher jobLauncher = context.getBean(JobLauncher.class);

		Job job = context.getBean(Job.class);

		ListItemWriter itemWriter = context.getBean(ListItemWriter.class);

		JobExecution jobExecution = jobLauncher.run(job, new JobParameters());

		JobExplorer jobExplorer = context.getBean(JobExplorer.class);

		while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
			Thread.sleep(1000);
		}

		List<Map<Object, Object>> writtenItems = itemWriter.getWrittenItems();

		assertThat(writtenItems.size()).isEqualTo(1);
		assertThat(writtenItems.get(0).get("one")).isEqualTo("1 2 3");
		assertThat(writtenItems.get(0).get("two")).isEqualTo("4 5 six");
	});
}
 
Example #6
Source File: FlatFileItemWriterAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testFormattedFieldExtractorFileGeneration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(FormattedFieldExtractorJobConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemWriterAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilewriter.name=fooWriter",
					String.format(
							"spring.batch.job.flatfilewriter.resource=file://%s",
							this.outputFile.getAbsolutePath()),
					"spring.batch.job.flatfilewriter.encoding=UTF-8",
					"spring.batch.job.flatfilewriter.formatted=true",
					"spring.batch.job.flatfilewriter.names=item",
					"spring.batch.job.flatfilewriter.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");
	});
}
 
Example #7
Source File: FlatFileItemWriterAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testFieldExtractorFileGeneration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(FieldExtractorConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemWriterAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilewriter.name=fooWriter",
					String.format(
							"spring.batch.job.flatfilewriter.resource=file://%s",
							this.outputFile.getAbsolutePath()),
					"spring.batch.job.flatfilewriter.encoding=UTF-8",
					"spring.batch.job.flatfilewriter.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");
	});
}
 
Example #8
Source File: FlatFileItemWriterAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomLineAggregatorFileGeneration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(LineAggregatorConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemWriterAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilewriter.name=fooWriter",
					String.format(
							"spring.batch.job.flatfilewriter.resource=file://%s",
							this.outputFile.getAbsolutePath()),
					"spring.batch.job.flatfilewriter.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");
	});
}
 
Example #9
Source File: FlatFileItemWriterAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeaderFooterFileGeneration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(HeaderFooterConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemWriterAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilewriter.name=fooWriter",
					String.format(
							"spring.batch.job.flatfilewriter.resource=file://%s",
							this.outputFile.getAbsolutePath()),
					"spring.batch.job.flatfilewriter.encoding=UTF-8",
					"spring.batch.job.flatfilewriter.delimited=true",
					"spring.batch.job.flatfilewriter.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");
	});
}
 
Example #10
Source File: FlatFileItemReaderAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 4 votes vote down vote up
/**
 * Contents of the file to be read (included here because it's UTF-16):
 *
 * <pre>
 * 1@2@3@4@5@six
 * # This should be ignored
 * 7@8@9@10@11@twelve
 * $ So should this
 * 13@14@15@16@17@eighteen
 * 19@20@21@22@23@%twenty four%
 * 15@26@27@28@29@thirty
 * 31@32@33@34@35@thirty six
 * 37@38@39@40@41@forty two
 * 43@44@45@46@47@forty eight
 * 49@50@51@52@53@fifty four
 * 55@56@57@58@59@sixty
 * </pre>
 */
@Test
public void testFullDelimitedConfiguration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(JobConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemReaderAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilereader.savestate=true",
					"spring.batch.job.flatfilereader.name=fullDelimitedConfiguration",
					"spring.batch.job.flatfilereader.maxItemCount=5",
					"spring.batch.job.flatfilereader.currentItemCount=2",
					"spring.batch.job.flatfilereader.comments=#,$",
					"spring.batch.job.flatfilereader.resource=/testUTF16.csv",
					"spring.batch.job.flatfilereader.strict=true",
					"spring.batch.job.flatfilereader.encoding=UTF-16",
					"spring.batch.job.flatfilereader.linesToSkip=1",
					"spring.batch.job.flatfilereader.delimited=true",
					"spring.batch.job.flatfilereader.delimiter=@",
					"spring.batch.job.flatfilereader.quoteCharacter=%",
					"spring.batch.job.flatfilereader.includedFields=1,3,5",
					"spring.batch.job.flatfilereader.names=foo,bar,baz",
					"spring.batch.job.flatfilereader.parsingStrict=false");

	applicationContextRunner.run((context) -> {
		JobLauncher jobLauncher = context.getBean(JobLauncher.class);

		Job job = context.getBean(Job.class);

		ListItemWriter itemWriter = context.getBean(ListItemWriter.class);

		JobExecution jobExecution = jobLauncher.run(job, new JobParameters());

		JobExplorer jobExplorer = context.getBean(JobExplorer.class);

		while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
			Thread.sleep(1000);
		}

		List writtenItems = itemWriter.getWrittenItems();

		assertThat(writtenItems.size()).isEqualTo(3);
		assertThat(((Map) writtenItems.get(0)).get("foo")).isEqualTo("20");
		assertThat(((Map) writtenItems.get(0)).get("bar")).isEqualTo("22");
		assertThat(((Map) writtenItems.get(0)).get("baz")).isEqualTo("twenty four");
		assertThat(((Map) writtenItems.get(1)).get("foo")).isEqualTo("26");
		assertThat(((Map) writtenItems.get(1)).get("bar")).isEqualTo("28");
		assertThat(((Map) writtenItems.get(1)).get("baz")).isEqualTo("thirty");
		assertThat(((Map) writtenItems.get(2)).get("foo")).isEqualTo("32");
		assertThat(((Map) writtenItems.get(2)).get("bar")).isEqualTo("34");
		assertThat(((Map) writtenItems.get(2)).get("baz")).isEqualTo("thirty six");
	});
}
 
Example #11
Source File: FlatFileItemReaderAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 4 votes vote down vote up
@Test
public void testFixedWidthConfiguration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(JobConfiguration.class)
			.withConfiguration(AutoConfigurations.of(
					PropertyPlaceholderAutoConfiguration.class,
					BatchAutoConfiguration.class,
					SingleStepJobAutoConfiguration.class,
					FlatFileItemReaderAutoConfiguration.class, RangeConverter.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilereader.savestate=true",
					"spring.batch.job.flatfilereader.name=fixedWidthConfiguration",
					"spring.batch.job.flatfilereader.comments=#,$",
					"spring.batch.job.flatfilereader.resource=/test.txt",
					"spring.batch.job.flatfilereader.strict=true",
					"spring.batch.job.flatfilereader.fixedLength=true",
					"spring.batch.job.flatfilereader.ranges=3-4,7-8,11",
					"spring.batch.job.flatfilereader.names=foo,bar,baz",
					"spring.batch.job.flatfilereader.parsingStrict=false");

	applicationContextRunner.run((context) -> {
		JobLauncher jobLauncher = context.getBean(JobLauncher.class);

		Job job = context.getBean(Job.class);

		ListItemWriter itemWriter = context.getBean(ListItemWriter.class);

		JobExecution jobExecution = jobLauncher.run(job, new JobParameters());

		JobExplorer jobExplorer = context.getBean(JobExplorer.class);

		while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
			Thread.sleep(1000);
		}

		List writtenItems = itemWriter.getWrittenItems();

		assertThat(writtenItems.size()).isEqualTo(6);
		assertThat(((Map) writtenItems.get(0)).get("foo")).isEqualTo("2");
		assertThat(((Map) writtenItems.get(0)).get("bar")).isEqualTo("4");
		assertThat(((Map) writtenItems.get(0)).get("baz")).isEqualTo("six");
		assertThat(((Map) writtenItems.get(1)).get("foo")).isEqualTo("8");
		assertThat(((Map) writtenItems.get(1)).get("bar")).isEqualTo("10");
		assertThat(((Map) writtenItems.get(1)).get("baz")).isEqualTo("twelve");
		assertThat(((Map) writtenItems.get(2)).get("foo")).isEqualTo("14");
		assertThat(((Map) writtenItems.get(2)).get("bar")).isEqualTo("16");
		assertThat(((Map) writtenItems.get(2)).get("baz")).isEqualTo("eighteen");
		assertThat(((Map) writtenItems.get(3)).get("foo")).isEqualTo("20");
		assertThat(((Map) writtenItems.get(3)).get("bar")).isEqualTo("22");
		assertThat(((Map) writtenItems.get(3)).get("baz")).isEqualTo("twenty four");
		assertThat(((Map) writtenItems.get(4)).get("foo")).isEqualTo("26");
		assertThat(((Map) writtenItems.get(4)).get("bar")).isEqualTo("28");
		assertThat(((Map) writtenItems.get(4)).get("baz")).isEqualTo("thirty");
		assertThat(((Map) writtenItems.get(5)).get("foo")).isEqualTo("32");
		assertThat(((Map) writtenItems.get(5)).get("bar")).isEqualTo("34");
		assertThat(((Map) writtenItems.get(5)).get("baz")).isEqualTo("thirty six");
	});
}
 
Example #12
Source File: FlatFileItemReaderAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 4 votes vote down vote up
/**
 * This test requires an input file with an even number of records
 */
@Test
public void testCustomRecordSeparatorAndSkippedLines() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(
					RecordSeparatorAndSkippedLinesJobConfiguration.class)
			.withConfiguration(AutoConfigurations.of(
					PropertyPlaceholderAutoConfiguration.class,
					BatchAutoConfiguration.class,
					SingleStepJobAutoConfiguration.class,
					FlatFileItemReaderAutoConfiguration.class, RangeConverter.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilereader.name=fixedWidthConfiguration",
					"spring.batch.job.flatfilereader.resource=/test.txt",
					"spring.batch.job.flatfilereader.linesToSkip=2",
					"spring.batch.job.flatfilereader.fixedLength=true",
					"spring.batch.job.flatfilereader.ranges=3-4,7-8,11",
					"spring.batch.job.flatfilereader.names=foo,bar,baz",
					"spring.batch.job.flatfilereader.strict=true");

	applicationContextRunner.run((context) -> {
		JobLauncher jobLauncher = context.getBean(JobLauncher.class);

		Job job = context.getBean(Job.class);

		ListItemWriter itemWriter = context.getBean(ListItemWriter.class);

		JobExecution jobExecution = jobLauncher.run(job, new JobParameters());

		JobExplorer jobExplorer = context.getBean(JobExplorer.class);

		while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
			Thread.sleep(1000);
		}

		ListLineCallbackHandler callbackHandler = context
				.getBean(ListLineCallbackHandler.class);

		assertThat(callbackHandler.getLines().size()).isEqualTo(2);

		List writtenItems = itemWriter.getWrittenItems();

		assertThat(writtenItems.size()).isEqualTo(2);
	});
}
 
Example #13
Source File: FlatFileItemWriterAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 4 votes vote down vote up
@Test
public void testDelimitedFileGeneration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(DelimitedJobConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemWriterAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilewriter.name=fooWriter",
					String.format(
							"spring.batch.job.flatfilewriter.resource=file://%s",
							this.outputFile.getAbsolutePath()),
					"spring.batch.job.flatfilewriter.encoding=UTF-16",
					"spring.batch.job.flatfilewriter.saveState=false",
					"spring.batch.job.flatfilewriter.shouldDeleteIfEmpty=true",
					"spring.batch.job.flatfilewriter.delimited=true",
					"spring.batch.job.flatfilewriter.names=item",
					"spring.batch.job.flatfilewriter.append=true",
					"spring.batch.job.flatfilewriter.forceSync=true",
					"spring.batch.job.flatfilewriter.shouldDeleteIfExists=false",
					"spring.batch.job.flatfilewriter.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();
	});
}
 
Example #14
Source File: FlatFileItemWriterAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 4 votes vote down vote up
@Test
public void testFormattedFileGeneration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(FormattedJobConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemWriterAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=2",
					"spring.batch.job.flatfilewriter.name=fooWriter",
					String.format(
							"spring.batch.job.flatfilewriter.resource=file://%s",
							this.outputFile.getAbsolutePath()),
					"spring.batch.job.flatfilewriter.encoding=UTF-8",
					"spring.batch.job.flatfilewriter.formatted=true",
					"spring.batch.job.flatfilewriter.names=item",
					"spring.batch.job.flatfilewriter.format=item = %s",
					"spring.batch.job.flatfilewriter.minimumLength=8",
					"spring.batch.job.flatfilewriter.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");
	});
}