Java Code Examples for org.springframework.tests.sample.beans.ITestBean#exceptional()

The following examples show how to use org.springframework.tests.sample.beans.ITestBean#exceptional() . 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: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAfterReturningAdvisorIsNotInvokedOnException() {
	CountingAfterReturningAdvice car = new CountingAfterReturningAdvice();
	TestBean target = new TestBean();
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(new NopInterceptor());
	pf.addAdvice(car);
	assertEquals("Advice was wrapped in Advisor and added", car, pf.getAdvisors()[1].getAdvice());
	ITestBean proxied = (ITestBean) createProxy(pf);
	assertEquals(0, car.getCalls());
	int age = 10;
	proxied.setAge(age);
	assertEquals(age, proxied.getAge());
	assertEquals(2, car.getCalls());
	Exception exc = new Exception();
	// On exception it won't be invoked
	try {
		proxied.exceptional(exc);
		fail();
	}
	catch (Throwable t) {
		assertSame(exc, t);
	}
	assertEquals(2, car.getCalls());
}
 
Example 2
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 3
Source File: AbstractTransactionAspectTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void transactionExceptionPropagatedWithCallbackPreference() throws Throwable {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(exceptionalMethod, txatt);

	MockCallbackPreferringTransactionManager ptm = new MockCallbackPreferringTransactionManager();

	TestBean tb = new TestBean();
	ITestBean itb = (ITestBean) advised(tb, ptm, tas);

	checkTransactionStatus(false);
	try {
		itb.exceptional(new OptimisticLockingFailureException(""));
		fail("Should have thrown OptimisticLockingFailureException");
	}
	catch (OptimisticLockingFailureException ex) {
		// expected
	}
	checkTransactionStatus(false);

	assertSame(txatt, ptm.getDefinition());
	assertFalse(ptm.getStatus().isRollbackOnly());
}
 
Example 4
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAfterReturningAdvisorIsNotInvokedOnException() {
	CountingAfterReturningAdvice car = new CountingAfterReturningAdvice();
	TestBean target = new TestBean();
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(new NopInterceptor());
	pf.addAdvice(car);
	assertEquals("Advice was wrapped in Advisor and added", car, pf.getAdvisors()[1].getAdvice());
	ITestBean proxied = (ITestBean) createProxy(pf);
	assertEquals(0, car.getCalls());
	int age = 10;
	proxied.setAge(age);
	assertEquals(age, proxied.getAge());
	assertEquals(2, car.getCalls());
	Exception exc = new Exception();
	// On exception it won't be invoked
	try {
		proxied.exceptional(exc);
		fail();
	}
	catch (Throwable t) {
		assertSame(exc, t);
	}
	assertEquals(2, car.getCalls());
}
 
Example 5
Source File: RegexpMethodPointcutAdvisorIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSinglePattern() throws Throwable {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
	ITestBean advised = (ITestBean) bf.getBean("settersAdvised");
	// Interceptor behind regexp advisor
	NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
	assertEquals(0, nop.getCount());

	int newAge = 12;
	// Not advised
	advised.exceptional(null);
	assertEquals(0, nop.getCount());
	advised.setAge(newAge);
	assertEquals(newAge, advised.getAge());
	// Only setter fired
	assertEquals(1, nop.getCount());
}
 
Example 6
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 7
Source File: AbstractTransactionAspectTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void transactionExceptionPropagatedWithCallbackPreference() throws Throwable {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(exceptionalMethod, txatt);

	MockCallbackPreferringTransactionManager ptm = new MockCallbackPreferringTransactionManager();

	TestBean tb = new TestBean();
	ITestBean itb = (ITestBean) advised(tb, ptm, tas);

	checkTransactionStatus(false);
	try {
		itb.exceptional(new OptimisticLockingFailureException(""));
		fail("Should have thrown OptimisticLockingFailureException");
	}
	catch (OptimisticLockingFailureException ex) {
		// expected
	}
	checkTransactionStatus(false);

	assertSame(txatt, ptm.getDefinition());
	assertFalse(ptm.getStatus().isRollbackOnly());
}
 
