DefaultUnitOfWorkManager implements UnitOfWorkManager { @Override public UnitOfWork newUnitOfWork() { return new ManagedUnitOfWork(factory.create(eventManager), this::associate, this::dissociate, this::dissociate); } DefaultUnitOfWorkManager(UnitOfWorkFactory factory);  DefaultUnitOfWorkManager(UnitOfWork fallbackUnitOfWork, UnitOfWorkFactory factory); @Override UnitOfWork currentUnitOfWork(); @Override UnitOfWork newUnitOfWork(); @Override EventManager eventManager();  }
@Test public void testUnitOfWorkStartEnd() { UnitOfWork unit = unitOfWorkManager.newUnitOfWork(); assertThat(unit).isNotNull().isInstanceOf(ManagedUnitOfWork.class); assertThat(((ManagedUnitOfWork)unit).delegate()).isInstanceOf(CollectingUnitOfWork.class); final AtomicInteger counter = new AtomicInteger(0); assertThat(counter.get()).isEqualTo(0); BaseWorkUnit dummyWork = new BaseWorkUnit(counter, (d) -> ((AtomicInteger) d).incrementAndGet()); unit.start(); unit.intercept(dummyWork); unit.end(); assertThat(counter.get()).isEqualTo(1); }
@Test public void testUnitOfWorkStartAbort() { UnitOfWork unit = unitOfWorkManager.newUnitOfWork(); assertThat(unit).isNotNull().isInstanceOf(ManagedUnitOfWork.class); final AtomicInteger counter = new AtomicInteger(0); assertThat(counter.get()).isEqualTo(0); BaseWorkUnit dummyWork = new BaseWorkUnit(counter, (d) -> ((AtomicInteger) d).incrementAndGet()); unit.start(); unit.intercept(dummyWork); unit.abort(); assertThat(counter.get()).isEqualTo(0); }
@Test public void testUnitOfWorkStartOnFinishedUnit() { UnitOfWork unit = unitOfWorkManager.newUnitOfWork(); assertThat(unit).isNotNull().isInstanceOf(ManagedUnitOfWork.class); unit.start(); unit.abort(); assertThrows(IllegalStateException.class, () -> unit.start(), "Cannot start already completed unit"); assertThrows(IllegalStateException.class, () -> unit.end(), "Cannot end already completed unit"); assertThrows(IllegalStateException.class, () -> unit.abort(), "Cannot abort already completed unit"); assertThrows(IllegalStateException.class, () -> unit.intercept(null), "Cannot intercept on already completed unit"); }
@Test public void testUnitOfWorkAddWorkOnNotStarted() { UnitOfWork unit = unitOfWorkManager.newUnitOfWork(); assertThat(unit).isNotNull().isInstanceOf(ManagedUnitOfWork.class); final AtomicInteger counter = new AtomicInteger(0); assertThat(counter.get()).isEqualTo(0); WorkUnit<AtomicInteger> dummyWork = WorkUnit.create(counter, (d) -> d.incrementAndGet()); assertThrows(IllegalStateException.class, () -> unit.intercept(dummyWork), "Cannot intercept on not started unit"); }
@Test public void testUnitOfWorkStartEndOrdered() { UnitOfWork unit = unitOfWorkManager.newUnitOfWork(); assertThat(unit).isNotNull().isInstanceOf(ManagedUnitOfWork.class); assertThat(((ManagedUnitOfWork)unit).delegate()).isInstanceOf(CollectingUnitOfWork.class); final AtomicInteger counter = new AtomicInteger(0); assertThat(counter.get()).isEqualTo(0); final AtomicInteger picounter = new AtomicInteger(0); BaseWorkUnit dummyWork = new BaseWorkUnit(counter, (d) -> ((AtomicInteger) d).incrementAndGet()); ProcessInstanceWorkUnit<?> piWork = new ProcessInstanceWorkUnit<>(null, (d) -> picounter.set(counter.get())); unit.start(); unit.intercept(dummyWork); unit.intercept(piWork); unit.end(); assertThat(counter.get()).isEqualTo(1); assertThat(picounter.get()).isEqualTo(0); }