diff --git a/execution/src/main/java/org/springframework/batch/execution/tasklet/support/InputSourceItemProvider.java b/execution/src/main/java/org/springframework/batch/execution/tasklet/support/InputSourceItemProvider.java index 6ede52db3..c98d272b2 100644 --- a/execution/src/main/java/org/springframework/batch/execution/tasklet/support/InputSourceItemProvider.java +++ b/execution/src/main/java/org/springframework/batch/execution/tasklet/support/InputSourceItemProvider.java @@ -1,91 +1,98 @@ -/* - * 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.execution.tasklet.support; - -import java.util.Properties; - -import org.springframework.batch.io.InputSource; -import org.springframework.batch.item.provider.AbstractItemProvider; -import org.springframework.batch.restart.RestartData; -import org.springframework.batch.restart.Restartable; -import org.springframework.batch.statistics.StatisticsProvider; - -/** - * Simple wrapper around {@link InputSource}. The input source is expected to - * take care of open and close operations. If necessary it should be registered - * as a step scoped bean to ensure that the lifecycle methods are called. - * - * @auther Dave Syer - */ -public class InputSourceItemProvider extends AbstractItemProvider implements Restartable, StatisticsProvider { - - private InputSource source; - - /** - * Get the next object from the input source. - * @see org.springframework.batch.item.ItemProvider#next() - */ - public Object next() { - Object value = source.read(); - return value; - } - - /** - * @see Restartable#getRestartData() - * @throws IllegalStateException if the parent template is not itself - * {@link Restartable}. - */ - public RestartData getRestartData() { - if (!(source instanceof Restartable)) { - throw new IllegalStateException("Input Template is not Restartable"); - } - return ((Restartable) source).getRestartData(); - } - - /** - * @see Restartable#restoreFrom(RestartData) - * @throws IllegalStateException if the parent template is not itself - * {@link Restartable}. - */ - public void restoreFrom(RestartData data) { - if (!(source instanceof Restartable)) { - throw new IllegalStateException("Input Template is not Restartable"); - } - ((Restartable) source).restoreFrom(data); - } - - /** - * @return delegates to the parent template of it is a - * {@link StatisticsProvider}, otherwise returns an empty - * {@link Properties} instance. - * @see StatisticsProvider#getStatistics() - */ - public Properties getStatistics() { - if (!(source instanceof StatisticsProvider)) { - return new Properties(); - } - return ((StatisticsProvider) source).getStatistics(); - } - - /** - * Setter for input source. - * @param source - */ - public void setInputSource(InputSource source) { - this.source = source; - } -} +/* + * 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.execution.tasklet.support; + +import java.util.Properties; + +import org.springframework.batch.io.InputSource; +import org.springframework.batch.io.Skippable; +import org.springframework.batch.item.provider.AbstractItemProvider; +import org.springframework.batch.restart.RestartData; +import org.springframework.batch.restart.Restartable; +import org.springframework.batch.statistics.StatisticsProvider; + +/** + * Simple wrapper around {@link InputSource}. The input source is expected to + * take care of open and close operations. If necessary it should be registered + * as a step scoped bean to ensure that the lifecycle methods are called. + * + * @author Dave Syer + */ +public class InputSourceItemProvider extends AbstractItemProvider implements Restartable, StatisticsProvider, Skippable { + + private InputSource source; + + /** + * Get the next object from the input source. + * @see org.springframework.batch.item.ItemProvider#next() + */ + public Object next() { + Object value = source.read(); + return value; + } + + /** + * @see Restartable#getRestartData() + * @throws IllegalStateException if the parent template is not itself + * {@link Restartable}. + */ + public RestartData getRestartData() { + if (!(source instanceof Restartable)) { + throw new IllegalStateException("Input Template is not Restartable"); + } + return ((Restartable) source).getRestartData(); + } + + /** + * @see Restartable#restoreFrom(RestartData) + * @throws IllegalStateException if the parent template is not itself + * {@link Restartable}. + */ + public void restoreFrom(RestartData data) { + if (!(source instanceof Restartable)) { + throw new IllegalStateException("Input Template is not Restartable"); + } + ((Restartable) source).restoreFrom(data); + } + + /** + * @return delegates to the parent template of it is a + * {@link StatisticsProvider}, otherwise returns an empty + * {@link Properties} instance. + * @see StatisticsProvider#getStatistics() + */ + public Properties getStatistics() { + if (!(source instanceof StatisticsProvider)) { + return new Properties(); + } + return ((StatisticsProvider) source).getStatistics(); + } + + /** + * Setter for input source. + * @param source + */ + public void setInputSource(InputSource source) { + this.source = source; + } + + public void skip() { + if (source instanceof Skippable) { + ((Skippable)source).skip(); + } + } +} diff --git a/execution/src/main/java/org/springframework/batch/execution/tasklet/support/OutputSourceItemProcessor.java b/execution/src/main/java/org/springframework/batch/execution/tasklet/support/OutputSourceItemProcessor.java index 6b26d9f28..4cb0996bb 100644 --- a/execution/src/main/java/org/springframework/batch/execution/tasklet/support/OutputSourceItemProcessor.java +++ b/execution/src/main/java/org/springframework/batch/execution/tasklet/support/OutputSourceItemProcessor.java @@ -3,6 +3,7 @@ package org.springframework.batch.execution.tasklet.support; import java.util.Properties; import org.springframework.batch.io.OutputSource; +import org.springframework.batch.io.Skippable; import org.springframework.batch.item.ItemProcessor; import org.springframework.batch.restart.RestartData; import org.springframework.batch.restart.Restartable; @@ -14,7 +15,7 @@ import org.springframework.batch.statistics.StatisticsProvider; * * @author Dave Syer */ -public class OutputSourceItemProcessor implements ItemProcessor, Restartable, +public class OutputSourceItemProcessor implements ItemProcessor, Restartable, Skippable, StatisticsProvider { private OutputSource source; @@ -72,4 +73,10 @@ public class OutputSourceItemProcessor implements ItemProcessor, Restartable, return ((StatisticsProvider) source).getStatistics(); } + public void skip() { + if (source instanceof Skippable) { + ((Skippable)source).skip(); + } + } + } diff --git a/execution/src/test/java/org/springframework/batch/execution/tasklet/support/InputSourceItemProviderTests.java b/execution/src/test/java/org/springframework/batch/execution/tasklet/support/InputSourceItemProviderTests.java index ed3407e83..ae0a75ea5 100644 --- a/execution/src/test/java/org/springframework/batch/execution/tasklet/support/InputSourceItemProviderTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/tasklet/support/InputSourceItemProviderTests.java @@ -1,113 +1,123 @@ -/* - * 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.execution.tasklet.support; - -import java.util.Properties; - -import junit.framework.TestCase; - -import org.springframework.batch.execution.tasklet.support.InputSourceItemProvider; -import org.springframework.batch.io.InputSource; -import org.springframework.batch.restart.GenericRestartData; -import org.springframework.batch.restart.RestartData; -import org.springframework.batch.restart.Restartable; -import org.springframework.batch.statistics.StatisticsProvider; -import org.springframework.batch.support.PropertiesConverter; - -/** - * Unit test for {@link InputSourceItemProvider} - * - * @author Robert Kasanicky - */ -public class InputSourceItemProviderTests extends TestCase { - - // object under test - private InputSourceItemProvider itemProvider = new InputSourceItemProvider(); - - private InputSource source; - - // create input template and inject it to data provider - protected void setUp() throws Exception { - source = new MockInputSource(this); - itemProvider.setInputSource(source); - } - - /** - * Uses input template to provide the domain object. - */ - public void testNext() { - Object result = itemProvider.next(); - assertSame("domain object is provided by the input template", this, result); - } - - /** - * Gets statistics from the input template - */ - public void testGetStatistics() { - Properties props = itemProvider.getStatistics(); - assertEquals("b", props.getProperty("a")); - } - - /** - * Gets restart data from the input template - */ - public void testGetRestartData() { - Properties props = itemProvider.getRestartData().getProperties(); - assertEquals("foo", props.getProperty("value")); - } - - /** - * Forwared restart data to input template - */ - public void testRestoreFrom() { - itemProvider.restoreFrom(new GenericRestartData(PropertiesConverter.stringToProperties("value=bar"))); - assertEquals("bar", itemProvider.next()); - } - - private class MockInputSource implements InputSource, StatisticsProvider, Restartable { - - private Object value; - - public Properties getStatistics() { - return PropertiesConverter.stringToProperties("a=b"); - } - - public RestartData getRestartData() { - return new GenericRestartData(PropertiesConverter.stringToProperties("value=foo")); - } - - public void restoreFrom(RestartData data) { - value = data.getProperties().getProperty("value"); - } - - public MockInputSource(Object value) { - this.value = value; - } - - public Object read() { - return value; - } - - public void close() { - } - - public void open() { - } - - } - -} +/* + * 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.execution.tasklet.support; + +import java.util.Properties; + +import junit.framework.TestCase; + +import org.springframework.batch.execution.tasklet.support.InputSourceItemProvider; +import org.springframework.batch.io.InputSource; +import org.springframework.batch.io.Skippable; +import org.springframework.batch.restart.GenericRestartData; +import org.springframework.batch.restart.RestartData; +import org.springframework.batch.restart.Restartable; +import org.springframework.batch.statistics.StatisticsProvider; +import org.springframework.batch.support.PropertiesConverter; + +/** + * Unit test for {@link InputSourceItemProvider} + * + * @author Robert Kasanicky + */ +public class InputSourceItemProviderTests extends TestCase { + + // object under test + private InputSourceItemProvider itemProvider = new InputSourceItemProvider(); + + private InputSource source; + + // create input template and inject it to data provider + protected void setUp() throws Exception { + source = new MockInputSource(this); + itemProvider.setInputSource(source); + } + + /** + * Uses input template to provide the domain object. + */ + public void testNext() { + Object result = itemProvider.next(); + assertSame("domain object is provided by the input template", this, result); + } + + /** + * Gets statistics from the input template + */ + public void testGetStatistics() { + Properties props = itemProvider.getStatistics(); + assertEquals("b", props.getProperty("a")); + } + + /** + * Gets restart data from the input template + */ + public void testGetRestartData() { + Properties props = itemProvider.getRestartData().getProperties(); + assertEquals("foo", props.getProperty("value")); + } + + /** + * Forwared restart data to input template + */ + public void testRestoreFrom() { + itemProvider.restoreFrom(new GenericRestartData(PropertiesConverter.stringToProperties("value=bar"))); + assertEquals("bar", itemProvider.next()); + } + + public void testSkip() { + itemProvider.skip(); + assertEquals("after skip", itemProvider.next()); + } + + private class MockInputSource implements InputSource, StatisticsProvider, Restartable, Skippable { + + private Object value; + + public Properties getStatistics() { + return PropertiesConverter.stringToProperties("a=b"); + } + + public RestartData getRestartData() { + return new GenericRestartData(PropertiesConverter.stringToProperties("value=foo")); + } + + public void restoreFrom(RestartData data) { + value = data.getProperties().getProperty("value"); + } + + public MockInputSource(Object value) { + this.value = value; + } + + public Object read() { + return value; + } + + public void close() { + } + + public void open() { + } + + public void skip() { + value = "after skip"; + } + + } + +} diff --git a/execution/src/test/java/org/springframework/batch/execution/tasklet/support/OutputSourceItemProcessorTests.java b/execution/src/test/java/org/springframework/batch/execution/tasklet/support/OutputSourceItemProcessorTests.java index 42355deb5..fac54e90c 100644 --- a/execution/src/test/java/org/springframework/batch/execution/tasklet/support/OutputSourceItemProcessorTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/tasklet/support/OutputSourceItemProcessorTests.java @@ -23,6 +23,7 @@ import junit.framework.TestCase; import org.springframework.batch.execution.tasklet.support.OutputSourceItemProcessor; import org.springframework.batch.io.OutputSource; +import org.springframework.batch.io.Skippable; import org.springframework.batch.restart.GenericRestartData; import org.springframework.batch.restart.RestartData; import org.springframework.batch.restart.Restartable; @@ -118,6 +119,12 @@ public class OutputSourceItemProcessorTests extends TestCase { Properties props = processor.getStatistics(); assertEquals(null, props.getProperty("a")); } + + public void testSkip() { + processor.skip(); + assertEquals(1, list.size()); + assertEquals("after skip", list.get(0)); + } private List list = new ArrayList(); @@ -125,7 +132,7 @@ public class OutputSourceItemProcessorTests extends TestCase { * @author Dave Syer * */ - public class MockOutputSource implements OutputSource, StatisticsProvider, Restartable { + public class MockOutputSource implements OutputSource, StatisticsProvider, Restartable, Skippable { private String value; @@ -155,6 +162,10 @@ public class OutputSourceItemProcessorTests extends TestCase { value = data.getProperties().getProperty("value"); } + public void skip() { + list.add("after skip"); + } + } }