BATCH-679: move flow abstraction to core

This commit is contained in:
dsyer
2008-10-30 07:45:26 +00:00
parent 185cc05ed4
commit 3590375d46
34 changed files with 49 additions and 57 deletions

View File

@@ -0,0 +1,243 @@
/*
* 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.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.springframework.batch.core.job.flow.FlowExecution;
import org.springframework.batch.core.job.flow.FlowExecutionException;
import org.springframework.batch.core.job.flow.FlowExecutionListenerSupport;
import org.springframework.batch.core.job.flow.SimpleFlow;
import org.springframework.batch.core.job.flow.StateTransition;
/**
* @author Dave Syer
*
*/
public class BasicFlowTests {
private SimpleFlow<Object> flow = new SimpleFlow<Object>("job");
private Object executor = "data";
@Test(expected = IllegalArgumentException.class)
public void testEmptySteps() throws Exception {
flow.setStateTransitions(Collections.<StateTransition<Object>> emptySet());
flow.afterPropertiesSet();
}
@Test(expected = IllegalArgumentException.class)
public void testNoNextStepSpecified() throws Exception {
flow.setStateTransitions(Collections.singleton(StateTransition.createStateTransition(new StateSupport<Object>(
"step"), "foo")));
flow.afterPropertiesSet();
}
@Test(expected = IllegalArgumentException.class)
public void testNoStartStep() throws Exception {
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StateSupport<Object>("step"),
FlowExecution.FAILED, "step"), StateTransition
.createEndStateTransition(new StateSupport<Object>("step"))));
flow.afterPropertiesSet();
}
@Test(expected = IllegalArgumentException.class)
public void testNoEndStep() throws Exception {
flow.setStateTransitions(Collections.singleton(StateTransition.createStateTransition(new StateSupport<Object>(
"step"), FlowExecution.FAILED, "step")));
flow.setStartStateName("step");
flow.afterPropertiesSet();
}
@Test(expected = IllegalArgumentException.class)
public void testMultipleStartSteps() throws Exception {
flow.setStateTransitions(collect(StateTransition.createEndStateTransition(new StubState("step1")),
StateTransition.createEndStateTransition(new StubState("step2"))));
flow.afterPropertiesSet();
}
@Test
public void testNoMatchForNextStep() throws Exception {
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1"), "FOO", "step2"),
StateTransition.createEndStateTransition(new StubState("step2"))));
flow.afterPropertiesSet();
try {
flow.start(executor);
fail("Expected JobExecutionException");
}
catch (FlowExecutionException e) {
// expected
String message = e.getMessage();
assertTrue("Wrong message: " + message, message.toLowerCase().contains("next state not found"));
}
}
@Test
public void testOneStep() throws Exception {
flow.setStateTransitions(Collections
.singleton(StateTransition.createEndStateTransition(new StubState("step1"))));
flow.afterPropertiesSet();
FlowExecution execution = flow.start(executor);
assertEquals(FlowExecution.COMPLETED, execution.getStatus());
assertEquals("step1", execution.getName());
}
@Test
public void testOneStepWithListenerCallsClose() throws Exception {
flow.setStateTransitions(Collections
.singleton(StateTransition.createEndStateTransition(new StubState("step1"))));
flow.afterPropertiesSet();
final List<FlowExecution> list = new ArrayList<FlowExecution>();
executor = new FlowExecutionListenerSupport() {
@Override
public void close(FlowExecution result) {
list.add(result);
}
};
FlowExecution execution = flow.start(executor);
assertEquals(1, list.size());
assertEquals(FlowExecution.COMPLETED, execution.getStatus());
assertEquals("step1", execution.getName());
}
@Test
public void testExplicitStartStep() throws Exception {
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step"),
FlowExecution.FAILED, "step"), StateTransition.createEndStateTransition(new StubState("step"))));
flow.setStartStateName("step");
flow.afterPropertiesSet();
FlowExecution execution = flow.start(executor);
assertEquals(FlowExecution.COMPLETED, execution.getStatus());
assertEquals("step", execution.getName());
}
@Test
public void testTwoSteps() throws Exception {
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1"), "step2"),
StateTransition.createEndStateTransition(new StubState("step2"))));
flow.afterPropertiesSet();
FlowExecution execution = flow.start(executor);
assertEquals(FlowExecution.COMPLETED, execution.getStatus());
assertEquals("step2", execution.getName());
}
@Test
public void testResume() throws Exception {
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1"), "step2"),
StateTransition.createEndStateTransition(new StubState("step2"))));
flow.afterPropertiesSet();
FlowExecution execution = flow.resume("step2", executor);
assertEquals(FlowExecution.COMPLETED, execution.getStatus());
assertEquals("step2", execution.getName());
}
@Test
public void testFailedStep() throws Exception {
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1") {
@Override
public String handle(Object executor) {
return FlowExecution.FAILED;
}
}, "step2"), StateTransition.createEndStateTransition(new StubState("step2"))));
flow.afterPropertiesSet();
FlowExecution execution = flow.start(executor);
assertEquals(FlowExecution.COMPLETED, execution.getStatus());
assertEquals("step2", execution.getName());
}
@Test
public void testBranching() throws Exception {
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1"), "step2"),
StateTransition.createStateTransition(new StubState("step1"), FlowExecution.COMPLETED, "step3"),
StateTransition.createEndStateTransition(new StubState("step2")), StateTransition
.createEndStateTransition(new StubState("step3"))));
flow.afterPropertiesSet();
FlowExecution execution = flow.start(executor);
assertEquals(FlowExecution.COMPLETED, execution.getStatus());
assertEquals("step3", execution.getName());
}
@Test
public void testPause() throws Exception {
flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1"), "step2"),
StateTransition.createStateTransition(new StubState("step2") {
private boolean paused = false;
@Override
public String handle(Object executor) throws Exception {
if (!paused) {
paused = true;
return FlowExecution.PAUSED;
}
paused = false;
return FlowExecution.COMPLETED;
}
}, "step3"), StateTransition.createEndStateTransition(new StubState("step3"))));
flow.afterPropertiesSet();
FlowExecution execution = flow.start(executor);
assertEquals(FlowExecution.PAUSED, execution.getStatus());
assertEquals("step2", execution.getName());
execution = flow.resume(execution.getName(), executor);
assertEquals(FlowExecution.COMPLETED, execution.getStatus());
assertEquals("step3", execution.getName());
}
private Collection<StateTransition<Object>> collect(StateTransition<Object> s1, StateTransition<Object> s2) {
Collection<StateTransition<Object>> list = new ArrayList<StateTransition<Object>>();
list.add(s1);
list.add(s2);
return list;
}
private Collection<StateTransition<Object>> collect(StateTransition<Object> s1, StateTransition<Object> s2,
StateTransition<Object> s3) {
Collection<StateTransition<Object>> list = collect(s1, s2);
list.add(s3);
return list;
}
private Collection<StateTransition<Object>> collect(StateTransition<Object> s1, StateTransition<Object> s2,
StateTransition<Object> s3, StateTransition<Object> s4) {
Collection<StateTransition<Object>> list = collect(s1, s2, s3);
list.add(s4);
return list;
}
/**
* @author Dave Syer
*
*/
private static class StubState extends StateSupport<Object> {
/**
* @param string
*/
public StubState(String string) {
super(string);
}
}
}

