@Test
  public void resetEntityCorrectly() {
    PooledEngine engine = new PooledEngine();

    ComponentCounterListener addedListener = new ComponentCounterListener();
    ComponentCounterListener removedListener = new ComponentCounterListener();

    // force the engine to create a Family so family bits get set
    ImmutableArray<Entity> familyEntities =
        engine.getEntitiesFor(Family.all(PositionComponent.class).get());

    Entity[] entities = new Entity[10];
    final int totalEntities = 10;

    for (int i = 0; i < totalEntities; i++) {
      entities[i] = engine.createEntity();

      entities[i].flags = 5;

      entities[i].componentAdded.add(addedListener);
      entities[i].componentRemoved.add(removedListener);

      entities[i].add(engine.createComponent(PositionComponent.class));
      engine.addEntity(entities[i]);

      assertNotNull(entities[i].componentOperationHandler);
      assertEquals(1, entities[i].getComponents().size());
      assertFalse(entities[i].getFamilyBits().isEmpty());
      assertTrue(familyEntities.contains(entities[i], true));
    }

    assertEquals(totalEntities, addedListener.totalCalls);
    assertEquals(0, removedListener.totalCalls);

    engine.removeAllEntities();

    assertEquals(totalEntities, addedListener.totalCalls);
    assertEquals(totalEntities, removedListener.totalCalls);

    for (int i = 0; i < totalEntities; i++) {
      assertEquals(0, entities[i].flags);
      assertNull(entities[i].componentOperationHandler);
      assertEquals(0, entities[i].getComponents().size());
      assertTrue(entities[i].getFamilyBits().isEmpty());
      assertFalse(familyEntities.contains(entities[i], true));

      entities[i].componentAdded.dispatch(entities[i]);
      entities[i].componentRemoved.dispatch(entities[i]);
    }

    assertEquals(totalEntities, addedListener.totalCalls);
    assertEquals(totalEntities, removedListener.totalCalls);
  }
  @Test
  public void entityRemovalListenerOrder() {
    PooledEngine engine = new PooledEngine();

    CombinedSystem combinedSystem = new CombinedSystem();

    engine.addSystem(combinedSystem);
    engine.addEntityListener(Family.all(PositionComponent.class).get(), new MyPositionListener());

    for (int i = 0; i < 10; i++) {
      Entity entity = engine.createEntity();
      entity.add(engine.createComponent(PositionComponent.class));
      engine.addEntity(entity);
    }

    assertEquals(10, combinedSystem.allEntities.size());

    for (int i = 0; i < 10; i++) {
      engine.update(deltaTime);
    }

    engine.removeAllEntities();
  }
 @Override
 public void addedToEngine(Engine engine) {
   entities = engine.getEntitiesFor(Family.all(PositionComponent.class).get());
 }