BATCH-1011:

* Moved logic for updating JobExecution status based on last StepExecution from AbstractJob to SimpleJob because it does not apply to FlowJob.
  * A job now can have a status different from any of its steps.  For example, the following job will be COMPLETED while the step will be FAILED:
    <step id="failingStep">
       <end on="FAILED" /> 
    </step>
* Added error checking to StepParser to make sure the same pattern does not appear more than once.
* Modified EndState.handle() so that the ExitStatus is only updated if BatchStatus is updated.
  * If a job contains a <split> such that there is more than one EndState reached, the Job's status will be the highest precedence of all the statuses.  For example, the following job will be FAILED:
    <split id="split1">
      <flow>
        <step name="failingStep"/>
      </flow>
      <flow>
        <step name="step1"/>
      </flow>
    </split>
* If a step has no transitions defined, then the default will be:
    <fail on="FAILED" />
    <end on="*" />
This commit is contained in:
dhgarrette
2009-02-06 23:13:21 +00:00
parent 50acf7c670
commit dfb78ff0ca
24 changed files with 739 additions and 326 deletions

View File

@@ -0,0 +1,78 @@
/*
* 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.fail;
import java.util.ArrayList;
import org.junit.Before;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author Dan Garrette
* @since 2.0
*/
public abstract class AbstractJobParserTests {
@Autowired
protected Job job;
@Autowired
private JobRepository jobRepository;
@Autowired
protected ArrayList<String> stepNamesList = new ArrayList<String>();
@Before
public void setUp() {
MapJobRepositoryFactoryBean.clear();
stepNamesList.clear();
}
/**
* @return JobExecution
*/
protected JobExecution createJobExecution() throws JobInstanceAlreadyCompleteException, JobRestartException,
JobExecutionAlreadyRunningException {
return jobRepository.createJobExecution(job.getName(), new JobParameters());
}
/**
* @param jobExecution
* @param stepName
* @return the StepExecution corresponding to the specified step
*/
protected StepExecution getStepExecution(JobExecution jobExecution, String stepName) {
for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
if (stepExecution.getStepName().equals(stepName)) {
return stepExecution;
}
}
fail("No stepExecution found with name: [" + stepName + "]");
return null;
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dan Garrette
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class DefaultFailureJobParserTests extends AbstractJobParserTests {
@Test
public void testDefaultFailure() throws Exception {
JobExecution jobExecution = createJobExecution();
job.execute(jobExecution);
assertEquals(2, stepNamesList.size());
assertTrue(stepNamesList.contains("step1"));
assertTrue(stepNamesList.contains("failingStep"));
assertEquals(BatchStatus.FAILED, jobExecution.getStatus());
assertEquals(ExitStatus.FAILED, jobExecution.getExitStatus());
StepExecution stepExecution1 = getStepExecution(jobExecution, "step1");
assertEquals(BatchStatus.COMPLETED, stepExecution1.getStatus());
assertEquals(ExitStatus.COMPLETED, stepExecution1.getExitStatus());
StepExecution stepExecution2 = getStepExecution(jobExecution, "failingStep");
assertEquals(BatchStatus.FAILED, stepExecution2.getStatus());
assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution2.getExitStatus().getExitCode());
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dan Garrette
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class DefaultSuccessJobParserTests extends AbstractJobParserTests {
@Test
public void testDefaultSuccess() throws Exception {
JobExecution jobExecution = createJobExecution();
job.execute(jobExecution);
assertEquals(2, stepNamesList.size());
assertTrue(stepNamesList.contains("step1"));
assertTrue(stepNamesList.contains("step2"));
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
StepExecution stepExecution1 = getStepExecution(jobExecution, "step1");
assertEquals(BatchStatus.COMPLETED, stepExecution1.getStatus());
assertEquals(ExitStatus.COMPLETED, stepExecution1.getExitStatus());
StepExecution stepExecution2 = getStepExecution(jobExecution, "step2");
assertEquals(BatchStatus.COMPLETED, stepExecution2.getStatus());
assertEquals(ExitStatus.COMPLETED, stepExecution2.getExitStatus());
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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 org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.ClassUtils;
/**
* @author Dan Garrette
* @since 2.0
*/
@RunWith(JUnit4ClassRunner.class)
public class DuplicateTransitionJobParserTests extends AbstractJobParserTests {
@Test(expected = BeanDefinitionStoreException.class)
public void testNextAttributeWithNestedElement() throws Exception {
new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(getClass(),
"NextAttributeMultipleFinalJobParserTests-context.xml"));
}
@Test(expected = BeanDefinitionStoreException.class)
public void testDuplicateTransition() throws Exception {
new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(getClass(),
"DuplicateTransitionJobParserTests-context.xml"));
}
}

View File

@@ -16,20 +16,14 @@
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 java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
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.batch.core.StepExecution;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -39,33 +33,22 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class EndTransitionDefaultStatusJobParserTests {
@Autowired
private Job job;
@Autowired
private JobRepository jobRepository;
@Autowired
private ArrayList<String> stepNamesList;
@Before
public void setUp() {
MapJobRepositoryFactoryBean.clear();
}
public class EndTransitionDefaultStatusJobParserTests extends AbstractJobParserTests {
@Test
public void testEndTransitionDefaultStatus() throws Exception {
assertNotNull(job);
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters());
JobExecution jobExecution = createJobExecution();
job.execute(jobExecution);
assertEquals(1, stepNamesList.size());
assertTrue(stepNamesList.contains("failingStep"));
// TODO: BATCH-1011
// assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
// assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
StepExecution stepExecution1 = getStepExecution(jobExecution, "failingStep");
assertEquals(BatchStatus.FAILED, stepExecution1.getStatus());
assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution1.getExitStatus().getExitCode());
}
}

View File

@@ -16,21 +16,16 @@
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 static org.junit.Assert.fail;
import java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
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;
@@ -40,51 +35,43 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class EndTransitionJobParserTests {
@Autowired
private Job job;
@Autowired
private JobRepository jobRepository;
@Autowired
private ArrayList<String> stepNamesList;
@Before
public void setUp() {
MapJobRepositoryFactoryBean.clear();
}
public class EndTransitionJobParserTests extends AbstractJobParserTests {
@Test
public void testEndTransition() throws Exception {
//
// First Launch
//
assertNotNull(job);
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters());
JobExecution jobExecution = createJobExecution();
job.execute(jobExecution);
assertEquals(2, stepNamesList.size());
assertTrue(stepNamesList.contains("step1"));
assertTrue(stepNamesList.contains("failingStep"));
// TODO: BATCH-1011
// assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
// assertEquals("EARLY TERMINATION (FAIL)", jobExecution.getExitStatus().getExitCode());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
assertEquals("EARLY TERMINATION (FAIL)", jobExecution.getExitStatus().getExitCode());
StepExecution stepExecution1 = getStepExecution(jobExecution, "step1");
assertEquals(BatchStatus.COMPLETED, stepExecution1.getStatus());
assertEquals(ExitStatus.COMPLETED, stepExecution1.getExitStatus());
StepExecution stepExecution2 = getStepExecution(jobExecution, "failingStep");
assertEquals(BatchStatus.FAILED, stepExecution2.getStatus());
assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution2.getExitStatus().getExitCode());
//
// Second Launch
//
stepNamesList.clear();
try{
jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters());
// TODO: BATCH-1011
//fail("JobInstanceAlreadyCompleteException expected");
try {
jobExecution = createJobExecution();
fail("JobInstanceAlreadyCompleteException expected");
} catch (JobInstanceAlreadyCompleteException e) {
//
// Expected
//
}
catch(JobInstanceAlreadyCompleteException e)
{
}
}
}

