@SuppressWarnings("unchecked")
@Test
public void testAggregatesRetrievedFromCache() {
DefaultUnitOfWork.startAndGet();
final StubAggregate aggregate1 = new StubAggregate();
aggregate1.doSomething();
// ensure the cached aggregate has been committed before being cached.
doThrow(new AssertionError("Aggregate should not have a null version when cached"))
.when(cache).put(eq(aggregate1.getIdentifier()), argThat(new TypeSafeMatcher<StubAggregate>() {
@Override
public boolean matchesSafely(StubAggregate item) {
return item.getVersion() == null;
}
@Override
public void describeTo(Description description) {
description.appendText("An aggregate with a non-null version");
}
}));
testSubject.add(aggregate1);
CurrentUnitOfWork.commit();
DefaultUnitOfWork.startAndGet();
StubAggregate reloadedAggregate1 = testSubject.load(aggregate1.getIdentifier(), null);
assertSame(aggregate1, reloadedAggregate1);
aggregate1.doSomething();
aggregate1.doSomething();
CurrentUnitOfWork.commit();
DefaultUnitOfWork.startAndGet();
DomainEventStream events = mockEventStore.readEvents("mock", aggregate1.getIdentifier());
List<EventMessage> eventList = new ArrayList<EventMessage>();
while (events.hasNext()) {
eventList.add(events.next());
}
assertEquals(3, eventList.size());
verify(mockEventBus).publish(isA(EventMessage.class));
verify(mockEventBus).publish(isA(EventMessage.class), isA(EventMessage.class));
verifyNoMoreInteractions(mockEventBus);
ehCache.removeAll();
reloadedAggregate1 = testSubject.load(aggregate1.getIdentifier(), null);
assertNotSame(aggregate1, reloadedAggregate1);
assertEquals(aggregate1.getVersion(),
reloadedAggregate1.getVersion());
}