Move BATCH-105: The functionality in "DefaultFlatFileItemProvider" (validating and mapping) should be provided by the infrastructure layer (DefaultFlatFileInputSource).

http://opensource.atlassian.com/projects/spring/browse/BATCH-105
This commit is contained in:
dsyer
2007-08-21 13:16:29 +00:00
parent 9482829412
commit 3c259f3ce8
2 changed files with 0 additions and 0 deletions

View File

@@ -1,149 +0,0 @@
package org.springframework.batch.execution.tasklet.support;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.processor.CompositeItemProcessor;
import org.springframework.batch.restart.GenericRestartData;
import org.springframework.batch.restart.RestartData;
import org.springframework.batch.restart.Restartable;
import org.springframework.batch.statistics.StatisticsProvider;
/**
* Tests for {@link CompositeItemProcessor}
*
* @author Robert Kasanicky
*/
public class CompositeItemProcessorTests extends TestCase {
// object under test
private CompositeItemProcessor itemProcessor = new CompositeItemProcessor();
/**
* Regular usage scenario.
* All injected processors should be called.
*/
public void testProcess() throws Exception {
final int NUMBER_OF_PROCESSORS = 10;
Object data = new Object();
List controls = new ArrayList(NUMBER_OF_PROCESSORS);
List processors = new ArrayList(NUMBER_OF_PROCESSORS);
for (int i = 0; i < NUMBER_OF_PROCESSORS; i++) {
MockControl control = MockControl.createStrictControl(ItemProcessor.class);
ItemProcessor processor = (ItemProcessor) control.getMock();
processor.process(data);
control.setVoidCallable();
control.replay();
processors.add(processor);
controls.add(control);
}
itemProcessor.setItemProcessors(processors);
itemProcessor.process(data);
for (Iterator iterator = controls.iterator(); iterator.hasNext();) {
MockControl control = (MockControl) iterator.next();
control.verify();
}
}
/**
* Statistics of injected ItemProcessors should be returned under keys prefixed with their list index.
*/
public void testStatistics() {
final ItemProcessor p1 = new ItemProcessorStub();
final ItemProcessor p2 = new ItemProcessorStub();
List itemProcessors = new ArrayList(){{
add(p1);
add(p2);
}};
itemProcessor.setItemProcessors(itemProcessors);
Properties stats = itemProcessor.getStatistics();
assertEquals(String.valueOf(p1.hashCode()), stats.getProperty("0#" + ItemProcessorStub.STATS_KEY));
assertEquals(String.valueOf(p2.hashCode()), stats.getProperty("1#" + ItemProcessorStub.STATS_KEY));
}
/**
* All Restartable processors should be restarted, not-Restartable processors should be ignored.
*/
public void testRestart() {
//this mock with undefined behavior makes sure not-Restartable processor is ignored
MockControl p1c = MockControl.createStrictControl(ItemProcessor.class);
final ItemProcessor p1 = (ItemProcessor) p1c.getMock();
final ItemProcessor p2 = new ItemProcessorStub();
final ItemProcessor p3 = new ItemProcessorStub();
List itemProcessors = new ArrayList(){{
add(p1);
add(p2);
add(p3);
}};
itemProcessor.setItemProcessors(itemProcessors);
RestartData rd = itemProcessor.getRestartData();
itemProcessor.restoreFrom(rd);
for (Iterator iterator = itemProcessors.iterator(); iterator.hasNext();) {
ItemProcessor processor = (ItemProcessor) iterator.next();
if (processor instanceof ItemProcessorStub) {
assertTrue("Injected processors are restarted",
((ItemProcessorStub)processor).restarted);
}
}
}
/**
* Stub for testing restart. Checks the restart data received is the same that was returned by
* <code>getRestartData()</code>
*/
private static class ItemProcessorStub implements ItemProcessor, Restartable, StatisticsProvider {
private static final String RESTART_KEY = "restartData";
private static final String STATS_KEY = "stats";
private boolean restarted = false;
private final int hashCode = this.hashCode();
public RestartData getRestartData() {
Properties props = new Properties(){{
setProperty(RESTART_KEY, String.valueOf(hashCode));
}};
return new GenericRestartData(props);
}
public void restoreFrom(RestartData data) {
if (Integer.valueOf(data.getProperties().getProperty(RESTART_KEY)).intValue() != hashCode()) {
fail("received restart data is not the same which was saved");
}
restarted = true;
}
public void process(Object data) throws Exception {
// do nothing
}
public Properties getStatistics() {
return new Properties() {{
setProperty(STATS_KEY, String.valueOf(hashCode));
}};
}
}
}

View File

@@ -1,171 +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.execution.tasklet.support;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import junit.framework.TestCase;
import org.springframework.batch.io.OutputSource;
import org.springframework.batch.io.Skippable;
import org.springframework.batch.item.processor.OutputSourceItemProcessor;
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;
/**
* @author Dave Syer
*
*/
public class OutputSourceItemProcessorTests extends TestCase {
private OutputSourceItemProcessor processor = new OutputSourceItemProcessor();
private OutputSource source;
/*
* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
source = new MockOutputSource("test");
processor.setOutputSource(source);
}
public void testProcess() throws Exception {
processor.process("foo");
assertEquals(1, list.size());
assertEquals("test:foo", list.get(0));
}
/**
* Gets statistics from the input template
*/
public void testGetStatistics() {
Properties props = processor.getStatistics();
assertEquals("b", props.getProperty("a"));
}
/**
* Gets restart data from the input template
*/
public void testGetRestartData() {
Properties props = processor.getRestartData().getProperties();
assertEquals("foo", props.getProperty("value"));
}
/**
* Forward restart data to input template
* @throws Exception
*/
public void testRestoreFrom() throws Exception {
processor.restoreFrom(new GenericRestartData(PropertiesConverter.stringToProperties("value=bar")));
processor.process("foo");
assertEquals("bar:foo", list.get(0));
}
/**
* Forward restart data to input template
* @throws Exception
*/
public void testGetRestartDataWithoutRestartable() throws Exception {
processor.setOutputSource(null);
try {
processor.getRestartData();
fail("Expected IllegalStateException");
}
catch (IllegalStateException e) {
// expected
}
}
/**
* Forward restart data to input template
* @throws Exception
*/
public void testRestoreFromWithoutRestartable() throws Exception {
processor.setOutputSource(null);
try {
processor.restoreFrom(new GenericRestartData(PropertiesConverter.stringToProperties("value=bar")));
fail("Expected IllegalStateException");
}
catch (IllegalStateException e) {
// expected
}
}
/**
* Gets statistics from the input template
*/
public void testGetStatisticsWithoutStatisticsProvider() {
processor.setOutputSource(null);
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();
/**
* @author Dave Syer
*
*/
public class MockOutputSource implements OutputSource, StatisticsProvider, Restartable, Skippable {
private String value;
public MockOutputSource(String string) {
this.value = string;
}
public void write(Object output) {
list.add(value+":"+output);
}
public void close() {
}
public void open() {
}
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 void skip() {
list.add("after skip");
}
}
}