OPEN - issue 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 11:04:27 +00:00
parent 63b097a2d2
commit c7948c453a
2 changed files with 0 additions and 295 deletions

View File

@@ -1,106 +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.Properties;
import org.springframework.batch.io.file.FieldSet;
import org.springframework.batch.io.file.FieldSetMapper;
import org.springframework.batch.item.provider.AbstractFieldSetItemProvider;
import org.springframework.batch.item.validator.Validator;
import org.springframework.batch.restart.RestartData;
import org.springframework.batch.restart.Restartable;
import org.springframework.batch.statistics.StatisticsProvider;
/**
*
* Uses a {@link FieldSetMapper} to convert each line from an input source. Also
* adds {@link Restartable} as mandatory behaviour, delegating to the parent
* provider's input source.
*
* @author Dave Syer
*/
public class DefaultFlatFileItemProvider extends AbstractFieldSetItemProvider implements Restartable,
StatisticsProvider {
private FieldSetMapper mapper;
private Validator validator;
/*
* (non-Javadoc)
* @see org.springframework.batch.item.provider.AbstractFieldSetItemProvider#doNext(org.springframework.batch.io.line.FieldSet)
*/
protected Object transform(FieldSet fieldSet) {
Object value = mapper.mapLine(fieldSet);
if (validator!=null) {
validator.validate(value);
}
return value;
}
/**
* @param mapper the mapper to set
*/
public void setMapper(FieldSetMapper mapper) {
this.mapper = mapper;
}
/**
* @param validator the validator to set
*/
public void setValidator(Validator validator) {
this.validator = validator;
}
/**
* @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();
}
}

View File

@@ -1,189 +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.execution.tasklet.support.DefaultFlatFileItemProvider;
import org.springframework.batch.io.exception.ValidationException;
import org.springframework.batch.io.file.FieldSet;
import org.springframework.batch.io.file.FieldSetInputSource;
import org.springframework.batch.io.file.FieldSetMapper;
import org.springframework.batch.io.file.support.DefaultFlatFileInputSource;
import org.springframework.batch.item.validator.Validator;
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;
import org.springframework.core.io.ByteArrayResource;
/**
* Unit tests for {@link DefaultFlatFileItemProvider}
*
* @author Robert Kasanicky
*/
public class DefaultFlatFileItemProviderTests extends TestCase {
public static String FOO = "foo";
// object under test
private DefaultFlatFileItemProvider itemProvider = new DefaultFlatFileItemProvider();
// Input source
private DefaultFlatFileInputSource source;
//mock mapper
private FieldSetMapper mapper;
private List list = new ArrayList();
// create mock objects and inject them into data provider
protected void setUp() throws Exception {
source = new DefaultFlatFileInputSource();
source.setResource(new ByteArrayResource("a,b".getBytes()));
mapper = new FieldSetMapper() {
public Object mapLine(FieldSet fs) {
return FOO;
}
};
itemProvider.setSource(source);
itemProvider.setMapper(mapper);
assertTrue(Restartable.class.isAssignableFrom(DefaultFlatFileInputSource.class));
assertTrue(FieldSetInputSource.class.isAssignableFrom(DefaultFlatFileInputSource.class));
assertTrue(StatisticsProvider.class.isAssignableFrom(DefaultFlatFileInputSource.class));
}
/**
* Uses input template to provide the domain object.
*/
public void testNext() {
Object result = itemProvider.next();
assertSame("domain object is provided by the input template", FOO, result);
}
/**
* Uses input template to provide the domain object.
*/
public void testNextWithValidator() {
itemProvider.setValidator(new Validator() {
public void validate(Object value) throws ValidationException {
list.add(value);
}
});
itemProvider.next();
assertSame("domain object is provided by the input template", FOO, list.get(0));
}
/**
* Uses input template to provide the domain object.
*/
public void testNextWithValidatorAndInvalidData() {
itemProvider.setValidator(new Validator() {
public void validate(Object value) throws ValidationException {
throw new ValidationException("Invalid input");
}
});
try {
itemProvider.next();
fail("Expected ValidationException");
} catch (ValidationException e) {
// expected
assertEquals("Invalid input", e.getMessage());
}
}
/**
* Gets statistics from the input template
*/
public void testGetStatistics() {
Properties statistics = ((StatisticsProvider) source).getStatistics();
assertEquals(statistics, itemProvider.getStatistics());
}
/**
* Gets statistics from the input template
*/
public void testGetStatisticsWithoutStatisticsProvider() {
itemProvider.setSource(null);
Properties props = itemProvider.getStatistics();
assertEquals(null, props.getProperty("a"));
}
/**
* Gets restart data from the input template
*/
public void testGetRestartData() {
RestartData data = ((Restartable) source).getRestartData();
assertEquals(data.getProperties(), itemProvider.getRestartData().getProperties());
}
/**
* Forwarded restart data to input template
*/
public void testRestoreFrom() {
final List list = new ArrayList();
RestartData data = new RestartData() {
public Properties getProperties() {
list.add(FOO);
return ((Restartable) source).getRestartData().getProperties();
}};
itemProvider.restoreFrom(data);
//assertEquals(1, list.size()); getProperties are called multiple times due to null checks
assertTrue(list.size() > 0);
}
/**
* Forward restart data to input template
* @throws Exception
*/
public void testRestoreFromWithoutRestartable() throws Exception {
itemProvider.setSource(null);
try {
itemProvider.restoreFrom(new GenericRestartData(PropertiesConverter.stringToProperties("value=bar")));
fail("Expected IllegalStateException");
}
catch (IllegalStateException e) {
// expected
}
}
/**
* Forward restart data to input template
* @throws Exception
*/
public void testGetRestartDataWithoutRestartable() throws Exception {
itemProvider.setSource(null);
try {
itemProvider.getRestartData();
fail("Expected IllegalStateException");
}
catch (IllegalStateException e) {
// expected
}
}
}