View File

@@ -16,21 +16,14 @@
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 java.util.ArrayList;
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.ExitStatus;
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.batch.core.StepExecution;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -40,33 +33,22 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class FailTransitionDefaultStatusJobParserTests {
@Autowired
private Job job;
@Autowired
private JobRepository jobRepository;
@Autowired
private ArrayList<String> stepNamesList;
@Before
public void setUp() {
MapJobRepositoryFactoryBean.clear();
}
public class FailTransitionDefaultStatusJobParserTests extends AbstractJobParserTests {
@Test
public void testFailTransitionDefaultStatus() throws Exception {
assertNotNull(job);
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters());
JobExecution jobExecution = createJobExecution();
job.execute(jobExecution);
assertEquals(1, stepNamesList.size());
assertTrue(stepNamesList.contains("step1"));
assertEquals(BatchStatus.FAILED, jobExecution.getStatus());
// TODO: BATCH-1011
// assertEquals(ExitStatus.FAILED, jobExecution.getExitStatus());
assertEquals(ExitStatus.FAILED, jobExecution.getExitStatus());
StepExecution stepExecution1 = getStepExecution(jobExecution, "step1");
assertEquals(BatchStatus.COMPLETED, stepExecution1.getStatus());
assertEquals(ExitStatus.COMPLETED, stepExecution1.getExitStatus());
}

View File

@@ -16,21 +16,14 @@
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 java.util.ArrayList;
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.ExitStatus;
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.batch.core.StepExecution;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -40,49 +33,46 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class FailTransitionJobParserTests {
@Autowired
private Job job;
@Autowired
private JobRepository jobRepository;
@Autowired
private ArrayList<String> stepNamesList;
@Before
public void setUp() {
MapJobRepositoryFactoryBean.clear();
}
public class FailTransitionJobParserTests extends AbstractJobParserTests {
@Test
public void testFailTransition() throws Exception {
//
// First Launch
//
assertNotNull(job);
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters());
JobExecution jobExecution = createJobExecution();
job.execute(jobExecution);
assertEquals(2, stepNamesList.size());
assertTrue(stepNamesList.contains("step1"));
assertTrue(stepNamesList.contains("failingStep"));
assertEquals(BatchStatus.FAILED, jobExecution.getStatus());
// TODO: BATCH-1011
// assertEquals("EARLY TERMINATION (FAIL)", jobExecution.getExitStatus().getExitCode());
assertEquals("EARLY TERMINATION (FAIL)", jobExecution.getExitStatus().getExitCode());
StepExecution stepExecution1 = getStepExecution(jobExecution, "step1");
assertEquals(BatchStatus.COMPLETED, stepExecution1.getStatus());
assertEquals(ExitStatus.COMPLETED, stepExecution1.getExitStatus());
StepExecution stepExecution2 = getStepExecution(jobExecution, "failingStep");
assertEquals(BatchStatus.FAILED, stepExecution2.getStatus());
assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution2.getExitStatus().getExitCode());
//
// Second Launch
//
stepNamesList.clear();
jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters());
jobExecution = createJobExecution();
job.execute(jobExecution);
assertEquals(1, stepNamesList.size()); //step1 is not executed
assertEquals(1, stepNamesList.size()); // step1 is not executed
assertTrue(stepNamesList.contains("failingStep"));
assertEquals(BatchStatus.FAILED, jobExecution.getStatus());
// TODO: BATCH-1011
// assertEquals("EARLY TERMINATION (FAIL)", jobExecution.getExitStatus().getExitCode());
assertEquals("EARLY TERMINATION (FAIL)", jobExecution.getExitStatus().getExitCode());
StepExecution stepExecution3 = getStepExecution(jobExecution, "failingStep");
assertEquals(BatchStatus.FAILED, stepExecution3.getStatus());
assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution3.getExitStatus().getExitCode());
}

