org.springframework.tests.transaction.CallCountingTransactionManager Java Examples

The following examples show how to use org.springframework.tests.transaction.CallCountingTransactionManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AnnotationTransactionNamespaceHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void invokeTransactional() throws Exception {
	TransactionalTestBean testBean = getTestBean();
	CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");

	// try with transactional
	assertEquals("Should not have any started transactions", 0, ptm.begun);
	testBean.findAllFoos();
	assertEquals("Should have 1 started transaction", 1, ptm.begun);
	assertEquals("Should have 1 committed transaction", 1, ptm.commits);

	// try with non-transaction
	testBean.doSomething();
	assertEquals("Should not have started another transaction", 1, ptm.begun);

	// try with exceptional
	try {
		testBean.exceptional(new IllegalArgumentException("foo"));
		fail("Should NEVER get here");
	}
	catch (Throwable throwable) {
		assertEquals("Should have another started transaction", 2, ptm.begun);
		assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
	}
}
 
Example #2
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: AnnotationTransactionAttributeSourceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void serializable() throws Exception {
	TestBean1 tb = new TestBean1();
	CallCountingTransactionManager ptm = new CallCountingTransactionManager();
	AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource();
	TransactionInterceptor ti = new TransactionInterceptor(ptm, tas);

	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setInterfaces(ITestBean1.class);
	proxyFactory.addAdvice(ti);
	proxyFactory.setTarget(tb);
	ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy();
	proxy.getAge();
	assertEquals(1, ptm.commits);

	ITestBean1 serializedProxy = (ITestBean1) SerializationTestUtils.serializeAndDeserialize(proxy);
	serializedProxy.getAge();
	Advised advised = (Advised) serializedProxy;
	TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
	CallCountingTransactionManager serializedPtm =
			(CallCountingTransactionManager) serializedTi.getTransactionManager();
	assertEquals(2, serializedPtm.commits);
}
 
Example #4
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRollbackRulesOnMethodPreventRollback() throws Exception {
	BeanFactory bf = getBeanFactory();
	Rollback rb = (Rollback) bf.getBean("rollback");

	CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);

	assertEquals(0, txMan.commits);
	// Should NOT roll back on ServletException
	try {
		rb.echoException(new ServletException());
	}
	catch (ServletException ex) {

	}
	assertEquals("Transaction counts match", 1, txMan.commits);
}
 
Example #5
Source File: EnableTransactionManagementIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void explicitTxManager() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ExplicitTxManagerConfig.class);
	ctx.refresh();

	FooRepository fooRepository = ctx.getBean(FooRepository.class);
	fooRepository.findAll();

	CallCountingTransactionManager txManager1 = ctx.getBean("txManager1", CallCountingTransactionManager.class);
	assertThat(txManager1.begun, equalTo(1));
	assertThat(txManager1.commits, equalTo(1));
	assertThat(txManager1.rollbacks, equalTo(0));

	CallCountingTransactionManager txManager2 = ctx.getBean("txManager2", CallCountingTransactionManager.class);
	assertThat(txManager2.begun, equalTo(0));
	assertThat(txManager2.commits, equalTo(0));
	assertThat(txManager2.rollbacks, equalTo(0));
}
 
Example #6
Source File: BeanFactoryTransactionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Test that we can set the target to a dynamic TargetSource.
 */
@Test
public void testDynamicTargetSource() {
	// Install facade
	CallCountingTransactionManager txMan = new CallCountingTransactionManager();
	PlatformTransactionManagerFacade.delegate = txMan;

	TestBean tb = (TestBean) factory.getBean("hotSwapped");
	assertEquals(666, tb.getAge());
	int newAge = 557;
	tb.setAge(newAge);
	assertEquals(newAge, tb.getAge());

	TestBean target2 = new TestBean();
	target2.setAge(65);
	HotSwappableTargetSource ts = (HotSwappableTargetSource) factory.getBean("swapper");
	ts.swap(target2);
	assertEquals(target2.getAge(), tb.getAge());
	tb.setAge(newAge);
	assertEquals(newAge, target2.getAge());

	assertEquals(0, txMan.inflight);
	assertEquals(2, txMan.commits);
	assertEquals(0, txMan.rollbacks);
}
 
Example #7
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example #8
Source File: EnableTransactionManagementTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void spr11915TransactionManagerAsManualSingleton() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr11915Config.class);
	TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
	CallCountingTransactionManager txManager = ctx.getBean("qualifiedTransactionManager", CallCountingTransactionManager.class);

	bean.saveQualifiedFoo();
	assertThat(txManager.begun, equalTo(1));
	assertThat(txManager.commits, equalTo(1));
	assertThat(txManager.rollbacks, equalTo(0));

	bean.saveQualifiedFooWithAttributeAlias();
	assertThat(txManager.begun, equalTo(2));
	assertThat(txManager.commits, equalTo(2));
	assertThat(txManager.rollbacks, equalTo(0));

	ctx.close();
}
 
