Search in sources :

Example 21 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-framework by TAKETODAY.

the class TestContextTransactionUtils method retrieveDataSource.

/**
 * Retrieve the {@link DataSource} to use for the supplied {@linkplain TestContext
 * test context}.
 * <p>The following algorithm is used to retrieve the {@code DataSource} from
 * the {@link cn.taketoday.context.ApplicationContext ApplicationContext}
 * of the supplied test context:
 * <ol>
 * <li>Look up the {@code DataSource} by type and name, if the supplied
 * {@code name} is non-empty, throwing a {@link BeansException} if the named
 * {@code DataSource} does not exist.
 * <li>Attempt to look up the single {@code DataSource} by type.
 * <li>Attempt to look up the <em>primary</em> {@code DataSource} by type.
 * <li>Attempt to look up the {@code DataSource} by type and the
 * {@linkplain #DEFAULT_DATA_SOURCE_NAME default data source name}.
 * </ol>
 *
 * @param testContext the test context for which the {@code DataSource}
 * should be retrieved; never {@code null}
 * @param name the name of the {@code DataSource} to retrieve
 * (may be {@code null} or <em>empty</em>)
 * @return the {@code DataSource} to use, or {@code null} if not found
 * @throws BeansException if an error occurs while retrieving an explicitly
 * named {@code DataSource}
 */
@Nullable
public static DataSource retrieveDataSource(TestContext testContext, @Nullable String name) {
    Assert.notNull(testContext, "TestContext must not be null");
    BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
    try {
        // Look up by type and explicit name
        if (StringUtils.hasText(name)) {
            return bf.getBean(name, DataSource.class);
        }
    } catch (BeansException ex) {
        logger.error(String.format("Failed to retrieve DataSource named '%s' for test context %s", name, testContext), ex);
        throw ex;
    }
    try {
        // Look up single bean by type
        Map<String, DataSource> dataSources = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, DataSource.class);
        if (dataSources.size() == 1) {
            return dataSources.values().iterator().next();
        }
        try {
            // look up single bean by type, with support for 'primary' beans
            return bf.getBean(DataSource.class);
        } catch (BeansException ex) {
            logBeansException(testContext, ex, PlatformTransactionManager.class);
        }
        // look up by type and default name
        return bf.getBean(DEFAULT_DATA_SOURCE_NAME, DataSource.class);
    } catch (BeansException ex) {
        logBeansException(testContext, ex, DataSource.class);
        return null;
    }
}
Also used : BeanFactory(cn.taketoday.beans.factory.BeanFactory) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) BeansException(cn.taketoday.beans.BeansException) DataSource(javax.sql.DataSource) Nullable(cn.taketoday.lang.Nullable)

Example 22 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-framework by TAKETODAY.

the class Rollback method testRollbackRulesOnMethodCauseRollback.

/**
 * Should not roll back on servlet exception.
 */
@Test
void testRollbackRulesOnMethodCauseRollback() throws Exception {
    BeanFactory bf = getBeanFactory();
    Rollback rb = (Rollback) bf.getBean("rollback");
    CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);
    OrderedTxCheckAdvisor txc = (OrderedTxCheckAdvisor) bf.getBean("orderedBeforeTransaction");
    assertThat(txc.getCountingBeforeAdvice().getCalls()).isEqualTo(0);
    assertThat(txMan.commits).isEqualTo(0);
    rb.echoException(null);
    // Fires only on setters
    assertThat(txc.getCountingBeforeAdvice().getCalls()).isEqualTo(0);
    assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1);
    assertThat(txMan.rollbacks).isEqualTo(0);
    Exception ex = new Exception();
    try {
        rb.echoException(ex);
    } catch (Exception actual) {
        assertThat(actual).isEqualTo(ex);
    }
    assertThat(txMan.rollbacks).as("Transaction counts match").isEqualTo(1);
}
Also used : CallCountingTransactionManager(cn.taketoday.testfixture.transaction.CallCountingTransactionManager) BeanFactory(cn.taketoday.beans.factory.BeanFactory) NoTransactionException(cn.taketoday.transaction.NoTransactionException) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) Test(org.junit.jupiter.api.Test)

