Search in sources :

Example 6 with CustomNumberEditor

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);
}
Also used : CustomNumberEditor(org.springframework.beans.propertyeditors.CustomNumberEditor) GenericBean(org.springframework.tests.sample.beans.GenericBean) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with CustomNumberEditor

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);
}
Also used : BigInteger(java.math.BigInteger) CustomNumberEditor(org.springframework.beans.propertyeditors.CustomNumberEditor) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 8 with CustomNumberEditor

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);
}
Also used : CustomNumberEditor(org.springframework.beans.propertyeditors.CustomNumberEditor) PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) HashMap(java.util.HashMap) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) AbstractCollection(java.util.AbstractCollection) ArrayList(java.util.ArrayList) GenericBean(org.springframework.tests.sample.beans.GenericBean) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with CustomNumberEditor

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);
}
Also used : CustomNumberEditor(org.springframework.beans.propertyeditors.CustomNumberEditor) PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) NumberFormat(java.text.NumberFormat) Test(org.junit.Test)

Example 10 with CustomNumberEditor

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());
}
Also used : BeanWithObjectProperty(org.springframework.tests.sample.beans.BeanWithObjectProperty) CustomNumberEditor(org.springframework.beans.propertyeditors.CustomNumberEditor) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Test(org.junit.Test)

Aggregations

CustomNumberEditor (org.springframework.beans.propertyeditors.CustomNumberEditor)10 Test (org.junit.Test)9 HashSet (java.util.HashSet)5 GenericBean (org.springframework.tests.sample.beans.GenericBean)5 PropertyEditorRegistrar (org.springframework.beans.PropertyEditorRegistrar)4 PropertyEditorRegistry (org.springframework.beans.PropertyEditorRegistry)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)3 BigInteger (java.math.BigInteger)2 NumberFormat (java.text.NumberFormat)2 AbstractCollection (java.util.AbstractCollection)2 Collection (java.util.Collection)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 BigDecimal (java.math.BigDecimal)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1