@Test public void testget() throws Exception { TransactionSynchronizationRegistry transactionSynchronizationRegistry = mockSupport.createMock(TransactionSynchronizationRegistry.class); Contextual<LocalBean> contextual = mockSupport.createMock(Contextual.class); CreationalContext<LocalBean> creationalContext = mockSupport.createMock(CreationalContext.class); ContextualPassivationCapable<LocalPassivationCapableBean> passivationCapableContextual = mockSupport.createMock(ContextualPassivationCapable.class); CreationalContext<LocalPassivationCapableBean> passivationCapableCreationalContext = mockSupport.createMock(CreationalContext.class); setupMocksForInactiveTransaction(transactionSynchronizationRegistry); mockSupport.replayAll(); TransactionScopedContextImpl transactionScopedContext = new TransactionScopedContextImpl(); try { transactionScopedContext.get(contextual, creationalContext); fail("Should have gotten a ContextNotActiveException."); } catch (ContextNotActiveException ignore) {} mockSupport.verifyAll(); mockSupport.resetAll(); LocalBean localBean = new LocalBean(); setupMocksForActiveTransaction(transactionSynchronizationRegistry); setupMocksForGetContextualInstance(transactionSynchronizationRegistry, contextual, null, null); setupMocksForCreateContextualInstance(transactionSynchronizationRegistry, contextual, creationalContext, contextual, localBean ); mockSupport.replayAll(); LocalBean retrievedLocalBean = transactionScopedContext.get(contextual, creationalContext); assertSame( localBean, retrievedLocalBean ); mockSupport.verifyAll(); mockSupport.resetAll(); TransactionScopedBean<LocalBean> transactionScopedBean = TransactionScopedBeanTest.getTransactionScopedBean( mockSupport, localBean, contextual, creationalContext, transactionScopedContext); setupMocksForActiveTransaction(transactionSynchronizationRegistry); setupMocksForGetContextualInstance(transactionSynchronizationRegistry, contextual, transactionScopedBean, localBean ); mockSupport.replayAll(); retrievedLocalBean = transactionScopedContext.get(contextual, creationalContext); assertSame( localBean, retrievedLocalBean ); mockSupport.verifyAll(); mockSupport.resetAll(); String beanId = "PCCId"; LocalPassivationCapableBean localPassivationCapableBean = new LocalPassivationCapableBean(); TransactionScopedBean<LocalPassivationCapableBean> transactionScopedPassivationCapableBean = TransactionScopedBeanTest.getTransactionScopedBean( mockSupport, localPassivationCapableBean, passivationCapableContextual, passivationCapableCreationalContext, transactionScopedContext); setupMocksForActiveTransaction(transactionSynchronizationRegistry); expect( passivationCapableContextual.getId() ).andReturn(beanId); setupMocksForGetContextualInstance(transactionSynchronizationRegistry, beanId, transactionScopedPassivationCapableBean, localPassivationCapableBean ); mockSupport.replayAll(); LocalPassivationCapableBean retrievedLocalPassivationCapableBean = transactionScopedContext.get(passivationCapableContextual, passivationCapableCreationalContext); assertSame(localPassivationCapableBean, retrievedLocalPassivationCapableBean); mockSupport.verifyAll(); mockSupport.resetAll(); setupMocksForInactiveTransaction(transactionSynchronizationRegistry); mockSupport.replayAll(); transactionScopedContext = new TransactionScopedContextImpl(); try { transactionScopedContext.get(contextual); fail("Should have gotten a ContextNotActiveException."); } catch (ContextNotActiveException ignore) {} mockSupport.verifyAll(); mockSupport.resetAll(); setupMocksForActiveTransaction(transactionSynchronizationRegistry); setupMocksForGetContextualInstance(transactionSynchronizationRegistry, contextual, transactionScopedBean, localBean); mockSupport.replayAll(); retrievedLocalBean = transactionScopedContext.get(contextual); assertSame( localBean, retrievedLocalBean ); mockSupport.verifyAll(); mockSupport.resetAll(); }
@Override public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) { TransactionSynchronizationRegistry transactionSynchronizationRegistry = getTransactionSynchronizationRegistry(); Object beanId = getContextualId(contextual); T contextualInstance = getContextualInstance(beanId, transactionSynchronizationRegistry); if (contextualInstance == null) { contextualInstance = createContextualInstance(contextual, beanId, creationalContext, transactionSynchronizationRegistry); } return contextualInstance; }
TransactionScopedContextImpl implements Context { @Override public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) { TransactionSynchronizationRegistry transactionSynchronizationRegistry = getTransactionSynchronizationRegistry(); Object beanId = getContextualId(contextual); T contextualInstance = getContextualInstance(beanId, transactionSynchronizationRegistry); if (contextualInstance == null) { contextualInstance = createContextualInstance(contextual, beanId, creationalContext, transactionSynchronizationRegistry); } return contextualInstance; } }
TransactionScopedContextImpl implements Context { @Override public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) { TransactionSynchronizationRegistry transactionSynchronizationRegistry = getTransactionSynchronizationRegistry(); Object beanId = getContextualId(contextual); T contextualInstance = getContextualInstance(beanId, transactionSynchronizationRegistry); if (contextualInstance == null) { contextualInstance = createContextualInstance(contextual, beanId, creationalContext, transactionSynchronizationRegistry); } return contextualInstance; } TransactionScopedContextImpl(); }
TransactionScopedContextImpl implements Context { @Override public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) { TransactionSynchronizationRegistry transactionSynchronizationRegistry = getTransactionSynchronizationRegistry(); Object beanId = getContextualId(contextual); T contextualInstance = getContextualInstance(beanId, transactionSynchronizationRegistry); if (contextualInstance == null) { contextualInstance = createContextualInstance(contextual, beanId, creationalContext, transactionSynchronizationRegistry); } return contextualInstance; } TransactionScopedContextImpl(); ConcurrentHashMap<TransactionSynchronizationRegistry, Set<TransactionScopedBean>> getBeansPerTransaction(); @Override Class<? extends Annotation> getScope(); @Override T get(Contextual<T> contextual, CreationalContext<T> creationalContext); @Override T get(Contextual<T> contextual); @Override /** * Determines if this context object is active. * * @return true if there is a current global transaction and its status is * {@Link jakarta.transaction.Status.STATUS_ACTIVE} * false otherwise */ boolean isActive(); }
TransactionScopedContextImpl implements Context { @Override public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) { TransactionSynchronizationRegistry transactionSynchronizationRegistry = getTransactionSynchronizationRegistry(); Object beanId = getContextualId(contextual); T contextualInstance = getContextualInstance(beanId, transactionSynchronizationRegistry); if (contextualInstance == null) { contextualInstance = createContextualInstance(contextual, beanId, creationalContext, transactionSynchronizationRegistry); } return contextualInstance; } TransactionScopedContextImpl(); ConcurrentHashMap<TransactionSynchronizationRegistry, Set<TransactionScopedBean>> getBeansPerTransaction(); @Override Class<? extends Annotation> getScope(); @Override T get(Contextual<T> contextual, CreationalContext<T> creationalContext); @Override T get(Contextual<T> contextual); @Override /** * Determines if this context object is active. * * @return true if there is a current global transaction and its status is * {@Link jakarta.transaction.Status.STATUS_ACTIVE} * false otherwise */ boolean isActive(); static final String TRANSACTION_SYNCHRONIZATION_REGISTRY_JNDI_NAME; }