Example 8
Source File: RegexpMethodPointcutAdvisorIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSinglePattern() throws Throwable {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
	ITestBean advised = (ITestBean) bf.getBean("settersAdvised");
	// Interceptor behind regexp advisor
	NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
	assertEquals(0, nop.getCount());

	int newAge = 12;
	// Not advised
	advised.exceptional(null);
	assertEquals(0, nop.getCount());
	advised.setAge(newAge);
	assertEquals(newAge, advised.getAge());
	// Only setter fired
	assertEquals(1, nop.getCount());
}
 
Example 9
Source File: RegexpMethodPointcutAdvisorIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSinglePattern() throws Throwable {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
	ITestBean advised = (ITestBean) bf.getBean("settersAdvised");
	// Interceptor behind regexp advisor
	NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
	assertEquals(0, nop.getCount());

	int newAge = 12;
	// Not advised
	advised.exceptional(null);
	assertEquals(0, nop.getCount());
	advised.setAge(newAge);
	assertEquals(newAge, advised.getAge());
	// Only setter fired
	assertEquals(1, nop.getCount());
}
 
Example 10
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 11
Source File: AbstractTransactionAspectTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void transactionExceptionPropagatedWithCallbackPreference() throws Throwable {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(exceptionalMethod, txatt);

	MockCallbackPreferringTransactionManager ptm = new MockCallbackPreferringTransactionManager();

	TestBean tb = new TestBean();
	ITestBean itb = (ITestBean) advised(tb, ptm, tas);

	checkTransactionStatus(false);
	try {
		itb.exceptional(new OptimisticLockingFailureException(""));
		fail("Should have thrown OptimisticLockingFailureException");
	}
	catch (OptimisticLockingFailureException ex) {
		// expected
	}
	checkTransactionStatus(false);

	assertSame(txatt, ptm.getDefinition());
	assertFalse(ptm.getStatus().isRollbackOnly());
}
 
Example 12
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAfterReturningAdvisorIsNotInvokedOnException() {
	CountingAfterReturningAdvice car = new CountingAfterReturningAdvice();
	TestBean target = new TestBean();
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(new NopInterceptor());
	pf.addAdvice(car);
	assertEquals("Advice was wrapped in Advisor and added", car, pf.getAdvisors()[1].getAdvice());
	ITestBean proxied = (ITestBean) createProxy(pf);
	assertEquals(0, car.getCalls());
	int age = 10;
	proxied.setAge(age);
	assertEquals(age, proxied.getAge());
	assertEquals(2, car.getCalls());
	Exception exc = new Exception();
	// On exception it won't be invoked
	try {
		proxied.exceptional(exc);
		fail();
	}
	catch (Throwable t) {
		assertSame(exc, t);
	}
	assertEquals(2, car.getCalls());
}
 
Example 13
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testMultiAdvice() throws Throwable {
	CountingMultiAdvice cca = new CountingMultiAdvice();
	@SuppressWarnings("serial")
	Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
		@Override
		public boolean matches(Method m, @Nullable Class<?> targetClass) {
			return m.getParameterCount() == 0 || "exceptional".equals(m.getName());
		}
	};
	TestBean target = new TestBean();
	target.setAge(80);
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(new NopInterceptor());
	pf.addAdvisor(matchesNoArgs);
	assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
	ITestBean proxied = (ITestBean) createProxy(pf);

	assertEquals(0, cca.getCalls());
	assertEquals(0, cca.getCalls("getAge"));
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(2, cca.getCalls());
	assertEquals(2, cca.getCalls("getAge"));
	assertEquals(0, cca.getCalls("setAge"));
	// Won't be advised
	proxied.setAge(26);
	assertEquals(2, cca.getCalls());
	assertEquals(26, proxied.getAge());
	assertEquals(4, cca.getCalls());
	try {
		proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException)null));
		fail("Should have thrown CannotGetJdbcConnectionException");
	}
	catch (SpecializedUncheckedException ex) {
		// expected
	}
	assertEquals(6, cca.getCalls());
}
 
Example 14
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testDeclaredException() throws Throwable {
	final Exception expectedException = new Exception();
	// Test return value
	MethodInterceptor mi = new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			throw expectedException;
		}
	};
	AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
	pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
	pc.addAdvice(mi);

	// We don't care about the object
	mockTargetSource.setTarget(new TestBean());
	pc.setTargetSource(mockTargetSource);
	AopProxy aop = createAopProxy(pc);

	try {
		ITestBean tb = (ITestBean) aop.getProxy();
		// Note: exception param below isn't used
		tb.exceptional(expectedException);
		fail("Should have thrown exception raised by interceptor");
	}
	catch (Exception thrown) {
		assertEquals("exception matches", expectedException, thrown);
	}
}
 
