CommonTxDao implements CommonDao<T, String> { public T getSingle(QueryParameter... query) { return this.getSingle(Arrays.asList(query)); } @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 testGetSingleWithoutTransactionDelegation() { mockery.checking(new Expectations() { { exactly(2).of(txManager).getCurrentTransaction(); will(returnValue(null)); exactly(2).of(readDao).getSingle(with(equal(PARAM_LIST))); will(returnValue(TEST_DOMAINS.get(0))); } }); Assert.assertSame(TEST_DOMAINS.get(0), this.services.readDao.getSingle(PARAM_LIST)); Assert.assertSame(TEST_DOMAINS.get(0), this.services.readDao.getSingle(PARAM_ARRAY)); mockery.assertIsSatisfied(); }
@Test public void testGetSingleWithTransactionButNullReturnValue() { mockery.checking(new Expectations() { { exactly(2).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(2).of(readDao).getSingle(with(equal(PARAM_LIST))); will(returnValue(null)); } }); Assert.assertNull(this.services.readDao.getSingle(PARAM_LIST)); Assert.assertNull(this.services.readDao.getSingle(PARAM_ARRAY)); mockery.assertIsSatisfied(); }
@Test public void testGetSingleWithTransactionIsolation() { mockery.checking(new Expectations() { { exactly(6).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(2).of(tx).isIsolatedTransaction(); will(returnValue(true)); exactly(2).of(tx).getId(); will(returnValue("1")); exactly(2).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(4).of(val).getOpState(); will(returnValue(OpState.UPDATE)); exactly(2).of(val).getCurrentState(); will(returnValue(TEST_DOMAINS.get(0))); exactly(2).of(readDao).getSingle(with(equal(PARAM_LIST))); will(returnValue(TEST_DOMAINS.get(0))); } }); Assert.assertSame(TEST_DOMAINS.get(0), this.services.readDao.getSingle(PARAM_LIST)); Assert.assertSame(TEST_DOMAINS.get(0), this.services.readDao.getSingle(PARAM_ARRAY)); mockery.assertIsSatisfied(); }