Search in sources :

Example 1 with TransactionEvent

use of com.codename1.rad.models.EntityList.TransactionEvent in project CodeRAD by shannah.

the class EntityListTest method testTransactions.

private void testTransactions() throws Exception {
    EntityList el = new EntityList();
    Entity bob = new BaseEntity();
    Entity gary = new BaseEntity();
    Entity june = new BaseEntity();
    class Stats {

        private Object data;

        private int count;

        private void reset() {
            data = null;
            count = 0;
        }
    }
    // Warm it up.. make sure no NPEs or anything
    el.add(bob);
    el.add(gary);
    el.remove(bob);
    el.remove(gary);
    Stats stats = new Stats();
    ActionListener<EntityListEvent> l1 = evt -> {
        if (evt instanceof EntityAddedEvent || evt instanceof EntityRemovedEvent) {
            stats.count++;
            stats.data = evt;
        }
    };
    el.addActionListener(l1);
    el.add(bob);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityAddedEvent);
    assertEqual(1, el.size());
    EntityAddedEvent eae = (EntityAddedEvent) stats.data;
    assertEqual(bob, eae.getEntity());
    assertNull(eae.getTransaction(), "EntityAddedEvent not in transaction should return null for getTransaction()");
    stats.reset();
    el.startTransaction();
    assertEqual(0, stats.count);
    assertNull(stats.data);
    el.remove(bob);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityRemovedEvent);
    assertEqual(0, el.size());
    EntityRemovedEvent ere = (EntityRemovedEvent) stats.data;
    assertEqual(bob, ere.getEntity());
    assertNotNull(ere.getTransaction(), "EntityRemovedEvent inside transaction should return non-null for getTransaction()");
    Throwable t = null;
    try {
        el.startTransaction();
    } catch (IllegalStateException ex) {
        t = ex;
    }
    assertTrue(t instanceof IllegalStateException, "Starting a transaction while another one is active should raise an IllegalStateException");
    el.commitTransaction();
    el.removeActionListener(l1);
    l1 = evt -> {
        if (evt instanceof EntityAddedEvent || evt instanceof EntityRemovedEvent || evt instanceof TransactionEvent) {
            stats.count++;
            stats.data = evt;
        }
    };
    el.addActionListener(l1);
    stats.reset();
    el.startTransaction();
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof TransactionEvent);
    TransactionEvent te = (TransactionEvent) stats.data;
    assertTrue(te.isEmpty());
    assertTrue(!te.isComplete());
    stats.reset();
    el.add(bob);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityAddedEvent);
    eae = (EntityAddedEvent) stats.data;
    assertEqual(bob, eae.getEntity());
    assertEqual(eae.getTransaction(), te, "EntityAddedEvent.getTransaction() should return same TransactionEvent from startTransaction");
    stats.reset();
    el.add(gary);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityAddedEvent);
    eae = (EntityAddedEvent) stats.data;
    assertEqual(gary, eae.getEntity());
    stats.reset();
    el.commitTransaction();
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof TransactionEvent);
    te = (TransactionEvent) stats.data;
    assertEqual(2, te.size());
    assertTrue(te.isComplete());
    assertEqual(bob, te.get(0).getEntity());
    assertEqual(gary, te.get(1).getEntity());
    t = null;
    try {
        el.commitTransaction();
    } catch (IllegalStateException ex) {
        t = ex;
    }
    assertTrue(t instanceof IllegalStateException, "commitTransaction() on list with no current transaction should raise an IllegalStateException");
    el.startTransaction();
    stats.reset();
    el.clear();
    assertEqual(0, el.size(), "EntityList.add() and EntityList.remove() should immediately change the list even if transaction is in progress");
    assertEqual(2, stats.count, "EntityList.clear() should trigger remove events for each item in list");
    el.commitTransaction();
    assertEqual(0, el.size());
    assertEqual(3, stats.count);
    assertTrue(stats.data instanceof TransactionEvent, "commitTransaction() didn't fire a transaction event");
    te = (TransactionEvent) stats.data;
    assertEqual(2, te.size(), "TransactionEvent size() should reflect the number of add or remove operations in transaction.");
    assertEqual(bob, te.get(0).getEntity());
    assertEqual(gary, te.get(1).getEntity());
}
Also used : AbstractTest(com.codename1.testing.AbstractTest) Observer(java.util.Observer) TransactionEvent(com.codename1.rad.models.EntityList.TransactionEvent) EntityAddedEvent(com.codename1.rad.models.EntityList.EntityAddedEvent) EntityRemovedEvent(com.codename1.rad.models.EntityList.EntityRemovedEvent) EntityList(com.codename1.rad.models.EntityList) BaseEntity(com.codename1.rad.models.BaseEntity) EntityListEvent(com.codename1.rad.models.EntityList.EntityListEvent) ActionListener(com.codename1.ui.events.ActionListener) Entity(com.codename1.rad.models.Entity) BaseEntity(com.codename1.rad.models.BaseEntity) Entity(com.codename1.rad.models.Entity) EntityList(com.codename1.rad.models.EntityList) BaseEntity(com.codename1.rad.models.BaseEntity) EntityListEvent(com.codename1.rad.models.EntityList.EntityListEvent) TransactionEvent(com.codename1.rad.models.EntityList.TransactionEvent) EntityRemovedEvent(com.codename1.rad.models.EntityList.EntityRemovedEvent) EntityAddedEvent(com.codename1.rad.models.EntityList.EntityAddedEvent)

Aggregations

BaseEntity (com.codename1.rad.models.BaseEntity)1 Entity (com.codename1.rad.models.Entity)1 EntityList (com.codename1.rad.models.EntityList)1 EntityAddedEvent (com.codename1.rad.models.EntityList.EntityAddedEvent)1 EntityListEvent (com.codename1.rad.models.EntityList.EntityListEvent)1 EntityRemovedEvent (com.codename1.rad.models.EntityList.EntityRemovedEvent)1 TransactionEvent (com.codename1.rad.models.EntityList.TransactionEvent)1 AbstractTest (com.codename1.testing.AbstractTest)1 ActionListener (com.codename1.ui.events.ActionListener)1 Observer (java.util.Observer)1