use of org.springframework.beans.PropertyEditorRegistry in project spring-framework by spring-projects.
the class CustomEditorConfigurerTests method testCustomEditorConfigurerWithPropertyEditorRegistrar.
@Test
public void testCustomEditorConfigurerWithPropertyEditorRegistrar() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer();
final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
cec.setPropertyEditorRegistrars(new PropertyEditorRegistrar[] { new PropertyEditorRegistrar() {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
}
} });
cec.postProcessBeanFactory(bf);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("date", "2.12.1975");
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
bd1.setPropertyValues(pvs);
bf.registerBeanDefinition("tb1", bd1);
pvs = new MutablePropertyValues();
pvs.add("someMap[myKey]", new TypedStringValue("2.12.1975", Date.class));
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
bd2.setPropertyValues(pvs);
bf.registerBeanDefinition("tb2", bd2);
TestBean tb1 = (TestBean) bf.getBean("tb1");
assertEquals(df.parse("2.12.1975"), tb1.getDate());
TestBean tb2 = (TestBean) bf.getBean("tb2");
assertEquals(df.parse("2.12.1975"), tb2.getSomeMap().get("myKey"));
}
use of org.springframework.beans.PropertyEditorRegistry in project spring-framework by spring-projects.
the class BeanFactoryGenericsTests method testGenericMapWithCollectionValueConstructor.
@Test
public void testGenericMapWithCollectionValueConstructor() throws MalformedURLException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
}
});
RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
Map<String, AbstractCollection<?>> input = new HashMap<>();
HashSet<Integer> value1 = new HashSet<>();
value1.add(new Integer(1));
input.put("1", value1);
ArrayList<Boolean> value2 = new ArrayList<>();
value2.add(Boolean.TRUE);
input.put("2", value2);
rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
rbd.getConstructorArgumentValues().addGenericArgumentValue(input);
bf.registerBeanDefinition("genericBean", rbd);
GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
use of org.springframework.beans.PropertyEditorRegistry in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testCustomEditorWithBeanReference.
@Test
public void testCustomEditorWithBeanReference() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", new RuntimeBeanReference("myFloat"));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("testBean", bd);
lbf.registerSingleton("myFloat", "1,1");
TestBean testBean = (TestBean) lbf.getBean("testBean");
assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
Aggregations