Example #9
Source File: AnnotationTransactionNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void invokeTransactional() throws Exception {
	TransactionalTestBean testBean = getTestBean();
	CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");

	// try with transactional
	assertEquals("Should not have any started transactions", 0, ptm.begun);
	testBean.findAllFoos();
	assertEquals("Should have 1 started transaction", 1, ptm.begun);
	assertEquals("Should have 1 committed transaction", 1, ptm.commits);

	// try with non-transaction
	testBean.doSomething();
	assertEquals("Should not have started another transaction", 1, ptm.begun);

	// try with exceptional
	try {
		testBean.exceptional(new IllegalArgumentException("foo"));
		fail("Should NEVER get here");
	}
	catch (Throwable throwable) {
		assertEquals("Should have another started transaction", 2, ptm.begun);
		assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
	}
}
 
Example #10
Source File: BeanFactoryTransactionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Test that we can set the target to a dynamic TargetSource.
 */
@Test
public void testDynamicTargetSource() throws NoSuchMethodException {
	// Install facade
	CallCountingTransactionManager txMan = new CallCountingTransactionManager();
	PlatformTransactionManagerFacade.delegate = txMan;

	TestBean tb = (TestBean) factory.getBean("hotSwapped");
	assertEquals(666, tb.getAge());
	int newAge = 557;
	tb.setAge(newAge);
	assertEquals(newAge, tb.getAge());

	TestBean target2 = new TestBean();
	target2.setAge(65);
	HotSwappableTargetSource ts = (HotSwappableTargetSource) factory.getBean("swapper");
	ts.swap(target2);
	assertEquals(target2.getAge(), tb.getAge());
	tb.setAge(newAge);
	assertEquals(newAge, target2.getAge());

	assertEquals(0, txMan.inflight);
	assertEquals(2, txMan.commits);
	assertEquals(0, txMan.rollbacks);
}
 
Example #11
Source File: EnableTransactionManagementTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void spr11915() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr11915Config.class);
	TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
	CallCountingTransactionManager txManager = ctx.getBean("qualifiedTransactionManager", CallCountingTransactionManager.class);

	bean.saveQualifiedFoo();
	assertThat(txManager.begun, equalTo(1));
	assertThat(txManager.commits, equalTo(1));
	assertThat(txManager.rollbacks, equalTo(0));

	bean.saveQualifiedFooWithAttributeAlias();
	assertThat(txManager.begun, equalTo(2));
	assertThat(txManager.commits, equalTo(2));
	assertThat(txManager.rollbacks, equalTo(0));

	ctx.close();
}
 
Example #12
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRollbackRulesOnMethodPreventRollback() throws Exception {
	BeanFactory bf = getBeanFactory();
	Rollback rb = (Rollback) bf.getBean("rollback");

	CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);

	assertEquals(0, txMan.commits);
	// Should NOT roll back on ServletException
	try {
		rb.echoException(new ServletException());
	}
	catch (ServletException ex) {

	}
	assertEquals("Transaction counts match", 1, txMan.commits);
}
 
Example #13
Source File: EnableTransactionManagementIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void explicitTxManager() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ExplicitTxManagerConfig.class);
	ctx.refresh();

	FooRepository fooRepository = ctx.getBean(FooRepository.class);
	fooRepository.findAll();

	CallCountingTransactionManager txManager1 = ctx.getBean("txManager1", CallCountingTransactionManager.class);
	assertThat(txManager1.begun, equalTo(1));
	assertThat(txManager1.commits, equalTo(1));
	assertThat(txManager1.rollbacks, equalTo(0));

	CallCountingTransactionManager txManager2 = ctx.getBean("txManager2", CallCountingTransactionManager.class);
	assertThat(txManager2.begun, equalTo(0));
	assertThat(txManager2.commits, equalTo(0));
	assertThat(txManager2.rollbacks, equalTo(0));
}
 
Example #14
Source File: AnnotationDrivenTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void doTestWithMultipleTransactionManagers(ApplicationContext context) {
	CallCountingTransactionManager tm1 = context.getBean("transactionManager1", CallCountingTransactionManager.class);
	CallCountingTransactionManager tm2 = context.getBean("transactionManager2", CallCountingTransactionManager.class);
	TransactionalService service = context.getBean("service", TransactionalService.class);
	assertTrue(AopUtils.isCglibProxy(service));
	service.setSomething("someName");
	assertEquals(1, tm1.commits);
	assertEquals(0, tm2.commits);
	service.doSomething();
	assertEquals(1, tm1.commits);
	assertEquals(1, tm2.commits);
	service.setSomething("someName");
	assertEquals(2, tm1.commits);
	assertEquals(1, tm2.commits);
	service.doSomething();
	assertEquals(2, tm1.commits);
	assertEquals(2, tm2.commits);
}
 
