From de91cbe7285af726e170f749ae2d15792248382f Mon Sep 17 00:00:00 2001 From: dsyer Date: Tue, 6 Nov 2007 08:16:39 +0000 Subject: [PATCH] RESOLVED - issue BATCH-191: The getProperties in FieldSet does not handle null values http://opensource.atlassian.com/projects/spring/browse/BATCH-191 --- .../batch/io/file/FieldSet.java | 41 +++++++++++-------- .../batch/io/file/FieldSetTests.java | 7 ++++ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/infrastructure/src/main/java/org/springframework/batch/io/file/FieldSet.java b/infrastructure/src/main/java/org/springframework/batch/io/file/FieldSet.java index fc062c876..27a13d4f8 100644 --- a/infrastructure/src/main/java/org/springframework/batch/io/file/FieldSet.java +++ b/infrastructure/src/main/java/org/springframework/batch/io/file/FieldSet.java @@ -43,7 +43,7 @@ public final class FieldSet { private List names; public FieldSet(String[] tokens) { - this.tokens = tokens == null ? null : (String[])tokens.clone(); + this.tokens = tokens == null ? null : (String[]) tokens.clone(); } public FieldSet(String[] tokens, String[] names) { @@ -55,7 +55,7 @@ public final class FieldSet { + Arrays.asList(names) + ", values=" + Arrays.asList(tokens)); } - this.tokens = (String[])tokens.clone(); + this.tokens = (String[]) tokens.clone(); this.names = Arrays.asList(names); } @@ -539,7 +539,7 @@ public final class FieldSet { if (value != null) { return value.trim(); } else { - return value; + return null; } } @@ -566,7 +566,7 @@ public final class FieldSet { if (names != null) { return getProperties().toString(); } - + return tokens == null ? "" : Arrays.asList(tokens).toString(); } @@ -588,22 +588,24 @@ public final class FieldSet { } public int hashCode() { - //this algorithm was taken from java 1.5 jdk Arrays.hashCode(Object[]) - if (tokens == null) { - return 0; - } - - int result = 1; - - for (int i = 0; i < tokens.length; i++) { - result = 31 * result + (tokens[i] == null ? 0 : tokens[i].hashCode()); - } - - return result; + // this algorithm was taken from java 1.5 jdk Arrays.hashCode(Object[]) + if (tokens == null) { + return 0; + } + + int result = 1; + + for (int i = 0; i < tokens.length; i++) { + result = 31 * result + + (tokens[i] == null ? 0 : tokens[i].hashCode()); + } + + return result; } /** - * Construct name-value pairs from the field names and string values. + * Construct name-value pairs from the field names and string values. Null + * values are omitted. * * @return some properties representing the field set. * @@ -617,7 +619,10 @@ public final class FieldSet { } Properties props = new Properties(); for (int i = 0; i < tokens.length; i++) { - props.setProperty((String) names.get(i), readAndTrim(i)); + String value = readAndTrim(i); + if (value != null) { + props.setProperty((String) names.get(i), value); + } } return props; } diff --git a/infrastructure/src/test/java/org/springframework/batch/io/file/FieldSetTests.java b/infrastructure/src/test/java/org/springframework/batch/io/file/FieldSetTests.java index 9cd7662db..9b702f4d7 100644 --- a/infrastructure/src/test/java/org/springframework/batch/io/file/FieldSetTests.java +++ b/infrastructure/src/test/java/org/springframework/batch/io/file/FieldSetTests.java @@ -386,6 +386,13 @@ public class FieldSetTests extends TestCase { assertEquals("bar", new FieldSet(new String[] { "foo", "bar " }, new String[] { "Foo", "Bar"}).getProperties().getProperty("Bar")); } + public void testPropertiesWithNullValues() throws Exception{ + + fieldSet = new FieldSet(new String[] { null, "bar" }, new String[] { "Foo", "Bar"}); + assertEquals("bar", fieldSet.getProperties().getProperty("Bar")); + assertEquals(null, fieldSet.getProperties().getProperty("Foo")); + } + public void testAccessByNameWhenNamesMissing() throws Exception { try { new FieldSet(new String[] { "1", "2" }).readInt("a");