CommonTxDao implements CommonDao<T, String> { public T getById(String id) { if (isWithinTransaction()) { Transaction tx = txManager.getCurrentTransaction(); T txDto = null; Pair<TransactionStoreKey, TransactionStoreValue> pair; if (tx.isIsolatedTransaction()) { pair = memCache.getValueForIsolatedTransaction(tx.getId(), entityClass.getName(), id); } else { pair = memCache.getValueForNonIsolatedTransaction(entityClass.getName(), id); } if (pair != null && (pair.getValue().getOpState() == null || OpState.DELETE.equals(pair.getValue().getOpState()))) { return null; } if (pair == null) { return this.readDao.getById(id); } else { txDto = pair.getValue().<T>getCurrentState(); return txDto; } } else { return this.readDao.getById(id); } } @Inject  CommonTxDao(@Transactionable CommonReadDao<T, String> readDao,
                     @Transactionable CommonWriteDao<T> writeDao, TransactionService txService,
                     TransactionManager txManager, TransactionInMemoryCache memCache, Class<T> entityClass); Set<T> getByIds(List<String> ids); T getById(String id); T getSingle(QueryParameter... query); T getSingle(List<QueryParameter> query); Set<T> getAll(); List<T> getList(List<QueryParameter> query); OtherTemplate getOther(List<QueryParameter> query); List<OtherTemplate> getOtherList(List<QueryParameter> query); List<T> getList(QueryParameter... query); OtherTemplate getOther(QueryParameter... query); List<OtherTemplate> getOtherList(QueryParameter... query); void save(T... states); void update(T... states); void delete(T... states);  }
