KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > CustomEditorConfigurerTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.factory.config;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20 import java.text.DateFormat JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.Map JavaDoc;
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 /**
36  * @author Juergen Hoeller
37  * @since 31.07.2004
38  */

39 public class CustomEditorConfigurerTests extends TestCase {
40
41     public void testCustomEditorConfigurerWithClassNames() throws ParseException JavaDoc {
42         DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
43         CustomEditorConfigurer cec = new CustomEditorConfigurer();
44         Map JavaDoc editors = new HashMap JavaDoc();
45         DateFormat JavaDoc df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
46         editors.put(Date JavaDoc.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 JavaDoc.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 JavaDoc {
64         DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
65         CustomEditorConfigurer cec = new CustomEditorConfigurer();
66         Map JavaDoc editors = new HashMap JavaDoc();
67         DateFormat JavaDoc df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
68         editors.put(Date JavaDoc.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 JavaDoc {
81         DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
82         CustomEditorConfigurer cec = new CustomEditorConfigurer();
83         Map JavaDoc editors = new HashMap JavaDoc();
84         editors.put("java.lang.String[]", new PropertyEditorSupport JavaDoc() {
85             public void setAsText(String JavaDoc text) {
86                 setValue(new String JavaDoc[] {"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