CommonTxDao implements CommonDao<T, String> { public void delete(T... states) { if (states == null) { return; } if (isWithinTransaction()) { for (T state : states) { this.txService.delete(getTxElement(state)); } } else { this.writeDao.delete(states); } } @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 testDeleteWithNull() { this.services.writeDao.delete((DemoDomain[]) null); mockery.assertIsSatisfied(); }
@Test public void testDeleteWithoutTransaction() { final DemoDomain dto = getWriteTestData().get(0); mockery.checking(new Expectations() { { exactly(1).of(txManager).getCurrentTransaction(); will(returnValue(null)); exactly(1).of(writeDao).delete(with(new BaseMatcher<DemoDomain[]>() { public boolean matches(Object item) { DemoDomain[] args = (DemoDomain[]) item; DemoDomain[] expecteds = new DemoDomain[]{dto}; return Arrays.equals(expecteds, args); } public void describeTo(Description description) { } })); } }); this.services.writeDao.delete(dto); mockery.assertIsSatisfied(); }
@Test public void testDelete() { final DemoDomain dto = getWriteTestData().get(0); mockery.checking(new Expectations() { { exactly(2).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(1).of(tx).getId(); will(returnValue("1")); exactly(1).of(txService).delete(with(equal(getElement(dto)))); } }); this.services.writeDao.delete(dto); mockery.assertIsSatisfied(); }
@Test public void testDeletes() { final List<DemoDomain> dtos = getWriteTestData(); mockery.checking(new Expectations() { { exactly(dtos.size() + 1).of(txManager).getCurrentTransaction(); Transaction tx = mockery.mock(Transaction.class); will(returnValue(tx)); exactly(dtos.size()).of(tx).getId(); will(returnValue("1")); for (DemoDomain dto : dtos) { exactly(1).of(txService).delete(with(equal(getElement(dto)))); } } }); this.services.writeDao.delete(dtos.toArray(new DemoDomain[dtos.size()])); mockery.assertIsSatisfied(); }