@Test public void testGetByIdWithTransactionIsolationFromCache() { mockery.checking(new Expectations() { { exactly(2).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(1).of(tx).isIsolatedTransaction(); will(returnValue(true)); exactly(1).of(tx).getId(); will(returnValue("1")); exactly(1).of(memCache).getValueForIsolatedTransaction("1", DemoDomain.class.getName(), TEST_DOMAINS.get(0). getId()); TransactionStoreKey key = mockery.mock(TransactionStoreKey.class); TransactionStoreValue val = mockery.mock(TransactionStoreValue.class); Pair<TransactionStoreKey, TransactionStoreValue> pair = new Pair<TransactionStoreKey, TransactionStoreValue>( key, val); will(returnValue(pair)); exactly(2).of(val).getOpState(); will(returnValue(OpState.UPDATE)); exactly(1).of(val).getCurrentState(); will(returnValue(TEST_DOMAINS.get(0))); } }); Assert.assertSame(TEST_DOMAINS.get(0), this.services.readDao.getById("1")); mockery.assertIsSatisfied(); }
@Test public void testGetByIdWithTransactionIsolationFromCacheDeleteState() { mockery.checking(new Expectations() { { exactly(2).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(1).of(tx).isIsolatedTransaction(); will(returnValue(true)); exactly(1).of(tx).getId(); will(returnValue("1")); exactly(1).of(memCache).getValueForIsolatedTransaction("1", DemoDomain.class.getName(), TEST_DOMAINS.get(0). getId()); TransactionStoreKey key = mockery.mock(TransactionStoreKey.class); TransactionStoreValue val = mockery.mock(TransactionStoreValue.class); Pair<TransactionStoreKey, TransactionStoreValue> pair = new Pair<TransactionStoreKey, TransactionStoreValue>( key, val); will(returnValue(pair)); exactly(2).of(val).getOpState(); will(returnValue(OpState.DELETE)); } }); Assert.assertNull(this.services.readDao.getById("1")); mockery.assertIsSatisfied(); }
@Test public void testGetByIdWithTransactionIsolationFromCacheWithNullState() { mockery.checking(new Expectations() { { exactly(2).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(1).of(tx).isIsolatedTransaction(); will(returnValue(true)); exactly(1).of(tx).getId(); will(returnValue("1")); exactly(1).of(memCache).getValueForIsolatedTransaction("1", DemoDomain.class.getName(), TEST_DOMAINS.get(0). getId()); TransactionStoreKey key = mockery.mock(TransactionStoreKey.class); TransactionStoreValue val = mockery.mock(TransactionStoreValue.class); Pair<TransactionStoreKey, TransactionStoreValue> pair = new Pair<TransactionStoreKey, TransactionStoreValue>( key, val); will(returnValue(pair)); exactly(1).of(val).getOpState(); will(returnValue(null)); } }); Assert.assertNull(null, this.services.readDao.getById("1")); mockery.assertIsSatisfied(); }
@Test public void testGetByIdWithTransactionIsolationNotFromCache() { mockery.checking(new Expectations() { { exactly(2).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(1).of(tx).isIsolatedTransaction(); will(returnValue(true)); exactly(1).of(tx).getId(); will(returnValue("1")); final String id = TEST_DOMAINS.get(0).getId(); exactly(1).of(memCache).getValueForIsolatedTransaction("1", DemoDomain.class.getName(), id); will(returnValue(null)); exactly(1).of(readDao).getById(id); will(returnValue(TEST_DOMAINS.get(0))); } }); Assert.assertSame(TEST_DOMAINS.get(0), this.services.readDao.getById("1")); mockery.assertIsSatisfied(); }
@Test public void testGetByIdWithTransactionNonIsolationFromCache() { mockery.checking(new Expectations() { { exactly(2).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(1).of(tx).isIsolatedTransaction(); will(returnValue(false)); final String id = TEST_DOMAINS.get(0).getId(); exactly(1).of(memCache).getValueForNonIsolatedTransaction(DemoDomain.class.getName(), TEST_DOMAINS.get(0). getId()); TransactionStoreKey key = mockery.mock(TransactionStoreKey.class); TransactionStoreValue val = mockery.mock(TransactionStoreValue.class); Pair<TransactionStoreKey, TransactionStoreValue> pair = new Pair<TransactionStoreKey, TransactionStoreValue>( key, val); will(returnValue(pair)); exactly(2).of(val).getOpState(); will(returnValue(OpState.UPDATE)); exactly(1).of(val).getCurrentState(); will(returnValue(TEST_DOMAINS.get(0))); } }); Assert.assertSame(TEST_DOMAINS.get(0), this.services.readDao.getById("1")); mockery.assertIsSatisfied(); }
@Test public void testGetByIdWithTransactionNonIsolationFromCacheWithDeleteState() { mockery.checking(new Expectations() { { exactly(2).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(1).of(tx).isIsolatedTransaction(); will(returnValue(false)); final String id = TEST_DOMAINS.get(0).getId(); exactly(1).of(memCache).getValueForNonIsolatedTransaction(DemoDomain.class.getName(), TEST_DOMAINS.get(0). getId()); TransactionStoreKey key = mockery.mock(TransactionStoreKey.class); TransactionStoreValue val = mockery.mock(TransactionStoreValue.class); Pair<TransactionStoreKey, TransactionStoreValue> pair = new Pair<TransactionStoreKey, TransactionStoreValue>( key, val); will(returnValue(pair)); exactly(2).of(val).getOpState(); will(returnValue(OpState.DELETE)); } }); Assert.assertNull(this.services.readDao.getById("1")); mockery.assertIsSatisfied(); }
@Test public void testGetByIdWithTransactionNonIsolationFromCacheWithNullState() { mockery.checking(new Expectations() { { exactly(2).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(1).of(tx).isIsolatedTransaction(); will(returnValue(false)); final String id = TEST_DOMAINS.get(0).getId(); exactly(1).of(memCache).getValueForNonIsolatedTransaction(DemoDomain.class.getName(), TEST_DOMAINS.get(0). getId()); TransactionStoreKey key = mockery.mock(TransactionStoreKey.class); TransactionStoreValue val = mockery.mock(TransactionStoreValue.class); Pair<TransactionStoreKey, TransactionStoreValue> pair = new Pair<TransactionStoreKey, TransactionStoreValue>( key, val); will(returnValue(pair)); exactly(1).of(val).getOpState(); will(returnValue(null)); } }); Assert.assertNull(this.services.readDao.getById("1")); mockery.assertIsSatisfied(); }
@Test public void testGetByIdWithTransactionNonIsolationNotFromCache() { mockery.checking(new Expectations() { { exactly(2).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(1).of(tx).isIsolatedTransaction(); will(returnValue(false)); final String id = TEST_DOMAINS.get(0).getId(); exactly(1).of(memCache).getValueForNonIsolatedTransaction(DemoDomain.class.getName(), id); will(returnValue(null)); exactly(1).of(readDao).getById(id); will(returnValue(TEST_DOMAINS.get(0))); } }); Assert.assertSame(TEST_DOMAINS.get(0), this.services.readDao.getById("1")); mockery.assertIsSatisfied(); }