samples/openfixed/test/org/apidesign/openfixed/PostTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 20 Mar 2011 20:52:33 +0100
changeset 375 3abae898011d
parent 374 35da2d439e3d
permissions -rw-r--r--
More code snippets for openfixed project
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@375
    21
        // BEGIN: openfixed.usemount
jtulach@375
    22
        class PostListener extends BlockingListener 
jtulach@375
    23
        implements PostModificationListener {
jtulach@374
    24
            int cnt;
jtulach@374
    25
jtulach@374
    26
            @Override
jtulach@374
    27
            public synchronized void modification(ModificationEvent ev) {
jtulach@374
    28
                // registers for callback when batch processing is over:
jtulach@374
    29
                ev.postProcess(this);
jtulach@374
    30
                super.modification(ev);
jtulach@374
    31
            }
jtulach@374
    32
jtulach@374
    33
            @Override
jtulach@374
    34
            public synchronized void postProcess(PostModificationEvent ev) {
jtulach@375
    35
                // called when batch processing is over
jtulach@374
    36
                cnt++;
jtulach@374
    37
            }
jtulach@375
    38
        // FINISH: openfixed.usemount
jtulach@374
    39
            
jtulach@374
    40
            public synchronized void assertPostProcess(String msg, int expected) throws InterruptedException {
jtulach@374
    41
                for (int i = 0; i < 10; i++) {
jtulach@374
    42
                    if (cnt >= expected) {
jtulach@374
    43
                        break;
jtulach@374
    44
                    }
jtulach@374
    45
                    wait(1000);
jtulach@374
    46
                }
jtulach@374
    47
                assertEquals(msg, expected, cnt);
jtulach@374
    48
                cnt = 0;
jtulach@374
    49
            }
jtulach@374
    50
        }
jtulach@374
    51
        PostListener bl = new PostListener();
jtulach@374
    52
        
jtulach@374
    53
        Calculator calc = create();
jtulach@374
    54
        calc.addModificationListener(bl);
jtulach@374
    55
        
jtulach@374
    56
        calc.add(10);
jtulach@374
    57
        bl.first.await();
jtulach@374
    58
        
jtulach@374
    59
        calc.add(1);
jtulach@374
    60
        calc.add(2);
jtulach@374
    61
        calc.add(3);
jtulach@374
    62
        
jtulach@374
    63
        bl.cdl.countDown();
jtulach@374
    64
        bl.assertPostProcess("Two postprocessings (one for 10), then for the rest", 2);
jtulach@374
    65
    }
jtulach@374
    66
}