View File

@@ -0,0 +1,48 @@
/*
* 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.assertEquals;
import org.junit.Test;
import org.springframework.batch.core.job.flow.FlowExecutionException;
/**
* @author Dave Syer
*
*/
public class FlowExecutionExceptionTests {
/**
* Test method for {@link FlowExecutionException#FlowExecutionException(String)}.
*/
@Test
public void testFlowExecutionExceptionString() {
FlowExecutionException exception = new FlowExecutionException("foo");
assertEquals("foo", exception.getMessage());
}
/**
* Test method for {@link FlowExecutionException#FlowExecutionException(String, Throwable)}.
*/
@Test
public void testFlowExecutionExceptionStringThrowable() {
FlowExecutionException exception = new FlowExecutionException("foo", new RuntimeException("bar"));
assertEquals("foo", exception.getMessage());
assertEquals("bar", exception.getCause().getMessage());
}
}

View File

@@ -0,0 +1,77 @@
/*
* 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.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.batch.core.job.flow.FlowExecution;
/**
* @author Dave Syer
*
*/
public class FlowExecutionTests {
@Test
public void testBasicProperties() throws Exception {
FlowExecution execution = new FlowExecution("foo", "BAR");
assertEquals("foo",execution.getName());
assertEquals("BAR",execution.getStatus());
}
@Test
public void testAlphaOrdering() throws Exception {
FlowExecution first = new FlowExecution("foo", "BAR");
FlowExecution second = new FlowExecution("foo", "SPAM");
assertTrue("Should be negative",first.compareTo(second)<0);
assertTrue("Should be positive",second.compareTo(first)>0);
}
@Test
public void testEnumOrdering() throws Exception {
FlowExecution first = new FlowExecution("foo", FlowExecution.COMPLETED);
FlowExecution second = new FlowExecution("foo", FlowExecution.FAILED);
assertTrue("Should be negative",first.compareTo(second)<0);
assertTrue("Should be positive",second.compareTo(first)>0);
}
@Test
public void testEnumStartsWithOrdering() throws Exception {
FlowExecution first = new FlowExecution("foo", "COMPLETED.BAR");
FlowExecution second = new FlowExecution("foo", "FAILED.FOO");
assertTrue("Should be negative",first.compareTo(second)<0);
assertTrue("Should be positive",second.compareTo(first)>0);
}
@Test
public void testEnumStartsWithAlphaOrdering() throws Exception {
FlowExecution first = new FlowExecution("foo", "COMPLETED.BAR");
FlowExecution second = new FlowExecution("foo", "COMPLETED.FOO");
assertTrue("Should be negative",first.compareTo(second)<0);
assertTrue("Should be positive",second.compareTo(first)>0);
}
@Test
public void testEnumAndAlpha() throws Exception {
FlowExecution first = new FlowExecution("foo", "ZZZZZ");
FlowExecution second = new FlowExecution("foo", "FAILED.FOO");
assertTrue("Should be negative",first.compareTo(second)<0);
assertTrue("Should be positive",second.compareTo(first)>0);
}
}

