Search in sources :

Example 11 with DerivedTestBean

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

the class DataBinderTests method testDirectBindingToEmptyIndexedFieldWithRegisteredSpecificEditor.

@Test
void testDirectBindingToEmptyIndexedFieldWithRegisteredSpecificEditor() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, "map[key0]", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            DerivedTestBean tb = new DerivedTestBean();
            tb.setName("array" + text);
            setValue(tb);
        }

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    Errors errors = binder.getBindingResult();
    errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
    assertThat(errors.getFieldErrorCount("map[key0]")).isEqualTo(1);
    assertThat(errors.getFieldError("map[key0]").getCode()).isEqualTo("NOT_NULL");
    assertThat(errors.getFieldError("map[key0]").getCodes()[0]).isEqualTo("NOT_NULL.tb.map[key0]");
    assertThat(errors.getFieldError("map[key0]").getCodes()[1]).isEqualTo("NOT_NULL.tb.map");
    assertThat(errors.getFieldError("map[key0]").getCodes()[2]).isEqualTo("NOT_NULL.map[key0]");
    assertThat(errors.getFieldError("map[key0]").getCodes()[3]).isEqualTo("NOT_NULL.map");
    // This next code is only generated because of the registered editor, using the
    // registered type of the editor as guess for the content type of the collection.
    assertThat(errors.getFieldError("map[key0]").getCodes()[4]).isEqualTo("NOT_NULL.org.springframework.beans.testfixture.beans.TestBean");
    assertThat(errors.getFieldError("map[key0]").getCodes()[5]).isEqualTo("NOT_NULL");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 12 with DerivedTestBean

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

the class DefaultSingletonBeanRegistryTests method testDisposableBean.

@Test
public void testDisposableBean() {
    DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();
    DerivedTestBean tb = new DerivedTestBean();
    beanRegistry.registerSingleton("tb", tb);
    beanRegistry.registerDisposableBean("tb", tb);
    assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
    assertThat(beanRegistry.getSingleton("tb")).isSameAs(tb);
    assertThat(beanRegistry.getSingletonCount()).isEqualTo(1);
    String[] names = beanRegistry.getSingletonNames();
    assertThat(names.length).isEqualTo(1);
    assertThat(names[0]).isEqualTo("tb");
    assertThat(tb.wasDestroyed()).isFalse();
    beanRegistry.destroySingletons();
    assertThat(beanRegistry.getSingletonCount()).isEqualTo(0);
    assertThat(beanRegistry.getSingletonNames().length).isEqualTo(0);
    assertThat(tb.wasDestroyed()).isTrue();
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 13 with DerivedTestBean

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

the class ConcurrencyThrottleInterceptorTests method testSerializable.

@Test
public void testSerializable() throws Exception {
    DerivedTestBean tb = new DerivedTestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean.class);
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    proxyFactory.addAdvice(cti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    proxy.getAge();
    ITestBean serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
    Advised advised = (Advised) serializedProxy;
    ConcurrencyThrottleInterceptor serializedCti = (ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
    assertThat(serializedCti.getConcurrencyLimit()).isEqualTo(cti.getConcurrencyLimit());
    serializedProxy.getAge();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Advised(org.springframework.aop.framework.Advised) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 14 with DerivedTestBean

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

the class XmlBeanFactoryTests method lenientDependencyMatchingFactoryMethod.

@Test
void lenientDependencyMatchingFactoryMethod() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    LenientDependencyTestBean bean = (LenientDependencyTestBean) xbf.getBean("lenientDependencyTestBeanFactoryMethod");
    assertThat(bean.tb instanceof DerivedTestBean).isTrue();
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 15 with DerivedTestBean

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

the class XmlBeanFactoryTests method lenientDependencyMatching.

@Test
void lenientDependencyMatching() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    LenientDependencyTestBean bean = (LenientDependencyTestBean) xbf.getBean("lenientDependencyTestBean");
    assertThat(bean.tb instanceof DerivedTestBean).isTrue();
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Aggregations

DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)27 Test (org.junit.jupiter.api.Test)26 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)6 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)6 PropertyEditorSupport (java.beans.PropertyEditorSupport)4 WebApplicationContext (org.springframework.web.context.WebApplicationContext)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)2 MockHttpSession (org.springframework.web.testfixture.servlet.MockHttpSession)2 ServletContextEvent (jakarta.servlet.ServletContextEvent)1 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1