XD-1027: Create script-based batch ItemProcessor

This commit is contained in:
Chris Schaefer
2014-07-02 17:13:55 -04:00
committed by Michael Minella
parent 8f8600c14d
commit 587680ba56
7 changed files with 453 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.step.item;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;
import java.util.List;
/**
* <p>
* Test job utilizing a {@link org.springframework.batch.item.support.ScriptItemProcessor}.
* </p>
*
* @author Chris Schaefer
* @since 3.1
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ScriptItemProcessorTest {
@Autowired
private Job job;
@Autowired
private JobLauncher jobLauncher;
@Test
public void testScriptProcessorJob() throws Exception {
jobLauncher.run(job, new JobParameters());
}
public static class TestItemWriter implements ItemWriter<String> {
@Override
public void write(List<? extends String> items) throws Exception {
Assert.notNull(items, "Items cannot be null");
Assert.isTrue(!items.isEmpty(), "Items cannot be empty");
Assert.isTrue(items.size() == 1, "Items should only contain one entry");
String item = items.get(0);
Assert.isTrue("BLAH".equals(item), "Transformed item to write should have been: BLAH but got: " + item);
}
}
}

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<job id="scriptProcessorJob">
<step id="step1">
<tasklet>
<chunk reader="reader" processor="processor" writer="writer" commit-interval="1"/>
</tasklet>
</step>
</job>
<beans:bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
<beans:bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"
p:transactionManager-ref="transactionManager"/>
<beans:bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"
p:jobRepository-ref="jobRepository"/>
<beans:bean id="reader" class="org.springframework.batch.item.support.ListItemReader">
<beans:constructor-arg>
<beans:list>
<beans:value>blah</beans:value>
</beans:list>
</beans:constructor-arg>
</beans:bean>
<beans:bean id="processor" class="org.springframework.batch.item.support.ScriptItemProcessor"
p:script="org/springframework/batch/core/step/item/processor-test-simple.js"/>
<beans:bean id="writer" class="org.springframework.batch.core.step.item.ScriptItemProcessorTest$TestItemWriter"/>
</beans:beans>