View File

@@ -17,8 +17,6 @@ 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 static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
@@ -29,12 +27,9 @@ 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.BeanDefinitionStoreException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.ClassUtils;
/**
* @author Dave Syer
@@ -64,18 +59,4 @@ public class NextAttributeJobParserTests {
assertEquals(2, jobExecution.getStepExecutions().size());
}
@Test
public void testNextAttributeWithNestedElement() throws Exception {
try {
new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(getClass(),
"NextAttributeMultipleFinalJobParserTests-context.xml"));
fail("Expected BeanCreationException");
}
catch (BeanDefinitionStoreException e) {
// expected
String message = e.getMessage();
assertTrue("Wrong message; " + message, message.contains("Duplicate transition pattern"));
}
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dan Garrette
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class SplitDifferentResultsFailFirstJobParserTests extends AbstractJobParserTests {
@Test
public void testSplitDifferentResultsFailFirst() throws Exception {
JobExecution jobExecution = createJobExecution();
job.execute(jobExecution);
assertEquals(2, stepNamesList.size());
assertTrue(stepNamesList.contains("step1"));
assertTrue(stepNamesList.contains("failingStep"));
assertEquals(BatchStatus.FAILED, jobExecution.getStatus());
assertEquals(ExitStatus.FAILED, jobExecution.getExitStatus());
StepExecution stepExecution1 = getStepExecution(jobExecution, "step1");
assertEquals(BatchStatus.COMPLETED, stepExecution1.getStatus());
assertEquals(ExitStatus.COMPLETED, stepExecution1.getExitStatus());
StepExecution stepExecution2 = getStepExecution(jobExecution, "failingStep");
assertEquals(BatchStatus.FAILED, stepExecution2.getStatus());
assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution2.getExitStatus().getExitCode());
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dan Garrette
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class SplitDifferentResultsFailSecondJobParserTests extends AbstractJobParserTests {
@Test
public void testSplitDifferentResultsFailSecond() throws Exception {
JobExecution jobExecution = createJobExecution();
job.execute(jobExecution);
assertEquals(2, stepNamesList.size());
assertTrue(stepNamesList.contains("step1"));
assertTrue(stepNamesList.contains("failingStep"));
assertEquals(BatchStatus.FAILED, jobExecution.getStatus());
assertEquals(ExitStatus.FAILED, jobExecution.getExitStatus());
StepExecution stepExecution1 = getStepExecution(jobExecution, "step1");
assertEquals(BatchStatus.COMPLETED, stepExecution1.getStatus());
assertEquals(ExitStatus.COMPLETED, stepExecution1.getExitStatus());
StepExecution stepExecution2 = getStepExecution(jobExecution, "failingStep");
assertEquals(BatchStatus.FAILED, stepExecution2.getStatus());
assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution2.getExitStatus().getExitCode());
}
}

View File

@@ -16,25 +16,15 @@
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 java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
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.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -44,50 +34,42 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class StopJobParserTests {
@Autowired
@Qualifier("job")
private Job job;
@Autowired
private JobRepository jobRepository;
@Autowired
private ArrayList<String> stepNamesList;
@Before
public void setUp() {
MapJobRepositoryFactoryBean.clear();
}
public class StopJobParserTests extends AbstractJobParserTests {
@Test
public void testStopState() throws Exception {
assertNotNull(job);
//
// First Launch
//
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters());
JobExecution jobExecution = createJobExecution();
job.execute(jobExecution);
assertEquals(1, stepNamesList.size());
assertTrue(stepNamesList.contains("step1"));
assertEquals(BatchStatus.STOPPED, jobExecution.getStatus());
// TODO: BATCH-1011
//assertEquals(BatchStatus.STOPPED.toString(), jobExecution.getExitStatus().getExitCode());
assertEquals(ExitStatus.FAILED.getExitCode(), jobExecution.getExitStatus().getExitCode());
StepExecution stepExecution1 = getStepExecution(jobExecution, "step1");
assertEquals(BatchStatus.COMPLETED, stepExecution1.getStatus());
assertEquals(ExitStatus.COMPLETED, stepExecution1.getExitStatus());
//
// Second Launch
//
stepNamesList.clear();
jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters());
jobExecution = createJobExecution();
job.execute(jobExecution);
assertEquals(1, stepNamesList.size()); //step1 is not executed
assertEquals(1, stepNamesList.size()); // step1 is not executed
assertTrue(stepNamesList.contains("step2"));
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
StepExecution stepExecution2 = getStepExecution(jobExecution, "step2");
assertEquals(BatchStatus.COMPLETED, stepExecution2.getStatus());
assertEquals(ExitStatus.COMPLETED, stepExecution2.getExitStatus());
}
public static class TestDecider implements JobExecutionDecider {

View File

@@ -167,8 +167,7 @@ public class AbstractJobTests {
}
@Override
protected StepExecution doExecute(JobExecution execution) throws JobExecutionException {
return null;
protected void doExecute(JobExecution execution) throws JobExecutionException {
}
@Override

View File

@@ -21,7 +21,6 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
@@ -78,7 +77,8 @@ public class FlowJobTests {
flow.setStateTransitions(transitions);
job.setFlow(flow);
job.afterPropertiesSet();
StepExecution stepExecution = job.doExecute(jobExecution);
job.doExecute(jobExecution);
StepExecution stepExecution = getStepExecution(jobExecution, "step2");
assertEquals(ExitStatus.COMPLETED, stepExecution.getExitStatus());
assertEquals(2, jobExecution.getStepExecutions().size());
}
@@ -99,7 +99,8 @@ public class FlowJobTests {
flow.setStateTransitions(transitions);
job.setFlow(flow);
job.afterPropertiesSet();
StepExecution stepExecution = job.doExecute(jobExecution);
job.doExecute(jobExecution);
StepExecution stepExecution = getStepExecution(jobExecution, "step2");
assertEquals(ExitStatus.COMPLETED, stepExecution.getExitStatus());
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
assertEquals(2, jobExecution.getStepExecutions().size());
@@ -118,7 +119,7 @@ public class FlowJobTests {
jobRepository.update(stepExecution);
}
}), "step2"));
transitions.add(StateTransition.createEndStateTransition(new StepState(new StubStep("step2") {
transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2") {
@Override
public void execute(StepExecution stepExecution) throws JobInterruptedException,
UnexpectedJobExecutionException {
@@ -130,7 +131,22 @@ public class FlowJobTests {
super.execute(stepExecution);
}
}
})));
}), ExitStatus.COMPLETED.getExitCode(), "end0"));
transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2") {
@Override
public void execute(StepExecution stepExecution) throws JobInterruptedException,
UnexpectedJobExecutionException {
if (fail) {
stepExecution.setStatus(BatchStatus.FAILED);
stepExecution.setExitStatus(ExitStatus.FAILED);
jobRepository.update(stepExecution);
} else {
super.execute(stepExecution);
}
}
}), ExitStatus.FAILED.getExitCode(), "end1"));
transitions.add(StateTransition.createEndStateTransition(new EndState(BatchStatus.COMPLETED, "end0")));
transitions.add(StateTransition.createEndStateTransition(new EndState(BatchStatus.FAILED, "end1")));
flow.setStateTransitions(transitions);
job.setFlow(flow);
job.afterPropertiesSet();
@@ -211,7 +227,10 @@ public class FlowJobTests {
List<StateTransition> transitions = new ArrayList<StateTransition>();
transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "end"));
transitions.add(StateTransition.createStateTransition(new EndState(BatchStatus.STOPPED, "end"), "step2"));
transitions.add(StateTransition.createEndStateTransition(new StepState(new StubStep("step2"))));
transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), ExitStatus.COMPLETED.getExitCode(), "end0"));
transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), ExitStatus.FAILED.getExitCode(), "end1"));
transitions.add(StateTransition.createEndStateTransition(new EndState(BatchStatus.COMPLETED, "end0")));
transitions.add(StateTransition.createEndStateTransition(new EndState(BatchStatus.FAILED, "end1")));
flow.setStateTransitions(transitions);
job.setFlow(flow);
job.afterPropertiesSet();
@@ -240,18 +259,20 @@ public class FlowJobTests {
flow.setStateTransitions(transitions);
job.setFlow(flow);
job.afterPropertiesSet();
StepExecution stepExecution = job.doExecute(jobExecution);
job.doExecute(jobExecution);
StepExecution stepExecution = getStepExecution(jobExecution, "step3");
assertEquals(ExitStatus.COMPLETED, stepExecution.getExitStatus());
assertEquals(2, jobExecution.getStepExecutions().size());
assertEquals("step3", stepExecution.getStepName());
}
@Test
public void testBasicFlow() throws Throwable {
SimpleFlow flow = new SimpleFlow("job");
Step step = new StubStep("step");
flow.setStateTransitions(Collections.singletonList(StateTransition.createEndStateTransition(
new StepState(step), "*")));
List<StateTransition> transitions = new ArrayList<StateTransition>();
transitions.add(StateTransition.createStateTransition(new StepState(step), "end0"));
transitions.add(StateTransition.createEndStateTransition(new EndState(BatchStatus.COMPLETED, "end0")));
flow.setStateTransitions(transitions);
job.setFlow(flow);
job.execute(jobExecution);
if (!jobExecution.getAllFailureExceptions().isEmpty()) {
@@ -281,14 +302,14 @@ public class FlowJobTests {
flow.setStateTransitions(transitions);
job.setFlow(flow);
StepExecution stepExecution = job.doExecute(jobExecution);
job.doExecute(jobExecution);
StepExecution stepExecution = getStepExecution(jobExecution, "step3");
if (!jobExecution.getAllFailureExceptions().isEmpty()) {
throw jobExecution.getAllFailureExceptions().get(0);
}
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
assertEquals(2, jobExecution.getStepExecutions().size());
assertEquals("step3", stepExecution.getStepName());
}
@@ -360,4 +381,20 @@ public class FlowJobTests {
}
/**
* @param jobExecution
* @param stepName
* @return the StepExecution corresponding to the specified step
*/
private StepExecution getStepExecution(JobExecution jobExecution, String stepName)
{
for(StepExecution stepExecution : jobExecution.getStepExecutions()) {
if(stepExecution.getStepName().equals(stepName)) {
return stepExecution;
}
}
fail("No stepExecution found with name: [" + stepName + "]");
return null;
}
}