Search in sources :

Example 1 with ITestBean

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

the class RequestScopedProxyTests method testGetFromScopeThroughDynamicProxy.

@Test
public void testGetFromScopeThroughDynamicProxy() {
    String name = "requestScopedProxy";
    ITestBean bean = (ITestBean) this.beanFactory.getBean(name);
    // assertTrue(AopUtils.isJdkDynamicProxy(bean));
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    try {
        assertThat(request.getAttribute("scopedTarget." + name)).isNull();
        assertThat(bean.getName()).isEqualTo("scoped");
        assertThat(request.getAttribute("scopedTarget." + name)).isNotNull();
        TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
        assertThat(target.getClass()).isEqualTo(TestBean.class);
        assertThat(target.getName()).isEqualTo("scoped");
        assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
        assertThat(target.toString()).isEqualTo(bean.toString());
    } finally {
        RequestContextHolder.setRequestAttributes(null);
    }
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) CountingTestBean(org.springframework.beans.testfixture.beans.CountingTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 2 with ITestBean

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

the class TxNamespaceHandlerTests method invokeTransactional.

@Test
public void invokeTransactional() {
    ITestBean testBean = getTestBean();
    CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");
    // try with transactional
    assertThat(ptm.begun).as("Should not have any started transactions").isEqualTo(0);
    testBean.getName();
    assertThat(ptm.lastDefinition.isReadOnly()).isTrue();
    assertThat(ptm.lastDefinition.getTimeout()).isEqualTo(5);
    assertThat(ptm.begun).as("Should have 1 started transaction").isEqualTo(1);
    assertThat(ptm.commits).as("Should have 1 committed transaction").isEqualTo(1);
    // try with non-transaction
    testBean.haveBirthday();
    assertThat(ptm.begun).as("Should not have started another transaction").isEqualTo(1);
    // try with exceptional
    assertThatExceptionOfType(Throwable.class).isThrownBy(() -> testBean.exceptional(new IllegalArgumentException("foo")));
    assertThat(ptm.begun).as("Should have another started transaction").isEqualTo(2);
    assertThat(ptm.rollbacks).as("Should have 1 rolled back transaction").isEqualTo(1);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 3 with ITestBean

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

the class TxNamespaceHandlerTests method isProxy.

@Test
public void isProxy() {
    ITestBean bean = getTestBean();
    assertThat(AopUtils.isAopProxy(bean)).as("testBean is not a proxy").isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 4 with ITestBean

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

the class AbstractTransactionAspectTests method programmaticRollback.

/**
 * Test that TransactionStatus.setRollbackOnly works.
 */
@Test
public void programmaticRollback() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    Method m = getNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    given(ptm.getTransaction(txatt)).willReturn(status);
    final String name = "jenny";
    TestBean tb = new TestBean() {

        @Override
        public String getName() {
            TransactionStatus txStatus = TransactionInterceptor.currentTransactionStatus();
            txStatus.setRollbackOnly();
            return name;
        }
    };
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    // verification!?
    assertThat(name.equals(itb.getName())).isTrue();
    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) Method(java.lang.reflect.Method) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Example 5 with ITestBean

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

the class AbstractTransactionAspectTests method doTestRollbackOnException.

/**
 * Check that the given exception thrown by the target can produce the
 * desired behavior with the appropriate transaction attribute.
 * @param ex exception to be thrown by the target
 * @param shouldRollback whether this should cause a transaction rollback
 */
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute() {

        @Override
        public boolean rollbackOn(Throwable t) {
            assertThat(t == ex).isTrue();
            return shouldRollback;
        }
    };
    Method m = exceptionalMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // Gets additional call(s) from TransactionControl
    given(ptm.getTransaction(txatt)).willReturn(status);
    TransactionSystemException tex = new TransactionSystemException("system exception");
    if (rollbackException) {
        if (shouldRollback) {
            willThrow(tex).given(ptm).rollback(status);
        } else {
            willThrow(tex).given(ptm).commit(status);
        }
    }
    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    try {
        itb.exceptional(ex);
        fail("Should have thrown exception");
    } catch (Throwable t) {
        if (rollbackException) {
            assertThat(t).as("Caught wrong exception").isEqualTo(tex);
        } else {
            assertThat(t).as("Caught wrong exception").isEqualTo(ex);
        }
    }
    if (!rollbackException) {
        if (shouldRollback) {
            verify(ptm).rollback(status);
        } else {
            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) Method(java.lang.reflect.Method) TransactionSystemException(org.springframework.transaction.TransactionSystemException) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Aggregations

ITestBean (org.springframework.beans.testfixture.beans.ITestBean)210 Test (org.junit.jupiter.api.Test)199 TestBean (org.springframework.beans.testfixture.beans.TestBean)121 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)44 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)31 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)23 Advisor (org.springframework.aop.Advisor)22 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)21 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)20 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)19 Method (java.lang.reflect.Method)16 TimeStamped (org.springframework.core.testfixture.TimeStamped)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 TimestampIntroductionInterceptor (org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor)15 IOException (java.io.IOException)14 ProxyFactory (org.springframework.aop.framework.ProxyFactory)14 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)14 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)13 Advised (org.springframework.aop.framework.Advised)13