### Question:
TransactionScopedContextImpl implements Context { @Override public boolean isActive() { try { getTransactionSynchronizationRegistry(); return true; } catch (ContextNotActiveException ignore) { } return false; } 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 javax.transaction.Status.STATUS_ACTIVE} false otherwise */ boolean isActive(); static final String TRANSACTION_SYNCHRONIZATION_REGISTRY_JNDI_NAME; }### Answer:
@Test public void testisActive() throws Exception { TransactionSynchronizationRegistry transactionSynchronizationRegistry = mockSupport.createMock(TransactionSynchronizationRegistry.class); expect( initialContext.lookup(TRANSACTION_SYNCHRONIZATION_REGISTRY_JNDI_NAME)).andThrow( new NamingException() ); mockSupport.replayAll(); TransactionScopedContextImpl transactionScopedContext = new TransactionScopedContextImpl(); assertFalse( transactionScopedContext.isActive() ); mockSupport.verifyAll(); mockSupport.resetAll(); setupMocksForInactiveTransaction(transactionSynchronizationRegistry); mockSupport.replayAll(); assertFalse( transactionScopedContext.isActive() ); mockSupport.verifyAll(); mockSupport.resetAll(); setupMocksForActiveTransaction( transactionSynchronizationRegistry ); mockSupport.replayAll(); assertTrue(transactionScopedContext.isActive()); mockSupport.verifyAll(); mockSupport.resetAll(); setupMocksForActiveTransaction( transactionSynchronizationRegistry, Status.STATUS_ACTIVE ); mockSupport.replayAll(); assertTrue(transactionScopedContext.isActive()); mockSupport.verifyAll(); mockSupport.resetAll(); setupMocksForActiveTransaction( transactionSynchronizationRegistry, Status.STATUS_MARKED_ROLLBACK ); mockSupport.replayAll(); assertTrue(transactionScopedContext.isActive()); mockSupport.verifyAll(); mockSupport.resetAll(); setupMocksForActiveTransaction( transactionSynchronizationRegistry, Status.STATUS_PREPARED ); mockSupport.replayAll(); assertTrue(transactionScopedContext.isActive()); mockSupport.verifyAll(); mockSupport.resetAll(); setupMocksForActiveTransaction( transactionSynchronizationRegistry, Status.STATUS_UNKNOWN ); mockSupport.replayAll(); assertTrue(transactionScopedContext.isActive()); mockSupport.verifyAll(); mockSupport.resetAll(); setupMocksForActiveTransaction( transactionSynchronizationRegistry, Status.STATUS_PREPARING ); mockSupport.replayAll(); assertTrue(transactionScopedContext.isActive()); mockSupport.verifyAll(); mockSupport.resetAll(); setupMocksForActiveTransaction( transactionSynchronizationRegistry, Status.STATUS_COMMITTING ); mockSupport.replayAll(); assertTrue(transactionScopedContext.isActive()); mockSupport.verifyAll(); mockSupport.resetAll(); setupMocksForActiveTransaction( transactionSynchronizationRegistry, Status.STATUS_ROLLING_BACK ); mockSupport.replayAll(); assertTrue(transactionScopedContext.isActive()); mockSupport.verifyAll(); mockSupport.resetAll(); }