Search in sources :

Example 11 with GenericBean

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

the class BeanWrapperEnumTests method testStandardEnumSetWithMultipleValues.

@Test
public void testStandardEnumSetWithMultipleValues() {
    GenericBean<?> gb = new GenericBean<>();
    BeanWrapper bw = new BeanWrapperImpl(gb);
    bw.setConversionService(new DefaultConversionService());
    assertThat(gb.getStandardEnumSet()).isNull();
    bw.setPropertyValue("standardEnumSet", new String[] { "VALUE_1", "VALUE_2" });
    assertThat(gb.getStandardEnumSet().size()).isEqualTo(2);
    assertThat(gb.getStandardEnumSet().contains(CustomEnum.VALUE_1)).isTrue();
    assertThat(gb.getStandardEnumSet().contains(CustomEnum.VALUE_2)).isTrue();
}
Also used : DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) Test(org.junit.jupiter.api.Test)

Example 12 with GenericBean

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

the class BeanWrapperEnumTests method testStandardEnumMapWithMultipleValues.

@Test
public void testStandardEnumMapWithMultipleValues() {
    GenericBean<?> gb = new GenericBean<>();
    BeanWrapper bw = new BeanWrapperImpl(gb);
    bw.setConversionService(new DefaultConversionService());
    assertThat(gb.getStandardEnumMap()).isNull();
    Map<String, Integer> map = new LinkedHashMap<>();
    map.put("VALUE_1", 1);
    map.put("VALUE_2", 2);
    bw.setPropertyValue("standardEnumMap", map);
    assertThat(gb.getStandardEnumMap().size()).isEqualTo(2);
    assertThat(gb.getStandardEnumMap().get(CustomEnum.VALUE_1)).isEqualTo(1);
    assertThat(gb.getStandardEnumMap().get(CustomEnum.VALUE_2)).isEqualTo(2);
}
Also used : DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 13 with GenericBean

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

the class BeanFactoryGenericsTests method testGenericSetListConstructor.

@Test
void testGenericSetListConstructor() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
    Set<String> input = new HashSet<>();
    input.add("4");
    input.add("5");
    List<String> input2 = new ArrayList<>();
    input2.add("http://localhost:8080");
    input2.add("http://localhost:9090");
    rbd.getConstructorArgumentValues().addGenericArgumentValue(input);
    rbd.getConstructorArgumentValues().addGenericArgumentValue(input2);
    bf.registerBeanDefinition("genericBean", rbd);
    GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
    assertThat(gb.getIntegerSet().contains(4)).isTrue();
    assertThat(gb.getIntegerSet().contains(5)).isTrue();
    assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080"));
    assertThat(gb.getResourceList().get(1)).isEqualTo(new UrlResource("http://localhost:9090"));
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ArrayList(java.util.ArrayList) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 14 with GenericBean

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

the class BeanFactoryGenericsTests method testGenericSetListConstructorWithOptionalAutowiring.

@Test
void testGenericSetListConstructorWithOptionalAutowiring() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerSingleton("resource1", new UrlResource("http://localhost:8080"));
    bf.registerSingleton("resource2", new UrlResource("http://localhost:9090"));
    RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
    rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    bf.registerBeanDefinition("genericBean", rbd);
    GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
    assertThat(gb.getIntegerSet()).isNull();
    assertThat(gb.getResourceList()).isNull();
}
Also used : UrlResource(org.springframework.core.io.UrlResource) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) Test(org.junit.jupiter.api.Test)

Example 15 with GenericBean

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

the class BeanFactoryGenericsTests method testGenericMapResourceConstructor.

@Test
void testGenericMapResourceConstructor() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
    Map<String, String> input = new HashMap<>();
    input.put("4", "5");
    input.put("6", "7");
    rbd.getConstructorArgumentValues().addGenericArgumentValue(input);
    rbd.getConstructorArgumentValues().addGenericArgumentValue("http://localhost:8080");
    bf.registerBeanDefinition("genericBean", rbd);
    GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
    assertThat(gb.getShortMap().get(Short.valueOf("4"))).isEqualTo(5);
    assertThat(gb.getShortMap().get(Short.valueOf("6"))).isEqualTo(7);
    assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080"));
}
Also used : HashMap(java.util.HashMap) UrlResource(org.springframework.core.io.UrlResource) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)19 GenericBean (org.springframework.beans.testfixture.beans.GenericBean)19 UrlResource (org.springframework.core.io.UrlResource)9 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 HashMap (java.util.HashMap)3 CustomNumberEditor (org.springframework.beans.propertyeditors.CustomNumberEditor)3 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)2 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 Properties (java.util.Properties)1 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)1 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)1 TestBean (org.springframework.beans.testfixture.beans.TestBean)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 BindStatus (org.springframework.web.servlet.support.BindStatus)1