samples/openfixed/test/org/apidesign/openfixed/CalculatorBase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 20 Mar 2011 18:52:47 +0100
changeset 374 35da2d439e3d
child 375 3abae898011d
permissions -rw-r--r--
Calculator and various ways to deliver changes in its counter to listeners
jtulach@374
     1
package org.apidesign.openfixed;
jtulach@374
     2
jtulach@374
     3
import java.util.ArrayList;
jtulach@374
     4
import java.util.List;
jtulach@374
     5
import junit.framework.TestCase;
jtulach@374
     6
jtulach@374
     7
public abstract class CalculatorBase extends TestCase {
jtulach@374
     8
    
jtulach@374
     9
    public CalculatorBase(String testName) {
jtulach@374
    10
        super(testName);
jtulach@374
    11
    }
jtulach@374
    12
    
jtulach@374
    13
    protected abstract Calculator create();
jtulach@374
    14
jtulach@374
    15
    public void testSumAndListeners() throws Exception {
jtulach@374
    16
        Calculator a = create();
jtulach@374
    17
        MockListener l = new MockListener();
jtulach@374
    18
        a.addModificationListener(l);
jtulach@374
    19
        a.add(5);
jtulach@374
    20
        a.add(10);
jtulach@374
    21
        a.add(20);
jtulach@374
    22
        int ch = allChanges(l.assertEvents("Three changes", 3));
jtulach@374
    23
        assertEquals("35 was the change", 35, ch);
jtulach@374
    24
        assertEquals("Current value", 35, a.getSum());
jtulach@374
    25
        a.add(-5);
jtulach@374
    26
        int ch2 = allChanges(l.assertEvents("One change", 1));
jtulach@374
    27
        assertEquals("minus five was the change", -5, ch2);
jtulach@374
    28
        assertEquals("Final value", 30, a.getSum());
jtulach@374
    29
    }
jtulach@374
    30
    
jtulach@374
    31
    private static int allChanges(List<ModificationEvent> events) {
jtulach@374
    32
        int changes = 0;
jtulach@374
    33
        for (ModificationEvent me : events) {
jtulach@374
    34
            changes += me.getChange();
jtulach@374
    35
        }
jtulach@374
    36
        return changes;
jtulach@374
    37
    }
jtulach@374
    38
    
jtulach@374
    39
    public static class MockListener implements ModificationListener {
jtulach@374
    40
        private List<ModificationEvent> events;
jtulach@374
    41
        
jtulach@374
    42
        @Override
jtulach@374
    43
        public synchronized void modification(ModificationEvent ev) {
jtulach@374
    44
            if (events == null) {
jtulach@374
    45
                events = new ArrayList<ModificationEvent>();
jtulach@374
    46
            }
jtulach@374
    47
            events.add(ev);
jtulach@374
    48
        }
jtulach@374
    49
        
jtulach@374
    50
        public synchronized List<ModificationEvent> assertEvents(String msg, int cnt) 
jtulach@374
    51
        throws InterruptedException {
jtulach@374
    52
            for (int i = 0; i < 10; i++) {
jtulach@374
    53
                if (events != null && events.size() >= cnt) {
jtulach@374
    54
                    break;
jtulach@374
    55
                }
jtulach@374
    56
                wait(1000);
jtulach@374
    57
            }
jtulach@374
    58
            assertEquals(msg + ":\n" + events, cnt, events.size());
jtulach@374
    59
            List<ModificationEvent> res = events;
jtulach@374
    60
            events = null;
jtulach@374
    61
            return res;
jtulach@374
    62
        }
jtulach@374
    63
    } // end of ModificationListener
jtulach@374
    64
}