Search in sources :

Example 81 with TestBean

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

the class AbstractTransactionAspectTests method noTransaction.

@Test
public void noTransaction() throws Exception {
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    TestBean tb = new TestBean();
    TransactionAttributeSource tas = new MapTransactionAttributeSource();
    // All the methods in this class use the advised() template method
    // to obtain a transaction object, configured with the given PlatformTransactionManager
    // and transaction attribute source
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    checkTransactionStatus(false);
    itb.getName();
    checkTransactionStatus(false);
    // expect no calls
    verifyNoInteractions(ptm);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Example 82 with TestBean

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

the class AbstractTransactionAspectTests method enclosingTransactionWithNonTransactionMethodOnAdvisedInside.

@Test
public void enclosingTransactionWithNonTransactionMethodOnAdvisedInside() throws Throwable {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(exceptionalMethod, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // Expect a transaction
    given(ptm.getTransaction(txatt)).willReturn(status);
    final String spouseName = "innerName";
    TestBean outer = new TestBean() {

        @Override
        public void exceptional(Throwable t) throws Throwable {
            TransactionInfo ti = TransactionAspectSupport.currentTransactionInfo();
            assertThat(ti.hasTransaction()).isTrue();
            assertThat(getSpouse().getName()).isEqualTo(spouseName);
        }
    };
    TestBean inner = new TestBean() {

        @Override
        public String getName() {
            // Assert that we're in the inner proxy
            TransactionInfo ti = TransactionAspectSupport.currentTransactionInfo();
            assertThat(ti.hasTransaction()).isFalse();
            return spouseName;
        }
    };
    ITestBean outerProxy = (ITestBean) advised(outer, ptm, tas);
    ITestBean innerProxy = (ITestBean) advised(inner, ptm, tas);
    outer.setSpouse(innerProxy);
    checkTransactionStatus(false);
    // Will invoke inner.getName, which is non-transactional
    outerProxy.exceptional(null);
    checkTransactionStatus(false);
    verify(ptm).commit(status);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionInfo(org.springframework.transaction.interceptor.TransactionAspectSupport.TransactionInfo) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Example 83 with TestBean

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

the class SimpleTransactionScopeTests method getFromScope.

@Test
@SuppressWarnings("resource")
public void getFromScope() throws Exception {
    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();
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(TestBean.class)).withCauseInstanceOf(IllegalStateException.class);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(DerivedTestBean.class)).withCauseInstanceOf(IllegalStateException.class);
    TestBean bean1 = null;
    DerivedTestBean bean2 = null;
    DerivedTestBean bean2a = null;
    DerivedTestBean bean2b = null;
    TransactionSynchronizationManager.initSynchronization();
    try {
        bean1 = context.getBean(TestBean.class);
        assertThat(context.getBean(TestBean.class)).isSameAs(bean1);
        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();
        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();
        bean2b = context.getBean(DerivedTestBean.class);
        assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2b);
        assertThat(bean2b).isNotSameAs(bean2);
        assertThat(bean2b).isNotSameAs(bean2a);
    } finally {
        TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
        TransactionSynchronizationManager.clearSynchronization();
    }
    assertThat(bean2a.wasDestroyed()).isFalse();
    assertThat(bean2b.wasDestroyed()).isTrue();
    assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(TestBean.class)).withCauseInstanceOf(IllegalStateException.class);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(DerivedTestBean.class)).withCauseInstanceOf(IllegalStateException.class);
}
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) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 84 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean 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 85 with TestBean

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

the class AopNamespaceHandlerScopeIntegrationTests method testSessionScoping.

@Test
void testSessionScoping() throws Exception {
    MockHttpSession oldSession = new MockHttpSession();
    MockHttpSession newSession = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(oldSession);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    assertThat(AopUtils.isAopProxy(sessionScoped)).as("Should be AOP proxy").isTrue();
    boolean condition1 = sessionScoped instanceof TestBean;
    assertThat(condition1).as("Should not be target class proxy").isFalse();
    assertThat(sessionScopedAlias).isSameAs(sessionScoped);
    assertThat(AopUtils.isAopProxy(testBean)).as("Should be AOP proxy").isTrue();
    boolean condition = testBean instanceof TestBean;
    assertThat(condition).as("Regular bean should be JDK proxy").isFalse();
    String rob = "Rob Harrop";
    String bram = "Bram Smeets";
    assertThat(sessionScoped.getName()).isEqualTo(rob);
    sessionScoped.setName(bram);
    request.setSession(newSession);
    assertThat(sessionScoped.getName()).isEqualTo(rob);
    request.setSession(oldSession);
    assertThat(sessionScoped.getName()).isEqualTo(bram);
    assertThat(((Advised) sessionScoped).getAdvisors().length > 0).as("Should have advisors").isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.jupiter.api.Test)

Aggregations

TestBean (org.springframework.beans.testfixture.beans.TestBean)808 Test (org.junit.jupiter.api.Test)765 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)472 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)268 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)183 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)167 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)162 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)118 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)96 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)40 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)40 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)39 Properties (java.util.Properties)38 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)37 HashMap (java.util.HashMap)36 Errors (org.springframework.validation.Errors)35 PropertyEditorSupport (java.beans.PropertyEditorSupport)34 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 List (java.util.List)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28