Search in sources :

Example 16 with GenericBean

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

the class BeanFactoryGenericsTests method testGenericListOfArraysProperty.

@Test
void testGenericListOfArraysProperty() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("genericBeanTests.xml", getClass()));
    GenericBean<?> gb = (GenericBean<?>) bf.getBean("listOfArrays");
    assertThat(gb.getListOfArrays().size()).isEqualTo(1);
    String[] array = gb.getListOfArrays().get(0);
    assertThat(array).hasSize(2);
    assertThat(array[0]).isEqualTo("value1");
    assertThat(array[1]).isEqualTo("value2");
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 17 with GenericBean

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

the class BeanFactoryGenericsTests method testGenericMapResourceFactoryMethod.

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

Example 18 with GenericBean

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

the class BeanFactoryGenericsTests method testGenericSetListConstructorWithAutowiring.

@Test
void testGenericSetListConstructorWithAutowiring() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerSingleton("integer1", 4);
    bf.registerSingleton("integer2", 5);
    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().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) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) Test(org.junit.jupiter.api.Test)

Example 19 with GenericBean

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

the class OptionTagEnumTests method withJavaEnum.

@Test
@SuppressWarnings("rawtypes")
public void withJavaEnum() throws Exception {
    GenericBean testBean = new GenericBean();
    testBean.setCustomEnum(CustomEnum.VALUE_1);
    getPageContext().getRequest().setAttribute("testBean", testBean);
    String selectName = "testBean.customEnum";
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, false));
    this.tag.setValue("VALUE_1");
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
    result = this.tag.doEndTag();
    assertThat(result).isEqualTo(Tag.EVAL_PAGE);
    String output = getWriter().toString();
    assertOptionTagOpened(output);
    assertOptionTagClosed(output);
    assertContainsAttribute(output, "value", "VALUE_1");
    assertContainsAttribute(output, "selected", "selected");
}
Also used : GenericBean(org.springframework.beans.testfixture.beans.GenericBean) BindStatus(org.springframework.web.servlet.support.BindStatus) 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