From 9fa730100cd352901b500a2e24e66ee6b44102fa Mon Sep 17 00:00:00 2001 From: dsyer Date: Tue, 3 Mar 2009 12:23:59 +0000 Subject: [PATCH] OPEN - issue BATCH-1108: Add composite ItemWriter/Processor based on Classifier Move test case over from core as well --- .../DefaultPropertyEditorRegistrar.java | 8 +--- .../AnnotationMethodResolverTests.java | 2 +- .../DefaultPropertEditorRegistrarTests.java | 44 +++++++++++++------ 3 files changed, 33 insertions(+), 21 deletions(-) rename {spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml => spring-batch-infrastructure/src/test/java/org/springframework/batch/support}/AnnotationMethodResolverTests.java (97%) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultPropertyEditorRegistrar.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultPropertyEditorRegistrar.java index 5adf928fd..4deab2392 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultPropertyEditorRegistrar.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultPropertyEditorRegistrar.java @@ -74,12 +74,8 @@ public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar { throw new IllegalArgumentException("Invalid key [" + key + "] for custom editor: needs to be Class or String."); } - Object value = entry.getValue(); - if (!(value instanceof PropertyEditor)) { - throw new IllegalArgumentException("Mapped value [" + value + "] for custom editor key [" + key - + "] is not of required type [" + PropertyEditor.class.getName() + "]"); - } - this.customEditors.put(requiredType, (PropertyEditor) value); + PropertyEditor value = entry.getValue(); + this.customEditors.put(requiredType, value); } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/AnnotationMethodResolverTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/AnnotationMethodResolverTests.java similarity index 97% rename from spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/AnnotationMethodResolverTests.java rename to spring-batch-infrastructure/src/test/java/org/springframework/batch/support/AnnotationMethodResolverTests.java index b3f036741..13bbe78fd 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/AnnotationMethodResolverTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/AnnotationMethodResolverTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.batch.core.configuration.xml; +package org.springframework.batch.support; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultPropertEditorRegistrarTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultPropertEditorRegistrarTests.java index 97cb08f65..a203befaf 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultPropertEditorRegistrarTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultPropertEditorRegistrarTests.java @@ -16,22 +16,25 @@ package org.springframework.batch.support; +import static org.junit.Assert.assertEquals; + import java.util.Collections; import java.util.Properties; -import junit.framework.TestCase; - +import org.junit.Test; import org.springframework.beans.BeanWrapperImpl; +import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.propertyeditors.CustomNumberEditor; import org.springframework.beans.propertyeditors.PropertiesEditor; -public class DefaultPropertEditorRegistrarTests extends TestCase { +public class DefaultPropertEditorRegistrarTests { + @Test public void testIntArray() throws Exception { DefaultPropertyEditorRegistrar mapper = new DefaultPropertyEditorRegistrar(); BeanWithIntArray result = new BeanWithIntArray(); - BeanWrapperImpl wrapper = new BeanWrapperImpl(result); mapper.setCustomEditors(Collections.singletonMap(int[].class, new IntArrayPropertyEditor())); + BeanWrapperImpl wrapper = new BeanWrapperImpl(result); mapper.registerCustomEditors(wrapper); PropertiesEditor editor = new PropertiesEditor(); editor.setAsText("numbers=1,2,3, 4"); @@ -40,32 +43,45 @@ public class DefaultPropertEditorRegistrarTests extends TestCase { assertEquals(4, result.numbers[3]); } + @Test(expected = IllegalArgumentException.class) public void testSetCustomEditorsWithInvalidTypeName() throws Exception { DefaultPropertyEditorRegistrar mapper = new DefaultPropertyEditorRegistrar(); - try { - mapper.setCustomEditors(Collections.singletonMap("FOO", new CustomNumberEditor(Long.class, true))); - } catch (IllegalArgumentException e) { - // expected - } + mapper.setCustomEditors(Collections.singletonMap("FOO", new CustomNumberEditor(Long.class, true))); } + @Test + public void testSetCustomEditorsWithStringTypeName() throws Exception { + + DefaultPropertyEditorRegistrar mapper = new DefaultPropertyEditorRegistrar(); + mapper.setCustomEditors(Collections.singletonMap("java.lang.Long", new CustomNumberEditor(Long.class, true))); + BeanWithIntArray result = new BeanWithIntArray(); + BeanWrapperImpl wrapper = new BeanWrapperImpl(result); + mapper.registerCustomEditors(wrapper); + wrapper.setPropertyValues(new MutablePropertyValues(Collections.singletonMap("number", "123"))); + assertEquals(123L, result.number); + + } + + @Test(expected = IllegalArgumentException.class) public void testSetCustomEditorsWithInvalidType() throws Exception { DefaultPropertyEditorRegistrar mapper = new DefaultPropertyEditorRegistrar(); - try { - mapper.setCustomEditors(Collections.singletonMap(new Object(), new CustomNumberEditor(Long.class, true))); - } catch (IllegalArgumentException e) { - // expected - } + mapper.setCustomEditors(Collections.singletonMap(new Object(), new CustomNumberEditor(Long.class, true))); } private static class BeanWithIntArray { private int[] numbers; + private long number; + public void setNumbers(int[] numbers) { this.numbers = numbers; } + + public void setNumber(long number) { + this.number = number; + } } }