Example 15
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeclaredException() throws Throwable {
	final Exception expectedException = new Exception();
	// Test return value
	MethodInterceptor mi = new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			throw expectedException;
		}
	};
	AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
	pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
	pc.addAdvice(mi);

	// We don't care about the object
	mockTargetSource.setTarget(new Object());
	pc.setTargetSource(mockTargetSource);
	AopProxy aop = createAopProxy(pc);

	try {
		ITestBean tb = (ITestBean) aop.getProxy();
		// Note: exception param below isn't used
		tb.exceptional(expectedException);
		fail("Should have thrown exception raised by interceptor");
	}
	catch (Exception thrown) {
		assertEquals("exception matches", expectedException, thrown);
	}
}
 
Example 16
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiAdvice() throws Throwable {
	CountingMultiAdvice cca = new CountingMultiAdvice();
	@SuppressWarnings("serial")
	Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
		@Override
		public boolean matches(Method m, Class<?> targetClass) {
			return m.getParameterTypes().length == 0 || "exceptional".equals(m.getName());
		}
	};
	TestBean target = new TestBean();
	target.setAge(80);
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(new NopInterceptor());
	pf.addAdvisor(matchesNoArgs);
	assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
	ITestBean proxied = (ITestBean) createProxy(pf);

	assertEquals(0, cca.getCalls());
	assertEquals(0, cca.getCalls("getAge"));
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(2, cca.getCalls());
	assertEquals(2, cca.getCalls("getAge"));
	assertEquals(0, cca.getCalls("setAge"));
	// Won't be advised
	proxied.setAge(26);
	assertEquals(2, cca.getCalls());
	assertEquals(26, proxied.getAge());
	assertEquals(4, cca.getCalls());
	try {
		proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException)null));
		fail("Should have thrown CannotGetJdbcConnectionException");
	}
	catch (SpecializedUncheckedException ex) {
		// expected
	}
	assertEquals(6, cca.getCalls());
}
 
Example 17
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testMultiAdvice() throws Throwable {
	CountingMultiAdvice cca = new CountingMultiAdvice();
	@SuppressWarnings("serial")
	Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
		@Override
		public boolean matches(Method m, @Nullable Class<?> targetClass) {
			return m.getParameterCount() == 0 || "exceptional".equals(m.getName());
		}
	};
	TestBean target = new TestBean();
	target.setAge(80);
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(new NopInterceptor());
	pf.addAdvisor(matchesNoArgs);
	assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
	ITestBean proxied = (ITestBean) createProxy(pf);

	assertEquals(0, cca.getCalls());
	assertEquals(0, cca.getCalls("getAge"));
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(2, cca.getCalls());
	assertEquals(2, cca.getCalls("getAge"));
	assertEquals(0, cca.getCalls("setAge"));
	// Won't be advised
	proxied.setAge(26);
	assertEquals(2, cca.getCalls());
	assertEquals(26, proxied.getAge());
	assertEquals(4, cca.getCalls());
	try {
		proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException)null));
		fail("Should have thrown CannotGetJdbcConnectionException");
	}
	catch (SpecializedUncheckedException ex) {
		// expected
	}
	assertEquals(6, cca.getCalls());
}
 
Example 18
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testDeclaredException() throws Throwable {
	final Exception expectedException = new Exception();
	// Test return value
	MethodInterceptor mi = new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			throw expectedException;
		}
	};
	AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
	pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
	pc.addAdvice(mi);

	// We don't care about the object
	mockTargetSource.setTarget(new TestBean());
	pc.setTargetSource(mockTargetSource);
	AopProxy aop = createAopProxy(pc);

	try {
		ITestBean tb = (ITestBean) aop.getProxy();
		// Note: exception param below isn't used
		tb.exceptional(expectedException);
		fail("Should have thrown exception raised by interceptor");
	}
	catch (Exception thrown) {
		assertEquals("exception matches", expectedException, thrown);
	}
}