Search in sources :

Example 41 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-framework by spring-projects.

the class TransactionInterceptorTests method determineTransactionManagerWithQualifierAndDefaultName.

@Test
public void determineTransactionManagerWithQualifierAndDefaultName() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    associateTransactionManager(beanFactory, "defaultTransactionManager");
    TransactionInterceptor ti = transactionInterceptorWithTransactionManagerName("defaultTransactionManager", beanFactory);
    PlatformTransactionManager fooTransactionManager = associateTransactionManager(beanFactory, "fooTransactionManager");
    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    attribute.setQualifier("fooTransactionManager");
    assertSame(fooTransactionManager, ti.determineTransactionManager(attribute));
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 42 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-framework by spring-projects.

the class TransactionInterceptorTests method determineTransactionManagerWithEmptyQualifierAndDefaultName.

@Test
public void determineTransactionManagerWithEmptyQualifierAndDefaultName() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    PlatformTransactionManager defaultTransactionManager = associateTransactionManager(beanFactory, "defaultTransactionManager");
    TransactionInterceptor ti = transactionInterceptorWithTransactionManagerName("defaultTransactionManager", beanFactory);
    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    attribute.setQualifier("");
    assertSame(defaultTransactionManager, ti.determineTransactionManager(attribute));
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 43 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-framework by spring-projects.

the class ObjectFactoryCreatingFactoryBeanTests method testDoesNotComplainWhenTargetBeanNameRefersToSingleton.

@Test
public void testDoesNotComplainWhenTargetBeanNameRefersToSingleton() throws Exception {
    final String targetBeanName = "singleton";
    final String expectedSingleton = "Alicia Keys";
    BeanFactory beanFactory = mock(BeanFactory.class);
    given(beanFactory.getBean(targetBeanName)).willReturn(expectedSingleton);
    ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean();
    factory.setTargetBeanName(targetBeanName);
    factory.setBeanFactory(beanFactory);
    factory.afterPropertiesSet();
    ObjectFactory<?> objectFactory = factory.getObject();
    Object actualSingleton = objectFactory.getObject();
    assertSame(expectedSingleton, actualSingleton);
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 44 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-framework by spring-projects.

the class Rollback method testProgrammaticRollback.

@Test
public void testProgrammaticRollback() throws Exception {
    BeanFactory bf = getBeanFactory();
    Object bean = bf.getBean(TXMANAGER_BEAN_NAME);
    assertTrue(bean instanceof CallCountingTransactionManager);
    CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);
    Rollback rb = (Rollback) bf.getBean("rollback");
    assertEquals(0, txMan.commits);
    rb.rollbackOnly(false);
    assertEquals("Transaction counts match", 1, txMan.commits);
    assertEquals(0, txMan.rollbacks);
    // Will cause rollback only
    rb.rollbackOnly(true);
    assertEquals(1, txMan.rollbacks);
}
Also used : CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 45 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-framework by spring-projects.

the class Rollback method testRollbackRulesOnMethodCauseRollback.

/**
	 * Should not roll back on servlet exception.
	 */
@Test
public 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");
    assertEquals(0, txc.getCountingBeforeAdvice().getCalls());
    assertEquals(0, txMan.commits);
    rb.echoException(null);
    // Fires only on setters
    assertEquals(0, txc.getCountingBeforeAdvice().getCalls());
    assertEquals("Transaction counts match", 1, txMan.commits);
    assertEquals(0, txMan.rollbacks);
    Exception ex = new Exception();
    try {
        rb.echoException(ex);
    } catch (Exception actual) {
        assertEquals(ex, actual);
    }
    assertEquals("Transaction counts match", 1, txMan.rollbacks);
}
Also used : CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) BeanFactory(org.springframework.beans.factory.BeanFactory) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) NoTransactionException(org.springframework.transaction.NoTransactionException) Test(org.junit.Test)

Aggregations

BeanFactory (org.springframework.beans.factory.BeanFactory)62 Test (org.junit.Test)28 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)17 ITestBean (org.springframework.tests.sample.beans.ITestBean)11 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)10 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)10 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)8 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)7 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)5 FactoryBean (org.springframework.beans.factory.FactoryBean)4 SmartFactoryBean (org.springframework.beans.factory.SmartFactoryBean)4 CallCountingTransactionManager (org.springframework.tests.transaction.CallCountingTransactionManager)4 DataSource (javax.sql.DataSource)3 Ignite (org.apache.ignite.Ignite)3 TestInjectionLifecycleBean (org.apache.ignite.TestInjectionLifecycleBean)3 BeansException (org.springframework.beans.BeansException)3 BeanNotOfRequiredTypeException (org.springframework.beans.factory.BeanNotOfRequiredTypeException)3 HierarchicalBeanFactory (org.springframework.beans.factory.HierarchicalBeanFactory)3