}
}
@Test
public void testUowRolledBackOnOuterRollback() {
UnitOfWork outerUnit = new UnitOfWork() {
private boolean started = false;
private List<UnitOfWorkListener> listeners = new ArrayList<UnitOfWorkListener>();
@Override
public void commit() {
for (UnitOfWorkListener listener : listeners) {
listener.onPrepareCommit(this,
Collections.<AggregateRoot>emptySet(),
Collections.<EventMessage>emptyList());
listener.afterCommit(this);
listener.onCleanup(this);
}
started = false;
CurrentUnitOfWork.clear(this);
}
@Override
public void rollback() {
rollback(null);
}
@Override
public void rollback(Throwable cause) {
if (started) {
for (UnitOfWorkListener listener : listeners) {
listener.onRollback(this, cause);
listener.onCleanup(this);
}
started = false;
CurrentUnitOfWork.clear(this);
}
}
@Override
public void start() {
CurrentUnitOfWork.set(this);
started = true;
}
@Override
public boolean isStarted() {
return started;
}
@Override
public boolean isTransactional() {
return false;
}
@Override
public void registerListener(UnitOfWorkListener listener) {
this.listeners.add(listener);
}
@Override
public <T extends AggregateRoot> T registerAggregate(T aggregateRoot, EventBus eventBus,
SaveAggregateCallback<T> saveAggregateCallback) {
return aggregateRoot;
}
@Override
public void publishEvent(EventMessage<?> event, EventBus eventBus) {
}
@Override
public void attachResource(String name, Object resource) {
}
@Override
public void attachResource(String name, Object resource, boolean inherited) {
}
@Override
public <T> T getResource(String name) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public void attachInheritedResources(UnitOfWork inheritingUnitOfWork) {
}
}; // This is a unit that does not extend from NestableUnitOfWork
outerUnit.start();
UnitOfWork middleUnit = DefaultUnitOfWork.startAndGet();
UnitOfWork innerUnit = DefaultUnitOfWork.startAndGet();
final Set<UnitOfWork> rolledBack = new HashSet<UnitOfWork>();
final UnitOfWorkListenerAdapter listener = new UnitOfWorkListenerAdapter() {
@Override
public void onRollback(UnitOfWork unitOfWork, Throwable failureCause) {
rolledBack.add(unitOfWork);
}
};
final UnitOfWorkListener middleListener = mock(UnitOfWorkListener.class, "middleListener");
final UnitOfWorkListener innerListener = mock(UnitOfWorkListener.class, "innerListener");
final UnitOfWorkListener outerListener = mock(UnitOfWorkListener.class, "outerListener");
outerUnit.registerListener(outerListener);
middleUnit.registerListener(listener);
innerUnit.registerListener(listener);
middleUnit.registerListener(middleListener);
innerUnit.registerListener(innerListener);
innerUnit.commit();
middleUnit.commit();
verify(innerListener, never()).afterCommit(any(UnitOfWork.class));
verify(middleListener, never()).afterCommit(any(UnitOfWork.class));