Example #15
Source File: TxNamespaceHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void invokeTransactional() {
	ITestBean testBean = getTestBean();
	CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");

	// try with transactional
	assertEquals("Should not have any started transactions", 0, ptm.begun);
	testBean.getName();
	assertTrue(ptm.lastDefinition.isReadOnly());
	assertEquals("Should have 1 started transaction", 1, ptm.begun);
	assertEquals("Should have 1 committed transaction", 1, ptm.commits);

	// try with non-transaction
	testBean.haveBirthday();
	assertEquals("Should not have started another transaction", 1, ptm.begun);

	// try with exceptional
	try {
		testBean.exceptional(new IllegalArgumentException("foo"));
		fail("Should NEVER get here");
	}
	catch (Throwable throwable) {
		assertEquals("Should have another started transaction", 2, ptm.begun);
		assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
	}
}
 
Example #16
Source File: TxNamespaceHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void invokeTransactional() {
	ITestBean testBean = getTestBean();
	CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");

	// try with transactional
	assertEquals("Should not have any started transactions", 0, ptm.begun);
	testBean.getName();
	assertTrue(ptm.lastDefinition.isReadOnly());
	assertEquals("Should have 1 started transaction", 1, ptm.begun);
	assertEquals("Should have 1 committed transaction", 1, ptm.commits);

	// try with non-transaction
	testBean.haveBirthday();
	assertEquals("Should not have started another transaction", 1, ptm.begun);

	// try with exceptional
	try {
		testBean.exceptional(new IllegalArgumentException("foo"));
		fail("Should NEVER get here");
	}
	catch (Throwable throwable) {
		assertEquals("Should have another started transaction", 2, ptm.begun);
		assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
	}
}
 
Example #17
Source File: AnnotationDrivenTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void doTestWithMultipleTransactionManagers(ApplicationContext context) {
	CallCountingTransactionManager tm1 = context.getBean("transactionManager1", CallCountingTransactionManager.class);
	CallCountingTransactionManager tm2 = context.getBean("transactionManager2", CallCountingTransactionManager.class);
	TransactionalService service = context.getBean("service", TransactionalService.class);
	assertTrue(AopUtils.isCglibProxy(service));
	service.setSomething("someName");
	assertEquals(1, tm1.commits);
	assertEquals(0, tm2.commits);
	service.doSomething();
	assertEquals(1, tm1.commits);
	assertEquals(1, tm2.commits);
	service.setSomething("someName");
	assertEquals(2, tm1.commits);
	assertEquals(1, tm2.commits);
	service.doSomething();
	assertEquals(2, tm1.commits);
	assertEquals(2, tm2.commits);
}
 
Example #18
Source File: TxNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void invokeTransactional() throws Exception {
	ITestBean testBean = getTestBean();
	CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");

	// try with transactional
	assertEquals("Should not have any started transactions", 0, ptm.begun);
	testBean.getName();
	assertTrue(ptm.lastDefinition.isReadOnly());
	assertEquals("Should have 1 started transaction", 1, ptm.begun);
	assertEquals("Should have 1 committed transaction", 1, ptm.commits);

	// try with non-transaction
	testBean.haveBirthday();
	assertEquals("Should not have started another transaction", 1, ptm.begun);

	// try with exceptional
	try {
		testBean.exceptional(new IllegalArgumentException("foo"));
		fail("Should NEVER get here");
	}
	catch (Throwable throwable) {
		assertEquals("Should have another started transaction", 2, ptm.begun);
		assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
	}
}
 
Example #19
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #20
Source File: EnableTransactionManagementTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void spr11915TransactionManagerAsManualSingleton() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr11915Config.class);
	TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
	CallCountingTransactionManager txManager = ctx.getBean("qualifiedTransactionManager", CallCountingTransactionManager.class);

	bean.saveQualifiedFoo();
	assertThat(txManager.begun, equalTo(1));
	assertThat(txManager.commits, equalTo(1));
	assertThat(txManager.rollbacks, equalTo(0));

	bean.saveQualifiedFooWithAttributeAlias();
	assertThat(txManager.begun, equalTo(2));
	assertThat(txManager.commits, equalTo(2));
	assertThat(txManager.rollbacks, equalTo(0));

	ctx.close();
}
 
