samples/openfixed/test/org/apidesign/openfixed/PostTest.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.concurrent.CountDownLatch;
jtulach@374
     4
jtulach@374
     5
/** Test the Calculator.createPending() behavior.
jtulach@374
     6
 *
jtulach@374
     7
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@374
     8
 */
jtulach@374
     9
public final class PostTest extends PendingTest {
jtulach@374
    10
    
jtulach@374
    11
    public PostTest(String testName) {
jtulach@374
    12
        super(testName);
jtulach@374
    13
    }
jtulach@374
    14
jtulach@374
    15
    @Override
jtulach@374
    16
    protected Calculator create() {
jtulach@374
    17
        return Calculator.createBatch();
jtulach@374
    18
    }
jtulach@374
    19
jtulach@374
    20
    public void testPostModificationEvents() throws Exception {
jtulach@374
    21
        class PostListener extends BlockingListener implements PostModificationListener {
jtulach@374
    22
            int cnt;
jtulach@374
    23
jtulach@374
    24
            @Override
jtulach@374
    25
            public synchronized void modification(ModificationEvent ev) {
jtulach@374
    26
                // registers for callback when batch processing is over:
jtulach@374
    27
                ev.postProcess(this);
jtulach@374
    28
                super.modification(ev);
jtulach@374
    29
            }
jtulach@374
    30
jtulach@374
    31
            @Override
jtulach@374
    32
            public synchronized void postProcess(PostModificationEvent ev) {
jtulach@374
    33
                cnt++;
jtulach@374
    34
            }
jtulach@374
    35
            
jtulach@374
    36
            public synchronized void assertPostProcess(String msg, int expected) throws InterruptedException {
jtulach@374
    37
                for (int i = 0; i < 10; i++) {
jtulach@374
    38
                    if (cnt >= expected) {
jtulach@374
    39
                        break;
jtulach@374
    40
                    }
jtulach@374
    41
                    wait(1000);
jtulach@374
    42
                }
jtulach@374
    43
                assertEquals(msg, expected, cnt);
jtulach@374
    44
                cnt = 0;
jtulach@374
    45
            }
jtulach@374
    46
        }
jtulach@374
    47
        PostListener bl = new PostListener();
jtulach@374
    48
        
jtulach@374
    49
        Calculator calc = create();
jtulach@374
    50
        calc.addModificationListener(bl);
jtulach@374
    51
        
jtulach@374
    52
        calc.add(10);
jtulach@374
    53
        bl.first.await();
jtulach@374
    54
        
jtulach@374
    55
        calc.add(1);
jtulach@374
    56
        calc.add(2);
jtulach@374
    57
        calc.add(3);
jtulach@374
    58
        
jtulach@374
    59
        bl.cdl.countDown();
jtulach@374
    60
        bl.assertPostProcess("Two postprocessings (one for 10), then for the rest", 2);
jtulach@374
    61
    }
jtulach@374
    62
}