BATCH-63:improved <chunk-oriented> tests

This commit is contained in:
trisberg
2008-11-06 20:46:36 +00:00
parent bc21421e00
commit a5107fb223
11 changed files with 150 additions and 20 deletions

View File

@@ -201,14 +201,20 @@ public class StepParser {
String readerBeanId = element.getAttribute("reader");
if (StringUtils.hasText(readerBeanId)) {
RuntimeBeanReference taskletRef = new RuntimeBeanReference(readerBeanId);
bd.getPropertyValues().addPropertyValue("itemReader", taskletRef);
RuntimeBeanReference readerRef = new RuntimeBeanReference(readerBeanId);
bd.getPropertyValues().addPropertyValue("itemReader", readerRef);
}
String processorBeanId = element.getAttribute("processor");
if (StringUtils.hasText(processorBeanId)) {
RuntimeBeanReference processorRef = new RuntimeBeanReference(processorBeanId);
bd.getPropertyValues().addPropertyValue("itemProcessor", processorRef);
}
String writerBeanId = element.getAttribute("writer");
if (StringUtils.hasText(writerBeanId)) {
RuntimeBeanReference taskletRef = new RuntimeBeanReference(writerBeanId);
bd.getPropertyValues().addPropertyValue("itemWriter", taskletRef);
RuntimeBeanReference writerRef = new RuntimeBeanReference(writerBeanId);
bd.getPropertyValues().addPropertyValue("itemWriter", writerRef);
}
String jobRepository = element.getAttribute("job-repository");

View File

@@ -171,7 +171,7 @@
<xsd:element name="chunk-oriented" minOccurs="0" maxOccurs="1">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="processType"/>
<xsd:extension base="chunkOrientedType"/>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
@@ -212,7 +212,7 @@
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="processType">
<xsd:complexType name="chunkOrientedType">
<xsd:complexContent>
<xsd:extension base="stepDefType">
<xsd:attribute name="reader" type="xsd:string" use="required">

View File

@@ -0,0 +1,15 @@
package org.springframework.batch.core.configuration.xml;
public class AbstractTestComponent {
protected boolean executed = false;
public AbstractTestComponent() {
super();
}
public boolean isExecuted() {
return executed;
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2006-2007 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.configuration.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Thomas Risberg
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class StepWithChunkOrientedJobParserTests {
@Autowired
private Job job;
@Autowired
private JobRepository jobRepository;
@Autowired
private TestReader reader;
@Autowired
private TestProcessor processor;
@Autowired
private TestWriter writer;
@Before
public void setUp() {
MapJobRepositoryFactoryBean.clear();
}
@Test
public void testStepWithTask() throws Exception {
assertNotNull(job);
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters());
job.execute(jobExecution);
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
assertEquals(1, jobExecution.getStepExecutions().size());
assertTrue(reader.isExecuted());
assertTrue(processor.isExecuted());
assertTrue(writer.isExecuted());
}
}

View File

@@ -17,6 +17,7 @@ package org.springframework.batch.core.configuration.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
@@ -33,7 +34,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dave Syer
* @author Thomas Risberg
*
*/
@ContextConfiguration
@@ -46,6 +47,9 @@ public class StepWithTaskJobParserTests {
@Autowired
private JobRepository jobRepository;
@Autowired
private AbstractTestComponent tasklet;
@Before
public void setUp() {
MapJobRepositoryFactoryBean.clear();
@@ -57,6 +61,7 @@ public class StepWithTaskJobParserTests {
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters());
job.execute(jobExecution);
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
assertEquals(3, jobExecution.getStepExecutions().size());
assertEquals(2, jobExecution.getStepExecutions().size());
assertTrue(tasklet.isExecuted());
}
}

View File

@@ -0,0 +1,12 @@
package org.springframework.batch.core.configuration.xml;
import org.springframework.batch.item.ItemProcessor;
public class TestProcessor extends AbstractTestComponent implements ItemProcessor<String, String>{
public String process(String item) throws Exception {
executed = true;
return item;
}
}

View File

@@ -8,7 +8,7 @@ import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
public class TestReader implements ItemReader<String> {
public class TestReader extends AbstractTestComponent implements ItemReader<String> {
List<String> items = null;
@@ -21,6 +21,7 @@ public class TestReader implements ItemReader<String> {
public String read() throws Exception, UnexpectedInputException,
ParseException {
executed = true;
if (items.size() > 0) {
String item = items.remove(0);
return item;

View File

@@ -5,10 +5,11 @@ import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.core.AttributeAccessor;
public class DummyTasklet implements Tasklet {
public class TestTasklet extends AbstractTestComponent implements Tasklet {
public ExitStatus execute(StepContribution contribution,
AttributeAccessor attributes) throws Exception {
executed = true;
return ExitStatus.FINISHED;
}

View File

@@ -4,9 +4,10 @@ import java.util.List;
import org.springframework.batch.item.ItemWriter;
public class TestWriter implements ItemWriter<String> {
public class TestWriter extends AbstractTestComponent implements ItemWriter<String> {
public void write(List<? extends String> items) throws Exception {
executed = true;
}
}

View File

@@ -0,0 +1,21 @@
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<beans:import resource="common-context.xml" />
<job id="job">
<step name="step1">
<chunk-oriented reader="reader" processor="processor" writer="writer"/>
</step>
</job>
<beans:bean id="reader" class="org.springframework.batch.core.configuration.xml.TestReader"/>
<beans:bean id="processor" class="org.springframework.batch.core.configuration.xml.TestProcessor"/>
<beans:bean id="writer" class="org.springframework.batch.core.configuration.xml.TestWriter"/>
</beans:beans>

View File

@@ -11,18 +11,11 @@
<task tasklet="tasklet"/>
<next on="*" to="step2" />
</step>
<step name="step2" next="step3">
<step name="step2">
<task tasklet="tasklet"/>
</step>
<step name="step3">
<chunk-oriented reader="reader" writer="writer"/>
</step>
</job>
<beans:bean id="tasklet" class="org.springframework.batch.core.configuration.xml.DummyTasklet"/>
<beans:bean id="reader" class="org.springframework.batch.core.configuration.xml.TestReader"/>
<beans:bean id="writer" class="org.springframework.batch.core.configuration.xml.TestWriter"/>
<beans:bean id="tasklet" class="org.springframework.batch.core.configuration.xml.TestTasklet"/>
</beans:beans>