Search in sources :

Example 1 with GenericBean

use of org.springframework.tests.sample.beans.GenericBean in project spring-framework by spring-projects.

the class BeanFactoryGenericsTests method testGenericMapWithCollectionValueFactoryMethod.

@Test
public void testGenericMapWithCollectionValueFactoryMethod() 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);
    rbd.setFactoryMethodName("createInstance");
    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 2 with GenericBean

use of org.springframework.tests.sample.beans.GenericBean 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)

Aggregations

AbstractCollection (java.util.AbstractCollection)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 PropertyEditorRegistrar (org.springframework.beans.PropertyEditorRegistrar)2 PropertyEditorRegistry (org.springframework.beans.PropertyEditorRegistry)2 CustomNumberEditor (org.springframework.beans.propertyeditors.CustomNumberEditor)2 GenericBean (org.springframework.tests.sample.beans.GenericBean)2