Example #21
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactionAttributeOnMethod() throws Exception {
	BeanFactory bf = getBeanFactory();
	ITestBean test = (ITestBean) bf.getBean("test");

	CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);
	OrderedTxCheckAdvisor txc = (OrderedTxCheckAdvisor) bf.getBean("orderedBeforeTransaction");
	assertEquals(0, txc.getCountingBeforeAdvice().getCalls());

	assertEquals(0, txMan.commits);
	assertEquals("Initial value was correct", 4, test.getAge());
	int newAge = 5;
	test.setAge(newAge);
	assertEquals(1, txc.getCountingBeforeAdvice().getCalls());

	assertEquals("New value set correctly", newAge, test.getAge());
	assertEquals("Transaction counts match", 1, txMan.commits);
}
 
Example #22
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testTransactionAttributeOnMethod() throws Exception {
	BeanFactory bf = getBeanFactory();
	ITestBean test = (ITestBean) bf.getBean("test");

	CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);
	OrderedTxCheckAdvisor txc = (OrderedTxCheckAdvisor) bf.getBean("orderedBeforeTransaction");
	assertEquals(0, txc.getCountingBeforeAdvice().getCalls());

	assertEquals(0, txMan.commits);
	assertEquals("Initial value was correct", 4, test.getAge());
	int newAge = 5;
	test.setAge(newAge);
	assertEquals(1, txc.getCountingBeforeAdvice().getCalls());

	assertEquals("New value set correctly", newAge, test.getAge());
	assertEquals("Transaction counts match", 1, txMan.commits);
}
 
Example #23
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example #24
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testRollbackRulesOnMethodPreventRollback() throws Exception {
	BeanFactory bf = getBeanFactory();
	Rollback rb = (Rollback) bf.getBean("rollback");

	CallCountingTransactionManager txMan = (CallCountingTransactionManager) bf.getBean(TXMANAGER_BEAN_NAME);

	assertEquals(0, txMan.commits);
	// Should NOT roll back on ServletException
	try {
		rb.echoException(new ServletException());
	}
	catch (ServletException ex) {

	}
	assertEquals("Transaction counts match", 1, txMan.commits);
}
 
Example #25
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #26
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void serializable() throws Exception {
	TestBean1 tb = new TestBean1();
	CallCountingTransactionManager ptm = new CallCountingTransactionManager();
	AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource();
	TransactionInterceptor ti = new TransactionInterceptor(ptm, tas);

	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setInterfaces(ITestBean1.class);
	proxyFactory.addAdvice(ti);
	proxyFactory.setTarget(tb);
	ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy();
	proxy.getAge();
	assertEquals(1, ptm.commits);

	ITestBean1 serializedProxy = (ITestBean1) SerializationTestUtils.serializeAndDeserialize(proxy);
	serializedProxy.getAge();
	Advised advised = (Advised) serializedProxy;
	TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
	CallCountingTransactionManager serializedPtm =
			(CallCountingTransactionManager) serializedTi.getTransactionManager();
	assertEquals(2, serializedPtm.commits);
}
 
Example #27
Source File: AnnotationTransactionNamespaceHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void invokeTransactional() throws Exception {
	TransactionalTestBean testBean = getTestBean();
	CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");

	// try with transactional
	assertEquals("Should not have any started transactions", 0, ptm.begun);
	testBean.findAllFoos();
	assertEquals("Should have 1 started transaction", 1, ptm.begun);
	assertEquals("Should have 1 committed transaction", 1, ptm.commits);

	// try with non-transaction
	testBean.doSomething();
	assertEquals("Should not have started another transaction", 1, ptm.begun);

	// try with exceptional
	try {
		testBean.exceptional(new IllegalArgumentException("foo"));
		fail("Should NEVER get here");
	}
	catch (Throwable throwable) {
		assertEquals("Should have another started transaction", 2, ptm.begun);
		assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
	}
}
 
Example #28
Source File: TransactionManagerConfiguration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Bean
@Qualifier("noSynch")
public PlatformTransactionManager transactionManager2() {
	CallCountingTransactionManager tm = new CallCountingTransactionManager();
	tm.setTransactionSynchronization(CallCountingTransactionManager.SYNCHRONIZATION_NEVER);
	return tm;
}
 
Example #29
Source File: AnnotationTransactionNamespaceHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void nonPublicMethodsNotAdvised() {
	TransactionalTestBean testBean = getTestBean();
	CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");

	assertEquals("Should not have any started transactions", 0, ptm.begun);
	testBean.annotationsOnProtectedAreIgnored();
	assertEquals("Should not have any started transactions", 0, ptm.begun);
}
 
Example #30
Source File: EnableTransactionManagementIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void implicitTxManager() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ImplicitTxManagerConfig.class);
	ctx.refresh();

	FooRepository fooRepository = ctx.getBean(FooRepository.class);
	fooRepository.findAll();

	CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
	assertThat(txManager.begun, equalTo(1));
	assertThat(txManager.commits, equalTo(1));
	assertThat(txManager.rollbacks, equalTo(0));
}