use of org.springframework.beans.propertyeditors.CustomNumberEditor in project spring-framework by spring-projects.
the class BeanWrapperGenericsTests method testGenericMapElementWithCollectionValue.
@Test
public void testGenericMapElementWithCollectionValue() {
GenericBean<?> gb = new GenericBean<>();
gb.setCollectionMap(new HashMap<>());
BeanWrapper bw = new BeanWrapperImpl(gb);
bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
HashSet<Integer> value1 = new HashSet<>();
value1.add(new Integer(1));
bw.setPropertyValue("collectionMap[1]", value1);
assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
}
use of org.springframework.beans.propertyeditors.CustomNumberEditor in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setPrimitiveArrayPropertyLargeMatching.
@Test
public void setPrimitiveArrayPropertyLargeMatching() {
Assume.group(TestGroup.PERFORMANCE);
Assume.notLogging(LogFactory.getLog(AbstractPropertyAccessorTests.class));
PrimitiveArrayBean target = new PrimitiveArrayBean();
AbstractPropertyAccessor accessor = createAccessor(target);
int[] input = new int[1024];
StopWatch sw = new StopWatch();
sw.start("array1");
for (int i = 0; i < 1000; i++) {
accessor.setPropertyValue("array", input);
}
sw.stop();
assertEquals(1024, target.getArray().length);
assertEquals(0, target.getArray()[0]);
long time1 = sw.getLastTaskTimeMillis();
assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);
accessor.registerCustomEditor(String.class, new StringTrimmerEditor(false));
sw.start("array2");
for (int i = 0; i < 1000; i++) {
accessor.setPropertyValue("array", input);
}
sw.stop();
assertTrue("Took too long", sw.getLastTaskTimeMillis() < 125);
accessor.registerCustomEditor(int.class, "array.somePath", new CustomNumberEditor(Integer.class, false));
sw.start("array3");
for (int i = 0; i < 1000; i++) {
accessor.setPropertyValue("array", input);
}
sw.stop();
assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);
accessor.registerCustomEditor(int.class, "array[0].somePath", new CustomNumberEditor(Integer.class, false));
sw.start("array3");
for (int i = 0; i < 1000; i++) {
accessor.setPropertyValue("array", input);
}
sw.stop();
assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);
accessor.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, false));
sw.start("array4");
for (int i = 0; i < 100; i++) {
accessor.setPropertyValue("array", input);
}
sw.stop();
assertEquals(1024, target.getArray().length);
assertEquals(0, target.getArray()[0]);
assertTrue("Took too long", sw.getLastTaskTimeMillis() > time1);
}
use of org.springframework.beans.propertyeditors.CustomNumberEditor 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.propertyeditors.CustomNumberEditor 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);
}
use of org.springframework.beans.propertyeditors.CustomNumberEditor in project spring-framework by spring-projects.
the class DataBinderTests method testBindingWithCustomEditorOnObjectField.
@Test
public void testBindingWithCustomEditorOnObjectField() {
BeanWithObjectProperty tb = new BeanWithObjectProperty();
DataBinder binder = new DataBinder(tb);
binder.registerCustomEditor(Integer.class, "object", new CustomNumberEditor(Integer.class, true));
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("object", "1");
binder.bind(pvs);
assertEquals(new Integer(1), tb.getObject());
}
Aggregations