RESOLVED - issue BATCH-1443: Add JobStep: a Step implementation that executes a Job
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2006-2010 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.job;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
|
||||
import org.springframework.batch.support.PropertiesConverter;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class DefaultJobParametersExtractorJobParametersTests {
|
||||
|
||||
private DefaultJobParametersExtractor extractor = new DefaultJobParametersExtractor();
|
||||
|
||||
@Test
|
||||
public void testGetNamedJobParameters() throws Exception {
|
||||
StepExecution stepExecution = getStepExecution("foo=bar");
|
||||
extractor.setKeys(new String[] {"foo", "bar"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo=bar}", jobParameters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedLongStringParameters() throws Exception {
|
||||
StepExecution stepExecution = getStepExecution("foo=bar");
|
||||
extractor.setKeys(new String[] {"foo(string)", "bar"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo=bar}", jobParameters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedLongJobParameters() throws Exception {
|
||||
StepExecution stepExecution = getStepExecution("foo(long)=11");
|
||||
extractor.setKeys(new String[] {"foo(long)", "bar"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo=11}", jobParameters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedIntJobParameters() throws Exception {
|
||||
StepExecution stepExecution = getStepExecution("foo(long)=11");
|
||||
extractor.setKeys(new String[] {"foo(int)", "bar"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo=11}", jobParameters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedDoubleJobParameters() throws Exception {
|
||||
StepExecution stepExecution = getStepExecution("foo(double)=11.1");
|
||||
extractor.setKeys(new String[] {"foo(double)"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo=11.1}", jobParameters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedDateJobParameters() throws Exception {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
|
||||
Date date = dateFormat.parse(dateFormat.format(new Date()));
|
||||
StepExecution stepExecution = getStepExecution("foo(date)="+dateFormat.format(date));
|
||||
extractor.setKeys(new String[] {"foo(date)"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo="+date.getTime()+"}", jobParameters.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
private StepExecution getStepExecution(String parameters) {
|
||||
JobParameters jobParameters = new DefaultJobParametersConverter().getJobParameters(PropertiesConverter.stringToProperties(parameters));
|
||||
return new StepExecution("step", new JobExecution(new JobInstance(1L, jobParameters, "job")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2006-2010 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.job;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class DefaultJobParametersExtractorTests {
|
||||
|
||||
private DefaultJobParametersExtractor extractor = new DefaultJobParametersExtractor();
|
||||
private StepExecution stepExecution = new StepExecution("step", new JobExecution(0L));
|
||||
|
||||
@Test
|
||||
public void testGetEmptyJobParameters() throws Exception {
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{}", jobParameters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedJobParameters() throws Exception {
|
||||
stepExecution.getExecutionContext().put("foo", "bar");
|
||||
extractor.setKeys(new String[] {"foo", "bar"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo=bar}", jobParameters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedLongStringParameters() throws Exception {
|
||||
stepExecution.getExecutionContext().putString("foo","bar");
|
||||
extractor.setKeys(new String[] {"foo(string)", "bar"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo=bar}", jobParameters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedLongJobParameters() throws Exception {
|
||||
stepExecution.getExecutionContext().putLong("foo",11L);
|
||||
extractor.setKeys(new String[] {"foo(long)", "bar"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo=11}", jobParameters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedIntJobParameters() throws Exception {
|
||||
stepExecution.getExecutionContext().putInt("foo",11);
|
||||
extractor.setKeys(new String[] {"foo(int)", "bar"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo=11}", jobParameters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedDoubleJobParameters() throws Exception {
|
||||
stepExecution.getExecutionContext().putDouble("foo",11.1);
|
||||
extractor.setKeys(new String[] {"foo(double)"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo=11.1}", jobParameters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedDateJobParameters() throws Exception {
|
||||
Date date = new Date();
|
||||
stepExecution.getExecutionContext().put("foo",date);
|
||||
extractor.setKeys(new String[] {"foo(date)"});
|
||||
JobParameters jobParameters = extractor.getJobParameters(null, stepExecution);
|
||||
assertEquals("{foo="+date.getTime()+"}", jobParameters.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* Copyright 2006-2010 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.job;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.UnexpectedJobExecutionException;
|
||||
import org.springframework.batch.core.job.JobSupport;
|
||||
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class JobStepTests {
|
||||
|
||||
private JobStep step = new JobStep();
|
||||
|
||||
private StepExecution stepExecution;
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MapJobRepositoryFactoryBean.clear();
|
||||
step.setName("step");
|
||||
MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean();
|
||||
factory.setTransactionManager(new ResourcelessTransactionManager());
|
||||
jobRepository = (JobRepository) factory.getObject();
|
||||
step.setJobRepository(jobRepository);
|
||||
JobExecution jobExecution = jobRepository.createJobExecution("job", new JobParameters());
|
||||
stepExecution = jobExecution.createStepExecution("step");
|
||||
jobRepository.add(stepExecution);
|
||||
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
|
||||
jobLauncher.setJobRepository(jobRepository);
|
||||
jobLauncher.afterPropertiesSet();
|
||||
step.setJobLauncher(jobLauncher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.core.step.job.JobStep#afterPropertiesSet()}
|
||||
* .
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testAfterPropertiesSet() throws Exception {
|
||||
step.afterPropertiesSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.core.step.job.JobStep#afterPropertiesSet()}
|
||||
* .
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testAfterPropertiesSetWithNoLauncher() throws Exception {
|
||||
step.setJob(new JobSupport("child"));
|
||||
step.setJobLauncher(null);
|
||||
step.afterPropertiesSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.core.step.AbstractStep#execute(org.springframework.batch.core.StepExecution)}
|
||||
* .
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteSunnyDay() throws Exception {
|
||||
step.setJob(new JobSupport("child") {
|
||||
@Override
|
||||
public void execute(JobExecution execution) throws UnexpectedJobExecutionException {
|
||||
execution.setStatus(BatchStatus.COMPLETED);
|
||||
execution.setEndTime(new Date());
|
||||
}
|
||||
});
|
||||
step.afterPropertiesSet();
|
||||
step.execute(stepExecution);
|
||||
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
|
||||
assertTrue("Missing job parameters in execution context: " + stepExecution.getExecutionContext(), stepExecution
|
||||
.getExecutionContext().containsKey(JobStep.class.getName() + ".JOB_PARAMETERS"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteFailure() throws Exception {
|
||||
step.setJob(new JobSupport("child") {
|
||||
@Override
|
||||
public void execute(JobExecution execution) throws UnexpectedJobExecutionException {
|
||||
execution.setStatus(BatchStatus.FAILED);
|
||||
execution.setEndTime(new Date());
|
||||
}
|
||||
});
|
||||
step.afterPropertiesSet();
|
||||
step.execute(stepExecution);
|
||||
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteException() throws Exception {
|
||||
step.setJob(new JobSupport("child") {
|
||||
@Override
|
||||
public void execute(JobExecution execution) throws UnexpectedJobExecutionException {
|
||||
throw new RuntimeException("FOO");
|
||||
}
|
||||
});
|
||||
step.afterPropertiesSet();
|
||||
step.execute(stepExecution);
|
||||
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
|
||||
assertEquals("FOO", stepExecution.getFailureExceptions().get(0).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteRestart() throws Exception {
|
||||
|
||||
DefaultJobParametersExtractor jobParametersExtractor = new DefaultJobParametersExtractor();
|
||||
jobParametersExtractor.setKeys(new String[] {"foo"});
|
||||
ExecutionContext executionContext = stepExecution.getExecutionContext();
|
||||
executionContext.put("foo", "bar");
|
||||
step.setJobParametersExtractor(jobParametersExtractor);
|
||||
|
||||
step.setJob(new JobSupport("child") {
|
||||
@Override
|
||||
public void execute(JobExecution execution) throws UnexpectedJobExecutionException {
|
||||
assertEquals(1, execution.getJobInstance().getJobParameters().getParameters().size());
|
||||
execution.setStatus(BatchStatus.FAILED);
|
||||
execution.setEndTime(new Date());
|
||||
jobRepository.update(execution);
|
||||
throw new RuntimeException("FOO");
|
||||
}
|
||||
@Override
|
||||
public boolean isRestartable() {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
step.afterPropertiesSet();
|
||||
step.execute(stepExecution);
|
||||
assertEquals("FOO", stepExecution.getFailureExceptions().get(0).getMessage());
|
||||
JobExecution jobExecution = stepExecution.getJobExecution();
|
||||
jobExecution.setEndTime(new Date());
|
||||
jobRepository.update(jobExecution);
|
||||
|
||||
jobExecution = jobRepository.createJobExecution("job", new JobParameters());
|
||||
stepExecution = jobExecution.createStepExecution("step");
|
||||
// In a restart the surrounding Job would set up the context like this...
|
||||
stepExecution.setExecutionContext(executionContext);
|
||||
jobRepository.add(stepExecution);
|
||||
step.execute(stepExecution);
|
||||
assertEquals("FOO", stepExecution.getFailureExceptions().get(0).getMessage());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user