@Test
  public void testDisruptorCommandBusRepositoryNotAvailableOutsideOfInvokerThread() {
    DisruptorCommandBus commandBus = new DisruptorCommandBus(eventStore, eventBus);
    Repository<Aggregate> repository =
        commandBus.createRepository(new GenericAggregateFactory<Aggregate>(Aggregate.class));

    AggregateAnnotationCommandHandler<Aggregate> handler =
        new AggregateAnnotationCommandHandler<Aggregate>(Aggregate.class, repository);
    AggregateAnnotationCommandHandler.subscribe(handler, commandBus);
    DefaultCommandGateway gateway = new DefaultCommandGateway(commandBus);

    // Create the aggregate
    String aggregateId = "" + System.currentTimeMillis();
    gateway.sendAndWait(new CreateCommandAndEvent(aggregateId));

    // Load the aggretate from the repository -- from "worker" thread
    UnitOfWork uow = DefaultUnitOfWork.startAndGet();
    try {
      Aggregate aggregate = repository.load(aggregateId);
      fail("Expected IllegalStateException");
    } catch (IllegalStateException e) {
      assertTrue(e.getMessage().contains("DisruptorCommandBus"));
    } finally {
      uow.rollback();
    }
  }
 @Before
 public void setUp() {
   mockEventStore = mock(SnapshotEventStore.class);
   mockEventBus = mock(EventBus.class);
   testSubject = new EventSourcingRepository<TestAggregate>(new SubtAggregateFactory());
   testSubject.setEventBus(mockEventBus);
   testSubject.setEventStore(mockEventStore);
   unitOfWork = DefaultUnitOfWork.startAndGet();
 }
  @Test
  public void testCommandHandlerLoadsSameAggregateTwice() {
    DefaultUnitOfWork.startAndGet();
    StubAggregate stubAggregate = new StubAggregate(aggregateIdentifier);
    stubAggregate.doSomething();
    repository.add(stubAggregate);
    CurrentUnitOfWork.commit();

    DefaultUnitOfWork.startAndGet();
    repository.load(aggregateIdentifier).doSomething();
    repository.load(aggregateIdentifier).doSomething();
    CurrentUnitOfWork.commit();

    DomainEventStream es = mockEventStore.readEvents("", aggregateIdentifier);
    assertTrue(es.hasNext());
    assertEquals((Object) 0L, es.next().getSequenceNumber());
    assertTrue(es.hasNext());
    assertEquals((Object) 1L, es.next().getSequenceNumber());
    assertTrue(es.hasNext());
    assertEquals((Object) 2L, es.next().getSequenceNumber());
    assertFalse(es.hasNext());
  }