From 455ed2fadeb59592ca4c9aff9eb997170fa9ea02 Mon Sep 17 00:00:00 2001 From: dsyer Date: Sun, 21 Oct 2007 15:36:37 +0000 Subject: [PATCH] RESOLVED - issue BATCH-145: Move CollectionItemProvider to infrastructure from samples http://opensource.atlassian.com/projects/spring/browse/BATCH-145 Moved it into infrastructure. --- .../item/provider/CollectionItemProvider.java | 112 ------------------ .../src/main/resources/jobs/multilineJob.xml | 2 +- .../provider/CollectionItemProviderTests.java | 60 ---------- 3 files changed, 1 insertion(+), 173 deletions(-) delete mode 100644 samples/src/main/java/org/springframework/batch/sample/item/provider/CollectionItemProvider.java delete mode 100644 samples/src/test/java/org/springframework/batch/sample/item/provider/CollectionItemProviderTests.java diff --git a/samples/src/main/java/org/springframework/batch/sample/item/provider/CollectionItemProvider.java b/samples/src/main/java/org/springframework/batch/sample/item/provider/CollectionItemProvider.java deleted file mode 100644 index 14ef36fd1..000000000 --- a/samples/src/main/java/org/springframework/batch/sample/item/provider/CollectionItemProvider.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.sample.item.provider; - -import java.util.ArrayList; -import java.util.Collection; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.batch.io.InputSource; -import org.springframework.batch.io.file.FieldSetMapper; -import org.springframework.batch.item.ItemProvider; -import org.springframework.batch.item.provider.AbstractItemProvider; - -/** - * An {@link ItemProvider} that delivers a list as its item, storing up objects - * from the injected {@link InputSource} until they are ready to be packed out - * as a collection.
- * - * This class is thread safe (it can be used concurrently by multiple threads) as - * long as the {@link InputSource} is also thread safe. - * - * @author Dave Syer - * - */ -public class CollectionItemProvider extends AbstractItemProvider { - - private static final Log log = LogFactory - .getLog(CollectionItemProvider.class); - - private InputSource inputSource; - - /** - * Get the next list of records. - * - * @see org.springframework.batch.item.ItemProvider#next() - */ - public Object next() { - ResultHolder holder = new ResultHolder(); - - while (process(inputSource.read(), holder)) { - continue; - } - - if (!holder.exhausted) { - return holder.records; - } else { - return null; - } - } - - private boolean process(Object value, ResultHolder holder) { - // finish processing if we hit the end of file - if (value == null) { - log.debug("Exhausted InputSource"); - holder.exhausted = true; - return false; - } - - // start a new collection - if (value == FieldSetMapper.BEGIN_RECORD) { - log.debug("Start of new record detected"); - return true; - } - - // mark we are finished with current collection - if (value == FieldSetMapper.END_RECORD) { - log.debug("End of record detected"); - return false; - } - - // add a simple record to the current collection - log.debug("Mapping: " + value); - holder.records.add(value); - return true; - } - - /** - * Injection setter for {@link InputSource}. - * @param inputSource an {@link InputSource}. - */ - public void setInputSource(InputSource inputSource) { - this.inputSource = inputSource; - } - - /** - * Private class for temporary state management while item is being - * collected. - * - * @author Dave Syer - * - */ - private static class ResultHolder { - Collection records = new ArrayList(); - boolean exhausted = false; - } - -} diff --git a/samples/src/main/resources/jobs/multilineJob.xml b/samples/src/main/resources/jobs/multilineJob.xml index 931785563..bd729151b 100644 --- a/samples/src/main/resources/jobs/multilineJob.xml +++ b/samples/src/main/resources/jobs/multilineJob.xml @@ -21,7 +21,7 @@ class="org.springframework.batch.execution.tasklet.RestartableItemProviderTasklet"> + class="org.springframework.batch.item.provider.AggregateItemProvider"> diff --git a/samples/src/test/java/org/springframework/batch/sample/item/provider/CollectionItemProviderTests.java b/samples/src/test/java/org/springframework/batch/sample/item/provider/CollectionItemProviderTests.java deleted file mode 100644 index 7ae8ae66d..000000000 --- a/samples/src/test/java/org/springframework/batch/sample/item/provider/CollectionItemProviderTests.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.springframework.batch.sample.item.provider; - -import java.util.Collection; -import java.util.Iterator; - -import junit.framework.TestCase; - -import org.easymock.MockControl; -import org.springframework.batch.io.InputSource; -import org.springframework.batch.io.file.FieldSetMapper; - -public class CollectionItemProviderTests extends TestCase { - - private MockControl inputControl; - private InputSource input; - private CollectionItemProvider provider; - - public void setUp() { - - //create mock for input - inputControl = MockControl.createControl(InputSource.class); - input = (InputSource) inputControl.getMock(); - - //create provider - provider = new CollectionItemProvider(); - provider.setInputSource(input); - } - - public void testNext() { - - //set-up mock input - input.read(); - inputControl.setReturnValue(FieldSetMapper.BEGIN_RECORD); - input.read(); - inputControl.setReturnValue("line",3); - input.read(); - inputControl.setReturnValue(FieldSetMapper.END_RECORD); - input.read(); - inputControl.setReturnValue(null); - inputControl.replay(); - - //read object - Object result = provider.next(); - - //it should be collection of 3 strings "line" - assertTrue(result instanceof Collection); - Collection lines = (Collection)result; - assertEquals(3, lines.size()); - - for (Iterator i = lines.iterator(); i.hasNext();) { - assertEquals("line", i.next()); - } - - //read object again - it should return null - assertNull(provider.next()); - - //verify method calls - inputControl.verify(); - } -}