Search in sources :

Example 6 with GenericBean

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

the class BeanFactoryGenericsTests method testGenericSetListFactoryMethod.

@Test
void testGenericSetListFactoryMethod() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
    rbd.setFactoryMethodName("createInstance");
    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 7 with GenericBean

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

the class BeanFactoryGenericsTests method testGenericListProperty.

@Test
void testGenericListProperty() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
    List<String> input = new ArrayList<>();
    input.add("http://localhost:8080");
    input.add("http://localhost:9090");
    rbd.getPropertyValues().add("resourceList", input);
    bf.registerBeanDefinition("genericBean", rbd);
    GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
    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) Test(org.junit.jupiter.api.Test)

Example 8 with GenericBean

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

the class BeanWrapperGenericsTests method testGenericListOfArraysWithElementConversion.

@Test
void testGenericListOfArraysWithElementConversion() {
    GenericBean<String> gb = new GenericBean<>();
    ArrayList<String[]> list = new ArrayList<>();
    list.add(new String[] { "str1", "str2" });
    gb.setListOfArrays(list);
    BeanWrapper bw = new BeanWrapperImpl(gb);
    bw.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    bw.setPropertyValue("listOfArrays[0][1]", "str3 ");
    assertThat(bw.getPropertyValue("listOfArrays[0][1]")).isEqualTo("str3");
    assertThat(gb.getListOfArrays().get(0)[1]).isEqualTo("str3");
}
Also used : StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) ArrayList(java.util.ArrayList) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) Test(org.junit.jupiter.api.Test)

Example 9 with GenericBean

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

the class BeanWrapperGenericsTests method testGenericSetWithConversionFailure.

@Test
void testGenericSetWithConversionFailure() {
    GenericBean<?> gb = new GenericBean<>();
    BeanWrapper bw = new BeanWrapperImpl(gb);
    Set<TestBean> input = new HashSet<>();
    input.add(new TestBean());
    assertThatExceptionOfType(TypeMismatchException.class).isThrownBy(() -> bw.setPropertyValue("integerSet", input)).withMessageContaining("java.lang.Integer");
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 10 with GenericBean

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

the class BeanWrapperGenericsTests method testGenericListElement.

@Test
void testGenericListElement() throws Exception {
    GenericBean<?> gb = new GenericBean<>();
    gb.setResourceList(new ArrayList<>());
    BeanWrapper bw = new BeanWrapperImpl(gb);
    bw.setPropertyValue("resourceList[0]", "http://localhost:8080");
    assertThat(gb.getResourceList().get(0)).isEqualTo(new UrlResource("http://localhost:8080"));
}
Also used : 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