UserTransactionFactoryBean implements FactoryBean<UserTransaction> { @Override public UserTransaction getObject() throws Exception { return (UserTransaction)Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { getObjectType() }, new LazyInitializationHandler()); }  @Override UserTransaction getObject(); @Override Class<UserTransaction> getObjectType(); @Override boolean isSingleton();  }
@Test public void testGetObject_Null() throws Exception { Jta.configure(null, null); UserTransaction userTransaction = userTransactionFactoryBean.getObject(); assertNotNull(userTransaction); assertTrue(Proxy.isProxyClass(userTransaction.getClass())); try { userTransaction.getStatus(); fail("IllegalStateException should have been thrown"); } catch (IllegalStateException e) {} }
@Test(expected = IllegalStateException.class) public void testGetObject_JtaNotInitialized() throws Exception { userTransactionFactoryBean.getObject().getStatus(); }
@Test public void testGetObject() throws Exception { Jta.configure(this.transactionManager, this.userTransaction); UserTransaction userTransaction = userTransactionFactoryBean.getObject(); TargetedInvocationHandler<UserTransaction> handler = (TargetedInvocationHandler<UserTransaction>) Proxy.getInvocationHandler(userTransaction); assertNull(handler.getTarget()); userTransaction.getStatus(); assertEquals(this.userTransaction, handler.getTarget()); }