View File

@@ -35,8 +35,6 @@ import org.springframework.batch.core.UnexpectedJobExecutionException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.core.step.StepSupport;
import org.springframework.batch.flow.SimpleFlow;
import org.springframework.batch.flow.StateTransition;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;

View File

@@ -21,7 +21,6 @@ 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

View File

@@ -0,0 +1,50 @@
/*
* 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.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import org.springframework.batch.core.job.flow.FlowExecution;
import org.springframework.batch.core.job.flow.MaxValueFlowExecutionAggregator;
/**
* @author Dave Syer
*
*/
public class SimpleFlowExecutionAggregatorTests {
private MaxValueFlowExecutionAggregator aggregator = new MaxValueFlowExecutionAggregator();
@Test
public void testFailed() throws Exception {
FlowExecution first = new FlowExecution("foo", FlowExecution.COMPLETED);
FlowExecution second = new FlowExecution("foo", FlowExecution.FAILED);
assertTrue("Should be negative", first.compareTo(second)<0);
assertTrue("Should be positive", second.compareTo(first)>0);
assertEquals(FlowExecution.FAILED, aggregator.aggregate(Arrays.asList(first, second)));
}
@Test
public void testEmpty() throws Exception {
assertEquals(FlowExecution.UNKNOWN, aggregator.aggregate(Collections.<FlowExecution> emptySet()));
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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.assertEquals;
import java.util.ArrayList;
import java.util.Collection;
import org.easymock.EasyMock;
import org.junit.Test;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.job.flow.FlowExecution;
import org.springframework.batch.core.job.flow.SplitState;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
/**
* @author Dave Syer
*
*/
public class SplitStateTests {
@Test
public void testBasicHandling() throws Exception {
Collection<Flow<Object>> flows = new ArrayList<Flow<Object>>();
@SuppressWarnings("unchecked")
Flow<Object> flow1 = EasyMock.createMock(Flow.class);
@SuppressWarnings("unchecked")
Flow<Object> flow2 = EasyMock.createMock(Flow.class);
flows.add(flow1);
flows.add(flow2);
SplitState<Object> state = new SplitState<Object>(flows, "foo");
EasyMock.expect(flow1.start(null)).andReturn(new FlowExecution("step1", FlowExecution.COMPLETED));
EasyMock.expect(flow2.start(null)).andReturn(new FlowExecution("step1", FlowExecution.COMPLETED));
EasyMock.replay(flow1, flow2);
String result = state.handle(null);
assertEquals(FlowExecution.COMPLETED, result);
EasyMock.verify(flow1, flow2);
}
@Test
public void testConcurrentHandling() throws Exception {
Collection<Flow<Object>> flows = new ArrayList<Flow<Object>>();
@SuppressWarnings("unchecked")
Flow<Object> flow1 = EasyMock.createMock(Flow.class);
@SuppressWarnings("unchecked")
Flow<Object> flow2 = EasyMock.createMock(Flow.class);
flows.add(flow1);
flows.add(flow2);
SplitState<Object> state = new SplitState<Object>(flows, "foo");
state.setTaskExecutor(new SimpleAsyncTaskExecutor());
EasyMock.expect(flow1.start(null)).andReturn(new FlowExecution("step1", FlowExecution.COMPLETED));
EasyMock.expect(flow2.start(null)).andReturn(new FlowExecution("step1", FlowExecution.COMPLETED));
EasyMock.replay(flow1, flow2);
String result = state.handle(null);
assertEquals(FlowExecution.COMPLETED, result);
EasyMock.verify(flow1, flow2);
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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.job.flow.AbstractState;
import org.springframework.batch.core.job.flow.FlowExecution;
import org.springframework.batch.core.job.flow.State;
/**
* Base class for {@link State} implementations.
*
* @author Dave Syer
*
*/
public class StateSupport<T> extends AbstractState<T> {
/**
* @param name
*/
public StateSupport(String name) {
super(name);
}
@Override
public String handle(T context) throws Exception {
return FlowExecution.COMPLETED;
}
}

View File

@@ -0,0 +1,130 @@
/*
* 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.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.batch.core.job.flow.StateTransition;
/**
* @author Dave Syer
*
*/
public class StateTransitionTests {
@Test
public void testIsEnd() {
StateTransition<String> transition = StateTransition.createEndStateTransition(null, "");
assertTrue(transition.isEnd());
assertNull(transition.getNext());
}
@Test
public void testMatchesStar() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "*", "start");
assertTrue(transition.matches("CONTINUABLE"));
}
@Test
public void testMatchesNull() {
StateTransition<String> transition = StateTransition.createStateTransition(null, null, "start");
assertTrue(transition.matches("CONTINUABLE"));
}
@Test
public void testMatchesEmpty() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "", "start");
assertTrue(transition.matches("CONTINUABLE"));
}
@Test
public void testMatchesExact() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "CONTINUABLE", "start");
assertTrue(transition.matches("CONTINUABLE"));
}
@Test
public void testMatchesWildcard() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "CONTIN*", "start");
assertTrue(transition.matches("CONTINUABLE"));
}
@Test
public void testMatchesPlaceholder() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "CONTIN???LE", "start");
assertTrue(transition.matches("CONTINUABLE"));
}
@Test
public void testSimpleOrderingEqual() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "CONTIN???LE", "start");
assertEquals(0, transition.compareTo(transition));
}
@Test
public void testSimpleOrderingMoreGeneral() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "CONTIN???LE", "start");
StateTransition<String> other = StateTransition.createStateTransition(null, "CONTINUABLE", "start");
assertEquals(1, transition.compareTo(other));
assertEquals(-1, other.compareTo(transition));
}
@Test
public void testSimpleOrderingMostGeneral() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "*", "start");
StateTransition<String> other = StateTransition.createStateTransition(null, "CONTINUABLE", "start");
assertEquals(1, transition.compareTo(other));
assertEquals(-1, other.compareTo(transition));
}
@Test
public void testSubstringAndWildcard() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "CONTIN*", "start");
StateTransition<String> other = StateTransition.createStateTransition(null, "CONTINUABLE", "start");
assertEquals(1, transition.compareTo(other));
assertEquals(-1, other.compareTo(transition));
}
@Test
public void testSimpleOrderingMostToNextGeneral() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "*", "start");
StateTransition<String> other = StateTransition.createStateTransition(null, "C?", "start");
assertEquals(1, transition.compareTo(other));
assertEquals(-1, other.compareTo(transition));
}
@Test
public void testSimpleOrderingAdjacent() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "CON*", "start");
StateTransition<String> other = StateTransition.createStateTransition(null, "CON?", "start");
assertEquals(1, transition.compareTo(other));
assertEquals(-1, other.compareTo(transition));
}
@Test
public void testToString() {
StateTransition<String> transition = StateTransition.createStateTransition(null, "CONTIN???LE", "start");
String string = transition.toString();
assertTrue("Wrong string: " + string, string.contains("Transition"));
assertTrue("Wrong string: " + string, string.contains("start"));
assertTrue("Wrong string: " + string, string.contains("CONTIN???LE"));
assertTrue("Wrong string: " + string, string.contains("next="));
}
}