Example 23 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-framework by TAKETODAY.

the class Rollback method testRegexpApplied.

@Test
void testRegexpApplied() throws Exception {
    BeanFactory bf = getBeanFactory();
    ITestBean test = (ITestBean) bf.getBean("test");
    MethodCounter counter = (MethodCounter) bf.getBean("countingAdvice");
    assertThat(counter.getCalls()).isEqualTo(0);
    test.getName();
    assertThat(counter.getCalls()).isEqualTo(1);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) BeanFactory(cn.taketoday.beans.factory.BeanFactory) MethodCounter(cn.taketoday.aop.testfixture.advice.MethodCounter) Test(org.junit.jupiter.api.Test)

Example 24 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-framework by TAKETODAY.

the class Rollback method testRollbackRulesOnMethodPreventRollback.

@Test
void testRollbackRulesOnMethodPreventRollback() throws Exception {
    BeanFactory bf = getBeanFactory();
    Rollback rb = (Rollback) bf.getBean("rollback");
    CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);
    assertThat(txMan.commits).isEqualTo(0);
    // Should NOT roll back on ServletException
    try {
        rb.echoException(new ServletException());
    } catch (ServletException ex) {
    }
    assertThat(txMan.commits).as("Transaction counts match").isEqualTo(1);
}
Also used : ServletException(jakarta.servlet.ServletException) CallCountingTransactionManager(cn.taketoday.testfixture.transaction.CallCountingTransactionManager) BeanFactory(cn.taketoday.beans.factory.BeanFactory) Test(org.junit.jupiter.api.Test)

Example 25 with BeanFactory

use of cn.taketoday.beans.factory.BeanFactory in project today-framework by TAKETODAY.

the class BeanFactoryDataSourceLookupTests method testLookupWhereBeanFactoryYieldsNonDataSourceType.

@Test
public void testLookupWhereBeanFactoryYieldsNonDataSourceType() throws Exception {
    final BeanFactory beanFactory = mock(BeanFactory.class);
    given(beanFactory.getBean(DATASOURCE_BEAN_NAME, DataSource.class)).willThrow(new BeanNotOfRequiredTypeException(DATASOURCE_BEAN_NAME, DataSource.class, String.class));
    BeanFactoryDataSourceLookup lookup = new BeanFactoryDataSourceLookup(beanFactory);
    assertThatExceptionOfType(DataSourceLookupFailureException.class).isThrownBy(() -> lookup.getDataSource(DATASOURCE_BEAN_NAME));
}
Also used : BeanFactory(cn.taketoday.beans.factory.BeanFactory) BeanNotOfRequiredTypeException(cn.taketoday.beans.factory.BeanNotOfRequiredTypeException) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Aggregations

BeanFactory (cn.taketoday.beans.factory.BeanFactory)80 Test (org.junit.jupiter.api.Test)60 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)24 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)16 PlatformTransactionManager (cn.taketoday.transaction.PlatformTransactionManager)16 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)10 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)8 CallCountingTransactionManager (cn.taketoday.testfixture.transaction.CallCountingTransactionManager)8 TransactionManager (cn.taketoday.transaction.TransactionManager)8 NestedTestBean (cn.taketoday.beans.testfixture.beans.NestedTestBean)6 List (java.util.List)6 DataSource (javax.sql.DataSource)6 Advised (cn.taketoday.aop.framework.Advised)4 BeansException (cn.taketoday.beans.BeansException)4 FatalBeanException (cn.taketoday.beans.FatalBeanException)4 NoSuchBeanDefinitionException (cn.taketoday.beans.factory.NoSuchBeanDefinitionException)4 Nullable (cn.taketoday.lang.Nullable)4 Collection (java.util.Collection)4 Set (java.util.Set)4 ServletException (jakarta.servlet.ServletException)3