diff --git a/infrastructure/src/main/java/org/springframework/batch/io/exception/FlatFileParsingException.java b/infrastructure/src/main/java/org/springframework/batch/io/exception/FlatFileParsingException.java
new file mode 100644
index 000000000..c3cf23adf
--- /dev/null
+++ b/infrastructure/src/main/java/org/springframework/batch/io/exception/FlatFileParsingException.java
@@ -0,0 +1,62 @@
+/*
+ * 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.io.exception;
+
+/**
+ * Exception thrown when errors are encountered
+ * parsing flat files. The original input, typically
+ * a line, can be passed in, so that latter catches
+ * can write out the original input to a log, or
+ * an error table.
+ *
+ * @author Lucas Ward
+ *
+ */
+public class FlatFileParsingException extends ParsingException {
+
+ private String input;
+ private int lineNumber;
+
+ public FlatFileParsingException(String message, String input) {
+ super(message);
+ this.input = input;
+ }
+
+ public FlatFileParsingException(String message, String input, int lineNumber) {
+ super(message);
+ this.input = input;
+ this.lineNumber = lineNumber;
+ }
+
+ public FlatFileParsingException(String message, Throwable cause, String input, int lineNumber) {
+ super(message, cause);
+ this.input = input;
+ this.lineNumber = lineNumber;
+ }
+
+ public FlatFileParsingException(Throwable cause, String input) {
+ super(cause);
+ this.input = input;
+ }
+
+ public String getInput() {
+ return input;
+ }
+
+ public int getLineNumber() {
+ return lineNumber;
+ }
+}
diff --git a/infrastructure/src/main/java/org/springframework/batch/io/file/FieldSetInputSource.java b/infrastructure/src/main/java/org/springframework/batch/io/exception/ParsingException.java
similarity index 54%
rename from infrastructure/src/main/java/org/springframework/batch/io/file/FieldSetInputSource.java
rename to infrastructure/src/main/java/org/springframework/batch/io/exception/ParsingException.java
index fea876ef7..9bf5c3271 100644
--- a/infrastructure/src/main/java/org/springframework/batch/io/file/FieldSetInputSource.java
+++ b/infrastructure/src/main/java/org/springframework/batch/io/exception/ParsingException.java
@@ -1,35 +1,38 @@
-/*
- * 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.io.file;
-
-import org.springframework.batch.io.InputSource;
-
-/**
- * Common interface for reading input e.g. from a file or other stream-based
- * resource. Providers are expected to use this interface to access an input
- * source.
- *
- * If we had generics this would be a parameterised input source, but
- * for type safety with the current constraints we are going to use this
- * interface.
- *
- */
-public interface FieldSetInputSource extends InputSource {
-
- public FieldSet readFieldSet();
-
-}
+/*
+ * 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.io.exception;
+
+/**
+ * Exception indicating that an error has been encountered
+ * parsing io, typically from a file.
+ *
+ * @author Lucas Ward
+ *
+ */
+public class ParsingException extends RuntimeException {
+
+ public ParsingException(String message) {
+ super(message);
+ }
+
+ public ParsingException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public ParsingException(Throwable cause) {
+ super(cause);
+ }
+}
diff --git a/infrastructure/src/main/java/org/springframework/batch/io/file/support/DefaultFlatFileInputSource.java b/infrastructure/src/main/java/org/springframework/batch/io/file/support/DefaultFlatFileInputSource.java
index 2c4e3802a..6206e31e5 100644
--- a/infrastructure/src/main/java/org/springframework/batch/io/file/support/DefaultFlatFileInputSource.java
+++ b/infrastructure/src/main/java/org/springframework/batch/io/file/support/DefaultFlatFileInputSource.java
@@ -23,7 +23,6 @@ import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.io.Skippable;
-import org.springframework.batch.io.file.FieldSetInputSource;
import org.springframework.batch.repeat.synch.BatchTransactionSynchronizationManager;
import org.springframework.batch.restart.GenericRestartData;
import org.springframework.batch.restart.RestartData;
@@ -37,7 +36,7 @@ import org.springframework.transaction.support.TransactionSynchronizationAdapter
* This class is a {@link FieldSetInputSource} that supports restart,
* skipping invalid lines and storing statistics.
*
read method
- */
- public void testReadFieldSet() throws IOException {
- assertEquals("[FlatFileInputTemplate-TestData]", template.readFieldSet().toString());
- }
-
/**
* Regular usage of read method
*/
public void testRead() throws IOException {
- assertEquals("[FlatFileInputTemplate-TestData]", template.read().toString());
+ assertEquals("[FlatFileInputTemplate-TestData]", inputSource.read().toString());
}
/**
* Regular usage of read method
*/
public void testReadExhausted() throws IOException {
- assertEquals("[FlatFileInputTemplate-TestData]", template.read().toString());
- assertEquals(null, template.read());
+ assertEquals("[FlatFileInputTemplate-TestData]", inputSource.read().toString());
+ assertEquals(null, inputSource.read());
}
/**
* Regular usage of read method
*/
- public void testReadWithError() throws IOException {
- template.setTokenizer(new LineTokenizer() {
+ public void testReadWithTokenizerError() throws IOException {
+ inputSource.setTokenizer(new LineTokenizer() {
public FieldSet tokenize(String line) {
throw new RuntimeException("foo");
}
});
try {
- template.read();
- fail("Expected ValidationException");
- } catch (ValidationException e) {
- assertTrue(e.getMessage().indexOf("at line")>=0);
- assertTrue(e.getMessage().indexOf("at line 1")>=0);
+ inputSource.read();
+ fail("Expected ParsingException");
+ } catch (FlatFileParsingException e) {
+ assertEquals(e.getInput(), TEST_STRING);
+ assertEquals(e.getLineNumber(), 1);
+ }
+ }
+
+ public void testReadWithMapperError() throws IOException {
+ inputSource.setFieldSetMapper(new FieldSetMapper(){
+ public Object mapLine(FieldSet fs) {
+ throw new RuntimeException("foo");
+ }
+ });
+
+ try {
+ inputSource.read();
+ fail("Expected ParsingException");
+ } catch (FlatFileParsingException e) {
+ assertEquals(e.getInput(), TEST_STRING);
+ assertEquals(e.getLineNumber(), 1);
}
}
public void testReadBeforeOpen() throws Exception {
- template = new SimpleFlatFileInputSource();
- template.setResource(getInputResource(TEST_STRING));
- assertEquals("[FlatFileInputTemplate-TestData]", template.readFieldSet().toString());
+ inputSource = new SimpleFlatFileInputSource();
+ inputSource.setResource(getInputResource(TEST_STRING));
+ assertEquals("[FlatFileInputTemplate-TestData]", inputSource.read().toString());
}
public void testCloseBeforeOpen() throws Exception {
- template = new SimpleFlatFileInputSource();
- template.setResource(getInputResource(TEST_STRING));
- template.close();
+ inputSource = new SimpleFlatFileInputSource();
+ inputSource.setResource(getInputResource(TEST_STRING));
+ inputSource.close();
// The open still happens automatically on a read...
- assertEquals("[FlatFileInputTemplate-TestData]", template.readFieldSet().toString());
+ assertEquals("[FlatFileInputTemplate-TestData]", inputSource.read().toString());
}
public void testCloseOnDestroy() throws Exception {
final List list = new ArrayList();
- template = new SimpleFlatFileInputSource() {
+ inputSource = new SimpleFlatFileInputSource() {
public void close() {
list.add("close");
}
};
- template.destroy();
+ inputSource.destroy();
assertEquals(1, list.size());
}
public void testInitializationWithNullResource() throws Exception {
- template = new SimpleFlatFileInputSource();
+ inputSource = new SimpleFlatFileInputSource();
try {
- template.afterPropertiesSet();
+ inputSource.afterPropertiesSet();
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException e) {
@@ -155,23 +173,23 @@ public class SimpleFlatFileInputSourceTests extends TestCase {
}
public void testOpenTwiceHasNoEffect() throws Exception {
- template.open();
+ inputSource.open();
testRead();
}
public void testSetValidEncoding() throws Exception {
- template = new SimpleFlatFileInputSource();
- template.setEncoding("UTF-8");
- template.setResource(getInputResource(TEST_STRING));
+ inputSource = new SimpleFlatFileInputSource();
+ inputSource.setEncoding("UTF-8");
+ inputSource.setResource(getInputResource(TEST_STRING));
testRead();
}
public void testSetNullEncoding() throws Exception {
- template = new SimpleFlatFileInputSource();
- template.setEncoding(null);
- template.setResource(getInputResource(TEST_STRING));
+ inputSource = new SimpleFlatFileInputSource();
+ inputSource.setEncoding(null);
+ inputSource.setResource(getInputResource(TEST_STRING));
try {
- template.open();
+ inputSource.open();
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException e) {
@@ -180,11 +198,11 @@ public class SimpleFlatFileInputSourceTests extends TestCase {
}
public void testSetInvalidEncoding() throws Exception {
- template = new SimpleFlatFileInputSource();
- template.setEncoding("foo");
- template.setResource(getInputResource(TEST_STRING));
+ inputSource = new SimpleFlatFileInputSource();
+ inputSource.setEncoding("foo");
+ inputSource.setResource(getInputResource(TEST_STRING));
try {
- template.open();
+ inputSource.open();
fail("Expected BatchEnvironmentException");
}
catch (BatchEnvironmentException e) {
@@ -194,12 +212,12 @@ public class SimpleFlatFileInputSourceTests extends TestCase {
}
public void testEncoding() throws Exception {
- template.setEncoding("UTF-8");
+ inputSource.setEncoding("UTF-8");
testRead();
}
public void testRecordSeparator() throws Exception {
- template.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());
+ inputSource.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());
testRead();
}
diff --git a/infrastructure/src/test/java/org/springframework/batch/item/provider/AbstractFieldSetItemProviderTests.java b/infrastructure/src/test/java/org/springframework/batch/item/provider/AbstractFieldSetItemProviderTests.java
deleted file mode 100644
index 0f313ed08..000000000
--- a/infrastructure/src/test/java/org/springframework/batch/item/provider/AbstractFieldSetItemProviderTests.java
+++ /dev/null
@@ -1,59 +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.item.provider;
-
-import junit.framework.TestCase;
-
-import org.springframework.batch.io.file.FieldSet;
-import org.springframework.batch.io.file.FieldSetInputSource;
-import org.springframework.batch.io.file.support.SimpleFlatFileInputSource;
-import org.springframework.core.io.ByteArrayResource;
-
-/**
- * @author Dave Syer
- *
- */
-public class AbstractFieldSetItemProviderTests extends TestCase {
-
- public void testNotOpen() throws Exception {
- TestItemProvider provider = new TestItemProvider();
- provider.setSource(getInputSource("one\ntwo\nthree"));
- assertNotNull(provider.next());
- }
-
- public void testAfterPropertiesSet() throws Exception {
- TestItemProvider provider = new TestItemProvider();
- try {
- provider.afterPropertiesSet();
- fail("Expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- }
-
- private static class TestItemProvider extends AbstractFieldSetItemProvider {
- protected Object transform(FieldSet fieldSet) {
- return fieldSet.toString();
- }
- }
-
- private FieldSetInputSource getInputSource(String data) throws Exception {
- SimpleFlatFileInputSource template = new SimpleFlatFileInputSource();
- template.setResource(new ByteArrayResource(data.getBytes()));
- template.afterPropertiesSet();
- return template;
- }
-}
diff --git a/infrastructure/src/test/java/org/springframework/batch/item/provider/FieldSetItemProviderTests.java b/infrastructure/src/test/java/org/springframework/batch/item/provider/FieldSetItemProviderTests.java
deleted file mode 100644
index 6c5cf7bdc..000000000
--- a/infrastructure/src/test/java/org/springframework/batch/item/provider/FieldSetItemProviderTests.java
+++ /dev/null
@@ -1,54 +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.item.provider;
-
-import junit.framework.TestCase;
-
-import org.springframework.batch.io.file.FieldSet;
-import org.springframework.batch.io.file.support.SimpleFlatFileInputSource;
-import org.springframework.core.io.ByteArrayResource;
-
-public class FieldSetItemProviderTests extends TestCase {
-
- public void testNotOpen() throws Exception {
- TestItemProvider provider = new TestItemProvider("one\ntwo\nthree");
- assertNotNull(provider.next());
- }
-
- public void testNext() throws Exception {
- TestItemProvider provider = new TestItemProvider("one\ntwo\nthree");
- assertEquals("[one]", provider.next());
- assertEquals("[two]", provider.next());
- assertEquals("[three]", provider.next());
- assertEquals(null, provider.next());
- }
-
- private static class TestItemProvider extends AbstractFieldSetItemProvider {
- public TestItemProvider(String data) throws Exception {
- super();
- SimpleFlatFileInputSource template = new SimpleFlatFileInputSource();
- template.setResource(new ByteArrayResource(data.getBytes()));
- template.afterPropertiesSet();
- setSource(template);
- }
-
- protected Object transform(FieldSet fieldSet) {
- return fieldSet.toString();
- }
- }
-
-}
diff --git a/infrastructure/src/test/java/org/springframework/batch/item/provider/FlatFileItemProviderTests.java b/infrastructure/src/test/java/org/springframework/batch/item/provider/FlatFileItemProviderTests.java
deleted file mode 100644
index f872491f5..000000000
--- a/infrastructure/src/test/java/org/springframework/batch/item/provider/FlatFileItemProviderTests.java
+++ /dev/null
@@ -1,217 +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.item.provider;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Properties;
-
-import junit.framework.TestCase;
-
-import org.springframework.batch.io.Skippable;
-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.provider.FlatFileItemProvider;
-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 FlatFileItemProvider}
- *
- * @author Robert Kasanicky
- * @author Dave Syer
- */
-public class FlatFileItemProviderTests extends TestCase {
-
- public static String FOO = "foo";
- // object under test
- private FlatFileItemProvider itemProvider = new FlatFileItemProvider();
-
- // 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() {
- public void skip() {
- super.skip();
- list.add("skipped");
- }
- };
- 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(Skippable.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
- }
- }
-
- /**
- * Forward restart data to input template
- *
- * @throws Exception
- */
- public void testSkippable() throws Exception {
- assertEquals(0, list.size());
- itemProvider.skip();
- assertEquals(1, list.size());
- }
-
-}
diff --git a/infrastructure/src/test/java/org/springframework/batch/item/provider/InputSourceItemProviderTests.java b/infrastructure/src/test/java/org/springframework/batch/item/provider/InputSourceItemProviderTests.java
index 1157fece8..099819f58 100644
--- a/infrastructure/src/test/java/org/springframework/batch/item/provider/InputSourceItemProviderTests.java
+++ b/infrastructure/src/test/java/org/springframework/batch/item/provider/InputSourceItemProviderTests.java
@@ -31,7 +31,7 @@ import org.springframework.batch.support.PropertiesConverter;
/**
* Unit test for {@link InputSourceItemProvider}
- *
+ *
* @author Robert Kasanicky
*/
public class InputSourceItemProviderTests extends TestCase {
@@ -47,6 +47,21 @@ public class InputSourceItemProviderTests extends TestCase {
itemProvider.setInputSource(source);
}
+ public void testAfterPropertiesSet()throws Exception{
+ //shouldn't throw an exception since the input source is set
+ itemProvider.afterPropertiesSet();
+ }
+
+ public void testNullInputSource(){
+ try{
+ itemProvider.setInputSource(null);
+ itemProvider.afterPropertiesSet();
+ fail();
+ }catch(Exception ex){
+ assertTrue(ex instanceof IllegalArgumentException);
+ }
+ }
+
/**
* Uses input template to provide the domain object.
*/
@@ -78,7 +93,7 @@ public class InputSourceItemProviderTests extends TestCase {
itemProvider.restoreFrom(new GenericRestartData(PropertiesConverter.stringToProperties("value=bar")));
assertEquals("bar", itemProvider.next());
}
-
+
public void testSkip() {
itemProvider.skip();
assertEquals("after skip", itemProvider.next());
@@ -87,7 +102,7 @@ public class InputSourceItemProviderTests extends TestCase {
private class MockInputSource implements InputSource, StatisticsProvider, Restartable, Skippable {
private Object value;
-
+
public Properties getStatistics() {
return PropertiesConverter.stringToProperties("a=b");
}
diff --git a/infrastructure/src/test/java/org/springframework/batch/item/provider/ValidatingItemProviderTests.java b/infrastructure/src/test/java/org/springframework/batch/item/provider/ValidatingItemProviderTests.java
new file mode 100644
index 000000000..13ebc0769
--- /dev/null
+++ b/infrastructure/src/test/java/org/springframework/batch/item/provider/ValidatingItemProviderTests.java
@@ -0,0 +1,114 @@
+/*
+ * 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.item.provider;
+
+import org.easymock.MockControl;
+import org.springframework.batch.io.InputSource;
+import org.springframework.batch.io.exception.ValidationException;
+import org.springframework.batch.item.validator.Validator;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Lucas Ward
+ *
+ */
+public class ValidatingItemProviderTests extends TestCase {
+
+ InputSource inputSource;
+ ValidatingItemProvider itemProvider;
+ Validator validator;
+ MockControl validatorControl = MockControl.createControl(Validator.class);
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ inputSource = new MockInputSource(this);
+ validator = (Validator)validatorControl.getMock();
+ itemProvider = new ValidatingItemProvider();
+ itemProvider.setInputSource(inputSource);
+ itemProvider.setValidator(validator);
+ }
+
+ /*
+ * Super class' afterPropertieSet should be called to
+ * ensure InputSource is set.
+ */
+ public void testInputSourcePropertiesSet(){
+ try{
+ itemProvider.setInputSource(null);
+ itemProvider.afterPropertiesSet();
+ fail();
+ }catch(Exception ex){
+ assertTrue(ex instanceof IllegalArgumentException);
+ }
+ }
+
+ public void testValidatorPropertesSet(){
+ try{
+ itemProvider.setValidator(null);
+ itemProvider.afterPropertiesSet();
+ fail();
+ }catch(Exception ex){
+ assertTrue(ex instanceof IllegalArgumentException);
+ }
+ }
+
+ public void testValidation(){
+
+ validator.validate(this);
+ validatorControl.replay();
+ assertEquals(itemProvider.next(), this);
+ validatorControl.verify();
+ }
+
+ public void testValidationException(){
+
+ validator.validate(this);
+ validatorControl.setThrowable(new ValidationException(""));
+ validatorControl.replay();
+ try{
+ itemProvider.next();
+ fail();
+ }catch(ValidationException ex){
+ //expected
+ }
+ }
+
+ public void testNullInput(){
+ validatorControl.replay();
+ itemProvider.setInputSource(new MockInputSource(null));
+ assertNull(itemProvider.next());
+ //assert validator wasn't called.
+ validatorControl.verify();
+ }
+
+ private class MockInputSource implements InputSource{
+
+ Object value;
+
+ public MockInputSource(Object value){
+ this.value = value;
+ }
+
+ public Object read() {
+ return value;
+ }
+ }
+}
diff --git a/infrastructure/src/test/java/org/springframework/batch/repeat/support/AbstractTradeBatchTests.java b/infrastructure/src/test/java/org/springframework/batch/repeat/support/AbstractTradeBatchTests.java
index 8f265abb8..9d56e0fcb 100644
--- a/infrastructure/src/test/java/org/springframework/batch/repeat/support/AbstractTradeBatchTests.java
+++ b/infrastructure/src/test/java/org/springframework/batch/repeat/support/AbstractTradeBatchTests.java
@@ -19,17 +19,18 @@ package org.springframework.batch.repeat.support;
import junit.framework.TestCase;
import org.springframework.batch.io.file.FieldSet;
+import org.springframework.batch.io.file.FieldSetMapper;
import org.springframework.batch.io.file.support.SimpleFlatFileInputSource;
import org.springframework.batch.item.ItemProcessor;
-import org.springframework.batch.item.provider.AbstractFieldSetItemProvider;
+import org.springframework.batch.item.provider.InputSourceItemProvider;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* Base class for simple tests with small trade data set.
- *
+ *
* @author Dave Syer
- *
+ *
*/
public abstract class AbstractTradeBatchTests extends TestCase {
@@ -46,18 +47,21 @@ public abstract class AbstractTradeBatchTests extends TestCase {
provider = new TradeItemProvider(resource);
}
- protected static class TradeItemProvider extends AbstractFieldSetItemProvider {
+ protected static class TradeItemProvider extends InputSourceItemProvider {
protected TradeItemProvider(Resource resource) throws Exception {
super();
- SimpleFlatFileInputSource template = new SimpleFlatFileInputSource();
- template.setResource(resource);
- template.afterPropertiesSet();
- setSource(template);
+ SimpleFlatFileInputSource inputSource = new SimpleFlatFileInputSource();
+ inputSource.setResource(resource);
+ inputSource.setFieldSetMapper(new TradeMapper());
+ inputSource.afterPropertiesSet();
+ setInputSource(inputSource);
}
+ }
- protected Object transform(FieldSet fieldSet) {
- return new Trade(fieldSet);
+ protected static class TradeMapper implements FieldSetMapper{
+ public Object mapLine(FieldSet fs) {
+ return new Trade(fs);
}
}