HibernateTransactionDecoratorImpl implements HibernateTransactionDecorator { @Override public <T> T build(Class<T> serviceInterface, T delegate, String serviceId) { assert serviceInterface != null; assert delegate != null; assert InternalUtils.isNonBlank(serviceId); String description = String.format("<Hibernate Transaction interceptor for %s(%s)>", serviceId, serviceInterface.getName()); AspectInterceptorBuilder<T> builder = aspectDecorator.createBuilder(serviceInterface, delegate, description); advisor.addTransactionCommitAdvice(builder); return builder.build(); } HibernateTransactionDecoratorImpl(AspectDecorator aspectDecorator, HibernateTransactionAdvisor advisor); @Override T build(Class<T> serviceInterface, T delegate, String serviceId);  }
@Test public void undecorated() { VoidService delegate = newMock(VoidService.class); HibernateSessionManager manager = newMock(HibernateSessionManager.class); HibernateTransactionDecorator decorator = newHibernateSessionManagerDecorator(manager); VoidService interceptor = decorator.build(VoidService.class, delegate, "foo.Bar"); delegate.undecorated(); replay(); interceptor.undecorated(); verify(); assertToString(interceptor); }
@Test public void void_method() { VoidService delegate = newMock(VoidService.class); HibernateSessionManager manager = newMock(HibernateSessionManager.class); HibernateTransactionDecorator decorator = newHibernateSessionManagerDecorator(manager); VoidService interceptor = decorator.build(VoidService.class, delegate, "foo.Bar"); delegate.voidMethod(); manager.commit(); replay(); interceptor.voidMethod(); verify(); assertToString(interceptor); }
@Test public void void_method_with_param() { VoidService delegate = newMock(VoidService.class); HibernateSessionManager manager = newMock(HibernateSessionManager.class); HibernateTransactionDecorator decorator = newHibernateSessionManagerDecorator(manager); VoidService interceptor = decorator.build(VoidService.class, delegate, "foo.Bar"); delegate.voidMethodWithParam(777); manager.commit(); replay(); interceptor.voidMethodWithParam(777); verify(); assertToString(interceptor); }
@Test public void runtime_exception_will_abort_transaction() throws Exception { Performer delegate = newMock(Performer.class); HibernateSessionManager manager = newMock(HibernateSessionManager.class); HibernateTransactionDecorator decorator = newHibernateSessionManagerDecorator(manager); RuntimeException re = new RuntimeException("Unexpected."); delegate.perform(); TestBase.setThrowable(re); manager.abort(); replay(); Performer interceptor = decorator.build(Performer.class, delegate, "foo.Bar"); try { interceptor.perform(); TestBase.unreachable(); } catch (RuntimeException ex) { Assert.assertSame(ex, re); } verify(); }
@Test public void checked_exception_will_commit_transaction() throws Exception { Performer delegate = newMock(Performer.class); HibernateSessionManager manager = newMock(HibernateSessionManager.class); HibernateTransactionDecorator decorator = newHibernateSessionManagerDecorator(manager); SQLException se = new SQLException("Checked."); delegate.perform(); TestBase.setThrowable(se); manager.commit(); replay(); Performer interceptor = decorator.build(Performer.class, delegate, "foo.Bar"); try { interceptor.perform(); TestBase.unreachable(); } catch (SQLException ex) { Assert.assertSame(ex, se); } verify(); }
@Test public void return_type_method() { ReturnTypeService delegate = newTestService(); HibernateSessionManager manager = newMock(HibernateSessionManager.class); HibernateTransactionDecorator decorator = newHibernateSessionManagerDecorator(manager); ReturnTypeService interceptor = decorator.build(ReturnTypeService.class, delegate, "foo.Bar"); delegate.returnTypeMethod(); manager.commit(); replay(); Assert.assertEquals(interceptor.returnTypeMethod(), "Foo"); verify(); }
@Test public void return_type_method_with_param() { ReturnTypeService delegate = newTestService(); HibernateSessionManager manager = newMock(HibernateSessionManager.class); HibernateTransactionDecorator decorator = newHibernateSessionManagerDecorator(manager); ReturnTypeService interceptor = decorator.build(ReturnTypeService.class, delegate, "foo.Bar"); delegate.returnTypeMethodWithParam(5, 3); manager.commit(); replay(); Assert.assertEquals(interceptor.returnTypeMethodWithParam(5, 3), 8); verify(); Assert.assertEquals( interceptor.toString(), "Baz"); }