OPEN - issue BATCH-890, BATCH-879: Stop transition in XML namespace, decision state support.

Added StepExecution to decider interface and some protection for multithreaded job executions.
This commit is contained in:
dsyer
2008-10-29 11:15:24 +00:00
parent a4e78858e2
commit 67f5957348
17 changed files with 416 additions and 64 deletions

View File

@@ -92,6 +92,38 @@ public class JobExecutionTests {
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
}
/**
* Test method for
* {@link org.springframework.batch.core.JobExecution#getStatus()}.
*/
@Test
public void testUpgradeStatus() {
assertEquals(BatchStatus.STARTING, execution.getStatus());
execution.upgradeStatus(BatchStatus.COMPLETED);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
}
/**
* Test method for
* {@link org.springframework.batch.core.JobExecution#pause()}.
*/
@Test
public void testPause() {
execution.pause();
assertEquals(BatchStatus.PAUSED, execution.getStatus());
}
/**
* Test method for
* {@link org.springframework.batch.core.JobExecution#getStatus()}.
*/
@Test
public void testDowngradeStatus() {
execution.setStatus(BatchStatus.FAILED);
execution.upgradeStatus(BatchStatus.COMPLETED);
assertEquals(BatchStatus.FAILED, execution.getStatus());
}
/**
* Test method for
* {@link org.springframework.batch.core.JobExecution#getJobId()}.

View File

@@ -25,6 +25,7 @@ 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.StepExecution;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
@@ -64,7 +65,7 @@ public class DecisionJobParserTests {
}
public static class TestDecider implements JobExecutionDecider {
public String decide(JobExecution jobExecution) {
public String decide(JobExecution jobExecution, StepExecution stepExecution) {
return "FOO";
}
}

View File

@@ -25,6 +25,7 @@ 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.StepExecution;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
@@ -67,7 +68,7 @@ public class StopJobParserTests {
}
public static class TestDecider implements JobExecutionDecider {
public String decide(JobExecution jobExecution) {
public String decide(JobExecution jobExecution, StepExecution stepExecution) {
return "FOO";
}
}

View File

@@ -0,0 +1,103 @@
/*
* 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.job.flow;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
/**
* @author Dave Syer
*
*/
public class EndStateTests {
private JobExecution jobExecution;
@Before
public void setUp() {
jobExecution = new JobExecution(0L);
}
/**
* Test method for {@link EndState#handle(JobFlowExecutor)}.
* @throws Exception
*/
@Test
public void testHandleRestartSunnyDay() throws Exception {
BatchStatus status = jobExecution.getStatus();
EndState state = new EndState(BatchStatus.UNKNOWN, "end");
state.handle(new JobFlowExecutorSupport() {
@Override
public JobExecution getJobExecution() {
return jobExecution;
}
});
assertEquals(status, jobExecution.getStatus());
}
/**
* Test method for {@link EndState#handle(JobFlowExecutor)}.
* @throws Exception
*/
@Test
public void testHandleOngoingSunnyDay() throws Exception {
jobExecution.createStepExecution("foo");
EndState state = new EndState(BatchStatus.UNKNOWN, "end");
state.handle(new JobFlowExecutorSupport() {
@Override
public JobExecution getJobExecution() {
return jobExecution;
}
});
assertEquals(BatchStatus.UNKNOWN, jobExecution.getStatus());
}
/**
* Test method for {@link EndState#handle(JobFlowExecutor)}.
* @throws Exception
*/
@Test
public void testHandleOngoingAttemptedDowngrade() throws Exception {
jobExecution.setStatus(BatchStatus.FAILED);
jobExecution.createStepExecution("foo");
EndState state = new EndState(BatchStatus.COMPLETED, "end");
state.handle(new JobFlowExecutorSupport() {
@Override
public JobExecution getJobExecution() {
return jobExecution;
}
});
// Can't downgrade a status - if it failed then it failed
assertEquals(BatchStatus.FAILED, jobExecution.getStatus());
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.core.job.flow;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.util.ArrayList;
@@ -214,12 +215,15 @@ public class FlowJobTests {
@Test
public void testDecisionFlow() throws Throwable {
SimpleFlow<JobFlowExecutor> flow = new SimpleFlow<JobFlowExecutor>("job");
JobExecutionDecider decider = new JobExecutionDecider() {
public String decide(JobExecution jobExecution) {
public String decide(JobExecution jobExecution, StepExecution stepExecution) {
assertNotNull(stepExecution);
return "SWITCH";
}
};
Collection<StateTransition<JobFlowExecutor>> transitions = new ArrayList<StateTransition<JobFlowExecutor>>();
transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "*", "decision"));
transitions.add(StateTransition.createStateTransition(new DecisionState(decider, "decision"), "*", "step2"));
@@ -228,14 +232,17 @@ public class FlowJobTests {
transitions.add(StateTransition.createEndStateTransition(new StepState(new StubStep("step2")), "*"));
transitions.add(StateTransition.createEndStateTransition(new StepState(new StubStep("step3")), "*"));
flow.setStateTransitions(transitions);
job.setFlow(flow);
StepExecution stepExecution = job.doExecute(jobExecution);
if (!jobExecution.getAllFailureExceptions().isEmpty()) {
throw jobExecution.getAllFailureExceptions().get(0);
}
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
assertEquals(2, jobExecution.getStepExecutions().size());
assertEquals("step3", stepExecution.getStepName());
}
@Test

View File

@@ -0,0 +1,45 @@
/*
* 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.job.flow;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInterruptedException;
import org.springframework.batch.core.StartLimitExceededException;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.batch.flow.FlowExecution;
/**
* @author Dave Syer
*
*/
public class JobFlowExecutorSupport implements JobFlowExecutor {
public String executeStep(Step step) throws JobInterruptedException, JobRestartException,
StartLimitExceededException {
return FlowExecution.COMPLETED;
}
public JobExecution getJobExecution() {
return null;
}
public StepExecution getStepExecution() {
return null;
}
}