Search in sources :

Example 6 with DerivedTestBean

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

the class SimpleTransactionScopeTests method getWithTransactionManager.

@Test
public void getWithTransactionManager() throws Exception {
    try (GenericApplicationContext context = new GenericApplicationContext()) {
        context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
        GenericBeanDefinition bd1 = new GenericBeanDefinition();
        bd1.setBeanClass(TestBean.class);
        bd1.setScope("tx");
        bd1.setPrimary(true);
        context.registerBeanDefinition("txScopedObject1", bd1);
        GenericBeanDefinition bd2 = new GenericBeanDefinition();
        bd2.setBeanClass(DerivedTestBean.class);
        bd2.setScope("tx");
        context.registerBeanDefinition("txScopedObject2", bd2);
        context.refresh();
        CallCountingTransactionManager tm = new CallCountingTransactionManager();
        TransactionTemplate tt = new TransactionTemplate(tm);
        Set<DerivedTestBean> finallyDestroy = new HashSet<>();
        tt.execute(status -> {
            TestBean bean1 = context.getBean(TestBean.class);
            assertThat(context.getBean(TestBean.class)).isSameAs(bean1);
            DerivedTestBean bean2 = context.getBean(DerivedTestBean.class);
            assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2);
            context.getBeanFactory().destroyScopedBean("txScopedObject2");
            assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
            assertThat(bean2.wasDestroyed()).isTrue();
            DerivedTestBean bean2a = context.getBean(DerivedTestBean.class);
            assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2a);
            assertThat(bean2a).isNotSameAs(bean2);
            context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
            assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
            assertThat(bean2a.wasDestroyed()).isFalse();
            DerivedTestBean bean2b = context.getBean(DerivedTestBean.class);
            finallyDestroy.add(bean2b);
            assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2b);
            assertThat(bean2b).isNotSameAs(bean2);
            assertThat(bean2b).isNotSameAs(bean2a);
            Set<DerivedTestBean> immediatelyDestroy = new HashSet<>();
            TransactionTemplate tt2 = new TransactionTemplate(tm);
            tt2.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
            tt2.execute(status2 -> {
                DerivedTestBean bean2c = context.getBean(DerivedTestBean.class);
                immediatelyDestroy.add(bean2c);
                assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2c);
                assertThat(bean2c).isNotSameAs(bean2);
                assertThat(bean2c).isNotSameAs(bean2a);
                assertThat(bean2c).isNotSameAs(bean2b);
                return null;
            });
            assertThat(immediatelyDestroy.iterator().next().wasDestroyed()).isTrue();
            assertThat(bean2b.wasDestroyed()).isFalse();
            return null;
        });
        assertThat(finallyDestroy.iterator().next().wasDestroyed()).isTrue();
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 7 with DerivedTestBean

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

the class BeanFactoryTransactionTests method testGetsAreNotTransactionalWithProxyFactory3.

@Test
public void testGetsAreNotTransactionalWithProxyFactory3() {
    ITestBean testBean = (ITestBean) factory.getBean("proxyFactory3");
    boolean condition = testBean instanceof DerivedTestBean;
    assertThat(condition).as("testBean is a full proxy").isTrue();
    boolean condition1 = testBean instanceof TransactionalProxy;
    assertThat(condition1).isTrue();
    InvocationCounterPointcut txnCounter = (InvocationCounterPointcut) factory.getBean("txnInvocationCounterPointcut");
    InvocationCounterInterceptor preCounter = (InvocationCounterInterceptor) factory.getBean("preInvocationCounterInterceptor");
    InvocationCounterInterceptor postCounter = (InvocationCounterInterceptor) factory.getBean("postInvocationCounterInterceptor");
    txnCounter.counter = 0;
    preCounter.counter = 0;
    postCounter.counter = 0;
    doTestGetsAreNotTransactional(testBean);
    // Can't assert it's equal to 4 as the pointcut may be optimized and only invoked once
    assertThat(0 < txnCounter.counter && txnCounter.counter <= 4).isTrue();
    assertThat(preCounter.counter).isEqualTo(4);
    assertThat(postCounter.counter).isEqualTo(4);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 8 with DerivedTestBean

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

the class WebApplicationContextScopeTests method testRequestScope.

@Test
public void testRequestScope() {
    WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_REQUEST);
    MockHttpServletRequest request = new MockHttpServletRequest();
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    try {
        assertThat(request.getAttribute(NAME)).isNull();
        DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
        assertThat(request.getAttribute(NAME)).isSameAs(bean);
        assertThat(ac.getBean(NAME)).isSameAs(bean);
        requestAttributes.requestCompleted();
        assertThat(bean.wasDestroyed()).isTrue();
    } finally {
        RequestContextHolder.setRequestAttributes(null);
    }
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 9 with DerivedTestBean

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

the class RequestScopeTests method destructionAtRequestCompletion.

@Test
public void destructionAtRequestCompletion() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    String name = "requestScopedDisposableObject";
    assertThat(request.getAttribute(name)).isNull();
    DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
    assertThat(request.getAttribute(name)).isSameAs(bean);
    assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
    requestAttributes.requestCompleted();
    assertThat(bean.wasDestroyed()).isTrue();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 10 with DerivedTestBean

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

the class DataBinderTests method testDirectBindingToEmptyIndexedFieldWithRegisteredGenericEditor.

@Test
void testDirectBindingToEmptyIndexedFieldWithRegisteredGenericEditor() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, "map", 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)

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