@Test
void dispose() throws Exception
{

    EmbeddableComponentManager ecm = new EmbeddableComponentManager();
    // Register 2 components:
    // - a first one using a low dispose priority
    // - a second one using a default dispose priority
    // First component
    DefaultComponentDescriptor<Role> cd1 = new DefaultComponentDescriptor<>();
    cd1.setRoleType(Role.class);
    cd1.setRoleHint("instance1");
    cd1.setImplementation(DisposableWithPriorityRoleImpl.class);
    cd1.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
    ecm.registerComponent(cd1);
    DisposableWithPriorityRoleImpl instance1 = ecm.getInstance(Role.class, "instance1");
    // Second component
    DefaultComponentDescriptor<Role> cd2 = new DefaultComponentDescriptor<>();
    cd2.setRoleType(Role.class);
    cd2.setRoleHint("instance2");
    cd2.setImplementation(DisposableRoleImpl.class);
    cd2.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
    ecm.registerComponent(cd2);
    DisposableRoleImpl instance2 = ecm.getInstance(Role.class, "instance2");
    assertFalse(instance1.isFinalized());
    assertFalse(instance2.isFinalized());
    ecm.dispose();
    assertTrue(instance1.isFinalized());
    assertTrue(instance2.isFinalized());
    assertNull(ecm.getComponentDescriptor(Role.class, "instance1"));
    assertNull(ecm.getComponentDescriptor(Role.class, "instance2"));
    assertNotNull(ecm.getComponentDescriptor(ComponentManager.class, "default"));
    // Verify that dispose() has been called in the right order.
    // We check that the last component which had its dispose() called is DisposableWithPriorityRoleImpl since
    // it has the lowest priority.
    assertEquals("DisposableWithPriorityRoleImpl", lastDisposedComponent);
}