1 16 17 package org.springframework.beans.factory.config; 18 19 import java.beans.PropertyEditorSupport ; 20 import java.text.DateFormat ; 21 import java.text.ParseException ; 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 27 import junit.framework.TestCase; 28 29 import org.springframework.beans.MutablePropertyValues; 30 import org.springframework.beans.TestBean; 31 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 32 import org.springframework.beans.factory.support.RootBeanDefinition; 33 import org.springframework.beans.propertyeditors.CustomDateEditor; 34 35 39 public class CustomEditorConfigurerTests extends TestCase { 40 41 public void testCustomEditorConfigurerWithClassNames() throws ParseException { 42 DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); 43 CustomEditorConfigurer cec = new CustomEditorConfigurer(); 44 Map editors = new HashMap (); 45 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN); 46 editors.put(Date .class.getName(), new CustomDateEditor(df, true)); 47 cec.setCustomEditors(editors); 48 cec.postProcessBeanFactory(bf); 49 50 MutablePropertyValues pvs = new MutablePropertyValues(); 51 pvs.addPropertyValue("date", "2.12.1975"); 52 bf.registerBeanDefinition("tb1", new RootBeanDefinition(TestBean.class, pvs)); 53 pvs = new MutablePropertyValues(); 54 pvs.addPropertyValue("someMap[myKey]", new TypedStringValue("2.12.1975", Date .class)); 55 bf.registerBeanDefinition("tb2", new RootBeanDefinition(TestBean.class, pvs)); 56 57 TestBean tb1 = (TestBean) bf.getBean("tb1"); 58 assertEquals(df.parse("2.12.1975"), tb1.getDate()); 59 TestBean tb2 = (TestBean) bf.getBean("tb2"); 60 assertEquals(df.parse("2.12.1975"), tb2.getSomeMap().get("myKey")); 61 } 62 63 public void testCustomEditorConfigurerWithClasses() throws ParseException { 64 DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); 65 CustomEditorConfigurer cec = new CustomEditorConfigurer(); 66 Map editors = new HashMap (); 67 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN); 68 editors.put(Date .class, new CustomDateEditor(df, true)); 69 cec.setCustomEditors(editors); 70 cec.postProcessBeanFactory(bf); 71 72 MutablePropertyValues pvs = new MutablePropertyValues(); 73 pvs.addPropertyValue("date", "2.12.1975"); 74 bf.registerBeanDefinition("tb", new RootBeanDefinition(TestBean.class, pvs)); 75 76 TestBean tb = (TestBean) bf.getBean("tb"); 77 assertEquals(df.parse("2.12.1975"), tb.getDate()); 78 } 79 80 public void testCustomEditorConfigurerWithArray() throws ParseException { 81 DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); 82 CustomEditorConfigurer cec = new CustomEditorConfigurer(); 83 Map editors = new HashMap (); 84 editors.put("java.lang.String[]", new PropertyEditorSupport () { 85 public void setAsText(String text) { 86 setValue(new String [] {"test"}); 87 } 88 }); 89 cec.setCustomEditors(editors); 90 cec.postProcessBeanFactory(bf); 91 92 MutablePropertyValues pvs = new MutablePropertyValues(); 93 pvs.addPropertyValue("stringArray", "xxx"); 94 bf.registerBeanDefinition("tb", new RootBeanDefinition(TestBean.class, pvs)); 95 96 TestBean tb = (TestBean) bf.getBean("tb"); 97 assertTrue(tb.getStringArray() != null && tb.getStringArray().length == 1); 98 assertEquals("test", tb.getStringArray()[0]); 99 } 100 101 } 102 | Popular Tags |